I have C++ Plus code below for a game of battleship (one-way single player where the person guesses where the ships are located.) Currently the ships in this game only take up one spot on the grid. Could you modify the code to where there are 5 ships and the 5 ships have these sizes:
1) CARRIER = 5
2) BATTLESHIP = 4
3) CRUISER = 3
4) SUBMARINE = 3
5) DESTROYER = 2
To put this into perspective, this is an introductory C++ class. Currently, we haven't talked about enum or struct so that shouldn't be in the code, however we have talked about vectors. PLEASE DO NOT "MODIFY" MY CODE BY COPYING AND PASTING SOMETHING ENTIRELY DIFFERENT THAT DOESN'T EVEN HAVE THE SAME VARIABLE NAMES . I will thumbs up if the program runs. Also, I put comments throughout to make it easier to follow how things are working.
#include using namespace std; const int SIZE = 10; // Rows Columns const int SHIP_NUM = 5; // Number of ships //Function Prototypes void oceanGridDisplay(char oceanGrid[][SIZE]); void oceanGridBuild(char oceanGrid[][SIZE]); bool isSpaceEmpty(int spot[SHIP_NUM][5], int rowValue, int colValue); void shipPlacer(int spot[SHIP_NUM][5]); void startGame(char oceanGrid[SIZE][SIZE], int spot[SHIP_NUM][5]); // Main Function int main() { srand(time(NULL)); int spot[SHIP_NUM][5]; char oceanGrid[SIZE][SIZE]; oceanGridBuild(oceanGrid); shipPlacer(spot); // This for loop displays the ship's locations for(int i=0;i(spot[i+65][0])<<","<(i+65) << " |"; // Lines up the | perfectly on the grid for (int j = 0; j < SIZE; j++) { cout << " " << oceanGrid[i][j] << " |"; } cout << endl; } // Prints out bottom to close the grid for (int i = 0; i <= SIZE; i++) { cout << "---+"; } cout << endl; } /* This function goes through the grid and through the empty spots, it places a space that be turned into a H for hit or a M for miss */ void oceanGridBuild(char oceanGrid[][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) oceanGrid[i][j] = ' '; } } // This function determines whether a space is empty or not bool isSpaceEmpty(int spot[SHIP_NUM][5], int rowValue, int colValue) { for (int i = 0; i < SHIP_NUM; i++) { /* Because the user has to call in a coordinate with a char and an integer, and because we used an integer in the for loop we need to convert that into an char to actually compare the row values. However, the columns don't need to changed because the columns are represented by integers on the board. */ if (spot[static_cast(i)+65][0] == rowValue && spot[i][1] == colValue) return false; } return true; } void shipPlacer(int spot[SHIP_NUM][5]) { int rowIndex = (rand() % 10); // both randoms might be redundant now that I think about it, Will need variable declarations though int colIndex = (rand() % 10); // Might actually be redundant now that I that I think about it; I'll check later. spot[0][0] = rowIndex; spot[0][1] = colIndex; for (int i = 0; i < SHIP_NUM; i++) { // generates a random spot rowIndex = static_cast(rand() % 10)+65; colIndex = (rand() % 10); if (isSpaceEmpty(spot, rowIndex, colIndex)) // if we find a space that is empty, place a ship there. { spot[static_cast(i)+65][0] = rowIndex; spot[i][1] = colIndex; } else { i--; // decrement the ship because we want the cpu to place only 5 } } } void startGame(char oceanGrid[SIZE][SIZE], int spot[SHIP_NUM][5]) { int count = 0; int counterCount = 0; char row; int col; //While Loop to play the game. It goes until all the ships have been destroyed. while (count != SHIP_NUM) { cout << "Enter a coordinate: "; cin >> row >> col; // while loop to check for valid row and columns // still need to figure out how to prevent the user from doing 3D, 4J, 8A while (col > 9 || col < 0 || row < 'A' || row > 'J') { cout << "Please enter in a valid coordinate: "; cin >> row >> col; } //While loop to check to see if player already launched a missile there while (oceanGrid[row-65][col] == 'H' ||oceanGrid[row-65][col] == 'M') { cout << "You already launched a missile there!" << endl; cout << "Please enter a new coordinate: "; cin >> row >> col; } //Checks to seee if missile actually hits a ship; if (isSpaceEmpty(spot, row, col)) { oceanGrid[row-65][col] = 'M'; counterCount++; cout << "Unfortunately, you missed." << endl; } else { oceanGrid[row-65][col] = 'H'; count++; cout << "Excellent! You hit the ship." << endl; } oceanGridDisplay(oceanGrid); } cout << "The Game is now over." << endl; }