#include <iostream>
using namespace std;
bool does_exist(int width, int hight, int row, int col)
{
//returns true if row and col are valid number
if (row >= 0 && col >= 0 && row < width && col < hight) return true;
return false;
}
int main()
{
//option 2
const int hight = 10; //columns
const int width = 10; //rows
int ships; // how many ships will be generated
cin >> ships;
int map[width][hight];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < hight; j++)
{
map[i][j] = 0; //initiolising with zeros
}
}
cout << endl;
//1. simple ship initilising
for (int i = 0; i < ships; i++)
{
int row = rand() % 11;
int col = rand() % 11;
if (map[row][col] == 1) {
//try again
i--;
continue;
}
map[row][col] = 1; // 1 is ship
}
for (int i = 0; i < width; i++)
{
for (int j = 0; j < hight; j++)
{
cout << map[i][j] << " "; //printing map as a result
}
cout << endl;
}
cout << endl;
//2. ships cant be nearby
for (int i = 0; i < width; i++)
{
for (int j = 0; j < hight; j++)
{
map[i][j] = 0; //initiolising with zeros again
}
}
const int delta_row[4] = { -1, 0, 1, 0 };
const int delta_col[4] = { 0, -1, 0, 1 }; //check directions
for (int ship = 0; ship < ships; ship++)
{
int row = rand() % 11;
int col = rand() % 11;
for (int direction_index = 0; direction_index < 4; direction_index++)
{
if (does_exist(width, hight, row + delta_row[direction_index], col + delta_col[direction_index]))
{
if (map[row + delta_row[direction_index]][col + delta_col[direction_index]] == 1)
{
//there is a ship nearby
break;
}
else if (map[row][col] == 1) { break; } //there is a ship on this cell
else map[row][col] = 1;
}
}
}
for (int i = 0; i < width; i++)
{
for (int j = 0; j < hight; j++)
{
cout << map[i][j] << " "; //printing map as a result
}
cout << endl;
}
cout << endl;
//3. firing while all ships are not dead
bool there_are_ships = true;
int fires_count = 0;
int fire_map[width][hight];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < hight; j++)
{
fire_map[i][j] = 0; //initiolising with zeros
}
}
while (there_are_ships)
{
fires_count++;
int row = rand() % 11;
int col = rand() % 11;
if (map[row][col] == 1)
{
map[row][col] = 0;
ships--;
fire_map[row][col] = -1;
continue;
}
if (fire_map[row][col] >= 0) fire_map[row][col]++;
else fire_map[row][col]--; //+=1 if wasnt ship
if (ships == 0)
{
there_are_ships = false;
}
}
for (int i = 0; i < width; i++)
{
for (int j = 0; j < hight; j++)
{
cout << fire_map[i][j] << " "; //printing map as a result
}
cout << endl;
}
cout << endl;
return 0;
}