Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Imagine we are using a two-dimensional array as the basis for creating the game

ID: 3768985 • Letter: I

Question

Imagine we are using a two-dimensional array as the basis for creating the game battleship. In the game of battleship a '~' character entry in the array represents ocean, a '#' character represents a place ion the ocean where part of a ship is present, and an 'H' character represents a place in the ocean where part of a ship is present and has been hit by a torpedo. Thus, a ship with all 'H' characters means the ship has been sunk. Declare a two-dimensional array that is 25x25 that represents the entire ocean and an If statement that prints "HIT" if a torpedo hits a ship given the coordinates X and Y. Write a C++ progam that will read in a file representing a game board with 25 lines where each line has 25 characters corresponding to the description above. An example file might look like:

-------------------------
-------------------------
-------------------------
-----#####---------------
-------------------------
---------------#---------
---------------#---------
---------------#---------
---------------#---------
---------------#---------
-------------------------
-----------#####---------
-------------------------
-#####-------------------
-------------------------
---------#####-----------
------------------#------
------------------#------
------------------#------
------------------#------
--#####-----------#------
-------------------------
-------------------------

-------------------------
-------------------------

(not displaying exactly, should be 25x25)

You should write a function called Fire that will take an X adn Y coordinate and print "HUT" if a ship is hit and "MISS" if a ship is missed. If a ship is HIT you shoul update the array with an 'H' character to indicate the ship was nhit. If a ship is hit thath as alreadty been hit at that location you should print "HIT AGAIN". You should write a second function called FleetSunk that will determine if all the ships have been sunk. Your C++ program must then call these functions until all the ships have been sunk, at which point the program should display "The fleet was destroyed!".

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string.h>

using namespace std;

void Fire(/* inout*/char board);

void FleetSunk(/*in*/char board ,/*inout*/ int&);

void main()
{
   char board[25][25];       // Create a 25 x 25 array for gameboard


   // get board.dat and get ready to assign the value to the board
   ifstream infile;
   infile.open("board.dat");
  
   if ( !infile){               //check file
       cout << "Can not open file." << endl;
   }

// nested for loops to assign chars in the text file to spaces in an array.
   for (int i = 0; i < 25; i++) {           // goes through each row
       for (int j = 0; j < 25; j++){   //goes through each column
           char bS = 0;                   // bS is the character given that is being assigned to a space on the board  
           infile.get(bS);                   // get character from "board.dat"               
           board[i][j] = bS;        //assign that character to board[i][j]
       }
   }
  

   int fS = 0;                   // Create an int variable to determine when to exit loop   
  
   do {
      
      
       Fire(board);       //calls the fire function, passing board by reference
       FleetSunk(board, fS);   // calls the FleetSunk function passing board by value and fS by reference
  
   }
   while(fS == 0 );           // closes loop is all of the ships have been sunk

  
   system("PAUSE");
}


// The Fire function takes in board[] by reference and uses it for the fire function
void Fire(char board[25][25])
{
   int row = 0;           //initializes the int x and y that will represent row and column respectively
   int col = 0;

   cout << "Enter the Row and Column that you would like to try and hit :" ;
   cin >> row;   //
   cin >> col;


   switch (board[row][col]){       // switch statement to determine what character is at board[x][y]
       case '#':                   // if ship(#) then print 'Hit' and replace '#' with 'H'
           if (board[row-1][col] == 'H'){           //checks to see if any of the spaces around the spot hit has been hit previously
               cout << "HIT AGAIN" << endl;
           board[row][col] = 'H';
           }
           else if (board[row+1][col] == 'H'){
               cout << "HIT AGAIN" << endl;
           board[row][col] = 'H';
           }
           else if (board[row][col-1] == 'H'){
               cout << "HIT AGAIN" << endl;
           board[row][col] = 'H';
           }
           else if (board[row][col+1] == 'H'){
               cout << "HIT AGAIN" << endl;
           board[row][col] = 'H';
           }
           else{
           cout << "HIT" << endl;           // if there is no ship next to the spot hit then dispay "hit"
           board[row][col] = 'H';
           }
           break;
       case '~' :               // if ocean(~) then print miss and move on
           cout << "MISS" << endl;
           break;
       case 'H' :               // if 'H' then that means user already hit this location.
           cout << "You already destroyed these coordinates.";
           break;

   }

}
// checks game board for any #/ unhit ship pieces
void FleetSunk(char board[25][25], int& fS) {
   for (int i= 0; i < 25; i++){
       for (int j=0 ; j< 25; j++){
           if (board[i][j] == '#'){
               fS = 0;
               return;           // if there is ship left return back to program
           }
       }
   }  
   cout << "The Fleet has been destroyed!" << endl;
               fS =1;               // if there is no longer any # left fS = 1;
                  
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote