### Battleship game.

I'm looking to make a two player battleship game. I'm not going to worry about playing the game with a computer, that would be even more work. Basically, I need to make the game using functions no longer than 15 lines (not including comments or blank lines), including main(). The game will be on a 10x10 board, using the following pieces:

1 Aircraft Carrier, 5 spots
1 Battleship, 4 spots
1 Destroyer, 3 spots
1 Submarine, 3 spots
1 Patrol Boat, 2 spots

The players will each place their ships, and then begin to guess. I realize this potentially a pretty hefty program. But before I start, does anyone have any tips or algorithms I can use to get going? Thank you for any help.
Last edited on
Take it one step at a time. I'd start with the board (which would just be represented as a 2D array). After that, work on a placement function that places ships on the board where a player wants. I'd do it as ask the player for a single point, then ask horizontal or vertical. Pass this info to the placement function, check if it collides with another already present ship or goes out of bounds, if not, update your board array. Then just build a shoot function.
I took a bit of a break from this program, but I am back into it. I am only as far as printing my board (although it isn't quite what I want yet). Here is my code so far:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` ``````#include #include #include #include #include const int empty = 0; // contains water const int occupied = 1; // contains a ship const int missed = 2; // shot into ocean const int hit = 3; // shot and hit using namespace std; int board[ 10 ][ 10 ]; // Two-dimensional array for gameboard. void initialize_board( int array1[ 10 ][ 10 ] ) // Function to initialize the gameboard. { // create a blank board for (int x=0; x<10; x++) { for (int y=0; y<10; y++) { array1[x][y] = occupied; } } } void print_board(int array2[10][10]) { for(char a = 'A'; a <= 'J'; a++) { //letter coordinates cout << setw(5) << a; } cout << endl; for(int i = 1; i <= 10; i++) { //number coordinates if(i == 10) cout << i; else cout << " " << i ; for(int j = 0; j < 10 ; j++) { if(array2[i][j] == occupied || array2[i][j] == empty){ cout << setw(5) << " |" ; } else if(array2[i][j] == missed ) { cout << setw(5) << "O|"; } else if(array2[i][j] == hit ) { cout << setw(5) << "X|"; } } cout << "\n"; } } class cShip //Unused so far { int x1, y1, x2, y2; // The position of the front and back of the ship int size; int damage[]; // for storing the damage. public: cShip(int x1, int y1, int x2, int y2, int size); // constructor ~cShip(); // destructor for destroying the damage array bool isDestroyed(); // for polling the destroyed bool is3(int x, int y); // for polling the 3 }; int main() { initialize_board(board); determine_player_choice(); print_board(board); return 0; }``````

(I apologize if I missed any indentation errors, my copy paste didn't really translate perfectly.)

At this point, my next step is to have the players place their ships.

If there are any errors in my current coding or something that I could improve, let me know that as well!
Any help is appreciated.
Last edited on
Do you have pseudo code for this project worked up? That might help you focus if you don't. Off the top my head.. I'm thinking something like...

 ``123456789101112131415161718192021`` ``````int main() { //Initialize default variables //Create game board //Loop till all ships are placed //Draw board //A player places ships // Toggle to next player //Game Loop Start //Player X inputs valid attack grid // Check to see if attack is a hit // Remove/add damage to hit ships // Redraw game board // Check to see if game is over // If game over - exit loop/end program // Toggle next player's turn //Game Loop Return }``````

Each of those comments could be a function that accomplishes a task. I just posted a tic-tac-toe game that kinda follows this pseudocode. I realize it's not the same, but structurally it might give you some ideas...

http://www.cplusplus.com/forum/beginner/86525/

Good luck!
That's actually a really helpful idea. The way I've been doing this is so unorganized that I don't really know what is going to go where. And your code may not be quite the same, but is valuable as far as how to place symbols on the board and use two players. Thank you!
Last edited on
I'm having a lot of trouble figuring out how to place ships on the board. It seems like it would take a lot of code to create ships with a certain number of spots, then choose if they are horizontal or vertical, where to place them, and to check if they are on the board. And I need to do that for both players. Do I need two make a separate function for player 2's board? Or can I use a struct to create separate boards?

I'm updating the code for my functions above.
Last edited on
Ok, so I decided this project looks really fun and would be a great challenge! :D I went ahead and built the first part of the game (my design). This takes you through the players picking where their ships go.. but stops short of actually "playing" the game. I will probably finish it tomorrow.

It may be a bit confusing in places, but I heavily commented it so that should help alot.

I'm not a student so I didn't necessarily follow any of your exact setup but it should be easily adjusted for designing your game. Use this as a "helper" for you if you'd like; it should provide alot more help than the Tic-Tac-Toe program I linked you the other day... This one was quite fun to make! :D

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194`` ``````#include #include using namespace std; const int BOARD_WIDTH = 15; const int BOARD_HEIGHT = 10; const int SHIP_TYPES = 5; const char isWATER = 247; //ASCII Character Code const char isHIT = 'X'; const char isSHIP = 'S'; struct POINT { //A location on the grid defined //by X(horizontal) Y(vertical) coordinates int X; int Y; }; struct SHIP { //Ship name string name; //Total points on the grid int length; //Coordinates of those points POINT onGrid[5]; //0-4 max length of biggest ship //Whether or not those points are a "hit" bool hitFlag[5]; }ship[SHIP_TYPES]; struct PLAYER { char grid[BOARD_WIDTH][BOARD_HEIGHT]; }player[3]; //Ignore player 0, just using player's 1 & 2 enum DIRECTION {HORIZONTAL,VERTICAL}; struct PLACESHIPS { DIRECTION direction; SHIP shipType; }placeShip; //Functions void LoadShips(); void ResetBoard(); void DrawBoard(int); PLACESHIPS UserInputShipPlacement(); int main() { LoadShips(); ResetBoard(); //"PLACE SHIPS" phase of game //Loop through each player... for (int aplyr=1; aplyr<3; ++aplyr) { //Loop through each ship type to place for (int thisShip=0; thisShip> d >> x >> y; if (d!=0 && d!=1) return tmp; if (x<0 || x>=BOARD_WIDTH) return tmp; if (y<0 || y>=BOARD_HEIGHT) return tmp; //Good data tmp.direction = (DIRECTION)d; tmp.shipType.onGrid[0].X = x; tmp.shipType.onGrid[0].Y = y; return tmp; } void LoadShips() { //Sets the default data for the ships //we plan to include in the game //IMPORTANT!! > MUST MATCH SHIP_TYPES -Default=5 (0-4) ship[0].name = "Cruiser"; ship[0].length = 2; ship[1].name = "Frigate"; ship[1].length = 3; ship[2].name = "Submarine"; ship[2].length = 3; ship[3].name = "Escort"; ship[3].length = 4; ship[4].name = "Battleship"; ship[4].length = 5; } void ResetBoard() { //Loop through each player for (int plyr=1; plyr<3; ++plyr) { //For each grid point, set contents to 'water' for (int w=0; w= 10) //Numbers 2 characters long, add only 1 space after cout << w << " "; } cout << "\n"; //Loop through each grid point and display to console for (int h=0; h
Last edited on
Meh.. ok, so it's about a 95% solution. There's a tiny logic bug where it won't display a hit on the top row for some reason... still, overall it gets the job done...

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264`` ``````#include #include using namespace std; const int BOARD_WIDTH = 15; const int BOARD_HEIGHT = 10; const int SHIP_TYPES = 5; const char isWATER = 247; //ASCII Character Code const char isHIT = 'X'; const char isSHIP = 'S'; const char isMISS = '0'; struct POINT { //A location on the grid defined //by X(horizontal) Y(vertical) coordinates int X; int Y; }; struct SHIP { //Ship name string name; //Total points on the grid int length; //Coordinates of those points POINT onGrid[5]; //0-4 max length of biggest ship //Whether or not those points are a "hit" bool hitFlag[5]; }ship[SHIP_TYPES]; struct PLAYER { char grid[BOARD_WIDTH][BOARD_HEIGHT]; }player[3]; //Ignore player 0, just using player's 1 & 2 enum DIRECTION {HORIZONTAL,VERTICAL}; struct PLACESHIPS { DIRECTION direction; SHIP shipType; }; bool gameRunning = false; //Functions void LoadShips(); void ResetBoard(); void DrawBoard(int); PLACESHIPS UserInputShipPlacement(); bool UserInputAttack(int&,int&,int); bool GameOverCheck(int); int main() { LoadShips(); ResetBoard(); //"PLACE SHIPS" phase of game //Loop through each player... for (int aplyr=1; aplyr<3; ++aplyr) { //Loop through each ship type to place for (int thisShip=0; thisShip> x >> y; if (x<0 || x>=BOARD_WIDTH) return goodInput; if (y<0 || y>=BOARD_HEIGHT) return goodInput; goodInput = true; return goodInput; } PLACESHIPS UserInputShipPlacement() { int d, x, y; PLACESHIPS tmp; //Using this as a bad return tmp.shipType.onGrid[0].X = -1; //Get 3 integers from user cin >> d >> x >> y; if (d!=0 && d!=1) return tmp; if (x<0 || x>=BOARD_WIDTH) return tmp; if (y<0 || y>=BOARD_HEIGHT) return tmp; //Good data tmp.direction = (DIRECTION)d; tmp.shipType.onGrid[0].X = x; tmp.shipType.onGrid[0].Y = y; return tmp; } void LoadShips() { //Sets the default data for the ships //we plan to include in the game //IMPORTANT!! > MUST MATCH SHIP_TYPES -Default=5 (0-4) ship[0].name = "Cruiser"; ship[0].length = 2; ship[1].name = "Frigate"; ship[1].length = 3; ship[2].name = "Submarine"; ship[2].length = 3; ship[3].name = "Escort"; ship[3].length = 4; ship[4].name = "Battleship"; ship[4].length = 5; } void ResetBoard() { //Loop through each player for (int plyr=1; plyr<3; ++plyr) { //For each grid point, set contents to 'water' for (int w=0; w= 10) //Numbers 2 characters long, add only 1 space after cout << w << " "; } cout << "\n"; //Loop through each grid point and display to console for (int h=0; h
Topic archived. No new replies allowed.