• 15 апреля стартует «Курс «SQL-injection Master» ©» от команды The Codeby

    За 3 месяца вы пройдете путь от начальных навыков работы с SQL-запросами к базам данных до продвинутых техник. Научитесь находить уязвимости связанные с базами данных, и внедрять произвольный SQL-код в уязвимые приложения.

    На последнюю неделю приходится экзамен, где нужно будет показать свои навыки, взломав ряд уязвимых учебных сайтов, и добыть флаги. Успешно сдавшие экзамен получат сертификат.

    Запись на курс до 25 апреля. Получить промодоступ ...

Проблема "Морской Бой" - непонятная причина неработоспособности игры

N

Novy

Доброго времени суток Всем. Столкнулся с непонятной ошибкой в игре.
Всё началось с того, что я написал условие несоприкосновения кораблей по диагоналям/вертикалям/горизонталям (частично, всё ещё делаю, но проблема в другом).
Именно в части, где происходит расстановка торпедных катеров (одноклеточных корабликов) происходит зацикливание и ставится один и тот же торпедный катер несмотря на то, что счётчик (на котором, собственно и работает этот цикл, он же amount_of_checked_cells) считает кол-во клеток кораблей и по условию при помещении четырёх одноклеточных корабликов должен помещаться двухпалубный кораблик, далее, при установке шести таких клеток - трёхпалубный и так далее... Сам код скидываю ниже.
(P.S.: вся эта канитель происходит в функции Set_ships, строка 143)

C++:
#include<iostream>
#include<ctime>
#include<iomanip>
#include<string>
#include<Windows.h>
#include<windows.h>
using namespace std;

HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
#define _CRT_NO_SECURITY_WARNINGS

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;

// [x][y], где x - вертикаль, y - горизонталь

char WaterP1 = '~';
char WaterP2 = '~';
char TorpedeP1 = 'Т';
char TorpedeP2 = 'Т';
char DestroyerP1 = 'К';
char DestroyerP2 = 'К';
char CruiserP1 = 'Э';
char CruiserP2 = 'Э';
char BattleshipP1 = 'Л';
char BattleshipP2 = 'Л';
char ShipHitP1 = 'Х';
char ShipHitP2 = 'X';
char HitMissP1 = '*';
char HitMissP2 = '*';

void ifP1Win(char* p1) {
    system("cls");
    cout << "Игрок " << p1 << " ПОБЕДИЛ!!! Поздравляем!" << endl;
    system("pause");
}

void ifP2Win(char* p2) {
    system("cls");
    cout << "Игрок " << p2 << " ПОБЕДИЛ!!! Поздравляем!" << endl;
    system("pause");
}

