Help starting a Battleships game in C++?

Hi to anybody reading.
I am literally on the verge of giving up on life. I have to create a 1 player battleships game in C++ for my course assignment. I don't know what to do. Yes, I've learned the techniques and everything. I can't apply them. This is really depressing me now. I need to pass this module. Could anybody PLEASE help me?

Thank you
Do you want to use a graphics lib? If yes then I would go with SFML.


Do you have to make a computer opponent?
Hi ignotus1,

I would use a vector<vector<int> > to represent the grid representing the sea.

If its 1 player, you need to decide how the ships are to be deployed. One possibility is to use a function that utilises the random number generator rand(), with a random seed (determined by the system time, say).

This function could first select a number to represent a grid point to represent the position of one end of the ship.
The rand() generator could then be used again to select one of 4 directions (e.g. 1,2,3,4 to represent N, W, E, S) representing the orientation the ship. Lastly, the rand() function can be used again to determine the length of the ship from a predetermined selection. You could then place checks in this function to make sure that the ships don't overlap or protrude off the grid when orientating and deciding the length of each ship.

Once this has been done, you can successively ask the user to select grid positions. If the client selects an unoccupied grid location you can change the value of the int stored there (by passing the vector<vector<int> > by reference to some function) from its default (say 0) to 1 say. If the user selects a grid coordinate that is occupied by a ship then you can change the int stored there by calling some function from its default (this time say 2) to some other int (say 3).

The after each go, you can check whether the game is over by comparing the number of 1's, 2's and 3's left in the grid. You can decide what the criterion is for winning (since it is a 1 player game, this is flexible). One simple example is that you must uncover all battleships (i.e. convert all 2's to 3's) before uncovering all open sea (i.e. convert all 0's to 1's).

Hope that helps, good luck!
@ignotus1:

If you are feeling lost, it might be because you need to figure out a place to start. Rather than looking at the assignment as one "big" problem, subdivide it down into many smaller problems.

Start by choosing a data structure to represent the grid. A vector<vector<int>> is good, and so is a simple 2D array of int. Pick one. Your choice. Also, figure out how you want to represent a grid location containing part of a ship or open space. Using 0 to represent open space and non-zero to represent a ship is good. Again, your choice.

Next, start the project by writing a main function that does two things. First, it should "initialize" the grid to all-empty. Second, it should display the contents of the grid to the user. You know what an empty space "looks like" so you can easily verify that your code works to this point.

Next, move the code out of main into two functions -- initializeGrid and displayGrid and verify it still works.

Next, write a function that places one ship randomly on the grid. Test this code with a main() that calls initializeGrid, placeShip, and displayGrid in that order.

The next problems for you to solve beyond that are:
1. Write a function called "placeAllShips" that calls placeShip the correct number of times to place all of the ships on the board.
2. Write a function called "isGameOver" that determines whether or not all ships have been sunk.
3. Write a function that allows the user to enter a coordinate.
4. Write a function that "executes" the user's move.
5. Write a loop in main() that continues to ask the user for coordinates and executes them until the game is over.

At each step of the way, compile and run your code and ensure that it works before proceeding to the next step. Also, don't even think about the next step until you've finished the current one.

To make life a little easier for you: the random placement of ships is going to make testing a little hard. The way to do this in a testable manner is to NOT use srand() until you are sure things are working. This will have the effect of generating the same random sequence every time you run the program, which is great for testing. Only after you are sure things are working, then add the call to srand at the top of main.
Topic archived. No new replies allowed.