Home c++ C++ Naval Combat - some moments

C++ Naval Combat – some moments

Author

Date

Category

I want to write a sea battle console game. I wrote a little code here, but did not implement some points. Do not pay attention to such a number of connected libraries, I just feel calmer)

In my work there is a field (two-dimensional array) divided into 2 parts (from 1 to 5 enemy lines, from 6 and 10 lines my part) in which single-deck ships are shown in the form of “1” and empty cells in the form of “0” … I can enter coordinates by row and column and “destroy” these ships (input for me is allowed from 1 to 5 lines, and for a bot from 6 to 10). When destroyed, their value is changed to “5” for one iteration, so that it can be seen that it was shot at and later on “0” when it turns into empty space. When all ships are destroyed, the game ends and victory is declared

Something I can’t handle and that’s why I’m writing here. It is required that each (me and the bot) have 5 pieces – 1, 2, 3, 4, 5 – decked ships placed on the 10×10 field, and not 1 deck ships as at the moment and also when the bot hits my ship, he fired one space further in a random direction, and not at a random space throughout the map of his attack, as it is now. Also, I do not know how to hide the field that I am attacking, so that only the ships I knocked out are shown. I look forward to advice from those in the know. Please do not throw links to other people’s projects, I need to explain based on my code. Thank you

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <conio.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <dos.h>
using namespace std;
const int SIZEf = 10;
int field[SIZEf][SIZEf] = {};
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
char s1[256];
void GotoXY(int X, int Y)
{
    COORD coord = { X, Y };
    SetConsoleCursorPosition(hStdOut, coord);
}
void menu()
{
    int f = 1, k, code;
    cout << "\n\n"; // * figure
    for (int x = 0; x < 13; x++)
    {
        for (int y = 0; y < 13; y++)
        {
            if (x == 0 || x == y && y < 13 / 2 || x + y == 13 - 1 && y > 13 / 2 - 1)
            {
                cout << "    \t* ";
            }
            else
            {
                cout << "    \t  ";
            }
        }
        cout << endl;
    } // *
    GotoXY(27, 12);
    cout << "\t\t\t--> New game<--";
    GotoXY(30, 13);
    cout << "\t\t\t       Output";
    cout << "\n\n\t\t«Sea battle» — a game for two participants, in which the players take turns to name the coordinates on the opponent's map unknown to them. If the opponent has a ship at these coordinates(coordinates are busy), then the ship or part of it«drowned», and the victim gets the right to make one more move";
    do
    {
        k = 0;
        code = _getch();
        if (code == 224 || code == 0)
        {
            code = _getch();
        }
        if (code == 80)
        {
            f = f * (-1);
            if (f == 1)
            {
                GotoXY(27, 12);
                cout << "\t\t\t--> New game<--";
                GotoXY(30, 13);
                cout << "\t\t\t       Output";
            }
            else if (f == 2)
            {
                GotoXY(27, 12);
                cout << "\t\t\t    New game   ";
                GotoXY(30, 13);
                cout << "\t\t\t       Output";
            }
            else
            {
                GotoXY(27, 12);
                cout << "\t\t\t    New game   ";
                GotoXY(30, 13);
                cout << "\t\t\t   --> Output<--";
            }
        }
        else if (code == 72)
        {
            f = f * (-1);
            if (f == 1)
            {
                GotoXY(27, 12);
                cout << "\t\t\t--> New game<--";
                GotoXY(30, 13);
                cout << "\t\t\t       Output";
            }
            else if (f == 2)
            {
                GotoXY(27, 12);
                cout << "\t\t\t    New game   ";
                GotoXY(30, 13);
                cout << "\t\t\t       Output";
            }
            else
            {
                GotoXY(27, 12);
                cout << "\t\t\t    New game   ";
                GotoXY(30, 13);
                cout << "\t\t\t   --> Output<--";
            }
        }
        else if (code == 13)
        {
            k = 1;
        }
        cout << endl << endl << endl << endl << endl;
    }
    while (k == 0);
    {
        if (f != 1)
        {
            exit(0);
        }
    }
    system("cls");
    cout << endl << endl;
}
void figure1()
{
    system("cls");
    for (int x = 0; x < 15; x++) // * Created a square
    {
        for (int y = 0; y < 15; y++)
        {
            if (x == 0 || x == 15 - 1 || y == 0 || y == 15 - 1)
            {
                cout << "    \t* ";
            }
            else
            {
                cout << "    \t ";
            }
            if (x == 6) // ** We go down below to write the text inside the square.
            {
                cout << "      \t\t\t\t\t";
                string h = "Sea battle!";
                for (int i = 0; i < h.length(); i++)
                {
                    Sleep(60);
                    cout << h[i];
                }
                break;
            } // **
        }
        cout << endl; // endl to create lines for the square
    }
    Sleep(1500); // break// *
    cout << "\a"; // menu switching sound
}
void reminder1()
{
    system("cls");
    cout << "\n\n\n\n\n\n\n\n\n\t\t\t\t\t    Reminder:\n";
    cout << "\t\t\t    Click the cross at the top to exit the game\n\t\t\t       However, achievements are not saved.!";
    cout << "\n\t\t\t\t";
    string h = "................................."; // ** Artificial waiting with load simulation rendered by dots
    for (int i = 0; i < h.length(); i++)
    {
        Sleep(50);
        cout << h[i];
    } // **
    cout << "\n\n";
    Sleep(1000);
    system("cls");
}
void win()
{
    system("cls");
    for (int x = 0; x <= 2; x++)
    {
        if (x <= 1)
        {
            system("Color 05");
            Sleep(200);
            cout << "\n\n\n\n\n\n\n\t\t\t\t\t\t\tNS";
            system("Color 15");
            Sleep(200);
            cout << " O";
            system("Color 25");
            Sleep(200);
            cout << " with";
            system("Color 35");
            Sleep(200);
            cout << " d";
            system("Color 48");
            Sleep(200);
            cout << " p";
            system("Color 65");
            Sleep(200);
            cout << " a";
            system("Color 85");
            Sleep(200);
            cout << " v";
            system("Color 93");
            Sleep(200);
            cout << " l";
            system("Color A5");
            Sleep(200);
            cout << " I am";
            system("Color B5");
            Sleep(200);
            cout << " is";
            system("Color C0");
            Sleep(200);
            cout << " m";
            system("Color E5");
            Sleep(200);
            cout << "\n\n\t\t\t\t\t\t\t\tv";
            system("Color F5");
            Sleep(200);
            cout << " a";
            system("Color 75");
            Sleep(200);
            cout << " with";
            Sleep(200);
        }
        else
        {
            system("cls");
            for (int x = 0; x < 15; x++) // * Created a square
            {
                for (int y = 0; y < 15; y++)
                {
                    if (x == 0 || x == 15 - 1 || y == 0 || y == 15 - 1)
                    {
                        cout << "    \t* ";
                    }
                    else
                    {
                        cout << "    \t ";
                    }
                    if (x == 6) // ** We go down below to write the text inside the square.
                    {
                        cout << "      \t\t\t\t\t";
                        string h = "v vpal!";
                        for (int i = 0; i < h.length(); i++)
                        {
                            Sleep(60);
                            cout << h[i];
                        }
                        break;
                    } // **
                }
                cout << endl; // endl to create lines for the square
            } // *
        }
    }
}
void ad() // isOlaI am pislama 1
{
    system("cls");
    for (int x = 0; x < 15; x++) // * Created a square
    {
        for (int y = 0; y < 15; y++)
        {
            if (x == 0 || x == 15 - 1 || y == 0 || y == 15 - 1)
            {
                cout << "    \t* ";
            }
            else
            {
                cout << "    \t ";
            }
            if (x == 6) // ** We go down below to write the text inside the square.
            {
                cout << "      \t\t\t\tisOlaI am pislamaI am ispaI am:\n\n      \t\t\t\t             ";
                string h = "withavanda pama! Od OlO pam!\n\n      \t\t\t\t             Olis vp! Od OlO pam!\n\n      \t\t\t\t             withpis pisisI am! Od OlO pam!\n\n      \t\t\t\t             adis NSpiswithdis! Od OlO pam!\n\n      \t\t\t\t               OlOwithis witha OdOO OlO pama\n\n      \t\t\t\t                     withismlI am ism NSOm...\n\n";
                for (int i = 0; i < h.length(); i++)
                {
                    Sleep(40);
                    cout << h[i];
                }
                break;
            } // **
        }
        cout << endl; // endl to create lines for the square
    } //
    Sleep(3300);
    system("cls");
}
void tableShow()
{
    system("cls");
    cout << endl << endl << "\t\t\t\t";
    int counter = 0;
    for (int i = 0; i < SIZEf; i++)
    {
        counter++;
        cout << counter;
        cout << "\t";
    }
    cout << "\n\t\t\t\t==========================================================================";
    counter = 0;
    cout << endl << endl << "\t\t";
    for (int i = 0; i < SIZEf; i++)
    {
        for (int j = 0; j < SIZEf; j++)
        {
            if (i >= 0 && j == 0)
            {
                counter++;
                cout << "\t" << counter << " |";
            }
            cout << "\t" << field[i][j];
        }
        cout << endl << endl << endl << "\t\t";
    }
}
int main()
{
    setlocale(LC_ALL, "Rus");
    srand(unsigned(time(0)));
    system("Color 75");
    menu();
    figure1();
    reminder1();
    int x, y;
    int enemyShipCounter = 0; // * pawithNSOlOisis andOliswithvO vpaiswith Opalis
    while (true)
    {
        x = rand() % SIZEf;
        if (x <= 4)
        {
            y = rand() % SIZEf;
            field[x][y] = 1;
            enemyShipCounter++;
            if (enemyShipCounter == 10)
            {
                break;
            }
        }
    } // *
    int myShipCounter = 0; // * pawithNSOlOisis andOliswithvO mO Opalis
    while (true)
    {
        x = rand() % SIZEf;
        if (x >= 5)
        {
            y = rand() % SIZEf;
            field[x][y] = 1;
            myShipCounter++;
            if (myShipCounter == 10)
            {
                break;
            }
        }
    } // *
    tableShow();
    bool isMyRound = false;
    while (true)
    {
        cout << "\t\tvvisdis OOpda: ";
        do
        {
            cin >> x;
            cin >> y;
            if (x >= 6 || x <= 0 || y <= 0)
            {
                cout << "\n\t\t\t\tvvisdis OOpda OOpis vis 6 withpOand(6, 5, 4, 3, 2, 1), O withpislI am NSO vpaiswithOm NSOl!\n\n";
                cout << "\t\t\t\tvvisdis OOpda: ";
            }
        } while (x >= 6 || x <= 0 || y <= 0);
        {
            x--;
            y--;
        }
        if (field[x][y] == 1)
        {
            cout << "\n\t\t\t\tv NSONSalandNSO vpaiswithOm Opal!\a" << endl;
            field[x][y] = 5;
            Sleep(3300);
            tableShow();
            field[x][y] = 0;
            bool ship_detect = false;
            for (int i = 0; i < SIZEf - 5; i++)
            {
                for (int j = 0; j < SIZEf; j++)
                {
                    if (field[i][j] == 1)
                    {
                        ship_detect = true;
                        break;
                    }
                }
                if (ship_detect == true)
                {
                    break;
                }
            }
            if (ship_detect == false)
            {
                cout << "\t\tv vpal!!! vwithis OpalandwithONSispa NSOisNSlis!" << endl;
                Sleep(4000);
                win();
                Sleep(2000);
                ad();
                break;
            }
            isMyRound = true;
        }
        else
        {
            cout << "\n\t\t\t\tv NSpOmalwith!" << endl;
            field[x][y] = 0;
            isMyRound = false;
            Sleep(3400);
            tableShow();
        }
        while (isMyRound == false) // * Od Oa
        {
            cout << "\t\tOd Oa...\n\n";
            x = rand() % 5 + 6;
            y = rand() % 10 + 1;
            cout << "\t\t\t\tO vvisl OOpda: x[" << x-- << "] andy[" << y-- << "]\n";
            if (field[x][y] == 1)
            {
                cout << "\n\t\t\t\tO NSONSal NSO aism Opal!\a" << endl;
                field[x][y] = 5;
                Sleep(5500);
                tableShow();
                field[x][y] = 0;
                bool ship_detect = false;
                for (int i = 6; i < SIZEf; i++)
                {
                    for (int j = 0; j < SIZEf; j++)
                    {
                        if (field[i][j] == 1)
                        {
                            ship_detect = true;
                            break;
                        }
                    }
                    if (ship_detect == true)
                    {
                        break;
                    }
                }
                if (ship_detect == false)
                {
                    cout << "\t\t\t\tO vpal!!! vwithis aandOpalandNSOisNSlis!" << endl;
                    Sleep(5500);
                    ad();
                    break;
                }
            }
            else
            {
                cout << "\n\t\t\t\tO NSpOmalwithI am! vaa Oispisd..." << endl;
                isMyRound = true;
                field[x][y] = 0;
                Sleep(5500);
                tableShow();
            }
        } // *
    }
    cout << endl << endl << endl;
    system("pause");
    return 0;
}