bool Cell_checker(int PlayerNumber, int asked_cell) {

    char T;
    char D;
    char C;
    char BS;

    switch (PlayerNumber)
    {
    case 1:
    {
        T = TorpedeP1;
        D = DestroyerP1;
        C = CruiserP1;
        BS = BattleshipP1;
    } break;

    case 2:
    {
        T = TorpedeP2;
        D = DestroyerP2;
        C = CruiserP2;
        BS = BattleshipP2;
    } break;

    }

    if (asked_cell == T || asked_cell == D || asked_cell == C || asked_cell == BS)
        return true;
    else
        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 Print_hidden_table(char arr[][FIELD_SIZE]) {
    for (int i = 0; i < FIELD_SIZE; i++)
    {
        for (int j = 0; j < FIELD_SIZE; j++)
        {
            cout << '~' << " ";
        }
        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];
    char Torp;
    char Destr;
    char Crui;
    char BShip;

    if (PlayerNumber == 1) {
        Torp = TorpedeP1;
        Destr = DestroyerP1;
        Crui = CruiserP1;
        BShip = BattleshipP1;

        strcpy_s(PNEnd, "-ый");
    }
    else {
        Torp = TorpedeP2;
        Destr = DestroyerP2;
        Crui = CruiserP2;
        BShip = BattleshipP2;

        strcpy_s(PNEnd, "-ой");
    }

    int nmb_of_cells_for_current_ship = 1;
    char type_of_ship[100];
    int plant_choise_x, plant_choise_y;
    int amount_of_checked_cells = 0;

    while (amount_of_checked_cells < 21)
    {
        system("cls");

        Print_table(arr);

        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 клетка для размещения)");


        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
            && 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
            )
        {

            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 /* правая */
                )
            {
                arr[plant_choise_x][plant_choise_y] = Torp;
                amount_of_checked_cells++;
            }


            else if (amount_of_checked_cells > 4 && amount_of_checked_cells < 12) {

                if (nmb_of_cells_for_current_ship == 1)
                    nmb_of_cells_for_current_ship++;

                if (arr[plant_choise_x + 1][plant_choise_y] == Destr
                    && (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] == Destr                                // [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] == Destr                            // ---->
                        && 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] == Destr // [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] = Destr;
                    amount_of_checked_cells++;
                }

                if (amount_of_checked_cells >= 12)
                {
                    if (nmb_of_cells_for_current_ship == 2)
                        nmb_of_cells_for_current_ship++;

                    /// [1][2][3]

                    //[1]
                    if ((arr[plant_choise_x][plant_choise_y + 1] == Crui && arr[plant_choise_x][plant_choise_y + 2] == Crui
                        && 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] == Crui && arr[plant_choise_x][plant_choise_y - 2] == Crui

                            // [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] == Crui && arr[plant_choise_x][plant_choise_y + 1] == Crui

                            /// [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] == Crui && arr[plant_choise_x + 2][plant_choise_y] == Crui

                            // [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 - 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 // верхняя
                            && Cell_checker(PlayerNumber, arr[plant_choise_x - 1][plant_choise_y + 1]) == false // верхняя



                            )
                        ) {
                        arr[plant_choise_x][plant_choise_y] = Crui;
                        amount_of_checked_cells++;
                    }

                    if (amount_of_checked_cells == 17)
                    {
                        if (nmb_of_cells_for_current_ship == 3)
                            nmb_of_cells_for_current_ship++;

                        arr[plant_choise_x][plant_choise_y] = BShip;
                        break;
                    }
                }
            }
        }

        else
        {
            system("cls");
            cout << "Введите правильную ячейку! " << endl;
            Sleep(1250);
        }
    }
}

void Player_move(char arr1[][FIELD_SIZE], char arr2[][FIELD_SIZE], int PlayerNumber, char* p1, char* p2)
{
    int choise_x;
    int choise_y;

    while (true) {
        system("cls");
        choise_x = 0; choise_y = 0;

        if (PlayerNumber == 1) {

            Print_table(arr1);

            cout << "\n Введите координаты клетки, в которую вы хотите стрельнуть, " << p1 << ": ";
            cout << "\n Вам осталось потопить клеток кораблей: " << amount_of_ship_cells_left_to_drown_P2;
            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] != ShipHitP2
                && arr2[choise_x][choise_y] != HitMissP2)
            {
                arr2[choise_x][choise_y] = ShipHitP2;
                --amount_of_ship_cells_left_to_drown_P2;
                system("cls");
                cout << "Попадание!" << endl;
                system("pause");
                continue;
            }

            else if (arr2[choise_x][choise_y] == ShipHitP2
                || arr2[choise_x][choise_y] == HitMissP2) {
                system("cls");
                cout << "Вы по этой клетке уже попадали!" << endl;
                system("pause");
                continue;
            }

            else {
                arr2[choise_x][choise_y] = HitMissP2;
                system("cls");
                Pass2AnotherPlayer();
                break;
            }
        }

        else {
            Print_table(arr2);

            cout << "\n Введите координаты клетки, в которую вы хотите стрельнуть, " << p2 << ": ";
            cout << "\n Вам осталось потопить клеток кораблей: " << amount_of_ship_cells_left_to_drown_P1;
            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] != ShipHitP1
                && arr1[choise_x][choise_y] != HitMissP1)
            {
                arr1[choise_x][choise_y] = ShipHitP1;
                --amount_of_ship_cells_left_to_drown_P1;
                system("cls");
                cout << "Попадание!" << endl;
                system("pause");
                continue;
            }

            else if (arr1[choise_x][choise_y] == ShipHitP1
                || arr1[choise_x][choise_y] == HitMissP1) {
                system("cls");
                cout << "Вы по этой клетке уже попадали!" << endl;
                system("pause");
                continue;
            }

            else {
                arr1[choise_x][choise_y] = HitMissP1;
                system("cls");
                Pass2AnotherPlayer();
                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 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, WaterP1);
    Set_ships(arr1, 1);

    Pass2AnotherPlayer();

    Create_table(arr2, WaterP2);
    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);



    while (!Win)
    {
        Player_move(arr1, arr2, 1, Player1, Player2);
        Player_move(arr1, arr2, 2, Player1, Player2);
        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) {
            ifP1Win(Player1);
        }

        else if (Win(amount_of_ship_cells_left_to_drown_P1) == true) {
            ifP2Win(Player2);
        }

        cout << "Поле игрока с именем (никнеймом) " << Player1 << endl << endl;
        Print_table(arr1);

        cout << endl;

        cout << "Поле игрока с именем (никнеймом) " << Player2 << endl << endl;
        Print_table(arr2);

        system("pause");
    }



    system("pause");
    return 0;
}
 

