По-моему никто и не старается... Короче, исправил код, но появилась новая проблема (смотреть ошибки по этой ссылке:
https://cl.ly/2b2A1D0i2h2y)
Код:
#include<iostream>
#include<ctime>
#include<iomanip>
#include<string>
#include<Windows.h>
using namespace std;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
const int FIELD_SIZE = 10;
char Player1[100];
char Player2[100];
int amount_of_ship_cells_left_to_drown_P1;
int amount_of_ship_cells_left_to_drown_P2;
char WaterP1 = '~'; // 1
char WaterP2 = '~'; // 2
char TorpedeP1 = '^'; // 3
char TorpedeP2 = '^'; // 4
char DestroyerP1 = '^'; // 5
char DestroyerP2 = '^'; // 6
char CruiserP1 = '^'; // 7
char CruiserP2 = '^'; // 8
char BattleshipP1 = '^'; // 9
char BattleshipP2 = '^'; // 10
char ShipHit = 'x'; // 12
char HitMiss = '*'; // 13
bool Cell_checker(int PlayerNumber, int asked_cell) {
for (int i = PlayerNumber; i < 10 + PlayerNumber; i += 2)
{
if (asked_cell == i) {
return true;
}
}
return false;
}
void Print_table(char arr[][FIELD_SIZE]) {
for (int i = 0; i < FIELD_SIZE; i++)
{
for (int j = 0; j < FIELD_SIZE; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
void Players_names(char* Player1, char* Player2) {
cout << "Введите имя ";
SetConsoleTextAttribute(h, 3);
cout << "первого";
SetConsoleTextAttribute(h, 7);
cout << " игрока() : ";
cin >> Player1;
cout << "Введите имя ";
SetConsoleTextAttribute(h, 4);
cout << "второго";
SetConsoleTextAttribute(h, 7);
cout << " игрока() : ";
cin >> Player2;
}
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 Pass2AnotherPlayer() {
system("cls");
cout << "Передайте меня другому игроку, что-бы он поставил свои корабли. И не подглядывайте ;)" << endl;
Sleep(5000);
system("cls");
}
void Set_ships(char arr[][FIELD_SIZE], int PlayerNumber) {
char PNEnd[5];
if (PlayerNumber == 1)
strcpy_s(PNEnd, "-ый");
else
strcpy_s(PNEnd, "-ой");
int amount_of_checked_cells = 0;
while (amount_of_checked_cells < 21)
{
system("cls");
Print_table(arr);
int nmb_of_cells_for_current_ship = 1;
char type_of_ship[100];
if (nmb_of_cells_for_current_ship == 4)
strcpy_s(type_of_ship, "линкор (4 клетки для размещения)");
if (nmb_of_cells_for_current_ship == 3)
strcpy_s(type_of_ship, "крейсер (3 клетки для размещения)");
if (nmb_of_cells_for_current_ship == 2)
strcpy_s(type_of_ship, "эсминец (2 клетки для размещения)");
if (nmb_of_cells_for_current_ship == 1)
strcpy_s(type_of_ship, "торпедный катер (1 клетка для размещения)");
int plant_choise_x, plant_choise_y;
cout << "Введите порядковый номер клетки, на которую " << PlayerNumber << 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) {
arr[plant_choise_x][plant_choise_y] = PlayerNumber + 2; // 3, 4 ([])
amount_of_checked_cells++;
if (amount_of_checked_cells >= 4) {
arr[plant_choise_x][plant_choise_y] = PlayerNumber + 4; // 5, 6 ([][])
if (nmb_of_cells_for_current_ship == 1)
nmb_of_cells_for_current_ship++;
if (amount_of_checked_cells >= 10)
{
if (nmb_of_cells_for_current_ship == 2)
nmb_of_cells_for_current_ship++;
arr[plant_choise_x][plant_choise_y] = PlayerNumber + 6; // 7, 8 ([][][])
if (amount_of_checked_cells == 16)
{
if (nmb_of_cells_for_current_ship == 3)
nmb_of_cells_for_current_ship++;
arr[plant_choise_x][plant_choise_y] = PlayerNumber + 8; // 9, 10 ([][][][])
break;
}
}
}
}
else
{
system("cls");
cout << "Введите правильную ячейку! " << endl;
Sleep(1250);
}
}
}
void Hide_table(char arr[][FIELD_SIZE]) {
for (int i = 0; i < FIELD_SIZE; i++)
{
for (int j = 0; j < FIELD_SIZE; j++)
{
arr[i][j] = 178;
}
}
}
void Player_move(char arr1[][FIELD_SIZE], char arr2[][FIELD_SIZE], int PlayerNumber, char p1, char p2) {
int choise_x;
int choise_y;
while (!Win) {
system("cls");
if (PlayerNumber == 1) {
Win(amount_of_ship_cells_left_to_drown_P1);
if (Win(amount_of_ship_cells_left_to_drown_P1) == true) {
ifP1Win(p1);
}
if (Win(amount_of_ship_cells_left_to_drown_P2) == true) {
ifP2Win(p2);
}
Print_table(arr1);
cout << "\n Введите координаты клетки, в которую вы хотите стрельнуть, " << p1 << ": ";
cin >> arr2[choise_x][choise_y];
if (arr2[choise_x][choise_y] == TorpedeP2 || arr2[choise_x][choise_y] == DestroyerP2 || arr2[choise_x][choise_y] == CruiserP2 || arr2[choise_x][choise_y] == BattleshipP2)
arr2[choise_x][choise_y] = ShipHit;
else
arr2[choise_x][choise_y] = HitMiss;
}
else {
Print_table(arr2);
cout << "\n Введите координаты клетки, в которую вы хотите стрельнуть, " << p2 << ": ";
cin >> arr1[choise_x][choise_y];
if (arr1[choise_x][choise_y] == TorpedeP1 || arr1[choise_x][choise_y] == DestroyerP1 || arr1[choise_x][choise_y] == CruiserP1 || arr1[choise_x][choise_y] == BattleshipP1)
arr1[choise_x][choise_y] = ShipHit;
else
arr1[choise_x][choise_y] = HitMiss;
}
}
}
void ifP1Win(char p1) {
system("cls");
cout << "Игрок " << p1 << " ПОБЕДИЛ!!! Поздравляем!" << endl;
system("pause");
exit(1);
}
void ifP2Win(char p2) {
system("cls");
cout << "Игрок " << p2 << " ПОБЕДИЛ!!! Поздравляем!" << endl;
system("pause");
exit(1);
}
bool Win(int ship_cells) {
if (ship_cells == 0)
return true;
return false;
}
int main()
{
srand((unsigned)time(0));
setlocale(LC_ALL, "Russian");
char arr1[FIELD_SIZE][FIELD_SIZE];
char arr2[FIELD_SIZE][FIELD_SIZE];
Players_names(Player1, Player2);
system("cls");
cout << "Имя первого игрока: " << Player1 << endl;
cout << "Имя второго игрока: " << Player2 << endl << endl;
system("pause");
Create_table(arr1, '1');
Create_table(arr2, '2');
//Hide_table(arr1);
//Hide_table(arr2);
Set_ships(arr1, 1);
Pass2AnotherPlayer();
Set_ships(arr2, 2);
system("pause");
system("cls");
cout << "\t BETA TEST" << endl << endl;
cout << "Поле игрока с именем (или никнеймом) " << Player1 << endl;
Print_table(arr1);
cout << endl;
cout << "Поле игрока с именем (или никнеймом) " << Player2 << endl;
Print_table(arr2);
Player_move(arr1, arr2, 1, Player1, Player2);
Player_move(arr1, arr2, 2, Player1, Player2);
system("pause");
return 0;
}