Answer 1, authority 100%

To prevent enemy ships from being displayed until they are knocked down in the tableshow () function, change the code to:

void tableShow(){
system("cls");
cout << endl << endl << "\t\t\t\t";
int counter = 0;
for (int i = 0; i < SIZEf; i++)
{
    counter++;
    cout << counter;
    cout << "\t";
}
cout << "\n\t\t\t\t==========================================================================";
counter = 0;
cout << endl << endl << "\t\t";
for (int i = 0; i < SIZEf; i++)
{
    for (int j = 0; j < SIZEf; j++)
    {
        if (i >= 0 && j == 0)
        {
            counter++;
            cout << "\t" << counter << " |";
        }
        if (field[i][j] == 5){
            cout << "\t" << field[i][j];
        }
        else if (i < 5){
            cout << "\t0";
        }
        else{
            cout << "\t" << field[i][j];
        }
    }
    cout << endl << endl << endl << "\t\t";
}
}

Replace the number of killed ships with -1 for some functions and for the view.
In the main function, you need to add some variables:

int Direction = -1;
bool BotHit = false;
bool BotHitForSecondBotHit = false;
bool SecondBotHit = false;
bool BotMiss[4] = {false, false, false, false}; //NSONSadais Oa NSO ispm withOpOam

Next, during the bot, replace all x and y with BotX and BotY. After that add the following code:

