Deadline is Wednesday October 19th 2016 at 1159 PM Please H
Solution
#include <iostream>
using namespace std;
char grid[15][15];
int row, col;
int putship(char ship, int spots)
{
int dir; int i;
while (1) {
row = rand() %15;
col = rand() %15;
dir = rand() %4;
if (grid[row][col] != \'.\')
continue;
if (dir == 0) { // left direction
if ((col - spots + 1) < 0)
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 == 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) {
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()
{
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];
for (row = 0; row < 15; row++) {
for (col = 0; col < 15; col++) {
grid[row][col] = \'.\';
}
}
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
for (row = 0; row < 15; row++) {
for (col = 0; col < 15; col++) {
map[row][col] = grid[row][col];
}
}
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];
if ((type != \'.\') && (type != \'X\')) {
int pos;
int remains = 0;
cout << \"HIT!\ \";
grid[ro][co] = \'X\';
for (pos = 0; pos < 15; pos++) {
if ((grid[ro][pos] == type) || (grid[pos][co] == type)) {
remains = 1;
break;
}
}
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\ \";
}
if (sunk == 5) {
cout << \"You sunk all my battleships!\ \";
} else {
cout << \"You ran out of shots\ \";
}
for (row = 0; row < 15; row++) {
for (col = 0; col < 15; col++) {
grid[row][col] = map[row][col];
}
}
printGrid();
return 0;
}
![Deadline is Wednesday .. October 19th 2016 at 11:59 PM. Please Help ..Solution#include <iostream> using namespace std; char grid[15][15]; int row, col; in Deadline is Wednesday .. October 19th 2016 at 11:59 PM. Please Help ..Solution#include <iostream> using namespace std; char grid[15][15]; int row, col; in](/WebImages/32/deadline-is-wednesday-october-19th-2016-at-1159-pm-please-h-1091444-1761574764-0.webp)
![Deadline is Wednesday .. October 19th 2016 at 11:59 PM. Please Help ..Solution#include <iostream> using namespace std; char grid[15][15]; int row, col; in Deadline is Wednesday .. October 19th 2016 at 11:59 PM. Please Help ..Solution#include <iostream> using namespace std; char grid[15][15]; int row, col; in](/WebImages/32/deadline-is-wednesday-october-19th-2016-at-1159-pm-please-h-1091444-1761574764-1.webp)
![Deadline is Wednesday .. October 19th 2016 at 11:59 PM. Please Help ..Solution#include <iostream> using namespace std; char grid[15][15]; int row, col; in Deadline is Wednesday .. October 19th 2016 at 11:59 PM. Please Help ..Solution#include <iostream> using namespace std; char grid[15][15]; int row, col; in](/WebImages/32/deadline-is-wednesday-october-19th-2016-at-1159-pm-please-h-1091444-1761574764-2.webp)