sinner67

Green Team
24.03.2017
279
357
BIT
0
Код не проверял, но пробежался по нему. Думаю ошибка в условие где ты проверяешь клетки между 4 и 12.
У тебя:
Код:
else if (amount_of_checked_cells > 4 && amount_of_checked_cells < 12) {
Я бы попробовал поправить на:
Код:
if (amount_of_checked_cells > 4 && amount_of_checked_cells < 12) {

А вообще я бы на твоем месте попробовал реорганизовал код. Слишком, на мой взгляд, много нагромождения повторяющихся. Например проверки пустоты вокруг. Я бы постарался сделать функцию в которую просто вписываешь координату, а она уже проверяет пустоту вокруг.
 
N

Novy

...Всего-то? Я думал, что нужно организовать для каждого типа корабля условия того, что они не должны соприкасаться с клеткой, на место которой мы и ставим часть кораблика (по одной части корабля каждый раз) Просто я не знаю, как иначе проверить, есть ли корабль(и) по диагонали/горизонтали/вертикали в "поле зрения" клетки, на место которой ставится кораблик.
 

sinner67

Green Team
24.03.2017
279
357
BIT
0
Так моя догадка на счет условия верна оказалась?
 
N

Novy

К сожалению, нет. Поспрашивал на другом форуме - сказали, что проблема в том, что nmb_of_cells_for_current_ship конфликтует с amount_of_checked_cells из-за условий, из-за чего, предположительно, игра не идёт, как надо, но это не оказалось верным. Игре как-будто влом на то, что я удалил условия с nmb_of_cells_for_current_ship. И то, что я вообще исключил его из игры.
 
Последнее редактирование модератором:
N

Novy

Выяснилось следующее: ничего не выходит. Условие в условии, в котором было ещё одно условие, в котором было последнее условие, отвечавшее за ход цикла - это всё я удалил. Отдельный иф для одного типа корабля, другой - для другого сделал. Но... те же проблемы. Даю код видоизменённый, может, понятнее будет:
C++:
#include<iostream>
#include<ctime>
#include<iomanip>
#include<string>
#include<Windows.h>
#include<windows.h>
using namespace std;

HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
#define _CRT_NO_SECURITY_WARNINGS

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;

// [x][y], где x - вертикаль, y - горизонталь

char WaterP1 = '~';
char WaterP2 = '~';
char TorpedeP1 = 'Т';
char TorpedeP2 = 'Т';
char DestroyerP1 = 'К';
char DestroyerP2 = 'К';
char CruiserP1 = 'Э';
char CruiserP2 = 'Э';
char BattleshipP1 = 'Л';
char BattleshipP2 = 'Л';
char ShipHitP1 = 'Х';
char ShipHitP2 = 'X';
char HitMissP1 = '*';
char HitMissP2 = '*';

void ifP1Win(char* p1) {
    system("cls");
    cout << "Игрок " << p1 << " ПОБЕДИЛ!!! Поздравляем!" << endl;
    system("pause");
}

void ifP2Win(char* p2) {
    system("cls");
    cout << "Игрок " << p2 << " ПОБЕДИЛ!!! Поздравляем!" << endl;
    system("pause");
}

bool Cell_checker(int PlayerNumber, int asked_cell) {

    char T;
    char D;
    char C;
    char BS;

    switch (PlayerNumber)
    {
    case 1:
    {
        T = TorpedeP1;
        D = DestroyerP1;
        C = CruiserP1;
        BS = BattleshipP1;
    } break;

    case 2:
    {
        T = TorpedeP2;
        D = DestroyerP2;
        C = CruiserP2;
        BS = BattleshipP2;
    } break;

    }

    if (asked_cell == T || asked_cell == D || asked_cell == C || asked_cell == BS)
        return true;
    else
        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 Print_hidden_table(char arr[][FIELD_SIZE]) {
    for (int i = 0; i < FIELD_SIZE; i++)
    {
        for (int j = 0; j < FIELD_SIZE; j++)
        {
            cout << '~' << " ";
        }
        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];
    char Torp;
    char Destr;
    char Crui;
    char BShip;

    if (PlayerNumber == 1) {
        Torp = TorpedeP1;
        Destr = DestroyerP1;
        Crui = CruiserP1;
        BShip = BattleshipP1;

        strcpy_s(PNEnd, "-ый");
    }
    else {
        Torp = TorpedeP2;
        Destr = DestroyerP2;
        Crui = CruiserP2;
        BShip = BattleshipP2;

        strcpy_s(PNEnd, "-ой");
    }

    int nmb_of_cells_for_current_ship = 1;
    char type_of_ship[100];
    int plant_choise_x, plant_choise_y;
    int amount_of_checked_cells = 0;

    while (amount_of_checked_cells < 21)
    {
        system("cls");

        Print_table(arr);

        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 клетка для размещения)");


        cout << "Введите порядковый номер клетки, на которую " << PlayerNumber << PNEnd << " игрок хочет поместить кораблик (x,y).";
        cout << endl << "Помещается " << type_of_ship << "." << endl;
        cout << "   Ваши координаты: ";
        
        bool cell_is_checked = false;
        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)
        {
            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 /* правая */
                ) {
                arr[plant_choise_x][plant_choise_y] = Torp;
                cell_is_checked == true;
            }

        if (amount_of_checked_cells > 4 && amount_of_checked_cells < 12 &&
                arr[plant_choise_x + 1][plant_choise_y] == Destr
                && (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] == Destr                                // [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] == Destr                            // ---->
                    && 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] == Destr // [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] = Destr;
                cell_is_checked == true;
            }

            if (amount_of_checked_cells >= 12 && amount_of_checked_cells < 18 &&

                /// [1][2][3]

                //[1]
                (arr[plant_choise_x][plant_choise_y + 1] == Crui && arr[plant_choise_x][plant_choise_y + 2] == Crui
                    && 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] == Crui && arr[plant_choise_x][plant_choise_y - 2] == Crui

                    // [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] == Crui && arr[plant_choise_x][plant_choise_y + 1] == Crui

                    /// [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] == Crui && arr[plant_choise_x + 2][plant_choise_y] == Crui

                    // [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 // нижнеправая
                    ))
            {
                arr[plant_choise_x][plant_choise_y] = Crui;
                cell_is_checked == true;
            }

            if (cell_is_checked == true) {
                amount_of_checked_cells++;
                cell_is_checked == false;
            }
        }

    }
}

void Player_move(char arr1[][FIELD_SIZE], char arr2[][FIELD_SIZE], int PlayerNumber, char* p1, char* p2)
{
    int choise_x;
    int choise_y;

    while (true) {
        system("cls");
        choise_x = 0; choise_y = 0;

        if (PlayerNumber == 1) {

            Print_table(arr1);

            cout << "\n Введите координаты клетки, в которую вы хотите стрельнуть, " << p1 << ": ";
            cout << "\n Вам осталось потопить клеток кораблей: " << amount_of_ship_cells_left_to_drown_P2;
            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] != ShipHitP2
                && arr2[choise_x][choise_y] != HitMissP2)
            {
                arr2[choise_x][choise_y] = ShipHitP2;
                --amount_of_ship_cells_left_to_drown_P2;
                system("cls");
                cout << "Попадание!" << endl;
                system("pause");
                continue;
            }

            else if (arr2[choise_x][choise_y] == ShipHitP2
                || arr2[choise_x][choise_y] == HitMissP2) {
                system("cls");
                cout << "Вы по этой клетке уже попадали!" << endl;
                system("pause");
                continue;
            }

            else {
                arr2[choise_x][choise_y] = HitMissP2;
                system("cls");
                Pass2AnotherPlayer();
                break;
            }
        }

        else {
            Print_table(arr2);

            cout << "\n Введите координаты клетки, в которую вы хотите стрельнуть, " << p2 << ": ";
            cout << "\n Вам осталось потопить клеток кораблей: " << amount_of_ship_cells_left_to_drown_P1;
            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] != ShipHitP1
                && arr1[choise_x][choise_y] != HitMissP1)
            {
                arr1[choise_x][choise_y] = ShipHitP1;
                --amount_of_ship_cells_left_to_drown_P1;
                system("cls");
                cout << "Попадание!" << endl;
                system("pause");
                continue;
            }

            else if (arr1[choise_x][choise_y] == ShipHitP1
                || arr1[choise_x][choise_y] == HitMissP1) {
                system("cls");
                cout << "Вы по этой клетке уже попадали!" << endl;
                system("pause");
                continue;
            }

            else {
                arr1[choise_x][choise_y] = HitMissP1;
                system("cls");
                Pass2AnotherPlayer();
                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 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, WaterP1);
    Set_ships(arr1, 1);

    Pass2AnotherPlayer();

    Create_table(arr2, WaterP2);
    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);



    while (!Win)
    {
        Player_move(arr1, arr2, 1, Player1, Player2);
        Player_move(arr1, arr2, 2, Player1, Player2);
        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) {
            ifP1Win(Player1);
        }

        else if (Win(amount_of_ship_cells_left_to_drown_P1) == true) {
            ifP2Win(Player2);
        }

        cout << "Поле игрока с именем (никнеймом) " << Player1 << endl << endl;
        Print_table(arr1);

        cout << endl;

        cout << "Поле игрока с именем (никнеймом) " << Player2 << endl << endl;
        Print_table(arr2);

        system("pause");
    }



    system("pause");
    return 0;
}
 
