Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. const int rows = 5, cols = 5;
  11. int numOfGames, gamesPlayed, shotsTaken;
  12. short unsigned int boat1X, boat1Y, boat2X[2], boat2Y[2], compBoat1[2], compBoat2X[2], compBoat2Y[2], playerHits, compHits;
  13. char gameBoard[rows][cols], usersGameBoard[rows][cols], compGameBoard[rows][cols], posOrNeg, axis;
  14. bool boatPresent = false;
  15. bool spaceFreeAround = false;
  16. bool placeSafe = false;
  17. int boatCoord[2] ={0,0};
  18.  
  19. struct coordinates
  20. {
  21.     int xAxisCoordinate;
  22.     int yAxisCoordinate;
  23. };
  24.  
  25.  
  26. void initialise()
  27. {
  28.     for(int i = 0; i < rows; i++)
  29.         {
  30.             for(int index = 0; index < cols; index++)
  31.             {
  32.                 gameBoard[i][index] = 'O';
  33.                 usersGameBoard[i][index]= 'O';
  34.                 compGameBoard[i][index] = 'O';
  35.             }
  36.         }
  37. }
  38.  
  39. void displayBoards()
  40. {
  41.     cout << "\nThe opponents Board looks like:\n";
  42.     for(int i = 0; i < rows; i++)
  43.         {
  44.             for(int index = 0; index < cols; index++)
  45.             {
  46.                 cout << gameBoard[i][index] << " ";
  47.             }
  48.             cout << " \n";
  49.         }
  50.  
  51.     cout << "\nYour Board looks like:\n";
  52.     for(int i = 0; i < rows; i++)
  53.         {
  54.             for(int index = 0; index < cols; index++)
  55.             {
  56.                 cout << usersGameBoard[i][index] << " ";
  57.             }
  58.             cout << " \n";
  59.         }
  60. }
  61. bool outOfRange(int boatPosX, int boatPosY)
  62. {
  63.     bool result;
  64.  
  65.     if(boatPosX > cols || boatPosX < 1)
  66.         result = true;
  67.     else if(boatPosY > rows || boatPosY < 1)
  68.         result = true;
  69.     else
  70.         result = false;
  71.  
  72.     return result;
  73. }
  74.  
  75. void playBoatValBoat2Fr()
  76. {
  77.     /** The front of the 2 long boat **/
  78.  
  79.     while(boat2X[0] > cols || boat2X[0] <= 0 )
  80.     {
  81.         cout << "Invalid X coordinate. Please enter again.";
  82.         cin >> boat2X[0];
  83.     }
  84.  
  85.     while(boat2Y[0] > rows || boat2Y[0] <= 0)
  86.     {
  87.         cout << "Invalid Y coordinate. Please enter again.";
  88.         cin >> boat2Y[0];
  89.     }
  90.  
  91.     while ((boat2X[0] == boat1X) && (boat2Y[0] == boat1Y))
  92.     {
  93.         cout << "\nThis part of the boat overlaps with the other boat.\n";
  94.         cout << "Please re-enter the first part of the boats' x coordinate: ";
  95.         cin >> boat2X[0];
  96.         cout << "Please the enter the first part of the boats' y coordinate: ";
  97.         cin >> boat2Y[0];
  98.  
  99.         playBoatValBoat2Fr();
  100.     }
  101. }
  102.  
  103. void playBoatValBoat2Bk()
  104. {
  105.     /** The back of the 2 long boat **/
  106.  
  107.     if(boat2X[1] >= boat2X[0] )
  108.         while((boat2X[1] - boat2X[0] != 0) && (boat2Y[1] - boat2Y[0] != 0) || ((boat2X[1] == boat2X[0]) && (boat2Y[1] == boat2Y[0]) ) )
  109.         {
  110.            cout << "\nThe two parts of the boat aren't next to each other!\n";
  111.            cout << "What is the x coordinate next to the front of the boat: ";
  112.            cin >>  boat2X[1];
  113.            cout << "What is the y coordinate of the back of the boat: ";
  114.            cin >> boat2Y[1];
  115.         }
  116.     else
  117.         while((boat2X[0] - boat2X[1] != 0) && (boat2Y[0] - boat2Y[1] != 0) || ((boat2X[1] == boat2X[0]) && (boat2Y[1] == boat2Y[0])))
  118.         {
  119.            cout << "\nThe two parts of the boat aren't next to each other!\n";
  120.            cout << "What is the x coordinate next to the front of the boat: ";
  121.            cin >>  boat2X[1];
  122.            cout << "What is the y coordinate of the back of the boat: ";
  123.            cin >> boat2Y[1];
  124.         }
  125. }
  126. void playBoatPlace()
  127. {
  128.     bool boatRange = false;
  129.  
  130.     cout << "\n\nYou get 2 boats a one long boat and a two long boat!";
  131.     do{
  132.         cout << "\n\nWhat is the x coordinate of the 1 long boat: ";
  133.         cin >> boat1X;
  134.         cout << "What is the y coordinate of the 1 long boat: ";
  135.         cin >> boat1Y;
  136.  
  137.         boatRange = outOfRange(boat1X, boat1Y);
  138.     }while(boatRange == true);
  139.  
  140.     cout << "\nWhat is the x coord of the front of the two long boat: ";
  141.     cin >> boat2X[0];
  142.     cout << "\nWhat is the y coord of the front of the two long boat: ";
  143.     cin >> boat2Y[0];
  144.  
  145.     playBoatValBoat2Fr();
  146.  
  147.     cout << "\nWhat is the x coordinate of the back of the boat: ";
  148.     cin >> boat2X[1];
  149.     cout << "\nWhat is the y coordinate of the back of the boat: ";
  150.     cin >> boat2Y[1];
  151.  
  152.  
  153.  
  154.     playBoatValBoat2Bk();
  155.  
  156.     boat1X--;
  157.     boat1Y--;
  158.     boat2X[0]--;
  159.     boat2Y[0]--;
  160.     boat2X[1]--;
  161.     boat2Y[1]--;
  162.  
  163.     usersGameBoard[boat1Y][boat1X] = 'B';
  164.     usersGameBoard[boat2Y[0]][boat2X[0]] = 'B';
  165.     usersGameBoard[boat2Y[1]][boat2X[1]] = 'B';
  166. }
  167.  
  168. void compBoat2Place(int direction)
  169. {
  170.     switch(direction)
  171.     {
  172.         case 1: compBoat2X[1] = compBoat2X[0];
  173.                 compBoat2Y[1] = compBoat2Y[0] - 1;
  174.                 break;
  175.         case 2: compBoat2X[1] = compBoat2X[0] - 1;
  176.                 compBoat2Y[1] = compBoat2Y[0];
  177.                 break;
  178.         case 3: compBoat2X[1] = compBoat2X[0];
  179.                 compBoat2Y[1] = compBoat2Y[0] + 1;
  180.                 break;
  181.         case 4: compBoat2X[1] = compBoat2X[0] + 1;
  182.                 compBoat2Y[1] = compBoat2Y[0];
  183.                 break;
  184.     }
  185. }
  186.  
  187. void compBoatPlace()
  188. {
  189.     short int boatDir;
  190.     bool boat2Accept = false;
  191.  
  192.     compBoat1[0] = (rand() % cols);
  193.     compBoat1[1] = (rand() % rows);
  194.  
  195.     compBoat2X[0] = (rand() % cols);
  196.     compBoat2Y[0] = (rand() % rows);
  197.  
  198.     do
  199.     {
  200.         boatDir = (rand() % 4) + 1;
  201.  
  202.         compBoat2Place(boatDir);
  203.  
  204.         boat2Accept = outOfRange(compBoat2X[1], compBoat2Y[1]);
  205.     }while(boat2Accept == true);
  206. }
  207.  
  208. bool compShotVal(int xCoord, int yCoord)
  209. {
  210.     bool notValid;
  211.  
  212.     if(usersGameBoard[yCoord][xCoord] == 'H' || usersGameBoard[yCoord][xCoord] == 'M')
  213.         notValid = true;
  214.     else
  215.         notValid = false;
  216.  
  217.     return notValid;
  218. }
  219.  
  220. bool userShotVal(int x, int y)
  221. {
  222.     bool notValid;
  223.  
  224.     if(gameBoard[y][x] == 'H' || gameBoard[y][x] == 'M')
  225.         notValid = true;
  226.     else
  227.         notValid = false;
  228.  
  229.     return notValid;
  230. }
  231.  
  232. void updateUsersBoard(int coordX, int coordY, char let)
  233. {
  234.     usersGameBoard[coordY][coordX] = let;
  235. }
  236.  
  237.  
  238. void updateCompsBoard(int posX, int posY, char let)
  239. {
  240.     gameBoard[posY][posX] = let;
  241. }
  242.  
  243. bool checkForHitUser(int x, int y)
  244. {
  245.     bool boatHit = false;
  246.     char letter;
  247.  
  248.     if(x == compBoat1[0] && y == compBoat1[1])
  249.     {
  250.         boatHit = true;
  251.         letter = 'H';
  252.     }
  253.     else if(x == compBoat2X[0] && y == compBoat2Y[0])
  254.     {
  255.         boatHit = true;
  256.         letter = 'H';
  257.     }
  258.     else if(x == compBoat2X[1] && y == compBoat2Y[1])
  259.     {
  260.         boatHit = true;
  261.         letter = 'H';
  262.     }
  263.     else
  264.     {
  265.         boatHit = false;
  266.         letter = 'M';
  267.     }
  268.  
  269.     updateCompsBoard(x, y, letter);
  270.  
  271.     return boatHit;
  272. }
  273.  
  274. void compDirectGo()
  275. {
  276.     int move2D = 0, move1D = 0;
  277.  
  278.     move2D = (rand() % 2) + 1;
  279.     move1D = (rand() % 2) + 1;
  280.  
  281.     switch(move2D)
  282.     {
  283.         case 1: axis = 'X';
  284.                 break;
  285.         case 2: axis = 'Y';
  286.                 break;
  287.     }
  288.  
  289.     switch(move1D)
  290.     {
  291.         case 1: posOrNeg = 'P';
  292.                 break;
  293.         case 2: posOrNeg = 'N';
  294.                 break;
  295.     }
  296. }
  297.  
  298. coordinates compSmartMove()
  299. {
  300.     bool notInRange;
  301.     coordinates notSmartMove = {10,0};
  302.     coordinates coordGo= {0,0};
  303.     int spacePosX, spacePosY, spaceNegY, spaceNegX, boatXCoord, boatYCoord;
  304.  
  305.     spaceNegY = (boatCoord[1]- 1);
  306.     spacePosY = (boatCoord[1]+ 1);
  307.     spaceNegX = (boatCoord[0]-1);
  308.     spacePosX = (boatCoord[0]+1);
  309.     boatXCoord = boatCoord[0];
  310.     boatYCoord = boatCoord[1];
  311.  
  312.  
  313.     if(shotsTaken == 0)
  314.     {
  315.         boatPresent = false;
  316.         spaceFreeAround = false;
  317.         placeSafe = false;
  318.         boatCoord[0] = 0;
  319.         boatCoord[1] = 0;
  320.     }
  321.  
  322.     if((usersGameBoard[spaceNegY][boatXCoord] !='O' )&& (usersGameBoard[spacePosY][boatXCoord] != 'O') && (usersGameBoard[boatYCoord][spaceNegX] !='O') && (usersGameBoard[boatYCoord][spacePosX] !='O'))
  323.         spaceFreeAround = false;
  324.  
  325.     if(boatPresent == true && spaceFreeAround == true)
  326.     {
  327.     for(int i = 0; i < rows; i++)
  328.     {
  329.         for(int index = 0; index < cols; index++)
  330.         {
  331.             if(usersGameBoard[i][index] == 'H' && !(usersGameBoard[i-1][index] !='O' && usersGameBoard[i+1][index] != 'O' && usersGameBoard[i][index-1] !='O' && usersGameBoard[i][index+1] !='O'))
  332.             {
  333.                 boatCoord[1] = i;
  334.                 boatCoord[0] = index;
  335.                 boatPresent = true;
  336.                 spaceFreeAround = true;
  337.                 break;
  338.             }
  339.             else if(usersGameBoard[i][index] == 'H')
  340.             {
  341.                 boatPresent = true;
  342.                 spaceFreeAround = false;
  343.             }
  344.         }
  345.         if(boatPresent == true && spaceFreeAround == true)
  346.             break;
  347.     }
  348.     }
  349.  
  350.     if(boatPresent == true && spaceFreeAround == true)
  351.     {
  352.         while(notInRange == true )
  353.         {
  354.         compDirectGo();
  355.  
  356.         if(posOrNeg == 'P')
  357.         {
  358.             if(axis == 'X')
  359.             {
  360.                 coordGo.xAxisCoordinate = boatCoord[0] + 1;
  361.                 coordGo.yAxisCoordinate = boatCoord[1];
  362.             }
  363.             else if(axis == 'Y')
  364.             {
  365.                 coordGo.xAxisCoordinate = boatCoord[0];
  366.                 coordGo.yAxisCoordinate = boatCoord[1] + 1;
  367.             }
  368.         }
  369.         else if(posOrNeg == 'N')
  370.         {
  371.             if(axis == 'X')
  372.             {
  373.                 coordGo.xAxisCoordinate = boatCoord[0] - 1;
  374.                 coordGo.yAxisCoordinate = boatCoord[1];
  375.             }
  376.             else if(axis == 'Y')
  377.             {
  378.                 coordGo.xAxisCoordinate = boatCoord[0];
  379.                 coordGo.yAxisCoordinate = boatCoord[1] - 1;
  380.             }
  381.         }
  382.  
  383.         notInRange == outOfRange(coordGo.xAxisCoordinate, coordGo.yAxisCoordinate);
  384.  
  385.         if(notInRange == false)
  386.             notInRange = compShotVal(coordGo.xAxisCoordinate, coordGo.yAxisCoordinate);
  387.         }
  388.  
  389.         return coordGo;
  390.     }
  391.     else return notSmartMove;
  392.  
  393. }
  394.  
  395. bool checkForHitComp(int coordX, int coordY)
  396. {
  397.     bool isHit;
  398.     char letter;
  399.  
  400.     if(usersGameBoard[coordY][coordX] == 'B')
  401.     {
  402.         isHit = true;
  403.         letter = 'H';
  404.     }
  405.     else if(usersGameBoard[coordY][coordX] == 'O')
  406.     {
  407.         isHit = false;
  408.         letter = 'M';
  409.     }
  410.  
  411.     updateUsersBoard(coordX, coordY, letter);
  412.  
  413.     return isHit;
  414. }
  415.  
  416. void playerEnter()
  417. {
  418.     int positionX, positionY;
  419.     bool acceptable = false, hit;
  420.  
  421.     do{
  422.         cout << "\nWhere would you like to hit(x): ";
  423.         cin >> positionX;
  424.  
  425.         cout << "\nWhere would you like to hit(y): ";
  426.         cin >> positionY;
  427.  
  428.         acceptable = outOfRange(positionX, positionY);
  429.  
  430.         if(acceptable == false)
  431.             acceptable = userShotVal(positionX, positionY);
  432.     }while(acceptable == true);
  433.  
  434.     hit = checkForHitUser(positionX, positionY);
  435.  
  436.     if(hit == true)
  437.         playerHits++;
  438.  
  439. }
  440.  
  441. void computerGo()
  442. {
  443.     int positionX, positionY;
  444.     bool notUsable, successful;
  445.     coordinates smartMove;
  446.     do
  447.     {
  448.         positionX = rand() % cols;
  449.         positionY = rand() % rows;
  450.  
  451.         notUsable = outOfRange(positionX, positionY);
  452.  
  453.         if(notUsable == false)
  454.             notUsable = compShotVal(positionX, positionY);
  455.     }while(notUsable == true);
  456.  
  457.     smartMove = compSmartMove();
  458.  
  459.     if(smartMove.xAxisCoordinate == 10)
  460.         successful = checkForHitComp(positionX, positionY);
  461.     else
  462.         successful = checkForHitComp(smartMove.xAxisCoordinate, smartMove.yAxisCoordinate);
  463.  
  464.     if(successful == true)
  465.         compHits++;
  466.  
  467. }
  468.  
  469. int main()
  470. {
  471.     shotsTaken = 0;
  472.     int wins = 0, loses = 0;
  473.     gamesPlayed = 0;
  474.  
  475.     srand(time(NULL));
  476.  
  477.     cout << "\n\n\nWelcome to Battleships!!";
  478.  
  479.     cout << "\nHow many games would you like to play: ";
  480.     cin >> numOfGames;
  481.  
  482.     do
  483.     {
  484.         playerHits = 0;
  485.         compHits = 0;
  486.  
  487.         cout << "\n\nWins: " << wins << " Loses: " << loses;
  488.         initialise();
  489.  
  490.         displayBoards();
  491.  
  492.         playBoatPlace();
  493.  
  494.         displayBoards();
  495.  
  496.         compBoatPlace();
  497.  
  498.         do
  499.         {
  500.             playerEnter();
  501.  
  502.             if(playerHits == 3)
  503.                 break;
  504.  
  505.             computerGo();
  506.  
  507.             displayBoards();
  508.  
  509.             shotsTaken++;
  510.         }while(shotsTaken == 25 ||  compHits == 3 || playerHits == 3);
  511.  
  512.         if(compHits == 3 && playerHits < 3)
  513.             loses++;
  514.         else if(playerHits == 3 && compHits < 3)
  515.             wins++;
  516.  
  517.         gamesPlayed++;
  518.     }while(gamesPlayed != numOfGames);
  519. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement