Advertisement
Toliak

kek 13

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