N

Novy

Нашлись ошибки: не доставало условия остановки установки одно-палубок. И ошибки путания "==" с "=" (это уже называется поторопился 😃) Спасибо Всем, кто старался (или пытался) помочь с игрой. Благодарю! :)
 
N

Novy

Не всё так гладко теперь: когда я записываю в x значение 1, а в y 1 - x делает инкрементирование на 1, а y не меняется; когда я записываю в x 1, а в y 0 - запись идёт и в 10-ый x, и в 0-ой y; при кладке двухпалубок почему-то не работает условие перехода на постройку трёхпалубки; при помещении шести Э поместить Э рядом друг с другом нельзя почему-то. Короче, капут.. Я уже не знаю, как поступать. Видоизменённый код:
C++:
#include<iostream>
#include<ctime>
#include<iomanip>
#include<string>
#include<Windows.h>
#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;

// [x][y], где x - вертикаль, y - горизонталь

char WaterP1 = '~';
char WaterP2 = '~';
char TorpedeP1 = 'Т';
char TorpedeP2 = 'Т';
char DestroyerP1 = 'Э';
char DestroyerP2 = 'Э';
char CruiserP1 = 'К';
char CruiserP2 = 'К';
char BattleshipP1 = 'Л';
char BattleshipP2 = 'Л';
char ShipHitP1 = 'Х';
char ShipHitP2 = 'X';
char HitMissP1 = '*';
char HitMissP2 = '*';

