The program should be written in C++ 2. Write a program to play a game in which
ID: 665024 • Letter: T
Question
The program should be written in C++
2. Write a program to play a game in which you try to sink a fleet of five navy vessels by guessing their locations on a grid. The program uses random numbers to position its ships on a 15 X 15 grid. The ships are of different lengths as follows: Frigate: 2 locations Tender: 2 locations Destroyer: 3 locations Cruiser: 3 locations Carrier: 4 locations The program must pick one square as the starting location, then pick the direction of the ship on the board, and mark off the number of squares in that direction to represent the size of the ship. It must not allow a ship to overlap with another ship or to run off the board. The user enters coordinates in the range of 1 through 15 for the rows and A through O for the columns. The program checks this location, and reports whether it is a hit or a miss. If it is a hit, the program also checks whether the ship has been hit in every location that it occupies. If so, the ship is reported as sunk, and the program identifies which ship it is. The user gets 6o shots to attempt to sink the fleet. If the user sinks all of the ships before using all 6o shots, then he or she wins the game. At the end of the game, the program should output the grid, so that the user can see where the ships are located.Explanation / Answer
printGrid(); // DEBUG
at line 130 when you don't need it anymore.
<pre>
#include <iostream>
using namespace std;
char grid[15][15]; // 15x15 playing grid
int row, col; // rows and columns of the grid
// Put a ship on the map
int putship(char ship, int spots)
{
int dir; // direction
int i;
// Keep trying to find an open spot for the ship
while (1) {
// get a random row, column, direction
row = rand() %15;
col = rand() %15;
dir = rand() %4;
if (grid[row][col] != '.')
continue;
if (dir == 0) { // left direction
// check if putting the ship to the left will go out of bounds
// if it doesn't work, continue so we can do the while loop
// again and choose a new position randomly
if ((col - spots + 1) < 0)
continue;
// check if the spots are already taken by another ship
for (i = 1; i < spots; i++) {
if (grid[row][col - i] != '.')
continue;
}
// mark the ship into the grid
for (i = 0; i < spots; i++) {
grid[row][col - i] = ship;
}
} else if (dir == 1) { // up
if ((row - spots + 1) < 0) {
continue;
}
for (i = 1; i < spots; i++) {
if (grid[row - i][col] != '.') {
continue;
}
}
for (i = 0; i < spots; i++) {
grid[row - i][col] = ship;
}
} else if (dir == 2) { // right
if ((col + spots) > 15) {
continue;
}
for (i = 1; i < spots; i++) {
if (grid[row][col + i] != '.') {
continue;
}
}
for (i = 0; i < spots; i++) {
grid[row][col + i] = ship;
}
} else if (dir == 3) { // down
if ((row + spots) > 15) {
continue;
}
for (i = 1; i < spots; i++) {
if (grid[row + i][col] != '.') {
continue;
}
}
for (i = 0; i < spots; i++) {
grid[row + i][col] = ship;
}
}
break;
}
}
void printGrid()
{
// print the grid
cout << " ABCDEFGHIJKLMNO"<<endl;
for (row = 0; row < 15; row++) {
printf("%2d ", row+1);
for (col = 0; col < 15; col++) {
cout << grid[row][col];
}
cout << endl;
}
}
int main()
{
int shots = 60;
int sunk = 0;
char map[15][15];
// Initialize everything to zero
for (row = 0; row < 15; row++) {
for (col = 0; col < 15; col++) {
grid[row][col] = '.';
}
}
// Add the ships into the grid
putship('F', 2); // F for Frigate
putship('T', 2); // T for Tender
putship('D', 3); // D for Destroyer
putship('C', 3); // C for Cruiser
putship('A', 4); // A for Carrier
// Save a copy of the grid for display
for (row = 0; row < 15; row++) {
for (col = 0; col < 15; col++) {
map[row][col] = grid[row][col];
}
}
// Have user fire a shot and see if it hit anything
while (shots-- > 0) {
int r;
char c;
int ro, co;
char type;
printGrid(); // DEBUG
cout<<"enter position (for example B3): ";
cin >> c >> r;
ro = r-1;
co = c-'A';
type = grid[ro][co];
// check if it hit a ship spot
if ((type != '.') && (type != 'X')) {
int pos;
int remains = 0;
cout << "HIT! ";
grid[ro][co] = 'X';
// Check row and column to see if any part of the ship remains
for (pos = 0; pos < 15; pos++) {
if ((grid[ro][pos] == type) || (grid[pos][co] == type)) {
remains = 1;
break;
}
}
// check if a ship has been completely sunk
if (remains == 0) {
cout << "You sunk my ";
if (type == 'F') {
cout << "frigate! ";
} else if (type == 'T') {
cout << "tender! ";
} else if (type == 'D') {
cout << "destroyer! ";
} else if (type == 'C') {
cout << "cruiser! ";
} else if (type == 'A') {
cout << "carrier! ";
}
sunk++;
if (sunk == 5) {
break;
}
}
} else {
cout << "MISSED! ";
grid[ro][co] = 'X';
}
cout << shots << " Shots left ";
}
// check if all ships are sunk
if (sunk == 5) {
cout << "You sunk all my battleships! ";
} else {
cout << "You ran out of shots ";
}
// restore the original grid for display
for (row = 0; row < 15; row++) {
for (col = 0; col < 15; col++) {
grid[row][col] = map[row][col];
}
}
printGrid();
return 0;
}
</pre>
This is one more answer go through
When designing a program one of the first questions you need to address is what sort of data structures you're going to need. The "15 x 15 grid" suggests a 2-dimensional array. What you are going to put into the array are numbers representing what lies at each location. So start with something like:
enum position { OPEN, FRIG, TEND, DEST, CRUS, CARR };
position grid[15][15];
Then initialize your grid with:
for( int i=0; i<15; i++) {
for( int j=0; j<15; j++ {
grid[i][j] = OPEN;
}
}
Then place your ships with something like:
x = random(15); // pick a random latitude for starting location
y = random(15); // pick a random longitude
dir = random(4); // pick a random direction
// fill in that direction
Do that for each ship. Be sure and check to make sure your ships don't overlap and that none of them go off the grid.
To see if a particular set of coordinates hits a ship:
if( grid[i][j] != OPEN ) { /* whatever */ }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.