cout << "\t\tOd Oa...\n\n";
        if (BotHit == true){
            if (SecondBotHit == false){
                while (true){
                    Direction = 1 + rand() % 4;
                    if (Direction == 1 && BotMiss[0] == false && BotY > 0){
                        BotY--; //withpislI amis lisvisis
                        break;
                    }
                    else if (Direction == 2 && BotMiss[1] == false && BotX > 5){
                        BotX--; //withpislI amis vis
                        break;
                    }
                    else if (Direction == 3 && BotMiss[2] == false && BotY < 9){
                        BotY++; //withpislI amis NSpavisis
                        break;
                    }
                    else if (Direction == 4 && BotMiss[3] == false && BotX < 9){
                        BotX++; //withpislI amis is
                        break;
                    }
                    else{
                        BotMiss[Direction - 1] = true; //iswithlandda is NSOllOwith vwithpisl
                    }
                    if (BotMiss[0] == true && BotMiss[1] == true && BotMiss[2] == true && BotMiss[3] == true){
                        break; //iswithlandis landNSONSandvwithpisl NSO vwithism withOpOam
                    }
                }
            }
            else{ //NSpandvOpOm NSONSadaandwithpislI amis v aNSpavlis, v OOpOm pais withpislI aml
                if (Direction == 1 && BotY > 0){
                    BotY--;
                }
                else if (Direction == 2 && BotX > 5){
                    BotX--;
                }
                else if (Direction == 3 && BotY < 9){
                    BotY++;
                }
                else if (Direction == 4 && BotX < 9){
                    BotX++;
                }
                else{
                    SecondBotHit = false;
                }
            }
        }
        else{
            BotX = rand() % 4 + 5;
            BotY = rand() % 10;
        }

