Battleship program

This is a discussion on Battleship program within the C++ Programming forums, part of the General Programming Boards category; hi Guys. My book gives me no direction on how to do this, but its an exercise in a book. ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Battleship program

    hi Guys.

    My book gives me no direction on how to do this, but its an exercise in a book. I am to create a simple battleship console game.

    I have been doing ok, ( I am not allowed to use OOP ) but I have run into a small snag. I am doing this bit by bit, but when I enter the coordinates to place the player ships, I assign them ok. When I go to print the array to see the positions i have entered, they are correct, bu instead of printing ##### for the first ship, it keeps printing a single hash for all the ships, although the coordinates are correct.

    Should I be using strings and not char arrays to store the ships?

    Code:
    // function prototypes
    void setUpComputerBoard ( char [][ 20 ], const char*[] );
    void setUpPlayerBoard ( char [][ 20 ],  char*[] );
    void showGameBoard ( char[][ 20 ], char[][ 20 ], char*[] );
    
    // main function - driver //////////////////////////////////////////////////////
    //
    int main ( void )
    {
       srand((unsigned)time(0));
    
       const int MAX_ROWS = 20;
       const int MAX_COLS = 20;
       
       char playerBoard[ MAX_ROWS ][ MAX_COLS ] = {{ 0 , 0 }};
       
       char *pPlayerShips[ 5 ] = { "#####", "####", "###", "##", "#" };
       
       const char *pComputerShips[ 5 ] = { "#####", "####", "###", "##", "#" };
                                     
       char computerBoard[ MAX_ROWS ][ MAX_COLS ] = {{ 0, 0 }};
       
       setUpPlayerBoard ( playerBoard, pPlayerShips );
       showGameBoard ( playerBoard, computerBoard, pPlayerShips );
          
       std::cin.get(); // freeze console output window
       std::cin.ignore();
       
       return 0; // return value from int main
    }
    
    // function to set up the computer opponents board
    
    // function to set up the player board
    void setUpPlayerBoard ( char plBoard[][ 20 ],  char *pPlShips[] )
    {
       int xCorr = 0;
       int yCorr = 0;
       
       for ( int i = 0; i < 5; )
       {
          std::cout << "Enter y coordinate for ship: " << pPlShips[ i ]
                    << ": ";
          std::cin >> xCorr;
          
          std::cout << "Enter x coordinate for ship: " << pPlShips[ i ]
                    << ": ";
          std::cin >> yCorr;
          
          plBoard[ xCorr ][ yCorr ] = *pPlShips[ i ];
          i++;
       }  
    }
    
    void showGameBoard ( char plBoard[][ 20 ], char ComBoard[][ 20 ], char *pPlShips[] )
    {
       for ( int i = 0; i < 20; i++ )
       {
          for ( int j = 0; j < 20; j++ )
             std::cout << plBoard[ i ][ j ];
             std::cout << std::endl;
       }
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    Let's say that you can only place the ships horizontally. In this scenario, you could get the x/y coordinate for the most left '#'. Then, you would loop the size of the ship to place the hashes on the board.

    Something to this tune:

    Code:
    ...
    // if you had the 5 piece ship
    for(int i = 0; i < 5; i++) {
        plBoard[xCoord + i][yCoord] = "#";
    }
    ...
    Ofcourse you would have to check the position to make sure there is room. You may also need to change some of the details of your program. I don't know if vertical positioning is something you are required to do or not. This snipet should give you an idea of the direction to go (I hope!)

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Your problem is that when setting up the board you are setting a hash at the co-ords that the player places the ship, but not drawing out the rest of the ship. You also may want to ask the player if they are plcing their ship horizontally or vertically.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    thanks guys. Il take your advice on board and see what I can do
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Hey again!

    I wont bother repeating the above code but I have come up to the actual battle system now. All I have done to the original code above is added one new function which is below. I am trying to enter the coordinates to attack the computer ships. It compiles ok but I want to know am I on the right idea with how to go about this?

    Code:
    // function to play the main game
    void game ( char plBoard[][ 20 ], char comBoard[][ 20 ] )
    {
       int xMovement = 0;
       int yMovement = 0;
       
       enum GameStatus
       {
          WIN,
          LOST,
          CONTINUE
       };
       
       GameStatus status = CONTINUE;
       
       while ( status == CONTINUE )
       {
          std::cout << "\nEnter x position for attack: ";
          std::cin >> xMovement;
          std::cout << "Enter y position for attac: ";
          std::cin >> yMovement;
          
          for ( int i = 0; comBoard[ i ][ 0 ]; i++ )
          {
             if (( xMovement == comBoard[ i ][ 0 ] ) || ( yMovement == comBoard[ i ][ 0 ] ))
             {
                std::cout << "\nYou scored a hit!\n";   
             }
          
             else
             {
                std::cout << "\nNope, no armada there!\n";
             }
          }
       } 
    }
    I have no help at all from my book as it is an exercises. I have done well so far. If I am totally
    wrong then I applogise, but I think what I have is along the right tracks.

    Thanks for any help in advance
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21