Advertisement
Toliak

kek 11

Dec 20th, 2018
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. struct BattleShips // used a class as will have 2 instances, one for computer, one for human
  9. {
  10.     const static int rows = 10;
  11.     const static int columns = 10;
  12.     int coOrd;
  13.     int maximumShips;
  14.     int matrix[rows][columns];
  15.     BattleShips()
  16.     { // CLASS CONSTRUCTOR
  17.         coOrd = 0;
  18.         maximumShips = 10;
  19.     }
  20.  
  21.     void ClearGrid() // simple function to clear the matrix.
  22.     {
  23.         for (int y = 0; y < rows; y++) // loops while y is less than rows, and increments y
  24.         {
  25.             for (int x = 0; x < columns; x++) // loops while x is less than columns and increments x
  26.             {
  27.                 matrix[y][x] = 0; // sets each matrix row and column to 0
  28.             }
  29.         }
  30.     }
  31.  
  32.  
  33.     void ShowGrid()
  34.     {
  35.         cout << "   0 1 2 3 4 5 6 7 8 9" << endl;
  36.         cout << "-----------------------" << endl;
  37.         for (int x = 0; x < rows; x++)
  38.         {
  39.             cout << coOrd << "| ";
  40.             coOrd++;
  41.             for (int y = 0; y < rows; y++)
  42.             {
  43.                 cout << matrix[x][y] << " ";
  44.             }
  45.             cout << endl;
  46.         }
  47.     }
  48.  
  49.     int numberOfShips() // very similar to other methods but returns an int value, this will be to check the amount of ships left after the user inputs their data
  50.     {
  51.         int c = 0;
  52.         for (int x = 0; x < rows; x++)
  53.         {
  54.             for (int y = 0; y < columns; y++)
  55.             {
  56.                 if (matrix[x][y] == 1)
  57.                 {
  58.                     c++;
  59.                 }
  60.             }
  61.         }
  62.         return c;
  63.     }
  64.  
  65.     void setShips()
  66.     {
  67.         int ships = 0;
  68.         while (ships < maximumShips)
  69.         {
  70.             int xPos = rand() % 10; // modulo 10 to make an int between 0 and 9
  71.             int yPos = rand() % 10; // modulo 10 to make an int between 0 and 9
  72.  
  73.             if (matrix[xPos][yPos] != 1)
  74.             {
  75.                 ships++;
  76.                 matrix[xPos][yPos] = 1; // sets position to 1
  77.             }
  78.         }
  79.  
  80.     }
  81.  
  82.     void setShipsManual(int size, char rotation, int row, int column)
  83.     {
  84.         if (rotation == 'h') {
  85.             for (int i = column; i < column + size; i++) {
  86.                 matrix[row][i] = 1;
  87.             }
  88.         } else if (rotation == 'v') {
  89.             for (int i = row; i < row + size; i++) {
  90.                 matrix[i][column] = 1;
  91.             }
  92.         } else {
  93.             // asdasdasd
  94.         }
  95.     }
  96.  
  97.     bool Attack(int _x, int _y) // makes a function with a boolean return type
  98.     {
  99.         if (matrix[_x][_y] == 1) // checks if the given parameters are a 'ship' on the matrix
  100.         {
  101.             matrix[_x][_y] = 2; // sets the position to 2 so the same position can not be re used
  102.             return true; // returns a boolean 'true'
  103.         }
  104.         return false; // returns a boolean 'false'
  105.     }
  106. };
  107.  
  108.  
  109.  
  110. struct Boards // makes another class called boards, will be used to initiate the game boards the user can see
  111. {
  112.     const static int rows = 10; // makes constants called rows and columns set to 10; to make the game board matrix
  113.     const static int columns = 10;
  114.     char grid[rows][columns]; // makes an multidimensional array of data type 'char'
  115.     // CLASS CONSTRUCTOR NOT NECCESSARY AS ALL VALUES ASSIGNED IN PRIVATE BLOCK
  116.     void MakeBoards() // declares a function to make the game boards
  117.     {
  118.         for (int x = 0; x < rows; x++) // starts a for loop
  119.         {
  120.             for (int y = 0; y < columns; y++) // nested for loop
  121.             {
  122.                 grid[x][y] = '-'; // sets the element in the position to a '-'
  123.             }
  124.         }
  125.     }
  126.  
  127.     void MakeBoards(const BattleShips &ships)
  128.     {
  129.         for (int x = 0; x < rows; x++) // starts a for loop
  130.         {
  131.             for (int y = 0; y < columns; y++) // nested for loop
  132.             {
  133.                 if (ships.matrix[x][y] == 1)
  134.                     grid[x][y] = '=';
  135.                 else
  136.                     grid[x][y] = '-'; // sets the element in the position to a '-'
  137.             }
  138.         }
  139.     }
  140.  
  141.     void updateBoards(bool i, int x, int y) // makes a function to update the game boards, takes 3 paramaters. a boolean, and two integers
  142.     {
  143.         int xPos = x; // sets two variables equal to the int values passed in
  144.         int yPos = y;
  145.  
  146.         if (i == true) // checks if i is equal to 'true'
  147.         {
  148.             grid[xPos][yPos] = 'Y'; // sets the element in the position to 'Y'
  149.         }
  150.         else
  151.         {
  152.             grid[xPos][yPos] = 'N'; // sets the element in the position to 'N'
  153.         }
  154.     }
  155.  
  156.     void PrintBoards() // makes a function to print the boards
  157.     {
  158.         int amt = 0; // sets an int called amt to 0
  159.  
  160.         cout << "  0 1 2 3 4 5 6 7 8 9" << endl; // prints the top line  of coordinates
  161.         for (int x = 0; x < rows; x++) { // for loop
  162.             cout << amt << " "; // outputs the amount variable to the console + a space
  163.             amt++; // increments amt
  164.             for (int y = 0; y < columns; y++) { // nested for loop
  165.                 cout << grid[x][y] << " "; // outputs the element of the grid to the console, plus a space
  166.             }
  167.             cout << endl; // outputs an endline character
  168.         }
  169.     }
  170. };
  171.  
  172.  
  173.  
  174. int main() // this is what is run everytime the program starts
  175. {
  176.     char rerun; // declares a char variable called rerun
  177.  
  178.     do
  179.     {
  180.         srand((unsigned)time(NULL)); // to prevent sequence repetition between runs
  181.         BattleShips human; // instantiates the two objects
  182.         BattleShips cpu;
  183.  
  184.         Boards humanBoard; // instantiates the Board objects
  185.         Boards cpuBoard;
  186.  
  187.         human.ClearGrid(); // clears the existing matrix
  188.         //human.setShips(); // sets the position of the ships
  189.  
  190.         while (true) {
  191.             string command;
  192.             getline(cin, command);
  193.             if (command == "stop") {
  194.                 break;
  195.             } else if (command.size() >= 7) {
  196.                 int shipSize = command[0] - '0';
  197.                 char rotation = command[2];
  198.                 int row = command[4] - '0';
  199.                 int column = command[6] - '0';
  200.  
  201.                 human.setShipsManual(shipSize, rotation, row, column);
  202.             } else {
  203.                 // eto gg
  204.             }
  205.         }
  206.  
  207.  
  208.         cpu.ClearGrid();  // clears the existing grid
  209.         cpu.setShips();
  210.  
  211.         humanBoard.MakeBoards(human); // makes the gameboards
  212.         cpuBoard.MakeBoards();
  213.  
  214.         cout << "Your Board: " << endl;
  215.         humanBoard.PrintBoards(); // prints the new boards
  216.         cout << "CPU's Board" << endl;
  217.         cpuBoard.PrintBoards();
  218.  
  219.         int position1, position2; // makes two integers for the positions to be stored inside
  220.         char which; // makes a char variable called which, will be used to store the result of asking the user if they want to forfeit.
  221.         int found = 0; // makes an int called found, initializes it to 0
  222.         int toGo = 10; // makes an int called toGo, initializes it to 10
  223.  
  224.         int cpuFound = 0; // ^ does the same for the cpu counters
  225.         int cpuToGet = 10;
  226.  
  227.         bool isTrue; // initializes two boolean variables.
  228.         bool isTrueCpu;
  229.  
  230.         while (found < 10 || cpuFound < 10) // loops while found is less than 10.
  231.         {
  232.             int cpuX = rand() % 10; // makes a random integer between 0 and 9, hence the mod 10.
  233.             int cpuY = rand() % 10;
  234.  
  235.             if (cpu.Attack(cpuX, cpuY)) // checks the boolean output of the Attack function for the cpu
  236.             {
  237.                 isTrueCpu = true; // sets isTrueCpu to true
  238.                 cpuFound++; // increments cpuFound
  239.                 cpuToGet--; // decrements cpuToGet
  240.                 cout << "The Computer has found one of your battleships at: " << "(" << cpuX << ", " << cpuY << ")" << endl; // outputs the position that the cpu found a battleship at.
  241.             }
  242.             else // anything else
  243.             {
  244.                 isTrueCpu = false; // sets isTrueCpu to false
  245.                 cout << "The Computer did not find a battleship this time" << endl; // outputs that the cpu did not find a battleship this time.
  246.             }
  247.  
  248.             //------------------------------------------------------------------------------------------------------------
  249.  
  250.             position1 = 11; // sets position1 and position2 to 11
  251.             position2 = 11; // so that the while loop will initiate below
  252.  
  253.             while (position1 > 9 || position2 > 9) // loops while position1 is more than 9 OR position 2  is more than 9.
  254.             {
  255.                 if (cpuFound == 10 || found == 10)
  256.                 {
  257.                     break;
  258.                 }
  259.                 cout << "Please input the location on the grid you think a battleship is: "; // prompts the user to enter co-ordinates.
  260.  
  261.                 cin >> position1 >> position2; // takes the keyboard input and stores it in position
  262.  
  263.                 while (cin.fail()) // this will inintiate if the user enters a non integer input.
  264.                 {
  265.                     cin.clear(); // clears the cin.
  266.                     cin.ignore(); // ignores so it does not go in to an infinite loop
  267.                     cout << "not int, try again: "; cin >> position1 >> position2; // re prompts the user to enter their input
  268.                 }
  269.             }
  270.  
  271.             if (human.Attack(position1, position2)) // checks if the boolean value for the Attack function is true
  272.             {
  273.                 isTrue = true; // sets isTrue to true
  274.                 found++; // increments found
  275.                 toGo--; // decrements toGo
  276.                 cout << "You have found: " << found << " battleships, you have: " << toGo << " to go" << endl; // alerts the user of how many battleships they have found, and how many more they need to get.
  277.             }
  278.             else // anything else
  279.             {
  280.                 isTrue = false; // sets isTrue to false
  281.                 cout << "there is no ship at that position, keep trying" << endl; // alerts the user that there is no ship in that position
  282.             }
  283.  
  284.             cout << "There are: " << human.numberOfShips() << " left" << endl; // tells the user how many more ships there is
  285.             cout << "would you like to surrender (y/n)?: "; cin >> which; // asks the user if they want to surrender and stores the input into char.
  286.  
  287.             system("CLS"); // clears the console to eliminate clutter
  288.  
  289.             humanBoard.updateBoards(isTrue, position1, position2); // updates the boards
  290.             cpuBoard.updateBoards(isTrueCpu, cpuX, cpuY);
  291.  
  292.             cout << "Your Board: " << endl;
  293.             humanBoard.PrintBoards(); // prints the new boards
  294.             cout << "CPU's Board" << endl;
  295.             cpuBoard.PrintBoards();
  296.  
  297.             if (which == 'y') // checks if which is equal to y
  298.             {
  299.                 break; // breaks from the loop
  300.             }
  301.             else if (found == 10 || cpuFound == 10)
  302.             {
  303.                 break;
  304.             }
  305.  
  306.         }
  307.  
  308.         // this code is run when the loop is exited.
  309.  
  310.         cout << "Game Over!" << endl; // outputs game over to the user
  311.         cout << "your grid: " << endl; // outputs the text "your grid"
  312.         cout << "The number 2 represents ships that have been hit" << endl;
  313.         human.ShowGrid(); // shows the human objects grid
  314.         cout << "----------------------------------------------------" << endl;
  315.         cout << "CPU's Grid" << endl; // outputs "CPU's Grid"
  316.         cpu.ShowGrid(); // shows the cpu grid to the user
  317.  
  318.         cout << "Would you like to rerun, or exit (r/e)"; cin >> rerun; // ask the user if they want to rerun, is stored in rerun.
  319.     } while (rerun == 'r' || rerun == 'R'); // part of the do while loop, checks if rerun is equal to 'y'
  320.  
  321.     return 0;
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement