By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Battleship program

P: 2
Hi,

I was about to create a 8x8 battleship program.
there should be 8 ships in total with 1x1 size.
However, I do not know how to place the ships randomly.
Below are some of the codes that I started after doing some research

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4.  
  5. // create the boards as global arrays (accessible to ALL functions!)
  6. char PlayerBoard[8][8] = {                 { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  7.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  8.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  9.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  10.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  11.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  12.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  13.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
  14.                         };
  15.  
  16. char ComputerBoard[8][8] =  {             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  17.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  18.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  19.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  20.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  21.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  22.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  23.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
  24.                         };
  25. // 'w' == water
  26. // 'S' == ship
  27. // 'x' == missed, hit water
  28. // 'o' == hit, hit sunk ship
  29.  
  30.  
  31. void placeShipsRandomly();    
  32. void printBoards();            
  33.  
  34. void placeShipsRandomly()
  35. {
  36.  
  37. }
  38.  
  39.  
  40. void printBoards()
  41. {
  42.  
  43. }
Any help will be so appreciated...Thank you so much

Olivia
Jun 9 '07 #1
Share this Question
Share on Google+
19 Replies


ilikepython
Expert 100+
P: 844
Check out the rand and srand functions:
LINK
LINK
Jun 9 '07 #2

P: 2
Hi,

I was about to create a 8x8 battleship program.
there should be 8 ships in total with 1x1 size.
However, I do not know how to place the ships randomly.
Below are some of the codes that I started after doing some research

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4.  
  5. // create the boards as global arrays (accessible to ALL functions!)
  6. char PlayerBoard[8][8] = {                 { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  7.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  8.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  9.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  10.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  11.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  12.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  13.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
  14.                         };
  15.  
  16. char ComputerBoard[8][8] =  {             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  17.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  18.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  19.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  20.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  21.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  22.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  23.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
  24.                         };
  25. // 'w' == water
  26. // 'S' == ship
  27. // 'x' == missed, hit water
  28. // 'o' == hit, hit sunk ship
  29.  
  30.  
  31. void placeShipsRandomly();    
  32. void printBoards();            
  33.  
  34. void placeShipsRandomly()
  35. {
  36.  
  37. }
  38.  
  39.  
  40. void printBoards()
  41. {
  42.  
  43. }
Any help will be so appreciated...Thank you so much

Olivia
Hi, before I bother with how to randomly place the ship...i have a question of how to print the player and computer screen..

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4.  
  5. // create the boards as global arrays (accessible to ALL functions!)
  6. char PlayerBoard[8][8] = {                 { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  7.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  8.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  9.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  10.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  11.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  12.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  13.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
  14.                         };
  15.  
  16. char ComputerBoard[8][8] =  {             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  17.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  18.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  19.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  20.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  21.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  22.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
  23.                             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
  24.                         };
  25. // 'w' == water
  26. // 'S' == ship
  27. // 'x' == missed, hit water
  28. // 'o' == hit, hit sunk ship
  29.  
  30. void printBoards();
  31. void printBoards()
  32. {
  33.     for ( int pRow = 0; pRow<8; pRow++)
  34.     {
  35.         for ( int pColumn=0; pColumn<8;pColumn++)
  36.         {
  37.             cout << PlayerBoard [pRow][pColumn];
  38.  
  39.         }
  40.         cout << endl << endl;
  41.     }    
  42.  
  43.  
  44.     for ( int pRow = 0; pRow<8; pRow++)
  45.     {
  46.         for ( int pColumn=0; pColumn<8;pColumn++)
  47.         {
  48.             cout << PlayerBoard [pRow][pColumn];
  49.  
  50.         }
  51.         cout << endl;
  52.     }
  53. }
The above program will only allow me to create the maps one after another. What if I want to have them printed side by side?

Thanks in advance

Olivia
Jun 9 '07 #3

ilikepython
Expert 100+
P: 844
Ok, think about that. You need to print 16 columns and 8 rows (with some space in between). How do you think you have to adjust your for loops do achieve what you need?
Jun 9 '07 #4

P: 18
interesting...I tried to take up the challenge of getting the arrays to display side-by-side, succeeded in a sense, but failed ultimately, heres what I have:

Expand|Select|Wrap|Line Numbers
  1. cout << "Computer's Board \t" << "\tPlayer's Board" <<  endl;
  2.  
  3.  
  4. for (int y = 0; y < 8; y++)
  5. {
  6.     for (int x = 0; x < 8; x++)
  7.     {
  8.         cout << computerBoarddisplay[x][y] << " ";
  9.         if (x == 7)
  10.         {
  11.             cout << "\t\t";
  12.             for (int p = 0; p < 8; p++)
  13.             {
  14.                 for (int q = 0; q < 8; q++)
  15.                 {    
  16.                     cout << playerBoard[q][p]<< " ";
  17.                 }
  18.             }
  19.         }
  20.     }    
  21.     cout << "\n";
  22. }
they do display next to eachother, but the playerBoard function displays more than its supposed to...in a weird way.

I'm pretty new to C++, and I'm curious on how this battleship game program can be done, so I hope there is someone out there that can help both Olivia and my curiosity out.

Thanks.

edited: added code tags
Jun 11 '07 #5

Savage
Expert 100+
P: 1,764
It's because u have switched indexes:

Expand|Select|Wrap|Line Numbers
  1. cout << "\t\t";
  2. for (int p = 0; p < 8; p++)
  3. {
  4.    for (int q = 0; q < 8; q++)
  5.    { 
  6.      cout << playerBoard[q][p]<< " ";
  7.    }
  8. cout << "\n";
  9. }
this should be:

Expand|Select|Wrap|Line Numbers
  1. cout<<playerBoard[p][q];

And please use code tags when posting ur code
Savage
Jun 11 '07 #6

P: 18
my bad, I was wondering why my code looked different from the others, thanks for the heads up.

ps. I don't think there was anything wrong with the indexes...other than the fact that it may have effected how the characters came out...the problem is still left unchanged with how the playerBoard displays chaotically.

I tried adding this:
Expand|Select|Wrap|Line Numbers
  1. if (q == 7)
  2. {
  3.     continue;
  4. }
but it didn't change a thing...need some expert help!
Jun 11 '07 #7

Savage
Expert 100+
P: 1,764
my bad, I was wondering why my code looked different from the others, thanks for the heads up.

ps. I don't think there was anything wrong with the indexes...other than the fact that it may have effected how the characters came out...the problem is still left unchanged with how the playerBoard displays chaotically.

I tried adding this:
Expand|Select|Wrap|Line Numbers
  1. if (q == 7)
  2. {
  3.     continue;
  4. }
but it didn't change a thing...need some expert help!
How chaoticly?

Maybe it's reaching end of the screen and then it goes in to other row.

Savage
Jun 11 '07 #8

P: 18
it displays a lot of columns, and I think all the columns are all the same.

e.g.

wwwwswwww....
wwwwswwww..
wwwwswwww...
wwwwswwww..
wwwwswwww..
wwwwswwww..

the first array (computerBoard) displays fine, its just the playerBoard thats doing something wrong.

oh and while were at it, I'm pretty much done writing the program in whole, another problem I encountered is the plotting of the coords that the "player" bombs is incorrect, and the bombing happens twice before the player is allowed to choose where to bomb again...I'll post up the code after the display issue is solved.
Jun 11 '07 #9

Savage
Expert 100+
P: 1,764
interesting...I tried to take up the challenge of getting the arrays to display side-by-side, succeeded in a sense, but failed ultimately, heres what I have:

Expand|Select|Wrap|Line Numbers
  1. cout << "Computer's Board \t" << "\tPlayer's Board" <<  endl;
  2.  
  3.  
  4. for (int y = 0; y < 8; y++)
  5. {
  6.     for (int x = 0; x < 8; x++)
  7.     {
  8.         cout << computerBoarddisplay[x][y] << " ";
  9.         if (x == 7)
  10.         {
  11.             cout << "\t\t";
  12.             for (int p = 0; p < 8; p++)
  13.             {
  14.                 for (int q = 0; q < 8; q++)
  15.                 {    
  16.                     cout << playerBoard[q][p]<< " ";
  17.                 }
  18.             }
  19.         }
  20.     }    
  21.     cout << "\n";
  22. }
they do display next to eachother, but the playerBoard function displays more than its supposed to...in a weird way.

I'm pretty new to C++, and I'm curious on how this battleship game program can be done, so I hope there is someone out there that can help both Olivia and my curiosity out.

Thanks.

edited: added code tags
Now that I taked better look at this I see a lot of problems:

Expand|Select|Wrap|Line Numbers
  1. for (int y = 0; y < 8; y++)
  2. {
  3.     for (int x = 0; x < 8; x++)
  4.     {
  5.         cout << computerBoarddisplay[x][y] << " ";
  6.         if (x == 7)
  7.         {
  8.             cout << "\t\t";
  9.             for (int p = 0; p < 8; p++)//this loop should not exist
  10.             {
  11.                 for (int q = 0; q < 8; q++)
  12.                 {    
  13.                     cout << playerBoard[q][p]<< " ";//this should be: cout<<playerBoard[y][q];
  14.                 }
  15. //U need here a endl;
  16.             }
  17.         }
  18.     }
  19.  

This should do it.

It's also highly recomended that u read this article:
link

Savage
Jun 11 '07 #10

P: 18
thanks that worked perfectly, can't believe I overlooked this, haha well I guess I still have a lot to learn and experience.

lastly I don't want to resort to someone giving me all the answers just yet, so I'll work on the rest of the code a little more before I post it here for assistance, fair enough? =)
Jun 11 '07 #11

Savage
Expert 100+
P: 1,764
thanks that worked perfectly, can't believe I overlooked this, haha well I guess I still have a lot to learn and experience.

lastly I don't want to resort to someone giving me all the answers just yet, so I'll work on the rest of the code a little more before I post it here for assistance, fair enough? =)
Excelent,I love to work with people like u.This is just what ours posting guidelines says.

BTW:Have u readed them?(You are OK at all,but it is recomended that u read them if u haven't)

:D

Savage
Jun 11 '07 #12

ilikepython
Expert 100+
P: 844
Now that I taked better look at this I see a lot of problems:

Expand|Select|Wrap|Line Numbers
  1. for (int y = 0; y < 8; y++)
  2. {
  3.     for (int x = 0; x < 8; x++)
  4.     {
  5.         cout << computerBoarddisplay[x][y] << " ";
  6.         if (x == 7)
  7.         {
  8.             cout << "\t\t";
  9.             for (int p = 0; p < 8; p++)//this loop should not exist
  10.             {
  11.                 for (int q = 0; q < 8; q++)
  12.                 {    
  13.                     cout << playerBoard[q][p]<< " ";//this should be: cout<<playerBoard[y][q];
  14.                 }
  15. //U need here a endl;
  16.             }
  17.         }
  18.     }
  19.  

This should do it.

It's also highly recomended that u read this article:
link

Savage
Hmm, that's interesting. I was thinking something like this:
Expand|Select|Wrap|Line Numbers
  1. for (int y = 0; y < 8; y++)
  2. {
  3.     for (int x = 0; x < 16; x++)
  4.     {
  5.               if (x == 8)
  6.               {
  7.                     cout << "\t";
  8.               }
  9.  
  10.               if (x < 8)
  11.               {
  12.                     cout << computerBoarddisplay[y][x] << " ";
  13.               }
  14.  
  15.               else if (x >= 8)
  16.               {
  17.                     cout << playerBoarddisplay[y][x - 8] << " ";
  18.               }
  19.       }
  20. }
  21.  
Using the same for loops as the original only the inner one goes to 16.
Jun 11 '07 #13

P: 18
I solved it! apparently what really f'd me over (pardon the term) was a minor mistake in storing the two variables with cin.

Though I swear my first method should have worked it didn't. I had:

Expand|Select|Wrap|Line Numbers
  1. cin >> playerCoordx, playerCoordy;
I changed it to:

Expand|Select|Wrap|Line Numbers
  1. cin >> playerCoordx >> playerCoordy;
and it worked...it was a real discovery because I swore I used the first method before and it worked...oh well =)

A big thank you for Savage and the rest for their help, I hope to see you around these boards!

edit: I'm just curious if there is an easier way to mask the computer's board apart from creating another array and modifying both arrays when the player makes a move, however only resorting to the 'real' computer's board when determining hits, misses, and wins/loses.
Jun 11 '07 #14

P: 18
(for some reason I couldn't edit my last post)

@Savage and other experts.

I have a request,

I plan to post up my code in the next few days, the reason being, I know that this is an assignment task (I'm doing it out of leisure and practice), so out of good will if you will, I don't want to disclose this until past the due date for the sake of the people on the assignment.

That being said, I do have a working program, but I see many improvements and some possibly unnecessary code in my program, so I hope that once I post up my battleship program, everyone could have a take on how to improve it (flow-wise, efficiency, and effectiveness, rather than improving the game-interface or game-play).

The purpose of this is to improve insight of my own, and other programmers that were unable to solve or have solved this challenge with difficulty.

Thanks
Jun 11 '07 #15

Savage
Expert 100+
P: 1,764
Hmm, that's interesting. I was thinking something like this:
Expand|Select|Wrap|Line Numbers
  1. for (int y = 0; y < 8; y++)
  2. {
  3.     for (int x = 0; x < 16; x++)
  4.     {
  5.               if (x == 8)
  6.               {
  7.                     cout << "\t";
  8.               }
  9.  
  10.               if (x < 8)
  11.               {
  12.                     cout << computerBoarddisplay[y][x] << " ";
  13.               }
  14.  
  15.               else if (x >= 8)
  16.               {
  17.                     cout << playerBoarddisplay[y][x - 8] << " ";
  18.               }
  19.       }
  20. }
  21.  
Using the same for loops as the original only the inner one goes to 16.
Avoid those if-else statements,they can realy slow down whole programme.

Savage
Jun 11 '07 #16

Savage
Expert 100+
P: 1,764

A big thank you for Savage and the rest for their help, I hope to see you around these boards!
We are more than happy to help u.

edit: I'm just curious if there is an easier way to mask the computer's board apart from creating another array and modifying both arrays when the player makes a move, however only resorting to the 'real' computer's board when determining hits, misses, and wins/loses.
Hmm,might work.Do it and let us know if it works or if u have some problems.

Savage
Jun 11 '07 #17

P: 18
Hmm,might work.Do it and let us know if it works or if u have some problems.

Savage

I did it already, and it works, I was just wondering if there was an easier way to do it...like a pre-made function that could mask the output (cout), like give it a condition display only 'x', and 'o', while masking all the others ('w', 'S') to '*'.

I mean theres nothing wrong with my method...its just cumbersome and can be rather tedious in the long-run.
Jun 12 '07 #18

P: 18
After another close look at the guidelines, I noticed that it was inappriopriate to post up full-codes...so therefore I would like any expert that is willing to take a look at my code to leave a message or pm me and I'll be more than happy to pm the full-code back to you.
Jun 12 '07 #19

Savage
Expert 100+
P: 1,764
After another close look at the guidelines, I noticed that it was inappriopriate to post up full-codes...so therefore I would like any expert that is willing to take a look at my code to leave a message or pm me and I'll be more than happy to pm the full-code back to you.
I will be glad to take a look..

Please,PM me..

Savage
Jun 12 '07 #20

Post your reply

Sign in to post your reply or Sign up for a free account.