This will determine the side of the shot.
I noticed that you are generating coordinates incorrectly and displaying them. Arrays start at 0, which means we need to generate

BotX = rand() % 4 + 5;
BotY = rand() % 10;

And show the coordinates like this:

int CoutBotX = BotX + 1;
        int CoutBotY = BotY + 1;
        cout << "\t\t\t\tO vvisl OOpda: x[" << CoutBotX << "] andy[" << CoutBotY << "]\n";

To prevent the AI ​​from reacting to killed ships, add the following part of the code at the beginning of the miss:

if (field[BotX][BotY] != -1){
                cout << "\n\t\t\t\tO NSpOmalwithI am! vaa Oispisd..." << endl;
                isMyRound = true;
            }

Records a miss in one of the sides and checks whether the bot fired on all four sides, and also returns the coordinates for hitting the ship:

           BotHitForSecondBotHit = false;
            if (Direction != -1){
                BotMiss[Direction - 1] = true;
                if ((BotMiss[0] == true && BotMiss[1] == true && BotMiss[2] == true && BotMiss[3] == true) || SecondBotHit == true){
                    BotHit = false;
                    for (int x = 0; x != 4; x++){
                        BotMiss[x] = false;
                    }
                }
                if (SecondBotHit == false && BotHit == true){
                    if (Direction == 1){
                        BotY++; //aNSpavlI amis OOpda NSpavisis
                    }
                    else if (Direction == 2){
                        BotX++; //aNSpavlI amis OOpda is
                    }
                    else if (Direction == 3){
                        BotY--; //aNSpavlI amis OOpda lisvisis
                    }
                    else if (Direction == 4){
                        BotX--; //aNSpavlI amis OOpda vis
                    }
                }
            }
            SecondBotHit = false;

