N
Novy
Такое дельце: нужно быстро дописать игру. Проблема в том, что нечто просто заменяет все клетки кораблей на клетку воды перед выводом массивов на экран и в результате игра не работает. Пожалуйста, скажите мне, в чём тут ошибка? И ещё кое-что: как мне сделать так, что-бы клетки кораблей ставились вместе и никак не смогли поставиться отдельно друг от друга? Мне сегодня эту игру нужно сдать, как курсовую работу, так что просьба помочь как можно скорее, иначе меня выгонят из Академии, чего я очень не хочу. Проблема где-то либо в Set_ships либо при выводе массивов. Вот сам код:
C++:
#include<iostream>
#include<ctime>
#include<iomanip>
#include<string>
#include<Windows.h>
#include<windows.h>
using namespace std;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
char type_of_ship[100];
int plant_choise_x, plant_choise_y;
bool cell_is_checked = false;
int torp_checked = 0, destr_checked = 0, crui_checked = 0, bs_checked = 0;
const int FIELD_SIZE = 10;
char PlayerArr[FIELD_SIZE][FIELD_SIZE];
char BotArr[FIELD_SIZE][FIELD_SIZE];
char Player_Name[100];
char Bot_Name[100];
int amount_of_ship_cells_left_to_drown_P1;
int amount_of_ship_cells_left_to_drown_P2;
// [x][y], где x - вертикаль, y - горизонталь
char PlayerWater = '~';
char BotWater = '~';
char PlayerTorpede = 'Т';
char BotTorpede = 'Т';
char PlayerDestroyer = 'Э';
char BotDestroyer = 'Э';
char PlayerCruiser = 'К';
char BotCruiser = 'К';
char PlayerBattleship = 'Л';
char BotBattleship = 'Л';
char PlayerShipHit = 'Х';
char BotShipHit = 'X';
char PlayerMiss = '*';
char BotMiss = '*';
void ifPlayerWin(char* p1) {
system("cls");
cout << "Игрок " << Player_Name << " победил " << Bot_Name << "! Вау! Какая игра... ";
cout << "\n Спасибо за игру!" << endl;
system("pause");
exit(1);
}
void ifBotWin(char* p2) {
system("cls");
cout << "Машина оказалась лучше в Морском Бою чем " << Player_Name << "! Какое разочарование... Бывает...";
cout << "\n Повезёт в следующий раз! Спасибо за игру, " << Bot_Name << "!";
system("pause");
exit(1);
}
void Marker(char arr[][FIELD_SIZE], int x, int y)
{
for (int i = 0; i < 4; i++)
{
if (arr[x][y + i] == PlayerShipHit) {
arr[x][y + i] = PlayerMiss;
arr[x + 1][y + i] = PlayerMiss;
arr[x - 1][y + i] = PlayerMiss;
}
if (arr[x][y - i] == PlayerShipHit) {
arr[x][y - i] = PlayerMiss;
arr[x + 1][y - i] = PlayerMiss;
arr[x - 1][y - i] = PlayerMiss;
}
if (arr[x + i][y] == PlayerShipHit) {
arr[x + i][y] = PlayerMiss;
arr[x + i][y + 1] = PlayerMiss;
arr[x + i][y - 1] = PlayerMiss;
}
if (arr[x - i][y] == PlayerShipHit) {
arr[x - i][y] = PlayerMiss;
arr[x - i][y + 1] = PlayerMiss;
arr[x - i][y - 1] = PlayerMiss;
}
}
arr[x - 1][y - 1] = PlayerMiss;
arr[x - 1][y + 1] = PlayerMiss;
arr[x + 1][y + 1] = PlayerMiss;
arr[x + 1][y - 1] = PlayerMiss;
}
bool Cell_checker(int PlayerNumber, int asked_cell) {
char T;
char D;
char C;
char BS;
char water;
switch (PlayerNumber)
{
case 1:
{
T = PlayerTorpede;
D = PlayerDestroyer;
C = PlayerCruiser;
BS = PlayerBattleship;
water = PlayerWater;
} break;
case 2:
{
T = BotTorpede;
D = BotDestroyer;
C = BotCruiser;
BS = BotBattleship;
water = BotWater;
} break;
}
if ((asked_cell == T || asked_cell == D || asked_cell == C || asked_cell == BS) && asked_cell != water)
return true;
else
return false;
}
void Finisher(char arr[FIELD_SIZE][FIELD_SIZE], int x, int y) {
for (int i = 0; ; i++)
{
if (Cell_checker(1, arr[x + i][y]) == true /* низ */)
arr[x + i][y] = PlayerShipHit;
if (Cell_checker(1, arr[x - i][y]) == true /* верх */)
arr[x - i][y] = PlayerShipHit;
if (Cell_checker(1, arr[x][y + i]) == true /* право */)
arr[x][y + i] = PlayerShipHit;
if (Cell_checker(1, arr[x][y - i]) == true /* лево */)
arr[x][y - i] = PlayerShipHit;
if (Cell_checker(1, arr[x + 1][y - 1]) == true /* нижнее лево */)
arr[x + 1][y - 1] = PlayerShipHit;
if (Cell_checker(1, arr[x + 1][y + 1]) == true /* нижнее право */)
arr[x + 1][y + 1] = PlayerShipHit;
if (Cell_checker(1, arr[x - 1][y - 1]) == true /* верхнее лево */)
arr[x - 1][y - 1] = PlayerShipHit;
if (Cell_checker(1, arr[x - 1][y + 1]) == true /* верхнее право */)
arr[x - 1][y + 1] = PlayerShipHit;
else {
Marker(PlayerArr, plant_choise_x, plant_choise_y);
break;
}
}
}
void Print_table(char arr[][FIELD_SIZE]) {
for (int i = 0; i < FIELD_SIZE + 1; i++)
{
cout << "_ ";
}
cout << endl << "_|";
for (int i = 0; i < FIELD_SIZE; i++)
{
cout << i << "|"; // верхний ряд
}
cout << endl;
char arr_coord_x[10]{ '0','1','2','3','4','5','6','7','8','9' };
for (int i = 0; i < FIELD_SIZE; i++)
{
cout << arr_coord_x[i] << "|"; // боковой левый ряд
for (int j = 0; j < FIELD_SIZE; j++)
{
SetConsoleTextAttribute(h, 1);
cout << arr[i][j];
SetConsoleTextAttribute(h, 7);
cout << "|";
}
cout << endl;
}
}
void Players_names(char* Player, char* Bot) {
cout << "Введите Ваш никнейм : ";
cin >> Player;
cout << "Введите никнейм соперника (бота) : ";
cin >> Bot;
}
void Create_table(char arr[][FIELD_SIZE], char c) {
for (int i = 0; i < FIELD_SIZE; i++)
{
for (int j = 0; j < FIELD_SIZE; j++)
{
arr[i][j] = c;
}
}
}
void put_battleship(char arr[FIELD_SIZE][FIELD_SIZE], int &bs_checked, int PlayerNumber, int plant_choise_x, int plant_choise_y, bool cell_is_checked) {
char BS;
switch (PlayerNumber)
{
case 1:
{
BS = PlayerBattleship;
if ((Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false /* нижняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false /* верхняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false /* левая */
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false /* правая */)
||
/// [1][2][3][4]
(
Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false
&& arr[plant_choise_x][plant_choise_y + 1] == BS
&& arr[plant_choise_x][plant_choise_y + 2] == BS
&& arr[plant_choise_x][plant_choise_y + 3] == BS
// [1]
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false /* нижняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false /* верхняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false /* левая */
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false /* верхнелевая */
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false /* нижнелевая */
// [2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false /* нижняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false /* верхняя */
// [3]
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 2]) == false /* нижняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 2]) == false /* верхняя */
// [4]
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 3]) == false /* нижняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 3]) == false /* верхняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 4] == false) /* правая */
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 4]) == false /* верхнеправая */
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 4]) == false /* нижнеправая */
)
||
/// [4][3][2][1]
(
Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false
&& arr[plant_choise_x][plant_choise_y - 1] == BS
&& arr[plant_choise_x][plant_choise_y - 2] == BS
&& arr[plant_choise_x][plant_choise_y - 3] == BS
// [1]
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false // верхнеправая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false // нижнеправая
// [2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false // нижняя
// [3]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 2]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 2]) == false // нижняя
// [4]
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 4]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 3]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 3]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 4]) == false // нижнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 4]) == false // верхнелевая
)
||
/// [2][*1][3][4]
(
Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false
&& arr[plant_choise_x][plant_choise_y - 1] == BS
&& arr[plant_choise_x][plant_choise_y + 1] == BS
&& arr[plant_choise_x][plant_choise_y + 2] == BS
// [1]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false // нижняя
// [2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 2]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 2]) == false // нижнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 2]) == false // верхнелевая
// [3]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false // нижняя
// [4]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 2]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 2]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 3]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 2]) == false // нижнеправая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 2]) == false // верхнеправая
)
||
// [][][*][]
(true)
||
/*
[1]
[2]
[3]
[4]
*/
(true)
||
/*
[4]
[3]
[2]
[1]
*/
(true)
||
/*
[ ]
[ ]
[*]
[ ]
*/
(true)
||
/*
[ ]
[*]
[ ]
[ ]
*/
(true)
)
{
arr[plant_choise_x][plant_choise_y] = BS;
cell_is_checked = true;
}
if (cell_is_checked == true) {
bs_checked++;
cell_is_checked = false;
}
} break;
case 2:
{
BS = BotBattleship;
} break;
}
if (cell_is_checked == true) {
bs_checked++;
cell_is_checked = false;
}
}
void put_cruiser(char arr[FIELD_SIZE][FIELD_SIZE], int &crui_checked, int PlayerNumber, int plant_choise_x, int plant_choise_y, bool cell_is_checked) {
char C;
switch (PlayerNumber)
{
case 1:
{
C = PlayerCruiser;
} break;
case 2:
{
C = BotCruiser;
} break;
}
if (
/// [1][2][3]
//[1]
(Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false /* нижняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false /* верхняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false /* левая */
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false /* правая */)
||
(arr[plant_choise_x][plant_choise_y + 1] == C && arr[plant_choise_x][plant_choise_y + 2] == C
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false // верхнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false // нижелевая
//[2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false // нижняя
//[3]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 2]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 2]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 3]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 3]) == false // верхнеправая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 3]) == false // нижнеправая
)
||
/// [3][2][1]
(
Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false
&& arr[plant_choise_x][plant_choise_y - 1] == C && arr[plant_choise_x][plant_choise_y - 2] == C
// [1]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false // верхнеправая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false // нижнеправая
// [2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false // нижняя
// [3]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 2]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 2]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 3]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 3]) == false // верхнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 3]) == false // нижнелевая
)
||
(
Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false
&& arr[plant_choise_x][plant_choise_y - 1] == C && arr[plant_choise_x][plant_choise_y + 1] == C
/// [2][1][3]
// [1]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false // нижняя
// [2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 2]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 2]) == false // верхнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 2]) == false // нижнелевая
// [3]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 2]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 2]) == false // верхнеправая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 2]) == false // нижеправая
)
||
(
/*
[1]
[2]
[3]
*/
Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false
&& arr[plant_choise_x + 1][plant_choise_y] == C && arr[plant_choise_x + 2][plant_choise_y] == C
// [1]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false // верхнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false // верхнеправая
// [2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false // правая
// [3]
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 3][plant_choise_y]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 2][plant_choise_y - 1]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 2][plant_choise_y + 1]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 3][plant_choise_y - 1]) == false // нижнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 3][plant_choise_y + 1]) == false // нижнеправая
)
||
(
// [3]
// [2]
// [1]
Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false
&& arr[plant_choise_x + 1][plant_choise_y] == C
&& arr[plant_choise_x + 2][plant_choise_y] == C
// [1]
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false // нижнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false // нижнеправая
// [2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false // правая
// [3]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 3][plant_choise_y]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 2][plant_choise_y - 1]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 2][plant_choise_y + 1]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 3][plant_choise_y - 1]) == false // верхнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 3][plant_choise_y + 1]) == false // верхнеправая
)
||
(
// [2]
// [1]
// [3]
Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false
&& arr[plant_choise_x + 1][plant_choise_y] == C
&& arr[plant_choise_x - 1][plant_choise_y] == C
// [1]
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false // правая
// [2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 2][plant_choise_y]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 2][plant_choise_y - 1]) == false // верхнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 2][plant_choise_y + 1]) == false // верхнеправая
// [3]
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 2][plant_choise_y]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 2][plant_choise_y - 1]) == false // нижнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 2][plant_choise_y + 1]) == false // нижнеправая
)
)
{
arr[plant_choise_x][plant_choise_y] = C;
cell_is_checked = true;
}
if (cell_is_checked == true) {
crui_checked++;
cell_is_checked = false;
}
}
void put_destroyer(char arr[FIELD_SIZE][FIELD_SIZE], int &destr_checked, int PlayerNumber, int plant_choise_x, int plant_choise_y, bool cell_is_checked) {
char D;
bool ship_building = false;
switch (PlayerNumber)
{
case 1:
{
D = PlayerDestroyer;
} break;
case 2:
{
D = BotDestroyer;
} break;
}
if (
(
Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y] == false)
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false /* нижняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false /* верхняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false /* левая */
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false /* правая */)
||
(
arr[plant_choise_x + 1][plant_choise_y] == D
&& (Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false /// choise_cords (1)
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 2][plant_choise_y - 1]) == false // [1]
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 2][plant_choise_y + 1]) == false // [2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 2][plant_choise_y]) == false /// choise_cords (2)
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false
)
||
(arr[plant_choise_x - 1][plant_choise_y] == D // [2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false // [1]
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false /// choise_cords (1) // низ
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false // нижнелевая клетка
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false // нижнеправая клетка
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false // лево
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false // право
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 2][plant_choise_y]) == false /// choise_cords (2) // верх
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 2][plant_choise_y - 1]) == false // верхнелевая клетка
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 2][plant_choise_y + 1]) == false // верхнеправая клетка
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false // правая
)
||
(
arr[plant_choise_x][plant_choise_y + 1] == D // ---->
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false // [1][2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false /// choise_cords (1) // низ
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false // верх
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false // нижнелевая клетка
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false // верхнелевая клетка
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false // лево
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false /// choise_cords (2) // верх
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false // низ
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 2]) == false // верхнеправая клетка
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 2]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 2]) == false // нижнеправая
)
||
(
arr[plant_choise_x][plant_choise_y - 1] == D // [1] // [2][1]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false // верх (1)
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false // низ
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false // верхнеправая клетка
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false // нижнеправая кллетка
// [2]
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 2]) == false // верхнелевая клетка
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 2]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 2]) == false // верхнелевая
)
||
(
Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false // сама клетка
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false // верхняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false // нижняя
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false // левая
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false // правая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1]) == false // верхнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false // верхнеправая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1]) == false // нижнелевая
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1]) == false // нижнеправая
)
)) {
arr[plant_choise_x][plant_choise_y] = D;
cell_is_checked = true;
ship_building = true;
}
if (cell_is_checked == true) {
destr_checked++;
cell_is_checked = false;
}
}
void put_torpede(char arr[FIELD_SIZE][FIELD_SIZE], int &torp_checked, int PlayerNumber, int plant_choise_x, int plant_choise_y, bool cell_is_checked) {
char T;
switch (PlayerNumber)
{
case 1:
{
T = PlayerTorpede;
} break;
case 2:
{
T = BotTorpede;
} break;
}
if ( // торпедный катер
Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y]) == false // сама клетка
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y]) == false /* нижняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y]) == false /* верхняя */
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y - 1]) == false /* левая */
&& Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y + 1]) == false /* правая */
) {
arr[plant_choise_x][plant_choise_y] = T;
cell_is_checked = true;
}
if (cell_is_checked == true) {
torp_checked++;
cell_is_checked = false;
}
}
void Set_ships(char arr[][FIELD_SIZE], int PlayerNumber) {
char PNEnd[10];
char T;
char D;
char C;
char BS;
if (PlayerNumber == 1) {
T = PlayerTorpede;
D = PlayerDestroyer;
C = PlayerCruiser;
BS = PlayerBattleship;
arr = PlayerArr;
strcpy_s(PNEnd, " (Вы)");
while (bs_checked != 4)
{
system("cls");
Print_table(arr);
if (torp_checked != 4)
strcpy_s(type_of_ship, "торпедный катер (1 клетка для размещения)");
else if (torp_checked == 4)
strcpy_s(type_of_ship, "эсминец (2 клетки для размещения)");
else if (destr_checked == 6)
strcpy_s(type_of_ship, "крейсер (3 клетки для размещения)");
else if (crui_checked == 6)
strcpy_s(type_of_ship, "линкор (4 клетки для размещения)");
cout << "Введите порядковый номер клетки, на которую " << Player_Name << PNEnd << " хочет поместить кораблик (x,y).";
cout << endl << "Помещается " << type_of_ship << "." << endl;
cout << " Ваши координаты: ";
cin >> plant_choise_x >> plant_choise_y;
if (Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y] /* само место, куда ставится клетка*/) == false
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1] /* верхняя правая клетка */) == false
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1] /* нижняя левая клетка */) == false
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1] /* верхняя левая клетка */) == false
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1] /* нижняя правая клетка */) == false
&& plant_choise_x <= FIELD_SIZE && plant_choise_y <= FIELD_SIZE)
{
if (torp_checked >= 0 && torp_checked < 4) {
put_torpede(arr, torp_checked, PlayerNumber, plant_choise_x, plant_choise_y, cell_is_checked);
amount_of_ship_cells_left_to_drown_P1++;
}
if (torp_checked == 4 && destr_checked >= 0 && destr_checked < 6) {
put_destroyer(arr, destr_checked, PlayerNumber, plant_choise_x, plant_choise_y, cell_is_checked);
amount_of_ship_cells_left_to_drown_P1++;
}
if (destr_checked == 6 && crui_checked >= 0 && crui_checked < 6) {
put_cruiser(arr, crui_checked, PlayerNumber, plant_choise_x, plant_choise_y, cell_is_checked);
amount_of_ship_cells_left_to_drown_P1++;
}
if (crui_checked == 6 && bs_checked >= 0 && bs_checked < 4) {
put_battleship(arr, bs_checked, PlayerNumber, plant_choise_x, plant_choise_y, cell_is_checked);
amount_of_ship_cells_left_to_drown_P1++;
}
}
}
}
else if (PlayerNumber == 2) {
T = BotTorpede;
D = BotDestroyer;
C = BotCruiser;
BS = BotBattleship;
arr = BotArr;
while (bs_checked != 4) {
plant_choise_x = rand() % 11;
plant_choise_y = rand() % 11;
if (Cell_checker(PlayerNumber, arr[plant_choise_x][plant_choise_y] /* само место, куда ставится клетка*/) == false
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1] /* верхняя правая клетка */) == false
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y - 1] /* нижняя левая клетка */) == false
&& Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y - 1] /* верхняя левая клетка */) == false
&& Cell_checker(PlayerNumber, arr[plant_choise_x + 1][plant_choise_y + 1] /* нижняя правая клетка */) == false
&& plant_choise_x <= FIELD_SIZE && plant_choise_y <= FIELD_SIZE)
{
if (torp_checked >= 0 && torp_checked < 4) {
put_torpede(arr, torp_checked, PlayerNumber, plant_choise_x, plant_choise_y, cell_is_checked);
amount_of_ship_cells_left_to_drown_P2++;
}
if (torp_checked == 4 && destr_checked >= 0 && destr_checked < 6) {
put_destroyer(arr, destr_checked, PlayerNumber, plant_choise_x, plant_choise_y, cell_is_checked);
amount_of_ship_cells_left_to_drown_P2++;
}
if (destr_checked == 6 && crui_checked >= 0 && crui_checked < 6) {
put_cruiser(arr, crui_checked, PlayerNumber, plant_choise_x, plant_choise_y, cell_is_checked);
amount_of_ship_cells_left_to_drown_P2++;
}
if (crui_checked == 6 && bs_checked >= 0 && bs_checked < 4) {
put_battleship(arr, bs_checked, PlayerNumber, plant_choise_x, plant_choise_y, cell_is_checked);
amount_of_ship_cells_left_to_drown_P2++;
}
}
}
}
else {
T = PlayerTorpede;
D = PlayerDestroyer;
C = PlayerCruiser;
BS = PlayerBattleship;
arr = PlayerArr;
while (bs_checked != 4) {
system("cls");
plant_choise_x = rand() % 11;
plant_choise_y = rand() % 11;
Print_table(PlayerArr);
if (Cell_checker(1, arr[plant_choise_x][plant_choise_y] /* само место, куда ставится клетка*/) == false
&& Cell_checker(1, arr[plant_choise_x - 1][plant_choise_y + 1] /* верхняя правая клетка */) == false
&& Cell_checker(1, arr[plant_choise_x + 1][plant_choise_y - 1] /* нижняя левая клетка */) == false
&& Cell_checker(1, arr[plant_choise_x - 1][plant_choise_y - 1] /* верхняя левая клетка */) == false
&& Cell_checker(1, arr[plant_choise_x + 1][plant_choise_y + 1] /* нижняя правая клетка */) == false
&& plant_choise_x <= FIELD_SIZE && plant_choise_y <= FIELD_SIZE)
{
if (torp_checked >= 0 && torp_checked < 4) {
put_torpede(arr, torp_checked, 1, plant_choise_x, plant_choise_y, cell_is_checked);
amount_of_ship_cells_left_to_drown_P1++;
}
if (torp_checked == 4 && destr_checked >= 0 && destr_checked < 6) {
put_destroyer(arr, destr_checked, 1, plant_choise_x, plant_choise_y, cell_is_checked);
amount_of_ship_cells_left_to_drown_P1++;
}
if (destr_checked == 6 && crui_checked >= 0 && crui_checked < 6) {
put_cruiser(arr, crui_checked, 1, plant_choise_x, plant_choise_y, cell_is_checked);
amount_of_ship_cells_left_to_drown_P1++;
}
if (crui_checked == 6 && bs_checked >= 0 && bs_checked < 4) {
put_battleship(arr, bs_checked, 1, plant_choise_x, plant_choise_y, cell_is_checked);
amount_of_ship_cells_left_to_drown_P1++;
}
}
}
}
}
void Player_move(char PlayerArr[][FIELD_SIZE], char BotArr[][FIELD_SIZE], int PlayerNumber, char* p1, char* p2)
{
while (true) {
system("cls");
plant_choise_x = 0; plant_choise_y = 0;
if (PlayerNumber == 1) {
Print_table(PlayerArr);
cout << "\n Введите координаты клетки, в которую вы хотите стрельнуть, ";
SetConsoleTextAttribute(h, 1);
cout << p1;
SetConsoleTextAttribute(h, 7);
cout << ": ";
cout << "\n Вам осталось потопить клеток кораблей противника: " << amount_of_ship_cells_left_to_drown_P2;
cin >> BotArr[plant_choise_x][plant_choise_x];
if ((BotArr[plant_choise_x][plant_choise_x] == BotTorpede
|| BotArr[plant_choise_x][plant_choise_x] == BotDestroyer
|| BotArr[plant_choise_x][plant_choise_x] == BotCruiser
|| BotArr[plant_choise_x][plant_choise_x] == BotBattleship)
&& BotArr[plant_choise_x][plant_choise_x] != BotShipHit
&& BotArr[plant_choise_x][plant_choise_y] != BotMiss)
{
BotArr[plant_choise_x][plant_choise_y] = BotShipHit;
--amount_of_ship_cells_left_to_drown_P2;
system("cls");
cout << "Попадание!" << endl;
system("pause");
Finisher(PlayerArr, plant_choise_x, plant_choise_y);
continue;
}
else if (BotArr[plant_choise_x][plant_choise_y] == BotShipHit
|| BotArr[plant_choise_x][plant_choise_y] == BotMiss) {
continue;
}
else {
BotArr[plant_choise_x][plant_choise_y] = BotMiss;
break;
}
}
else {
Print_table(BotArr);
plant_choise_x = rand() % FIELD_SIZE;
plant_choise_y = rand() % FIELD_SIZE;
if ((PlayerArr[plant_choise_x][plant_choise_y] == PlayerTorpede
|| PlayerArr[plant_choise_x][plant_choise_y] == PlayerDestroyer
|| PlayerArr[plant_choise_x][plant_choise_y] == PlayerCruiser
|| PlayerArr[plant_choise_x][plant_choise_y] == PlayerBattleship)
&& PlayerArr[plant_choise_x][plant_choise_y] != PlayerShipHit
&& PlayerArr[plant_choise_x][plant_choise_y] != PlayerMiss)
{
PlayerArr[plant_choise_x][plant_choise_y] = PlayerShipHit;
Finisher(PlayerArr, plant_choise_x, plant_choise_y);
--amount_of_ship_cells_left_to_drown_P1;
continue;
}
else if (PlayerArr[plant_choise_x][plant_choise_y] == PlayerShipHit
|| PlayerArr[plant_choise_x][plant_choise_y] == PlayerMiss) {
continue;
}
else {
PlayerArr[plant_choise_x][plant_choise_y] = PlayerMiss;
break;
}
}
}
}
bool Win(int ship_cells) {
if (ship_cells == 0)
return true;
return false;
}
int main()
{
srand((unsigned)time(0));
setlocale(LC_ALL, "Russian");
char PlayerArr[FIELD_SIZE][FIELD_SIZE];
char BotArr[FIELD_SIZE][FIELD_SIZE];
Players_names(Player_Name, Bot_Name);
Create_table(PlayerArr, PlayerWater);
system("pause");
system("cls");
int choise;
cout << "Желаете ли Вы автоматически расстановить корабли или сами? \n\t Нажмите 1, если автоматически, \n\t Нажмите 2, если сами.. \n Ваш выбор: ";
cin >> choise;
system("cls");
if (choise == 1) {
Set_ships(PlayerArr, 3);
}
else {
Set_ships(PlayerArr, 1);
}
Create_table(BotArr, BotWater);
Set_ships(BotArr, 2);
system("pause");
system("cls");
cout << "Добро пожаловать в игру, " << Player_Name << "! \n";
cout << "Сегодня Вы будете играть против бота с никнеймом " << Bot_Name << ". \n";
cout << "Желаем ";
SetConsoleTextAttribute(h, 2);
cout << "хорошей";
SetConsoleTextAttribute(h, 7);
cout << " игры!" << endl;
system("pause");
system("cls");
cout << "\t BETA TEST" << endl << endl;
cout << "Поле игрока с именем (или никнеймом) " << Player_Name << endl;
Print_table(PlayerArr);
cout << endl;
cout << "Поле игрока с именем (или никнеймом) " << Bot_Name << endl;
Print_table(BotArr);
system("pause");
system("cls");
while (true)
{
Player_move(PlayerArr, BotArr, 1, Player_Name, Bot_Name);
Player_move(PlayerArr, BotArr, 2, Player_Name, Bot_Name);
Win(amount_of_ship_cells_left_to_drown_P1);
Win(amount_of_ship_cells_left_to_drown_P2);
if (Win(amount_of_ship_cells_left_to_drown_P2) == true) {
ifPlayerWin(Player_Name);
}
else if (Win(amount_of_ship_cells_left_to_drown_P1) == true) {
ifBotWin(Bot_Name);
}
cout << "Поле игрока с именем (никнеймом) " << Player_Name << endl << endl;
Print_table(PlayerArr);
cout << endl;
cout << "Поле игрока с именем (никнеймом) " << Bot_Name << endl << endl;
Print_table(BotArr);
system("pause");
}
system("pause");
return 0;
}