void ifP1Win(char* p1) {
    system("cls");
    cout << "Игрок " << p1 << " ПОБЕДИЛ!!! Поздравляем!" << endl;
    system("pause");
}

void ifP2Win(char* p2) {
    system("cls");
    cout << "Игрок " << p2 << " ПОБЕДИЛ!!! Поздравляем!" << endl;
    system("pause");
}

bool Cell_checker(int PlayerNumber, int asked_cell) {

    char T;
    char D;
    char C;
    char BS;

    switch (PlayerNumber)
    {
    case 1:
    {
        T = TorpedeP1;
        D = DestroyerP1;
        C = CruiserP1;
        BS = BattleshipP1;
    } break;

    case 2:
    {
        T = TorpedeP2;
        D = DestroyerP2;
        C = CruiserP2;
        BS = BattleshipP2;
    } break;

    }

    if (asked_cell == T || asked_cell == D || asked_cell == C || asked_cell == BS)
        return true;
    else
        return false;
}

void Print_table(char arr[][FIELD_SIZE]) {

    cout << "_|";
    for (int i = 0; i < FIELD_SIZE + 1; i++)
    {
        cout << i << "|"; // верхний ряд
    }
    cout << endl;
    char arr_coord_x[10]{ '1','2','3','4','5','6','7','8','9','10' };

    for (int i = 0; i < FIELD_SIZE; i++)
    {
        cout << arr_coord_x[i] << "|"; // боковой левый ряд

        for (int j = 0; j < FIELD_SIZE + 1; j++)
        {
            cout << arr[i][j] << "|";
        }
        cout << endl;
    }
}

void Print_hidden_table(char arr[][FIELD_SIZE]) {
    for (int i = 0; i < FIELD_SIZE; i++)
    {
        for (int j = 0; j < FIELD_SIZE; j++)
        {
            cout << '~' << " ";
        }
        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];
    char Torp;
    char Destr;
    char Crui;
    char BShip;

    if (PlayerNumber == 1) {
        Torp = TorpedeP1;
        Destr = DestroyerP1;
        Crui = CruiserP1;
        BShip = BattleshipP1;

        strcpy_s(PNEnd, "-ый");
    }
    else {
        Torp = TorpedeP2;
        Destr = DestroyerP2;
        Crui = CruiserP2;
        BShip = BattleshipP2;

        strcpy_s(PNEnd, "-ой");
    }

    int nmb_of_cells_for_current_ship = 1;
    char type_of_ship[100];
    int plant_choise_x, plant_choise_y;
    int amount_of_checked_cells = 0;   
    bool cell_is_checked = false;

    while (amount_of_checked_cells < 21)
    {
        system("cls");

        Print_table(arr);

        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 клетка для размещения)");


        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
            && 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 <= 10 && plant_choise_y <= 10)
        {
            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 /* правая */
                && amount_of_checked_cells >= 0 && amount_of_checked_cells < 4
                ) {
                arr[plant_choise_x][plant_choise_y] = Torp;
                cell_is_checked = true;
            }

            if (amount_of_checked_cells >= 4 && amount_of_checked_cells < 10
                && arr[plant_choise_x + 1][plant_choise_y] == Destr
                && (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] == Destr                                // [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] == Destr                            // ---->
                    && 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] == Destr // [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] = Destr;
                cell_is_checked = true;
            }

            if (amount_of_checked_cells >= 10 && amount_of_checked_cells < 16 &&

                /// [1][2][3]

                //[1]
                (arr[plant_choise_x][plant_choise_y + 1] == Crui && arr[plant_choise_x][plant_choise_y + 2] == Crui
                    && 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] == Crui && arr[plant_choise_x][plant_choise_y - 2] == Crui

                    // [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] == Crui && arr[plant_choise_x][plant_choise_y + 1] == Crui

                    /// [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] == Crui && arr[plant_choise_x + 2][plant_choise_y] == Crui

                    // [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] == Crui
                        && arr[plant_choise_x + 2][plant_choise_y] == Crui

                        // [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] == Crui
                            && arr[plant_choise_x - 1][plant_choise_y] == Crui

                            // [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 // нижнеправая
                        )
                        ||
                        (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][plant_choise_y] /* само место, куда ставится клетка*/) == false
                            )
                    )
            {
                arr[plant_choise_x][plant_choise_y] = Crui;
                cell_is_checked = true;
            }



            if (cell_is_checked == true) {
                amount_of_checked_cells++;
                cell_is_checked = false;
            }
        }
            else
            {
                system("cls");
                cout << "Введите допустимые координаты клетки, а не (" << plant_choise_x << ";" << plant_choise_y << ") !" << endl;
                system("pause");
                system("cls");
            }
    }
}