I advise you to either move to the beginning after checking the killed ship, or remove

field[i][j] = 0

The code looks like this:

while (isMyRound == false) // * Od Oa
    {
        cout << "\t\tOd Oa...\n\n";
        if (BotHit == true){
            if (SecondBotHit == false){
                while (true){
                    Direction = 1 + rand() % 4;
                    if (Direction == 1 && BotMiss[0] == false && BotY > 0){
                        BotY--; //withpislI amis lisvisis
                        break;
                    }
                    else if (Direction == 2 && BotMiss[1] == false && BotX > 5){
                        BotX--; //withpislI amis vis
                        break;
                    }
                    else if (Direction == 3 && BotMiss[2] == false && BotY < 9){
                        BotY++; //withpislI amis NSpavisis
                        break;
                    }
                    else if (Direction == 4 && BotMiss[3] == false && BotX < 9){
                        BotX++; //withpislI amis is
                        break;
                    }
                    else{
                        BotMiss[Direction - 1] = true; //iswithlandda is NSOllOwith vwithpisl
                    }
                    if (BotMiss[0] == true && BotMiss[1] == true && BotMiss[2] == true && BotMiss[3] == true){
                        break; //iswithlandis landNSONSandvwithpisl NSO vwithism withOpOam
                    }
                }
            }
            else{ //NSpandvOpOm NSONSadaandwithpislI amis v aNSpavlis, v OOpOm pais withpislI aml
                if (Direction == 1 && BotY > 0){
                    BotY--;
                }
                else if (Direction == 2 && BotX > 5){
                    BotX--;
                }
                else if (Direction == 3 && BotY < 9){
                    BotY++;
                }
                else if (Direction == 4 && BotX < 9){
                    BotX++;
                }
                else{
                    SecondBotHit = false;
                }
            }
        }
        else{
            BotX = rand() % 4 + 5;
            BotY = rand() % 10;
        }
        int CoutBotX = BotX + 1;
        int CoutBotY = BotY + 1;
        cout << "\t\t\t\tO vvisl OOpda: x[" << CoutBotX << "] andy[" << CoutBotY << "]\n";
        if (field[BotX][BotY] == 1)
        {
            if (BotHit == true){
                BotHitForSecondBotHit = true;
            }
            if (BotHitForSecondBotHit == true){
                SecondBotHit = true;
            }
            BotHit = true;
            cout << "\n\t\t\t\tO NSONSal NSO aism Opal!\a" << endl;
            field[BotX][BotY] = 5;
            Sleep(5500);
            tableShow();
            field[BotX][BotY] = -1;
            bool ship_detect = false;
            for (int i = 6; i < SIZEf; i++)
            {
                for (int j = 0; j < SIZEf; j++)
                {
                    if (field[i][j] == 1)
                    {
                        ship_detect = true;
                        break;
                    }
                }
                if (ship_detect == true)
                {
                    break;
                }
            }
            if (ship_detect == false)
            {
                cout << "\t\t\t\tO vpal!!! vwithis aandOpalandNSOisNSlis!" << endl;
                Sleep(5500);
                ad();
                break;
            }
        }
        else
        {
            if (field[BotX][BotY] != -1){
                cout << "\n\t\t\t\tO NSpOmalwithI am! vaa Oispisd..." << endl;
                isMyRound = true;
            }
            BotHitForSecondBotHit = false;
            if (Direction != -1){
                BotMiss[Direction - 1] = true;
                if ((BotMiss[0] == true && BotMiss[1] == true && BotMiss[2] == true && BotMiss[3] == true) || SecondBotHit == true){
                    BotHit = false;
                    for (int x = 0; x != 4; x++){
                        BotMiss[x] = false;
                    }
                }
                if (SecondBotHit == false && BotHit == true){
                    if (Direction == 1){
                        BotY++; //aNSpavlI amis OOpda NSpavisis
                    }
                    else if (Direction == 2){
                        BotX++; //aNSpavlI amis OOpda is
                    }
                    else if (Direction == 3){
                        BotY--; //aNSpavlI amis OOpda lisvisis
                    }
                    else if (Direction == 4){
                        BotX--; //aNSpavlI amis OOpda vis
                    }
                }
            }
            SecondBotHit = false;
            Sleep(5500);
            tableShow();
        }
    } // *

I hope I wrote everything correctly and this will help you.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions