C programming Problem In this program you will be mimicking
C programming
Problem:
In this program you will be mimicking a game of Battleship. Your game board is a line of 10 slots. You have to implement 3 methods. The following methods must be implemented:
void setBoard(int *)
This method sets up the human’s game board. The method prompts the user for 2 slots to place the “ship”. The program should ensure that the slots are consecutive. In other words, your ship cannot be placed at slot 2 and at slot 6. Since the board has only 10 slots, valid slots are in the range of 0 to 9.
void setComputerBoard(int *)
Exactly the same as the setBoard method except that the position of the computer’s ship is set randomly. The computer’s ship must also be in consecutive slots.
int playGame(int *, int *)
This method should do the following steps:
Have the computer pick at random a slot to “fire” at. If the slot the computer picked on the human’s board is a ship, a “HIT” message is displayed. If the slot on the human’s board is empty, a “MISS” message is displayed. Print out the computer’s board and the human’s board as described at the bottom of the page.
After the computer goes, have the human pick a slot to “fire” at. If the slot the human picked on the computer’s board is a ship, a “HIT” message is displayed. If the slot on the computer’s board is empty, a “MISS” message is displayed. Print out the computer’s board and the human’s board as described at the bottom of the page.
Repeat steps 1 and 2 until there is a winner. The computer wins if both of the human’s ship slots are hit. The human wins if both of the computer’s ship slots are hit.
The playGame method returns a 0 if the computer won. The playGame method returns a 1 if the human won.
The computer board is printed off with the following characters: (The human board is printed off similarly)
S – printed off in the slots where the computer’s ship is located.
M - printed off in the slots where the human guessed incorrectly.
H - printed off in the slots where the ship is located and where the human hit.
* - printed off in all other slots
A sample run of the code is below –
Enter 1st position: 4
Enter 2nd position: 5
Computer guesses 9
MISS!
Human Board:
0 1 2 3 4 5 6 7 8 9
* * * * S S * * * M
Computer Board:
0 1 2 3 4 5 6 7 8 9
* * S S * * * * * *
Enter guess: 4
You guessed 4
MISS!
Human Board:
0 1 2 3 4 5 6 7 8 9
* * * * S S * * * M
Computer Board:
0 1 2 3 4 5 6 7 8 9
* * S S M * * * * *
Computer guesses 4
HIT!
Human Board:
0 1 2 3 4 5 6 7 8 9
* * * * H S * * * M
Solution
#include <iostream>
#include <cstdlib>
#include <ctime>
class Board {
// The first and last rows and columns are outside the board. This
// simplifies the code by reducing in-bound checks. I am fixing the
// board size to 10x10 for convenience.
char array[12][12];
bool try_horizontally(int x, int y, int length) {
// Verify it fits
for (int i=0; i<length; ++i) {
if (array[x+i][y] != \'_\')
return false;
}
// Mark sourrounding area
for (int i=-1; i<=length; ++i) {
for (int j=-1; j<=1; ++j)
array[x+i][y+j] = \'.\';
}
// Mark the ship itself
for (int i=0; i<length; ++i)
array[x+i][y] = \'#\';
}
void remove_horizontally(int x, int y, int length) {
for (int i=0; i<length; ++i)
array[x+i][y] = \'_\';
}
bool try_vertically(int x, int y, int length) {
// Verify it fits
for (int i=0; i<length; ++i) {
if (array[x][y+i] != \'_\')
return false;
}
// Mark sourrounding area
for (int i=-1; i<=length; ++i) {
for (int j=-1; j<=1; ++j)
array[x+j][y+i] = \'.\';
}
// Mark the ship itself
for (int i=0; i<length; ++i)
array[x][y+i] = \'#\';
}
void remove_vertically(int x, int y, int length) {
for (int i=0; i<length; ++i)
array[x+i][y] = \'_\';
}
bool recursive_random_placement(int *begin, int *end) {
if (begin==end)
return true;
int length = *begin++;
for (int attempt = 0; attempt < 16; ++attempt) {
if (rand()%2) {
int x = 1+(rand()%(11-length));
int y = 1+(rand()%10);
if (try_horizontally(x,y,length)) {
if (recursive_random_placement(begin, end))
return true;
remove_horizontally(x,y,length);
}
}
else {
int x = 1+(rand()%10);
int y = 1+(rand()%(11-length));
if (try_vertically(x,y,length)) {
if (recursive_random_placement(begin, end))
return true;
remove_vertically(x,y,length);
}
}
}
return false;
}
friend std::ostream &operator<<(std::ostream &os, Board const &b);
void clean_up() {
for (int i=0; i<12; ++i) {
for (int j=0; j<12; ++j) {
if (array[i][j]==\'.\')
array[i][j]=\'_\';
}
}
}
public:
void clear() {
for (int i=0; i<12; ++i)
for (int j=0; j<12; ++j)
array[i][j]=\'_\';
}
void random_placement(int *begin, int *end) {
clear();
while (!recursive_random_placement(begin,end)) {
}
clean_up();
}
};
std::ostream &operator<<(std::ostream &os, Board const &b) {
for (int i=1; i<11; ++i) {
for (int j=1; j<11; ++j)
os << b.array[j][i] << \' \';
os << \'\ \';
}
}
int main() {
std::srand(std::time(0));
Board b;
int lengths[6]={5,4,3,3,2,2};
b.random_placement(lengths, lengths+6);
std::cout << b << \'\ \';
}