void Player_move(char arr1[][FIELD_SIZE], char arr2[][FIELD_SIZE], int PlayerNumber, char* p1, char* p2)
{
    int choise_x;
    int choise_y;

    while (true) {
        system("cls");
        choise_x = 0; choise_y = 0;

        if (PlayerNumber == 1) {

            Print_table(arr1);

            cout << "\n Введите координаты клетки, в которую вы хотите стрельнуть, ";
            SetConsoleTextAttribute(h, 1);
            cout << p1;
            SetConsoleTextAttribute(h, 0);
            cout << ": ";
            cout << "\n Вам осталось потопить клеток кораблей: " << amount_of_ship_cells_left_to_drown_P2;
            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] != ShipHitP2
                && arr2[choise_x][choise_y] != HitMissP2)
            {
                arr2[choise_x][choise_y] = ShipHitP2;
                --amount_of_ship_cells_left_to_drown_P2;
                system("cls");
                cout << "Попадание!" << endl;
                system("pause");
                continue;
            }

            else if (arr2[choise_x][choise_y] == ShipHitP2
                || arr2[choise_x][choise_y] == HitMissP2) {
                system("cls");
                cout << "Вы по этой клетке уже попадали!" << endl;
                system("pause");
                continue;
            }

            else {
                arr2[choise_x][choise_y] = HitMissP2;
                system("cls");
                Pass2AnotherPlayer();
                break;
            }
        }

        else {
            Print_table(arr2);

            cout << "\n Введите координаты клетки, в которую вы хотите стрельнуть, " << p2 << ": ";
            cout << "\n Вам осталось потопить клеток кораблей: " << amount_of_ship_cells_left_to_drown_P1;
            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] != ShipHitP1
                && arr1[choise_x][choise_y] != HitMissP1)
            {
                arr1[choise_x][choise_y] = ShipHitP1;
                --amount_of_ship_cells_left_to_drown_P1;
                system("cls");
                cout << "Попадание!" << endl;
                system("pause");
                continue;
            }

            else if (arr1[choise_x][choise_y] == ShipHitP1
                || arr1[choise_x][choise_y] == HitMissP1) {
                system("cls");
                cout << "Вы по этой клетке уже попадали!" << endl;
                system("pause");
                continue;
            }

            else {
                arr1[choise_x][choise_y] = HitMissP1;
                system("cls");
                Pass2AnotherPlayer();
                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 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, WaterP1);
    Set_ships(arr1, 1);

    Pass2AnotherPlayer();

    Create_table(arr2, WaterP2);
    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);

    while (!Win)
    {
        Player_move(arr1, arr2, 1, Player1, Player2);
        Player_move(arr1, arr2, 2, Player1, Player2);
        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) {
            ifP1Win(Player1);
        }

        else if (Win(amount_of_ship_cells_left_to_drown_P1) == true) {
            ifP2Win(Player2);
        }

        cout << "Поле игрока с именем (никнеймом) " << Player1 << endl << endl;
        Print_table(arr1);

        cout << endl;

        cout << "Поле игрока с именем (никнеймом) " << Player2 << endl << endl;
        Print_table(arr2);

        system("pause");
    }

    system("pause");
    return 0;
}
 
Мы в соцсетях:

Обучение наступательной кибербезопасности в игровой форме. Начать игру!