Battleship program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Olivia Jaden
    New Member
    • Jun 2007
    • 2

    Battleship program

    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

    [code=cpp]#include <iostream>
    #include <windows.h>
    using namespace std;

    // create the boards as global arrays (accessible to ALL functions!)
    char PlayerBoard[8][8] = { { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
    };

    char ComputerBoard[8][8] = { { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
    { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
    };
    // 'w' == water
    // 'S' == ship
    // 'x' == missed, hit water
    // 'o' == hit, hit sunk ship


    void placeShipsRando mly();
    void printBoards();

    void placeShipsRando mly()
    {

    }


    void printBoards()
    {

    }[/code]

    Any help will be so appreciated...T hank you so much

    Olivia
    Last edited by AdrianH; Jun 10 '07, 12:27 PM. Reason: Please use [code=cpp][/code] tags to improve readability.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Check out the rand and srand functions:
    LINK
    LINK
    Last edited by AdrianH; Jun 10 '07, 12:28 PM. Reason: Removed excessive quoting.

    Comment

    • Olivia Jaden
      New Member
      • Jun 2007
      • 2

      #3
      Originally posted by Olivia Jaden
      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

      Code:
      #include <iostream>
      #include <windows.h>
      using namespace std;
      
      // create the boards as global arrays (accessible to ALL functions!)
      char PlayerBoard[8][8] = {	             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
      						};
      
      char ComputerBoard[8][8] =  {             { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      							{ 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
      						};
      // 'w' == water
      // 'S' == ship
      // 'x' == missed, hit water
      // 'o' == hit, hit sunk ship
      
      
      void placeShipsRandomly();	
      void printBoards();			
      
      void placeShipsRandomly()
      {
      	
      }
      
      
      void printBoards()
      {
      
      }
      Any help will be so appreciated...T hank 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..

      [code=cpp]#include <iostream>
      #include <windows.h>
      using namespace std;

      // create the boards as global arrays (accessible to ALL functions!)
      char PlayerBoard[8][8] = { { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
      };

      char ComputerBoard[8][8] = { { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' },
      { 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w' }
      };
      // 'w' == water
      // 'S' == ship
      // 'x' == missed, hit water
      // 'o' == hit, hit sunk ship

      void printBoards();
      void printBoards()
      {
      for ( int pRow = 0; pRow<8; pRow++)
      {
      for ( int pColumn=0; pColumn<8;pColu mn++)
      {
      cout << PlayerBoard [pRow][pColumn];

      }
      cout << endl << endl;
      }


      for ( int pRow = 0; pRow<8; pRow++)
      {
      for ( int pColumn=0; pColumn<8;pColu mn++)
      {
      cout << PlayerBoard [pRow][pColumn];

      }
      cout << endl;
      }
      }[/code]

      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
      Last edited by AdrianH; Jun 10 '07, 12:30 PM. Reason: Please use [code=cpp][/code] tags to improve readability.

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        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?
        Last edited by AdrianH; Jun 10 '07, 12:31 PM. Reason: Removed excessive quoting.

        Comment

        • inihility
          New Member
          • Jun 2007
          • 18

          #5
          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:

          Code:
          cout << "Computer's Board \t" << "\tPlayer's Board" <<  endl;
          
          
          for (int y = 0; y < 8; y++)
          {
          	for (int x = 0; x < 8; x++)
          	{
          		cout << computerBoarddisplay[x][y] << " ";
          		if (x == 7)
          		{
          			cout << "\t\t";
          			for (int p = 0; p < 8; p++)
          			{
          				for (int q = 0; q < 8; q++)
          				{	
          					cout << playerBoard[q][p]<< " ";
          				}
          			}
          		}
          	}	
          	cout << "\n";
          }
          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

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            It's because u have switched indexes:

            [CODE=cpp]cout << "\t\t";
            for (int p = 0; p < 8; p++)
            {
            for (int q = 0; q < 8; q++)
            {
            cout << playerBoard[q][p]<< " ";
            }
            }
            cout << "\n";
            }[/CODE]

            this should be:

            [CODE=cpp]cout<<playerBoa rd[p][q];[/CODE]


            And please use code tags when posting ur code
            Savage

            Comment

            • inihility
              New Member
              • Jun 2007
              • 18

              #7
              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:
              Code:
              if (q == 7)
              {
              	continue;
              }
              but it didn't change a thing...need some expert help!

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #8
                Originally posted by inihility
                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:
                Code:
                if (q == 7)
                {
                	continue;
                }
                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

                Comment

                • inihility
                  New Member
                  • Jun 2007
                  • 18

                  #9
                  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.

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #10
                    Originally posted by inihility
                    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:

                    Code:
                    cout << "Computer's Board \t" << "\tPlayer's Board" <<  endl;
                    
                    
                    for (int y = 0; y < 8; y++)
                    {
                    	for (int x = 0; x < 8; x++)
                    	{
                    		cout << computerBoarddisplay[x][y] << " ";
                    		if (x == 7)
                    		{
                    			cout << "\t\t";
                    			for (int p = 0; p < 8; p++)
                    			{
                    				for (int q = 0; q < 8; q++)
                    				{	
                    					cout << playerBoard[q][p]<< " ";
                    				}
                    			}
                    		}
                    	}	
                    	cout << "\n";
                    }
                    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:

                    [CODE=cpp]for (int y = 0; y < 8; y++)
                    {
                    for (int x = 0; x < 8; x++)
                    {
                    cout << computerBoarddi splay[x][y] << " ";
                    if (x == 7)
                    {
                    cout << "\t\t";
                    for (int p = 0; p < 8; p++)//this loop should not exist
                    {
                    for (int q = 0; q < 8; q++)
                    {
                    cout << playerBoard[q][p]<< " ";//this should be: cout<<playerBoa rd[y][q];
                    }
                    //U need here a endl;
                    }
                    }
                    }
                    [/CODE]


                    This should do it.

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

                    Savage

                    Comment

                    • inihility
                      New Member
                      • Jun 2007
                      • 18

                      #11
                      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? =)

                      Comment

                      • Savage
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1759

                        #12
                        Originally posted by inihility
                        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

                        Comment

                        • ilikepython
                          Recognized Expert Contributor
                          • Feb 2007
                          • 844

                          #13
                          Originally posted by Savage
                          Now that I taked better look at this I see a lot of problems:

                          [CODE=cpp]for (int y = 0; y < 8; y++)
                          {
                          for (int x = 0; x < 8; x++)
                          {
                          cout << computerBoarddi splay[x][y] << " ";
                          if (x == 7)
                          {
                          cout << "\t\t";
                          for (int p = 0; p < 8; p++)//this loop should not exist
                          {
                          for (int q = 0; q < 8; q++)
                          {
                          cout << playerBoard[q][p]<< " ";//this should be: cout<<playerBoa rd[y][q];
                          }
                          //U need here a endl;
                          }
                          }
                          }
                          [/CODE]


                          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:
                          [code=cpp]
                          for (int y = 0; y < 8; y++)
                          {
                          for (int x = 0; x < 16; x++)
                          {
                          if (x == 8)
                          {
                          cout << "\t";
                          }

                          if (x < 8)
                          {
                          cout << computerBoarddi splay[y][x] << " ";
                          }

                          else if (x >= 8)
                          {
                          cout << playerBoarddisp lay[y][x - 8] << " ";
                          }
                          }
                          }
                          [/code]
                          Using the same for loops as the original only the inner one goes to 16.

                          Comment

                          • inihility
                            New Member
                            • Jun 2007
                            • 18

                            #14
                            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:

                            Code:
                            cin >> playerCoordx, playerCoordy;
                            I changed it to:

                            Code:
                            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.

                            Comment

                            • inihility
                              New Member
                              • Jun 2007
                              • 18

                              #15
                              (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

                              Comment

                              Working...