Home c++ C++ Sea battle - Multipalup ships

C++ Sea battle – Multipalup ships

Author

Date

Category

wrote the game the sea battle and implemented everything that is required in addition to one. Multipalup ships. I have all the ships are uninfell. How to change the code in such a way that it was on each side of 1, 2, 3, 4, 5 decks 5 ships?

# define _crt_secure_no_warnings
#Include & lt; iostream & gt;
#Include & lt; conio.h & gt;
#Include & lt; math.h & gt;
#Include & lt; Time.h & gt;
#Include & lt; Windows.h & gt;
#InClude & lt; Iomanip & gt;
#Include & lt; String & GT;
#Include & lt; stdio.h & gt;
#Include & lt; dos.h & gt;
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 & LT; & LT; "\ N \ n"; // * Figure
  for (int x = 0; x & lt; 13; x ++)
  {
    for (int y = 0; y & lt; 13; y ++)
    {
      if (x == 0 || x == y & amp; & amp; y & lt; 13/2 || x + y == 13 - 1 & amp; & amp; y & gt; 13/2 - 1)
      {
        COUT & LT; & LT; "\ t *";
      }
      ELSE.
      {
        COUT & LT; & LT; "\ t";
      }
    }
    COUT & LT; & LT; Endl;
  } // *
  Gotoxy (27, 12);
  COUT & LT; & LT; "\ t \ t \ t - & gt; new game & lt; -";
  Gotoxy (30, 13);
  COUT & LT; & LT; "\ t \ t \ t output";
  COUT & LT; & LT; "\ N \ N \ t \ t" Sea battle "- a game for two participants in which players take turns call coordinates on the opponent's unknown map. If an opponent has a ship on these coordinates (coordinates are occupied), then the ship or its part "Returns", and the fall receives the right to make another 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 & LT; & LT; "\ t \ t \ t - & gt; new game & lt; -";
        Gotoxy (30, 13);
        COUT & LT; & LT; "\ t \ t \ t output";
      }
      ELSE if (f == 2)
      {
        Gotoxy (27, 12);
        COUT & LT; & LT; "\ t \ t \ t new game";
        Gotoxy (30, 13);
        COUT & LT; & LT; "\ t \ t \ t output";
      }
      ELSE.
      {
        Gotoxy (27, 12);
        COUT & LT; & LT; "\ t \ t \ t new game";
        Gotoxy (30, 13);
        COUT & LT; & LT; "\ t \ t \ t - & gt; output & lt; -";
      }
    }
    ELSE if (code == 72)
    {
      f = f * (-1);
      if (f == 1)
      {
        Gotoxy (27, 12);
        COUT & LT; & LT; "\ t \ t \ t - & gt; new game & lt; -";
        Gotoxy (30, 13);
        COUT & LT; & LT; "\ t \ t \ t output";
      }
      ELSE if (f == 2)
      {
        Gotoxy (27, 12);
        COUT & LT; & LT; "\ t \ t \ t new game";
        Gotoxy (30, 13);
        COUT & LT; & LT; "\ t \ t \ t output";
      }
      ELSE.
      {
        Gotoxy (27, 12);
        COUT & LT; & LT; "\ t \ t \ t new game";
        Gotoxy (30, 13);
        COUT & LT; & LT; "\ t \ t \ t - & gt; output & lt; -";
      }
    }
    ELSE If (code == 13)
    {
      k = 1;
    }
    COUT & LT; & LT; Endl & lt; & lt; Endl & lt; & lt; Endl & lt; & lt; Endl & lt; & lt; Endl;
  }
  While (k == 0);
  {
    IF (F! = 1)
    {
      EXIT (0);
    }
  }
  System ("CLS");
  COUT & LT; & LT; Endl & lt; & lt; Endl;
}
Void Figure1 ()
{
  System ("CLS");
  For (int x = 0; x & lt; 15; x ++) // * Created a square
  {
    for (int y = 0; y & lt; 15; y ++)
    {
      if (x == 0 || x == 15 - 1 || y == 0 || y == 15 - 1)
      {
        COUT & LT; & LT; "\ t *";
      }
      ELSE.
      {
        COUT & LT; & LT; "\ t";
      } 
if (x == 6) // ** Give below to write text inside the square
      {
        COUT & LT; & LT; "\ t \ t \ t \ t \ t";
        String H = "Sea battle!";
        for (int i = 0; i & lt; h.length (); i ++)
        {
          Sleep (60);
          COUT & LT; & LT; h [i];
        }
        Break;
      } // **
    }
    COUT & LT; & LT; Endl; // Endl To create rows for a square
  }
  SLEEP (1500); // Pause // *
  COUT & LT; & LT; "\ a"; // Sound Switching Menu
}
Void Reminder1 ()
{
  System ("CLS");
  COUT & LT; & LT; "\ n \ n \ n \ n \ n \ n \ n \ n \ n \ t \ t \ t \ t \ t reminder: \ n";
  COUT & LT; & LT; "\ t \ t \ t Press the cross from above to get out of the game \ N \ t \ t \ t with the achievements are not saved!";
  COUT & LT; & LT; "\ n \ t \ t \ t \ t";
  String H = "................................."; // ** artificial expectation using the download imitation visualized by points
  for (int i = 0; i & lt; h.length (); i ++)
  {
    Sleep (50);
    COUT & LT; & LT; h [i];
  } // **
  COUT & LT; & LT; "\ N \ n";
  SLEEP (1000);
  System ("CLS");
}
void win ()
{
  System ("CLS");
  for (int x = 0; x & lt; = 2; x ++)
  {
    IF (X & LT; = 1)
    {
      System ("Color 05");
      Sleep (200);
      COUT & LT; & LT; "\ n \ n \ n \ n \ n \ n \ n \ t \ t \ t \ t \ t \ t \ tp";
      System ("Color 15");
      Sleep (200);
      COUT & LT; & LT; " O";
      System ("Color 25");
      Sleep (200);
      COUT & LT; & LT; "s";
      System ("Color 35");
      Sleep (200);
      COUT & LT; & LT; "D";
      System ("Color 48");
      Sleep (200);
      COUT & LT; & LT; " R";
      System ("Color 65");
      Sleep (200);
      COUT & LT; & LT; " a";
      System ("Color 85");
      Sleep (200);
      COUT & LT; & LT; " v";
      System ("Color 93");
      Sleep (200);
      COUT & LT; & LT; "L";
      System ("Color A5");
      Sleep (200);
      COUT & LT; & LT; " I am";
      System ("Color B5");
      Sleep (200);
      COUT & LT; & LT; "E";
      System ("Color C0");
      Sleep (200);
      COUT & LT; & LT; "M";
      System ("Color E5");
      Sleep (200);
      COUT & LT; & LT; "\ n \ n \ t \ t \ t \ t \ t \ t \ t \ t";
      System ("Color F5");
      Sleep (200);
      COUT & LT; & LT; " a";
      System ("Color 75");
      Sleep (200);
      COUT & LT; & LT; " with";
      Sleep (200);
    }
    ELSE.
    {
      System ("CLS");
      For (int x = 0; x & lt; 15; x ++) // * Created a square
      {
        for (int y = 0; y & lt; 15; y ++)
        {
          if (x == 0 || x == 15 - 1 || y == 0 || y == 15 - 1)
          {
            COUT & LT; & LT; "\ t *";
          }
          ELSE.
          {
            COUT & LT; & LT; "\ t";
          }
          if (x == 6) // ** Give below to write text inside the square
          {
            COUT & LT; & LT; "\ t \ t \ t \ t \ t";
            String H = "You won!";
            for (int i = 0; i & lt; h.length (); i ++)
            {
              Sleep (60);
              COUT & LT; & LT; h [i];
            }
            Break;
          } // **
        }
        COUT & LT; & LT; Endl; // Endl To create rows for a square
      } // *
    }
  }
}
Void Ad () // Small Advertising 1
{
  System ("CLS");
  For (int x = 0; x & lt; 15; x ++) // * Created a square
  {
    for (int y = 0; y & lt; 15; y ++)
    {
      if (x == 0 || x == 15 - 1 || y == 0 || y == 15 - 1)
      {
        COUT & LT; & LT; "\ t *";
      }
      ELSE.
      {
        COUT & LT; & LT; "\ t";
      }
      if (x == 6) // ** Give below to write text inside the square
      { 
COUT & LT; & LT; "\ T \ t \ t \ tNebolshaya advertising integration: \ n \ n \ t \ t \ t \ t";
        string h = "Betting Ramin! One only Ramin! \ n \ n \ t \ t \ t \ t Big wins! One only Ramin! \ n \ n \ t \ t \ t \ t Quick Solutions! One only Ramin! \ n \ n \ t \ t \ t \ t Reliable president! one only Ramin! \ n \ n \ t \ t \ t \ t Vote for one only Ramin \ n \ n \ t \ t \ t \ t The land him down ... \ n \ n ";
        for (int i = 0; i & lt; h.length (); i ++)
        {
          Sleep (40);
          COUT & LT; & LT; h [i];
        }
        Break;
      } // **
    }
    COUT & LT; & LT; Endl; // Endla to create lines for the square
  } //
  Sleep (3300);
  system ( "cls");
}
void tableShow ()
{
  system ( "cls");
  COUT & LT; & LT; Endl & lt; & lt; Endl & lt; & lt; "\ T \ t \ t \ t";
  int counter = 0;
  for (int i = 0; i & lt; SIZEf; i ++)
  {
    counter ++;
    COUT & LT; & LT; counter;
    COUT & LT; & LT; "\ T";
  }
  COUT & LT; & LT; "\ N \ t \ t \ t \ t ======================================= =================================== ";
  counter = 0;
  COUT & LT; & LT; Endl & lt; & lt; Endl & lt; & lt; "\ T \ t";
  for (int i = 0; i & lt; SIZEf; i ++)
  {
    if (i == 5)
    {
      COUT & LT; & LT; "\ T =============================================== =========================== \ n \ n \ n \ t \ t ";
    }
    for (int j = 0; j & lt; SIZEf; j ++)
    {
      if (i & gt; = 0 & amp; & amp; j == 0)
      {
        counter ++;
        COUT & LT; & LT; "\ T" & lt; & lt; counter & lt; & lt; "|";
      }
      if (field [i] [j] == 5)
      {
        COUT & LT; & LT; "\ T" & lt; & lt; field [i] [j];
      }
      else if (i & lt; 5)
      {
        COUT & LT; & LT; "\ T0";
      }
      ELSE.
      {
        COUT & LT; & LT; "\ T" & lt; & lt; field [i] [j];
      }
    }
    COUT & LT; & LT; Endl & lt; & lt; Endl & lt; & lt; Endl & lt; & lt; "\ T \ t";
  }
}
INT MAIN ()
{
  setlocale (LC_ALL, "Rus");
  srand (unsigned (time (0)));
  system ( "Color 75");
  // menu ();
  // figure1 ();
  // reminder1 ();
  int x, y;
  int enemyShipCounter = 0; // * Location and number of enemy ships
  while (true)
  {
    x = rand ()% SIZEf;
    if (x & lt; = 4)
    {
      y = rand ()% SIZEf;
      field [x] [y] = 1;
      enemyShipCounter ++;
      if (enemyShipCounter == 10)
      {
        Break;
      }
    }
  } // *
  int myShipCounter = 0; // * Location and number of my ships
  while (true)
  {
    x = rand ()% SIZEf;
    if (x & gt; = 5)
    {
      y = rand ()% SIZEf;
      field [x] [y] = 1;
      myShipCounter ++;
      if (myShipCounter == 10)
      {
        Break;
      }
    }
  } // *
  tableShow ();
  bool isMyRound = false;
  while (true)
  {
    COUT & LT; & LT; "\ T \ tVvedite coordinates";
    do.
    {
      CIN & GT; & GT; x;
      CIN & GT; & GT; y;
      if (x & gt; = 6 || x & lt; = 0 || y & lt; = 0)
      {
        COUT & LT; & LT; "\ N \ t \ t \ t \ tVvedite coordinates are above 6 bar (6, 5, 4, 3, 2, 1) to shoot the enemy field \ n \ n!";
        COUT & LT; & LT; "\ T \ t \ t \ tVvedite coordinates";
      }
    } While (x & gt; = 6 || x & lt; = 0 || y & lt; = 0);
    {
      x--;
      y--;
    }
    if (field [x] [y] == 1)
    {
      COUT & LT; & LT; "\ N \ t \ t \ t \ tVy hit by the enemy ship \ a!" & Lt; & lt; Endl;
      field [x] [y] = 5;
      Sleep (3300);
      tableShow ();
      bool ship_detect = false;
      for (int i = 0; i & lt; SIZEf - 5; i ++)
      {
        for (int j = 0; j & lt; SIZEf; j ++)
        {
          if (field [i] [j] == 1)
          {
            ship_detect = true;
            Break;
          }
        } 
if (Ship_Detect == True)
        {
          Break;
        }
      }
      if (ship_detect == false)
      {
        COUT & LT; & LT; "\ t \ tv won !!! All the warships of the opponent are warming!" & lt; & lt; Endl;
        Sleep (4000);
        win ();
        Sleep (2000);
        ad ();
        Break;
      }
      ismyround = true;
    }
    ELSE.
    {
      COUT & LT; & LT; "\ N \ t \ t \ t \ Tiles missed!" & lt; & lt; Endl;
      ismyround = false;
      SLEEP (3400);
      Tableshow ();
    }
    INT BOTX, BOTY; // * Bott
    int direction = -1;
    BOOL BOTHIT = FALSE;
    BOOL BOTHITFORSECONDBOTHIT = FALSE;
    BOOL SecondBothit = false;
    BOOL BOTMISS [4] = {False, False, False, False}; // getting a bot in four sides
    While (Ismyround == False)
    {
      COUT & LT; & LT; "\ t \ switch bot ... \ n \ n";
      If (Bothit == True)
      {
        IfBothit == False)
        {
          While (True)
          {
            Direction = 1 + Rand ()% 4;
            if (direction == 1 & amp; & amp; botmiss [0] == false & amp; & amp; boty & gt; 0)
            {
              Boty--; // shoot left
              Break;
            }
            ELSE If (Direction == 2 & amp; & amp; BotMiss [1] == False & amp; & amp; Botx & GT; 5)
            {
              BotX--; // shoots above
              Break;
            }
            ELSE If (Direction == 3 & amp; & amp; Botmiss [2] == False & amp; & amp; Boty & lt; 9)
            {
              Boty ++; // shoots the right
              Break;
            }
            ELSE If (Direction == 4 & amp; & amp; BotMiss [3] == False & amp; & amp; Botx & lt; 9)
            {
              BotX ++; // shoots below
              Break;
            }
            ELSE.
            {
              BotMiss [Direction - 1] = True; // If it did not work anywhere
            }
            If (Botmiss [0] == True & amp; & amp; BotMiss [1] == True & amp; & amp; BotMiss [2] == True & amp; & amp; BotMiss [3] == True)
            {
              Break; // If there were already attempts to shoot on all parties
            }
          }
        }
        ELSE.
        {// When the second hit shoots in the direction in which he used to shoot
          If (Direction == 1 & amp; & amp; Boty & gt; 0)
          {
            Boty--;
          }
          ELSE if (Direction == 2 & amp; & amp; botx & gt; 5)
          {
            BotX--;
          }
          ELSE If (Direction == 3 & amp; & amp; Boty & lt; 9)
          {
            Boty ++;
          }
          ELSE If (direction == 4 & amp; & amp; botx & lt; 9)
          {
            BotX ++;
          }
          ELSE.
          {
            SecondBothit = false;
          }
        }
      }
      ELSE.
      {
        Botx = Rand ()% 4 + 5;
        Boty = Rand ()% 10;
      }
      INT COUTBOTX = BOTX + 1;
      int coutboty = Boty + 1;
      COUT & LT; & LT; "\ t \ t \ t \ tbot introduced coordinates: x [" & lt; & lt; Coutbotx & lt; & lt; "] and y [" & lt; & lt; Coutboty & lt; & lt; "] \ n";
      if (Field [Botx] [Boty] == 1)
      {
        If (Bothit == True)
        {
          BOTHITFORSECONDBOTHIT = TRUE;
        }
        If (BothitForSecondBothit == True)
        {
          SecondBothit = True;
        }
        BOTHIT = TRUE; 
COUT & LT; & LT; "\ n \ t \ t \ t \ tbot hit our ship! \ a" & lt; & lt; Endl;
        Field [Botx] [Boty] = 5;
        SLEEP (5500);
        Tableshow ();
        Field [Botx] [Boty] = -1;
        BOOL SHIP_DETECT = FALSE;
        for (int i = 6; i & lt; sizef; i ++)
        {
          for (int j = 0; j & lt; sizef; j ++)
          {
            if (field [i] [j] == 1)
            {
              SHIP_DETECT = TRUE;
              Break;
            }
          }
          if (Ship_Detect == True)
          {
            Break;
          }
        }
        if (ship_detect == false)
        {
          COUT & LT; & LT; "\ t \ t \ t \ tbot won !!! All our ships are warming!" & lt; & lt; Endl;
          SLEEP (5500);
          ad ();
          Break;
        }
      }
      ELSE.
      {
        If (Field [Botx] [Boty]! = -1)
        {
          COUT & LT; & LT; "\ n \ t \ t \ t \ tbot missed! Your turn ..." & lt; & lt; Endl;
          ismyround = true;
        }
        BothitForSecondBothit = false;
        if (direction! = -1)
        {
          BotMiss [Direction - 1] = True;
          If ((Botmiss [0] == True & amp; & amp; BotMiss [1] == True & amp; & amp; Botmiss [2] == True & amp; & amp; BotMiss [3] == True) || secondbothit == True )
          {
            Bothit = false;
            for (int x = 0; x! = 4; x ++)
            {
              BotMiss [x] = false;
            }
          }
          if (secondbothit == false & amp; & amp; botit == true)
          {
            if (direction == 1)
            {
              Boty ++; // sends the coordinate to the right
            }
            ELSE If (Direction == 2)
            {
              BotX ++; // sends a coordinate below
            }
            ELSE If (Direction == 3)
            {
              Boty--; // directs the coordinate to the left
            }
            ELSE If (Direction == 4)
            {
              BotX--; // sends the coordinate above
            }
          }
        }
        SecondBothit = false;
        SLEEP (5500);
        Tableshow ();
      }
    } // *
  }
  COUT & LT; & LT; Endl & lt; & lt; Endl & lt; & lt; Endl;
  SYSTEM ("PAUSE");
  Return 0;
}
// Unrealized - Multipleup Ships

Answer 1, Authority 100%

The first thing that comes to the head is read about the OOP. One of the main qualities of a good code is the ability to conveniently edit it and supplement it. For this, the code needs to be well structured. If you were decided on abstractions and implemented several classes (for example, the class of field, ship, player), then you would not have any questions “and how to add”.

Regarding your question:
You can fill in the matrix numbers from 0 to 5. Let’s say five waters in a row is 5 deck.
Randomly choose a point and direction, at the same time you immediately check if the sequence is out of the array.

To check whether the 5-deck is destroyed, go around the array- if there are no top five, then the ship is destroyed.

If you need a few 2 Wow decks: you can select several numbers for them using the listing. For example, the number 2, 6,12 14 will be responsible for 2-decks:

enum two_ships {two1 = 2, two2 = 6, two3 = 12, two4 = 14};

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