Advertisement
Guest User

Controlled Projectile Release Simulation Software V3

a guest
Jul 23rd, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 68.29 KB | None | 0 0
  1. /***********************************************************/
  2. /* Controlled Projectile Release Simulation Software
  3.  * Codename: Battleships
  4.  * Version 3.0
  5.  *
  6.  * Dev Log:
  7.  * Version | Author | Comments
  8.  * 3.0     | BMS    | user vs ai
  9.  * 3.1     | BMS    | following up fix
  10.  *
  11.  * 1 current issues:
  12.  * 2: Mid-Game Crash
  13.  * Game runs fine on occasion and not on others, unsure as to
  14.  * reason. Keeping game log for crashes to attempt to recognise
  15.  * any sort of pattern but looks entirely random.
  16.  */
  17. /***********************************************************/
  18.  
  19. #include <iostream>
  20. #include <time.h>
  21.  
  22. using namespace std;
  23.  
  24. /**Class Definitions**/
  25.  
  26. /*Coordinate - used to combine two ints in coordinate style
  27.  *x and y           are the coordinate values
  28.  *setVal            was used to set the x and y values
  29.  *shipNumber        used to mark a ship value at the coordinate
  30.  */
  31.  
  32. class Coordinate
  33. {
  34. public:
  35.     int x, y;
  36.     void setVal(int, int);
  37.     int shipNumber;
  38. };
  39.  
  40. /*Ship - used to define multiple parameters to do with ship objects
  41.  *direction         is the compass direction used from start to map the ship
  42.  *coorX and coorY   are the arrays on coordinate values for the ship
  43.  *hit array         used to mark the parts of the ship that have sustained damage
  44.  *startC and endC   coordinates at either end of the ship
  45.  *length            marks the length of the ship in no of spaces
  46.  *sunkMarker        marks whether or not the ship is still afloat
  47.  *placeShip         function sets most ship values based on user input
  48.  *hitMark           function sets ship damage and sunkMarker if ship is at critical
  49.  */
  50.  
  51. class Ship
  52. {
  53.     char direction;
  54. public:
  55.     int coorX[5];
  56.     int coorY[5];
  57.     int hit[5];
  58.     Coordinate startC, endC;
  59.     int length;
  60.     int sunkMarker;
  61.     int placeShip(int, int, char);
  62.     int hitMark(Coordinate&);
  63. };
  64.  
  65. /**Prototype Functions**/
  66. void setShip (Ship&, int **, int);
  67. Coordinate takeShot (int **, int **, Coordinate&);
  68. void enemySetShip (Ship&, int **, int);
  69. Coordinate enemyTakeShot (int **, int **, Coordinate&, int&, Coordinate *);
  70. void boardSweep (Ship&, int **);
  71. Coordinate modalComparisonFunction (Coordinate *);
  72.  
  73. /**Functions**/
  74.  
  75. /* main - main code
  76.  * inputs - NONE
  77.  * output - int
  78.  */
  79.  
  80. int main(void)
  81. {
  82.     int rowCount = 10;                                                      //Setup variables
  83.     int colCount = 10;
  84.     int winMarker = 0;
  85.     int sunkResponse = 0;
  86.     int sunkCounter = 0;
  87.     int enemySunkCounter = 0;
  88.     Coordinate previousShot;
  89.     Coordinate currentShot;
  90.     Coordinate enemyPreviousShot;
  91.     Coordinate enemyCurrentShot;
  92.     int enemyShotOuput = 0;
  93.     Coordinate enemyShotHistory[5];
  94.     int turnCounter = 0;
  95.  
  96.     previousShot.x = 0;
  97.     previousShot.y = 0;
  98.     previousShot.shipNumber = 0;
  99.  
  100.     enemyPreviousShot.x = 0;
  101.     enemyPreviousShot.y = 0;
  102.     enemyPreviousShot.shipNumber = 0;
  103.  
  104.     for (int i = 0; i < 5; i++)
  105.     {
  106.         enemyShotHistory[i].x = 0;
  107.         enemyShotHistory[i].y = 0;
  108.         enemyShotHistory[i].shipNumber = 0;
  109.     }
  110.  
  111.     int** shipMatrix = new int*[rowCount];                                  //Create matrix for ship map
  112.     for(int i = 0; i < rowCount; ++i)
  113.     {
  114.         shipMatrix[i] = new int[colCount];
  115.         for(int j = 0; j < colCount; j++)
  116.         {
  117.             shipMatrix[i][j] = 0;
  118.         }
  119.     }
  120.  
  121.     int** hitMatrix = new int*[rowCount];                                   //Create matrix for hit map
  122.     for(int i = 0; i < rowCount; ++i)
  123.     {
  124.         hitMatrix[i] = new int[colCount];
  125.         for(int j = 0; j < colCount; j++)
  126.         {
  127.             hitMatrix[i][j] = 0;
  128.         }
  129.     }
  130.  
  131.     int** enemyMatrix = new int*[rowCount];                                 //Create matrix for enemy ship map
  132.     for(int i = 0; i < rowCount; ++i)
  133.     {
  134.         enemyMatrix[i] = new int[colCount];
  135.         for(int j = 0; j < colCount; j++)
  136.         {
  137.             enemyMatrix[i][j] = 0;
  138.         }
  139.     }
  140.  
  141.     int** enemyHitMatrix = new int*[rowCount];                              //Create matrix for enemy hit map
  142.     for(int i = 0; i < rowCount; ++i)
  143.     {
  144.         enemyHitMatrix[i] = new int[colCount];
  145.         for(int j = 0; j < colCount; j++)
  146.         {
  147.             enemyHitMatrix[i][j] = 0;
  148.         }
  149.     }
  150.  
  151.     cout << "BATTLESHIPS!" << endl;
  152.  
  153.     cout << endl;                                                           //Start of map printing function
  154.     cout << "  || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
  155.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  156.     for (int i = 0; i < 10; i++)
  157.     {
  158.         cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  159.         cout << (i + 1);
  160.         if (i != 9)
  161.         {
  162.             cout << " ";
  163.         }
  164.         cout << "|";
  165.         for (int j = 0; j < 10; j++)
  166.         {
  167.             int holder = hitMatrix[i][j];
  168.             cout << "| ";
  169.             if (holder == 0)
  170.             {
  171.                 cout << "-";
  172.             }
  173.             else if (holder == 1)
  174.             {
  175.                 cout << "/";
  176.             }
  177.             else if (holder == 2)
  178.             {
  179.                 cout << "H";
  180.             }
  181.             cout << " ";
  182.         }
  183.         cout << "|" << endl;
  184.     }
  185.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  186.     cout << endl;                                                           //End of map printing function
  187.  
  188.  
  189.     cout << "The position of your ships is set by:" << endl;                //Give user instruction
  190.     cout << "- An X coordinate (1-10)" << endl;
  191.     cout << "- Then a Y coordinate (1-10)" << endl;
  192.     cout << "- Then the direction from the coordinate (N/E/S/W)" << endl;
  193.     cout << "- Enter X on direction to start placement again" << endl;
  194.  
  195.     Ship Carrier;                                                           //Create carrier, ship class
  196.     Carrier.length = 5;
  197.     Carrier.sunkMarker = 0;
  198.  
  199.     cout << endl;
  200.     cout << "The first ship to be placed is the aircraft carrier" << endl;
  201.  
  202.     setShip(Carrier, shipMatrix, 0);                                        //Place carrier
  203.  
  204.     Ship Battleship;                                                        //Create battleship, ship class
  205.     Battleship.length = 4;
  206.     Battleship.sunkMarker = 0;
  207.  
  208.     cout << endl;
  209.     cout << "The next ship to be placed is the battleship" << endl;
  210.  
  211.     setShip(Battleship, shipMatrix, 0);                                     //Place battleship
  212.  
  213.     Ship Destroyer;                                                         //Create destroyer, ship class
  214.     Destroyer.length = 3;
  215.     Destroyer.sunkMarker = 0;
  216.  
  217.     cout << endl;
  218.     cout << "The next ship to be placed is the destroyer" << endl;
  219.  
  220.     setShip(Destroyer, shipMatrix, 0);                                      //Place destroyer
  221.  
  222.     Ship Submarine;                                                         //Create submarine, ship class
  223.     Submarine.length = 3;
  224.     Submarine.sunkMarker = 0;
  225.  
  226.     cout << endl;
  227.     cout << "The next ship to be placed is the submarine" << endl;
  228.  
  229.     setShip(Submarine, shipMatrix, 1);                                      //Place submarine
  230.  
  231.     Ship Patrol;                                                            //Create patrol boat, ship class
  232.     Patrol.length = 2;
  233.     Patrol.sunkMarker = 0;
  234.  
  235.     cout << endl;
  236.     cout << "The final ship to be placed is the patrol boat" << endl;
  237.  
  238.     setShip(Patrol, shipMatrix, 0);                                         //Place patrol boat
  239.  
  240.     cout << endl;
  241.  
  242.     srand(time(NULL));                                                      //Enemy setup
  243.  
  244.     Ship enemyCarrier;                                                      //Create carrier, ship class
  245.     enemyCarrier.length = 5;
  246.     enemyCarrier.sunkMarker = 0;
  247.     enemySetShip(enemyCarrier, enemyMatrix, 0);                             //Place carrier
  248.  
  249.     Ship enemyBattleship;                                                   //Create battleship, ship class
  250.     enemyBattleship.length = 4;
  251.     enemyBattleship.sunkMarker = 0;
  252.     enemySetShip(enemyBattleship, enemyMatrix, 0);                          //Place battleship
  253.  
  254.     Ship enemyDestroyer;                                                    //Create destroyer, ship class
  255.     enemyDestroyer.length = 3;
  256.     enemyDestroyer.sunkMarker = 0;
  257.     enemySetShip(enemyDestroyer, enemyMatrix, 0);                           //Place destroyer
  258.  
  259.     Ship enemySubmarine;                                                    //Create submarine, ship class
  260.     enemySubmarine.length = 3;
  261.     enemySubmarine.sunkMarker = 0;
  262.     enemySetShip(enemySubmarine, enemyMatrix, 1);                           //Place submarine
  263.  
  264.     Ship enemyPatrol;                                                       //Create patrol boat, ship class
  265.     enemyPatrol.length = 2;
  266.     enemyPatrol.sunkMarker = 0;
  267.     enemySetShip(enemyPatrol, enemyMatrix, 0);                              //Place patrol boat
  268.  
  269.     for (int i = 0; i < 5; i++)                                             //Set all boats hit markers off
  270.     {
  271.         Carrier.hit[i] = 0;
  272.         Battleship.hit[i] = 0;
  273.         Destroyer.hit[i] = 0;
  274.         Submarine.hit[i] = 0;
  275.         Patrol.hit[i] = 0;
  276.         enemyCarrier.hit[i] = 0;
  277.         enemyBattleship.hit[i] = 0;
  278.         enemyDestroyer.hit[i] = 0;
  279.         enemySubmarine.hit[i] = 0;
  280.         enemyPatrol.hit[i] = 0;
  281.     }
  282.  
  283.     cout << "The game will now begin!" << endl;
  284.  
  285.     for (int i = 0; i <= 32767; i++)                                        //Manually created delay (delay and sleep wouldnt work XD)
  286.     {
  287.         for (int j = 0; j <= 32767; j++);
  288.     }
  289.  
  290.     cout << endl;                                                           //Start of map printing function
  291.     cout << "  || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
  292.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  293.     for (int i = 0; i < 10; i++)
  294.     {
  295.         cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  296.         cout << (i + 1);
  297.         if (i != 9)
  298.         {
  299.             cout << " ";
  300.         }
  301.         cout << "|";
  302.         for (int j = 0; j < 10; j++)
  303.         {
  304.             int holder = hitMatrix[i][j];
  305.             cout << "| ";
  306.             if (holder == 0)
  307.             {
  308.                 cout << "-";
  309.             }
  310.             else if (holder == 1)
  311.             {
  312.                 cout << "/";
  313.             }
  314.             else if (holder == 2)
  315.             {
  316.                 cout << "H";
  317.             }
  318.             cout << " ";
  319.         }
  320.         cout << "|" << endl;
  321.     }
  322.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  323.     cout << endl;                                                           //End of map printing function
  324.  
  325.     do                                                                      //Start of play loop
  326.     {
  327.         turnCounter++;
  328.  
  329.         cout << "Turn " << turnCounter << endl << endl;
  330.  
  331.         currentShot = takeShot(enemyMatrix, hitMatrix, previousShot);       //Send to takeShot function
  332.  
  333.         switch (currentShot.shipNumber)                                     //Check returned ship number on space that was fired at
  334.         {
  335.         case 0:
  336.             break;
  337.         case 2:
  338.             sunkResponse = enemyPatrol.hitMark(currentShot);                //Patrol boat hit and sunk checker
  339.             if (sunkResponse == 1)
  340.             {
  341.                 cout << "You sunk the enemy Patrol Boat!" << endl;
  342.                 cout << endl;
  343.                 boardSweep(enemyPatrol, hitMatrix);
  344.                 sunkCounter++;
  345.             }
  346.             break;
  347.         case 3:
  348.             sunkResponse = enemyDestroyer.hitMark(currentShot);             //Destroyer hit and sunk checker
  349.             if (sunkResponse == 1)
  350.             {
  351.                 cout << "You sunk the enemy Destroyer!" << endl;
  352.                 cout << endl;
  353.                 boardSweep(enemyDestroyer, hitMatrix);
  354.                 sunkCounter++;
  355.             }
  356.             break;
  357.         case 4:
  358.             sunkResponse = enemyBattleship.hitMark(currentShot);            //Battleship hit and sunk checker
  359.             if (sunkResponse == 1)
  360.             {
  361.                 cout << "You sunk the enemy Battleship!" << endl;
  362.                 cout << endl;
  363.                 boardSweep(enemyBattleship, hitMatrix);
  364.                 sunkCounter++;
  365.             }
  366.             break;
  367.         case 5:
  368.             sunkResponse = enemyCarrier.hitMark(currentShot);               //Carrier hit and sunk checker
  369.             if (sunkResponse == 1)
  370.             {
  371.                 cout << "You sunk the enemy Aircraft Carrier!" << endl;
  372.                 cout << endl;
  373.                 boardSweep(enemyCarrier, hitMatrix);
  374.                 sunkCounter++;
  375.             }
  376.             break;
  377.         case 6:
  378.             sunkResponse = enemySubmarine.hitMark(currentShot);             //Submarine hit and sunk checker
  379.             if (sunkResponse == 1)
  380.             {
  381.                 cout << "You sunk the enemy Submarine!" << endl;
  382.                 cout << endl;
  383.                 boardSweep(enemySubmarine, hitMatrix);
  384.                 sunkCounter++;
  385.             }
  386.             break;
  387.         default:
  388.             break;
  389.         }
  390.  
  391.         previousShot = currentShot;
  392.  
  393.         if (sunkCounter == 5)                                               //If all boats hit break play loop
  394.         {
  395.             winMarker = 1;
  396.         }
  397.  
  398.         cout << endl;
  399.  
  400.         enemyCurrentShot = enemyTakeShot(shipMatrix, enemyHitMatrix, enemyPreviousShot, enemyShotOuput, enemyShotHistory);
  401.         /*Send to enemyTakeShot function*/
  402.  
  403.         switch (enemyCurrentShot.shipNumber)                                //Check returned ship number on space that was fired at
  404.         {
  405.         case 0:
  406.             break;
  407.         case 2:
  408.             sunkResponse = Patrol.hitMark(enemyCurrentShot);                //Patrol boat hit and sunk checker
  409.             if (sunkResponse == 1)
  410.             {
  411.                 cout << "The enemy sunk your Patrol Boat!" << endl;
  412.                 cout << endl;
  413.                 boardSweep(Patrol, enemyHitMatrix);
  414.                 enemySunkCounter++;
  415.                 enemyShotOuput = 0;
  416.             }
  417.             break;
  418.         case 3:
  419.             sunkResponse = Destroyer.hitMark(enemyCurrentShot);             //Destroyer hit and sunk checker
  420.             if (sunkResponse == 1)
  421.             {
  422.                 cout << "The enemy sunk your Destroyer!" << endl;
  423.                 cout << endl;
  424.                 boardSweep(Destroyer, enemyHitMatrix);
  425.                 enemySunkCounter++;
  426.                 enemyShotOuput = 0;
  427.             }
  428.             break;
  429.         case 4:
  430.             sunkResponse = Battleship.hitMark(enemyCurrentShot);            //Battleship hit and sunk checker
  431.             if (sunkResponse == 1)
  432.             {
  433.                 cout << "The enemy sunk your Battleship!" << endl;
  434.                 cout << endl;
  435.                 boardSweep(Battleship, enemyHitMatrix);
  436.                 enemySunkCounter++;
  437.                 enemyShotOuput = 0;
  438.             }
  439.             break;
  440.         case 5:
  441.             sunkResponse = Carrier.hitMark(enemyCurrentShot);               //Carrier hit and sunk checker
  442.             if (sunkResponse == 1)
  443.             {
  444.                 cout << "The enemy sunk your Aircraft Carrier!" << endl;
  445.                 cout << endl;
  446.                 boardSweep(Carrier, enemyHitMatrix);
  447.                 enemySunkCounter++;
  448.                 enemyShotOuput = 0;
  449.             }
  450.             break;
  451.         case 6:
  452.             sunkResponse = Submarine.hitMark(enemyCurrentShot);             //Submarine hit and sunk checker
  453.             if (sunkResponse == 1)
  454.             {
  455.                 cout << "The enemy sunk your Submarine!" << endl;
  456.                 cout << endl;
  457.                 boardSweep(Submarine, enemyHitMatrix);
  458.                 enemySunkCounter++;
  459.                 enemyShotOuput = 0;
  460.             }
  461.             break;
  462.         default:
  463.             break;
  464.         }
  465.  
  466.         enemyPreviousShot = enemyCurrentShot;
  467.  
  468.         if ((enemySunkCounter == 5) && (winMarker == 0))                    //If all boats hit break play loop
  469.         {
  470.             winMarker = 2;
  471.         }
  472.         else if ((enemySunkCounter == 5) && (winMarker == 1))
  473.         {
  474.             winMarker = 3;
  475.         }
  476.  
  477.         cout << endl;
  478.     }
  479.     while (winMarker == 0);
  480.  
  481.     cout << endl;                                                           //Start of map printing function
  482.     cout << "  || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
  483.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  484.     for (int i = 0; i < 10; i++)
  485.     {
  486.         cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  487.         cout << (i + 1);
  488.         if (i != 9)
  489.         {
  490.             cout << " ";
  491.         }
  492.         cout << "|";
  493.         for (int j = 0; j < 10; j++)
  494.         {
  495.             int holder = hitMatrix[i][j];
  496.             cout << "| ";
  497.             if (holder == 0)
  498.             {
  499.                 cout << "-";
  500.             }
  501.             else if (holder == 1)
  502.             {
  503.                 cout << "/";
  504.             }
  505.             else if (holder == 2)
  506.             {
  507.                 cout << "H";
  508.             }
  509.             cout << " ";
  510.         }
  511.         cout << "|" << endl;
  512.     }
  513.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  514.     cout << endl;                                                           //End of map printing function
  515.  
  516.     switch (winMarker)
  517.     {
  518.     case 1:
  519.         cout << "YOU WIN! You have sunk all enemy ships on turn " << turnCounter << "!" << endl;
  520.         break;                                                              //Victory message
  521.     case 2:
  522.         cout << "YOU LOSE! The enemy has sunk all your ships on turn " << turnCounter << "!" << endl;
  523.         break;                                                              //Defeat message
  524.     case 3:
  525.         cout << "DRAW! You sunk each others ships on the same turn! (Turn " << turnCounter << ")" << endl;
  526.         break;                                                              //Draw message
  527.     default:
  528.         cout << "ERROR - Winner could not be determined" << endl;           //Default message
  529.         break;
  530.     }
  531. }
  532.  
  533. /* setShip - setup function setting ships position from user input
  534.  * inputs -
  535.  * placement:       type - Ship Class       - creates a ship class holder for current passed ship
  536.  * shipMatrix:      type - int** (2d array) - creates a matrix holder for current ship map
  537.  * multiMark:       type - int              - value to check if the ship is destroyer or submarine
  538.  * output - NONE
  539.  */
  540.  
  541. void setShip(Ship &placement, int** shipMatrix, int multiMark)
  542. {
  543.     int markerOne = 0;                                                      //Setup variables
  544.     int markerTwo = 0;
  545.     int markerThree = 0;
  546.     int markerFour = 0;
  547.     int startX = 0;
  548.     int startY = 0;
  549.     char direction = ' ';
  550.     int errorNum = 0;
  551.     int placeX = 0;
  552.     int placeY = 0;
  553.  
  554.     cout << "The ship is " << placement.length << " spaces long" << endl;   //Tell user ship length
  555.     cout << endl;
  556.  
  557.     do
  558.     {
  559.         do
  560.         {
  561.             cout << "Please enter the X position for your ship" << endl;    //Get ship X coordinate
  562.             cin >> startX;
  563.             cin.clear();
  564.             cin.ignore();
  565.  
  566.  
  567.             if (0 < startX && startX <= 10)                                 //Range check X
  568.             {
  569.                 markerOne = 1;
  570.             }
  571.             else
  572.             {
  573.                 cout << "X must be a number between 1-10" << endl;
  574.             }
  575.         }
  576.         while (markerOne == 0);
  577.  
  578.         markerOne = 0;
  579.  
  580.         do
  581.         {
  582.             cout << "Please enter the Y position for your ship" << endl;    //Get ship Y coordinate
  583.             cin >> startY;
  584.             cin.clear();
  585.             cin.ignore();
  586.  
  587.             if (0 < startY && startY <= 10)                                 //Range check Y
  588.             {
  589.                 markerOne = 1;
  590.             }
  591.             else
  592.             {
  593.                 cout << "Y must be a number between 1-10" << endl;
  594.             }
  595.         }
  596.         while (markerOne == 0);
  597.  
  598.         markerOne = 0;
  599.  
  600.         do
  601.         {
  602.             cout << "Please enter the direction or X to reset" << endl;     //Get compass direction of ship from start
  603.             cin >> direction;
  604.             cin.clear();
  605.             cin.ignore();
  606.             cout << endl;
  607.  
  608.             switch (direction)                                              //Check input value for direction or reset key
  609.             {
  610.             case 'N':
  611.                 markerOne = 1;
  612.                 markerTwo = 1;
  613.                 break;
  614.             case 'E':
  615.                 markerOne = 1;
  616.                 markerTwo = 1;
  617.                 break;
  618.             case 'S':
  619.                 markerOne = 1;
  620.                 markerTwo = 1;
  621.                 break;
  622.             case 'W':
  623.                 markerOne = 1;
  624.                 markerTwo = 1;
  625.                 break;
  626.             case 'n':
  627.                 direction = 'N';
  628.                 markerOne = 1;
  629.                 markerTwo = 1;
  630.                 break;
  631.             case 'e':
  632.                 direction = 'E';
  633.                 markerOne = 1;
  634.                 markerTwo = 1;
  635.                 break;
  636.             case 's':
  637.                 direction = 'S';
  638.                 markerOne = 1;
  639.                 markerTwo = 1;
  640.                 break;
  641.             case 'w':
  642.                 direction = 'W';
  643.                 markerOne = 1;
  644.                 markerTwo = 1;
  645.                 break;
  646.             case 'X':
  647.                 markerOne = 1;
  648.                 cout << "Resetting entered values" << endl;
  649.                 break;
  650.             default:
  651.                 cout << "Please enter N/E/S/W or X to reset" << endl;
  652.                 break;
  653.             }
  654.         }
  655.         while (markerOne == 0);
  656.  
  657.         markerOne = 0;                                                      //Reset placement values
  658.         placement.startC.x = 0;
  659.         placement.startC.y = 0;
  660.         errorNum = placement.placeShip(startX, startY, direction);          //Send to ship placement function
  661.  
  662.         if (errorNum == 1)                                                  //Error - 1 edge of ship goes out of x range
  663.         {
  664.             cout << "The ship will not fit on the X-axis with these values" << endl;
  665.             markerTwo = 0;
  666.             errorNum = 0;
  667.         }
  668.         else if (errorNum == 2)                                             //Error - 1 edge of ship goes out of y range
  669.         {
  670.             cout << "The ship will not fit on the Y-axis with these values" << endl;
  671.             markerTwo = 0;
  672.             errorNum = 0;
  673.         }
  674.         else
  675.         {
  676.             if (multiMark == 1)
  677.             {
  678.                 placement.length = 6;
  679.             }
  680.  
  681.             if (placement.startC.x == placement.endC.x)                     //Equivalent of direction checker (N)
  682.             {
  683.                 if(placement.startC.y > placement.endC.y)
  684.                 {
  685.                     for (int i = placement.startC.y; i >= placement.endC.y; i--)
  686.                     {
  687.                         //For all squares check before placement
  688.                         placeX = placement.startC.x - 1;
  689.                         placeY = i - 1;
  690.  
  691.                         /*Checks to see if any of the places will collide or touch another ship*/
  692.                         for (int j = -1; j < 2; j++)
  693.                         {
  694.                             for (int k = -1; k < 2; k++)
  695.                             {
  696.                                 if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
  697.                                 {
  698.                                     if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
  699.                                     {
  700.                                         markerTwo = 0;
  701.                                         cout << "At X: " << (placeX + k) << " Y: " << (placeY + j) << " Against Mark: " << shipMatrix[placeY + j][placeX + k] << endl;
  702.                                         cout << "This placement will collide with another ship!" << endl;
  703.                                         markerFour = 1;
  704.                                     }
  705.                                 }
  706.                             }
  707.                         }
  708.                     }
  709.  
  710.                     placeX = 0;
  711.                     placeY = 0;
  712.  
  713.                     if (markerFour == 0)                                    //If there are no collision spaces
  714.                     {
  715.                         /*Place the ship onto the ship board*/
  716.                         for (int i = placement.startC.y; i >= placement.endC.y; i--)
  717.                         {
  718.                             placeX = placement.startC.x - 1;
  719.                             placeY = i - 1;
  720.  
  721.                             placement.coorX[markerThree] = placeX;
  722.                             placement.coorY[markerThree] = placeY;
  723.                             placement.hit[markerThree] = 0;
  724.                             markerThree++;
  725.  
  726.                             shipMatrix[placeY][placeX] = placement.length;
  727.                         }
  728.                     }
  729.                 }
  730.                 else if(placement.startC.y < placement.endC.y)              //Same as previous section (S)
  731.                 {
  732.                     for (int i = placement.startC.y; i <= placement.endC.y; i++)
  733.                     {
  734.                         placeX = placement.startC.x - 1;
  735.                         placeY = i - 1;
  736.  
  737.                         for (int j = -1; j < 2; j++)
  738.                         {
  739.                             for (int k = -1; k < 2; k++)
  740.                             {
  741.                                 if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
  742.                                 {
  743.                                     if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
  744.                                     {
  745.                                         markerTwo = 0;
  746.                                         cout << "At X: " << (placeX + k) << " Y: " << (placeY + j) << " Against Mark: " << shipMatrix[placeY + j][placeX + k] << endl;
  747.                                         cout << "This placement will collide with another ship!" << endl;
  748.                                         markerFour = 1;
  749.                                     }
  750.                                 }
  751.                             }
  752.                         }
  753.                     }
  754.  
  755.                     placeX = 0;
  756.                     placeY = 0;
  757.  
  758.                     if (markerFour == 0)
  759.                     {
  760.                         for (int i = placement.startC.y; i <= placement.endC.y; i++)
  761.                         {
  762.                             placeX = placement.startC.x - 1;
  763.                             placeY = i - 1;
  764.  
  765.                             placement.coorX[markerThree] = placeX;
  766.                             placement.coorY[markerThree] = placeY;
  767.                             placement.hit[markerThree] = 0;
  768.                             markerThree++;
  769.  
  770.                             shipMatrix[placeY][placeX] = placement.length;
  771.                         }
  772.                     }
  773.                 }
  774.             }
  775.             else if (placement.startC.y == placement.endC.y)                //Same as previous section (W)
  776.             {
  777.                 if(placement.startC.x > placement.endC.x)
  778.                 {
  779.                     for (int i = placement.startC.x; i >= placement.endC.x; i--)
  780.                     {
  781.                         placeX = i - 1;
  782.                         placeY = placement.startC.y - 1;
  783.  
  784.                         for (int j = -1; j < 2; j++)
  785.                         {
  786.                             for (int k = -1; k < 2; k++)
  787.                             {
  788.                                 if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
  789.                                 {
  790.                                     if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
  791.                                     {
  792.                                         markerTwo = 0;
  793.                                         cout << "At X: " << (placeX + k) << " Y: " << (placeY + j) << " Against Mark: " << shipMatrix[placeY + j][placeX + k] << endl;
  794.                                         cout << "This placement will collide with another ship!" << endl;
  795.                                         markerFour = 1;
  796.                                     }
  797.                                 }
  798.                             }
  799.                         }
  800.                     }
  801.  
  802.                     placeX = 0;
  803.                     placeY = 0;
  804.  
  805.                     if (markerFour == 0)
  806.                     {
  807.                         for (int i = placement.startC.x; i >= placement.endC.x; i--)
  808.                         {
  809.                             placeX = i - 1;
  810.                             placeY = placement.startC.y - 1;
  811.  
  812.                             placement.coorX[markerThree] = placeX;
  813.                             placement.coorY[markerThree] = placeY;
  814.                             placement.hit[markerThree] = 0;
  815.                             markerThree++;
  816.  
  817.                             shipMatrix[placeY][placeX] = placement.length;
  818.                         }
  819.                     }
  820.                 }
  821.                 else if(placement.startC.x < placement.endC.x)               //Same as previous section(E)
  822.                 {
  823.                     for (int i = placement.startC.x; i <= placement.endC.x; i++)
  824.                     {
  825.                         placeX = i - 1;
  826.                         placeY = placement.startC.y - 1;
  827.                         for (int j = -1; j < 2; j++)
  828.                         {
  829.                             for (int k = -1; k < 2; k++)
  830.                             {
  831.                                 if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
  832.                                 {
  833.                                     if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
  834.                                     {
  835.                                         markerTwo = 0;
  836.                                         cout << "At X: " << (placeX + k) << " Y: " << (placeY + j) << " Against Mark: " << shipMatrix[placeY + j][placeX + k] << endl;
  837.                                         cout << "This placement will collide with another ship!" << endl;
  838.                                         markerFour = 1;
  839.                                     }
  840.                                 }
  841.                             }
  842.                         }
  843.                     }
  844.  
  845.                     placeX = 0;
  846.                     placeY = 0;
  847.  
  848.                     if (markerFour == 0)
  849.                     {
  850.                         for (int i = placement.startC.x; i <= placement.endC.x; i++)
  851.                         {
  852.                             placeX = i - 1;
  853.                             placeY = placement.startC.y - 1;
  854.  
  855.                             placement.coorX[markerThree] = placeX;
  856.                             placement.coorY[markerThree] = placeY;
  857.                             placement.hit[markerThree] = 0;
  858.                             markerThree++;
  859.  
  860.                             shipMatrix[placeY][placeX] = placement.length;
  861.                         }
  862.                     }
  863.                 }
  864.             }
  865.         }
  866.  
  867.         if (multiMark == 1)
  868.         {
  869.             placement.length = 3;
  870.         }
  871.         markerFour = 0;
  872.         direction = ' ';
  873.         startX = 0;
  874.         startY = 0;
  875.         placeX = 0;
  876.         placeY = 0;
  877.     }
  878.     while (markerTwo == 0);
  879.  
  880.     markerTwo = 0;
  881.     markerThree = 0;
  882.  
  883.     cout << endl;                                                               //Start of map printing function for ship map
  884.     cout << "  || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
  885.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  886.     for (int i = 0; i < 10; i++)
  887.     {
  888.         cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  889.         cout << (i + 1);
  890.         if (i != 9)
  891.         {
  892.             cout << " ";
  893.         }
  894.         cout << "|";
  895.         for (int j = 0; j < 10; j++)
  896.         {
  897.             int holder = shipMatrix[i][j];
  898.             cout << "| ";
  899.             if (holder == 0)
  900.             {
  901.                 cout << "-";
  902.             }
  903.             else
  904.             {
  905.                 cout << holder;
  906.             }
  907.             cout << " ";
  908.         }
  909.         cout << "|" << endl;
  910.     }
  911.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  912.     cout << endl;                                                               //End of map printing function
  913. }
  914.  
  915.  
  916. /* enemySetShip - setup function setting ships position from random "ai" input
  917.  * inputs -
  918.  * placement:       type - Ship Class       - creates a ship class holder for current passed ship
  919.  * shipMatrix:      type - int** (2d array) - creates a matrix holder for current ship map
  920.  * multiMark:       type - int              - value to check if the ship is destroyer or submarine
  921.  * output - NONE
  922.  */
  923.  
  924. void enemySetShip(Ship &placement, int** shipMatrix, int multiMark)
  925. {
  926.     int markerOne = 0;                                                      //Setup variables
  927.     int markerTwo = 0;
  928.     int markerThree = 0;
  929.     int markerFour = 0;
  930.     int startX = 0;
  931.     int startY = 0;
  932.     int dirNum = 0;
  933.     char direction = ' ';
  934.     int errorNum = 0;
  935.     int placeX = 0;
  936.     int placeY = 0;
  937.  
  938.     do
  939.     {
  940.         do
  941.         {
  942.             startX = (rand() % 10) + 1;                                     //Get ship X coordinate
  943.  
  944.             if (0 < startX && startX <= 10)                                 //Range check X
  945.             {
  946.                 markerOne = 1;
  947.             }
  948.         }
  949.         while (markerOne == 0);
  950.  
  951.         markerOne = 0;
  952.  
  953.         do
  954.         {
  955.             startY = (rand() % 10) + 1;                                     //Get ship Y coordinate
  956.  
  957.             if (0 < startY && startY <= 10)                                 //Range check Y
  958.             {
  959.                 markerOne = 1;
  960.             }
  961.         }
  962.         while (markerOne == 0);
  963.  
  964.         markerOne = 0;
  965.  
  966.         do
  967.         {
  968.             dirNum = (rand() % 4);                                          //Get ship X coordinate
  969.  
  970.             switch (dirNum)                                                 //Check input value for direction or reset key
  971.             {
  972.             case 0:
  973.                 direction = 'N';
  974.                 markerOne = 1;
  975.                 markerTwo = 1;
  976.                 break;
  977.             case 1:
  978.                 direction = 'E';
  979.                 markerOne = 1;
  980.                 markerTwo = 1;
  981.                 break;
  982.             case 2:
  983.                 direction = 'S';
  984.                 markerOne = 1;
  985.                 markerTwo = 1;
  986.                 break;
  987.             case 3:
  988.                 direction = 'W';
  989.                 markerOne = 1;
  990.                 markerTwo = 1;
  991.                 break;
  992.             default:
  993.                 break;
  994.             }
  995.         }
  996.         while (markerOne == 0);
  997.  
  998.         markerOne = 0;                                                      //Reset placement values
  999.         placement.startC.x = 0;
  1000.         placement.startC.y = 0;
  1001.         errorNum = placement.placeShip(startX, startY, direction);          //Send to ship placement function
  1002.  
  1003.         if (errorNum == 1)                                                  //Error - 1 edge of ship goes out of x range
  1004.         {
  1005.             markerTwo = 0;
  1006.             errorNum = 0;
  1007.         }
  1008.         else if (errorNum == 2)                                             //Error - 1 edge of ship goes out of y range
  1009.         {
  1010.             markerTwo = 0;
  1011.             errorNum = 0;
  1012.         }
  1013.         else
  1014.         {
  1015.             if (multiMark == 1)
  1016.             {
  1017.                 placement.length = 6;                                       //To separate the destroyer and the submarine
  1018.             }
  1019.  
  1020.             if (placement.startC.x == placement.endC.x)                     //Equivalent of direction checker (N)
  1021.             {
  1022.                 if(placement.startC.y > placement.endC.y)
  1023.                 {
  1024.                     for (int i = placement.startC.y; i >= placement.endC.y; i--)
  1025.                     {
  1026.                         //For all squares check before placement
  1027.                         placeX = placement.startC.x - 1;
  1028.                         placeY = i - 1;
  1029.  
  1030.                         /*Checks to see if any of the places will collide or touch another ship*/
  1031.                         for (int j = -1; j < 2; j++)
  1032.                         {
  1033.                             for (int k = -1; k < 2; k++)
  1034.                             {
  1035.                                 if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
  1036.                                 {
  1037.                                     if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
  1038.                                     {
  1039.                                         markerTwo = 0;
  1040.                                         markerFour = 1;
  1041.                                     }
  1042.                                 }
  1043.                             }
  1044.                         }
  1045.                     }
  1046.  
  1047.                     placeX = 0;
  1048.                     placeY = 0;
  1049.  
  1050.                     if (markerFour == 0)                                    //If there are no collision spaces
  1051.                     {
  1052.                         /*Place the ship onto the ship board*/
  1053.                         for (int i = placement.startC.y; i >= placement.endC.y; i--)
  1054.                         {
  1055.                             placeX = placement.startC.x - 1;
  1056.                             placeY = i - 1;
  1057.  
  1058.                             placement.coorX[markerThree] = placeX;
  1059.                             placement.coorY[markerThree] = placeY;
  1060.                             placement.hit[markerThree] = 0;
  1061.                             markerThree++;
  1062.  
  1063.                             shipMatrix[placeY][placeX] = placement.length;
  1064.                         }
  1065.                     }
  1066.                 }
  1067.                 else if(placement.startC.y < placement.endC.y)              //Same as previous section (S)
  1068.                 {
  1069.                     for (int i = placement.startC.y; i <= placement.endC.y; i++)
  1070.                     {
  1071.                         placeX = placement.startC.x - 1;
  1072.                         placeY = i - 1;
  1073.  
  1074.                         for (int j = -1; j < 2; j++)
  1075.                         {
  1076.                             for (int k = -1; k < 2; k++)
  1077.                             {
  1078.                                 if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
  1079.                                 {
  1080.                                     if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
  1081.                                     {
  1082.                                         markerTwo = 0;
  1083.                                         markerFour = 1;
  1084.                                     }
  1085.                                 }
  1086.                             }
  1087.                         }
  1088.                     }
  1089.  
  1090.                     placeX = 0;
  1091.                     placeY = 0;
  1092.  
  1093.                     if (markerFour == 0)
  1094.                     {
  1095.                         for (int i = placement.startC.y; i <= placement.endC.y; i++)
  1096.                         {
  1097.                             placeX = placement.startC.x - 1;
  1098.                             placeY = i - 1;
  1099.  
  1100.                             placement.coorX[markerThree] = placeX;
  1101.                             placement.coorY[markerThree] = placeY;
  1102.                             placement.hit[markerThree] = 0;
  1103.                             markerThree++;
  1104.  
  1105.                             shipMatrix[placeY][placeX] = placement.length;
  1106.                         }
  1107.                     }
  1108.                 }
  1109.             }
  1110.             else if (placement.startC.y == placement.endC.y)                //Same as previous section (W)
  1111.             {
  1112.                 if(placement.startC.x > placement.endC.x)
  1113.                 {
  1114.                     for (int i = placement.startC.x; i >= placement.endC.x; i--)
  1115.                     {
  1116.                         placeX = i - 1;
  1117.                         placeY = placement.startC.y - 1;
  1118.  
  1119.                         for (int j = -1; j < 2; j++)
  1120.                         {
  1121.                             for (int k = -1; k < 2; k++)
  1122.                             {
  1123.                                 if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
  1124.                                 {
  1125.                                     if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
  1126.                                     {
  1127.                                         markerTwo = 0;
  1128.                                         markerFour = 1;
  1129.                                     }
  1130.                                 }
  1131.                             }
  1132.                         }
  1133.                     }
  1134.  
  1135.                     placeX = 0;
  1136.                     placeY = 0;
  1137.  
  1138.                     if (markerFour == 0)
  1139.                     {
  1140.                         for (int i = placement.startC.x; i >= placement.endC.x; i--)
  1141.                         {
  1142.                             placeX = i - 1;
  1143.                             placeY = placement.startC.y - 1;
  1144.  
  1145.                             placement.coorX[markerThree] = placeX;
  1146.                             placement.coorY[markerThree] = placeY;
  1147.                             placement.hit[markerThree] = 0;
  1148.                             markerThree++;
  1149.  
  1150.                             shipMatrix[placeY][placeX] = placement.length;
  1151.                         }
  1152.                     }
  1153.                 }
  1154.                 else if(placement.startC.x < placement.endC.x)               //Same as previous section(E)
  1155.                 {
  1156.                     for (int i = placement.startC.x; i <= placement.endC.x; i++)
  1157.                     {
  1158.                         placeX = i - 1;
  1159.                         placeY = placement.startC.y - 1;
  1160.                         for (int j = -1; j < 2; j++)
  1161.                         {
  1162.                             for (int k = -1; k < 2; k++)
  1163.                             {
  1164.                                 if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
  1165.                                 {
  1166.                                     if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
  1167.                                     {
  1168.                                         markerTwo = 0;
  1169.                                         markerFour = 1;
  1170.                                     }
  1171.                                 }
  1172.                             }
  1173.                         }
  1174.                     }
  1175.  
  1176.                     placeX = 0;
  1177.                     placeY = 0;
  1178.  
  1179.                     if (markerFour == 0)
  1180.                     {
  1181.                         for (int i = placement.startC.x; i <= placement.endC.x; i++)
  1182.                         {
  1183.                             placeX = i - 1;
  1184.                             placeY = placement.startC.y - 1;
  1185.  
  1186.                             placement.coorX[markerThree] = placeX;
  1187.                             placement.coorY[markerThree] = placeY;
  1188.                             placement.hit[markerThree] = 0;
  1189.                             markerThree++;
  1190.  
  1191.                             shipMatrix[placeY][placeX] = placement.length;
  1192.                         }
  1193.                     }
  1194.                 }
  1195.             }
  1196.         }
  1197.  
  1198.         if (multiMark == 1)
  1199.         {
  1200.             placement.length = 3;                                       //To separate the destroyer and the submarine
  1201.         }
  1202.  
  1203.         markerFour = 0;
  1204.         direction = ' ';
  1205.         startX = 0;
  1206.         startY = 0;
  1207.         placeX = 0;
  1208.         placeY = 0;
  1209.     }
  1210.     while (markerTwo == 0);
  1211.  
  1212.     markerTwo = 0;
  1213.     markerThree = 0;
  1214. }
  1215.  
  1216. /* takeShot - function to get shot values from user and apply the results
  1217.  * inputs -
  1218.  * shipMatrix:      type - int** (2d array) - creates a matrix holder for current ship map
  1219.  * hitMatrix:       type - int** (2d array) - creates a matrix holder for current hits map
  1220.  * previousShot:    type - Coordinate Class - creates a coordinate class holder for users previous shot
  1221.  * output -
  1222.  * shotAttempt:     type - Coordinate Class - returns a coordinate for the users current shot
  1223.  */
  1224.  
  1225. Coordinate takeShot(int** shipMatrix, int** hitMatrix, Coordinate &previousShot)
  1226. {
  1227.     Coordinate shotAttempt;                                                     //Setup variables
  1228.     int markerOne = 0;
  1229.     int markerTwo = 0;
  1230.     int shotX = 0;
  1231.     int shotY = 0;
  1232.  
  1233.     cout << "Your previous shot was at (" << previousShot.x << "," << previousShot.y << ")" << endl;
  1234.     cout << endl;
  1235.  
  1236.     do
  1237.     {
  1238.         cout << "Your turn!" << endl;
  1239.         cout << "Enter X shot coordinate:" << endl;                             //Get user x coordinate input
  1240.         do
  1241.         {
  1242.             cin >> shotAttempt.x;
  1243.             cin.clear();
  1244.             cin.ignore();
  1245.  
  1246.             if (shotAttempt.x < 1 || shotAttempt.x > 10)                        //Range check x
  1247.             {
  1248.                 cout << "X must be between 1-10" << endl;
  1249.             }
  1250.             else
  1251.             {
  1252.                 markerOne = 1;
  1253.             }
  1254.         }
  1255.         while (markerOne == 0);
  1256.  
  1257.         markerOne = 0;
  1258.         cout << "Enter Y shot coordinate:" << endl;                             //Get user y coordinate input
  1259.  
  1260.         do
  1261.         {
  1262.             cin >> shotAttempt.y;
  1263.             cin.clear();
  1264.             cin.ignore();
  1265.  
  1266.             if (shotAttempt.y < 1 || shotAttempt.y > 10)                        //Range check y
  1267.             {
  1268.                 cout << "Y must be between 1-10" << endl;
  1269.             }
  1270.             else
  1271.             {
  1272.                 markerOne = 1;
  1273.             }
  1274.         }
  1275.         while (markerOne == 0);
  1276.  
  1277.         markerOne = 0;
  1278.         shotX = shotAttempt.x - 1;
  1279.         shotY = shotAttempt.y - 1;
  1280.         if (hitMatrix[shotY][shotX] == 0)                                       //Check to see if there is already a shot a given position
  1281.         {
  1282.             if (shipMatrix[shotY][shotX] > 0)                                   //Check to see if its a hit...
  1283.             {
  1284.                 hitMatrix[shotY][shotX] = 2;
  1285.                 markerTwo = 1;
  1286.                 shotAttempt.shipNumber = shipMatrix[shotY][shotX];
  1287.             }
  1288.             else                                                                //...or a miss
  1289.             {
  1290.                 hitMatrix[shotY][shotX] = 1;
  1291.                 markerTwo = 1;
  1292.                 shotAttempt.shipNumber = 0;
  1293.             }
  1294.         }
  1295.         else                                                                    //If shot already at given position then notify the user
  1296.         {
  1297.             cout << "You have already shot there!" << endl;
  1298.         }
  1299.     }
  1300.     while (markerTwo == 0);
  1301.  
  1302.     markerTwo = 0;
  1303.  
  1304.     cout << endl;                                                               //Start map printing function
  1305.     cout << "  || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
  1306.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  1307.     for (int i = 0; i < 10; i++)
  1308.     {
  1309.         cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  1310.         cout << (i + 1);
  1311.         if (i != 9)
  1312.         {
  1313.             cout << " ";
  1314.         }
  1315.         cout << "|";
  1316.         for (int j = 0; j < 10; j++)
  1317.         {
  1318.             int holder = hitMatrix[i][j];
  1319.             cout << "| ";
  1320.             if (holder == 0)
  1321.             {
  1322.                 cout << "-";
  1323.             }
  1324.             else if (holder == 1)
  1325.             {
  1326.                 cout << "/";
  1327.             }
  1328.             else if (holder == 2)
  1329.             {
  1330.                 cout << "H";
  1331.             }
  1332.             cout << " ";
  1333.         }
  1334.         cout << "|" << endl;
  1335.     }
  1336.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  1337.     cout << endl;                                                               //End map printing function
  1338.  
  1339.     if (shipMatrix[shotY][shotX] > 0)                                           //Tell user if its hit or miss
  1340.     {
  1341.         cout << "HIT!" << endl;
  1342.     }
  1343.     else
  1344.     {
  1345.         cout << "MISS!" << endl;
  1346.     }
  1347.     return shotAttempt;                                                         //Return the coordinate of current shot
  1348. }
  1349.  
  1350. /* takeShot - function to get shot values from ai and apply the results
  1351.  * inputs -
  1352.  * shipMatrix:      type - int** (2d array) - creates a matrix holder for current ship map
  1353.  * hitMatrix:       type - int** (2d array) - creates a matrix holder for current hits map
  1354.  * previousShot:    type - Coordinate Class - creates a coordinate class holder for ais previous shot
  1355.  * enemyShotOutput: type - int &            - marker for output of ai previous shot type
  1356.  * enemyShotHistory:type - Coordinate* array- holds the array of previous 5 ai
  1357.  * output -
  1358.  * shotAttempt:     type - Coordinate Class - returns a coordinate for the users current shot
  1359.  */
  1360.  
  1361. Coordinate enemyTakeShot(int** shipMatrix, int** hitMatrix, Coordinate &previousShot, int &enemyShotOutput, Coordinate* enemyShotHistory)
  1362. {
  1363.     Coordinate shotAttempt;                                                     //Setup variables
  1364.     Coordinate previousHit;
  1365.     int markerOne = 0;
  1366.     int markerTwo = 0;
  1367.     int shotX = 0;
  1368.     int shotY = 0;
  1369.     int directionNum = 0;
  1370.     int markerHit = 0;
  1371.  
  1372.     previousHit.x = 0;
  1373.     previousHit.y = 0;
  1374.     previousHit.shipNumber = 0;
  1375.  
  1376.     cout << "The enemy previous shot was at (" << previousShot.x << "," << previousShot.y << ")" << endl;
  1377.     cout << endl;
  1378.  
  1379.     cout << "Enemy turn!" << endl;
  1380.  
  1381.     if (enemyShotOutput == 0)
  1382.     {
  1383.         do
  1384.         {
  1385.             do
  1386.             {
  1387.                 shotAttempt.x = (rand() % 10) + 1;                              //Get ship X coordinate
  1388.  
  1389.                 if (shotAttempt.x > 1 && shotAttempt.x <= 10)                   //Range check x
  1390.                 {
  1391.                     markerOne = 1;
  1392.                 }
  1393.             }
  1394.             while (markerOne == 0);
  1395.  
  1396.             markerOne = 0;
  1397.  
  1398.             do
  1399.             {
  1400.                 shotAttempt.y = (rand() % 10) + 1;                              //Get ship Y coordinate
  1401.  
  1402.                 if (shotAttempt.y > 1 && shotAttempt.y <= 10)                   //Range check y
  1403.                 {
  1404.                     markerOne = 1;
  1405.                 }
  1406.             }
  1407.             while (markerOne == 0);
  1408.  
  1409.             markerOne = 0;
  1410.             shotX = shotAttempt.x - 1;
  1411.             shotY = shotAttempt.y - 1;
  1412.             if (hitMatrix[shotY][shotX] == 0)                                   //Check to see if there is already a shot a given position
  1413.             {
  1414.                 if (shipMatrix[shotY][shotX] > 0)                               //Check to see if its a hit...
  1415.                 {
  1416.                     hitMatrix[shotY][shotX] = 2;
  1417.                     markerTwo = 1;
  1418.                     shotAttempt.shipNumber = shipMatrix[shotY][shotX];
  1419.                     enemyShotOutput = 1;
  1420.                 }
  1421.                 else                                                            //...or a miss
  1422.                 {
  1423.                     hitMatrix[shotY][shotX] = 1;
  1424.                     markerTwo = 1;
  1425.                     shotAttempt.shipNumber = 0;
  1426.                 }
  1427.             }
  1428.         }
  1429.         while (markerTwo == 0);
  1430.         markerTwo = 0;
  1431.     }
  1432.     else if (enemyShotOutput == 1)                                              //If previous shot was a hit with misses before it
  1433.     {
  1434.         for (int i = 0; i < 5; i++)
  1435.         {
  1436.             if ((enemyShotHistory[i].shipNumber != 0) && (markerHit == 0))
  1437.             {
  1438.                 markerHit = 1;
  1439.                 previousHit = enemyShotHistory[i];
  1440.             }
  1441.         }
  1442.  
  1443.         do
  1444.         {
  1445.             do
  1446.             {
  1447.                 directionNum = (rand() % 4);
  1448.  
  1449.                 switch (directionNum)                                           //Randomly choose a direction for next shot
  1450.                 {
  1451.                 case 0:                                                         //North
  1452.                     shotAttempt.x = previousHit.x;
  1453.                     shotAttempt.y = previousHit.y - 1;
  1454.                     break;
  1455.                 case 1:                                                         //East
  1456.                     shotAttempt.x = previousHit.x + 1;
  1457.                     shotAttempt.y = previousHit.y;
  1458.                     break;
  1459.                 case 2:                                                         //South
  1460.                     shotAttempt.x = previousHit.x;
  1461.                     shotAttempt.y = previousHit.y + 1;
  1462.                     break;
  1463.                 case 3:                                                         //West
  1464.                     shotAttempt.x = previousHit.x - 1;
  1465.                     shotAttempt.y = previousHit.y;
  1466.                     break;
  1467.                 }
  1468.  
  1469.                 if (shotAttempt.x > 1 && shotAttempt.x <= 10)                   //Range check x
  1470.                 {
  1471.                     markerOne = 1;
  1472.                 }
  1473.                 if (shotAttempt.y > 1 && shotAttempt.y <= 10)                   //Range check y
  1474.                 {
  1475.                     markerOne = 1;
  1476.                 }
  1477.             }
  1478.             while (markerOne == 0);
  1479.  
  1480.             markerOne = 0;
  1481.  
  1482.             shotX = shotAttempt.x - 1;
  1483.             shotY = shotAttempt.y - 1;
  1484.  
  1485.             if (hitMatrix[shotY][shotX] == 0)                                   //Check to see if there is already a shot a given position
  1486.             {
  1487.                 if (shipMatrix[shotY][shotX] > 0)                               //Check to see if its a hit...
  1488.                 {
  1489.                     hitMatrix[shotY][shotX] = 2;
  1490.                     markerTwo = 1;
  1491.                     shotAttempt.shipNumber = shipMatrix[shotY][shotX];
  1492.                     enemyShotOutput = 2;
  1493.                 }
  1494.                 else                                                            //...or a miss
  1495.                 {
  1496.                     hitMatrix[shotY][shotX] = 1;
  1497.                     markerTwo = 1;
  1498.                     shotAttempt.shipNumber = 0;
  1499.                 }
  1500.             }
  1501.         }
  1502.         while (markerTwo == 0);
  1503.  
  1504.         markerTwo = 0;
  1505.     }
  1506.     else if (enemyShotOutput == 2)                                              //If multiple previous shots were hits
  1507.     {
  1508.         do
  1509.         {
  1510.             do
  1511.             {
  1512.                 shotAttempt = modalComparisonFunction(enemyShotHistory);        //Find next shot
  1513.  
  1514.                 if (shotAttempt.x > 1 && shotAttempt.x <= 10)                   //Range check x
  1515.                 {
  1516.                     markerOne = 1;
  1517.                 }
  1518.                 if (shotAttempt.y > 1 && shotAttempt.y <= 10)                   //Range check y
  1519.                 {
  1520.                     markerOne = 1;
  1521.                 }
  1522.             }
  1523.             while (markerOne == 0);
  1524.  
  1525.             markerOne = 0;
  1526.  
  1527.             shotX = shotAttempt.x - 1;
  1528.             shotY = shotAttempt.y - 1;
  1529.  
  1530.             if (hitMatrix[shotY][shotX] == 0)                                   //Check to see if there is already a shot a given position
  1531.             {
  1532.                 if (shipMatrix[shotY][shotX] > 0)                               //Check to see if its a hit...
  1533.                 {
  1534.                     hitMatrix[shotY][shotX] = 2;
  1535.                     markerTwo = 1;
  1536.                     shotAttempt.shipNumber = shipMatrix[shotY][shotX];
  1537.                 }
  1538.                 else                                                            //...or a miss
  1539.                 {
  1540.                     hitMatrix[shotY][shotX] = 1;
  1541.                     markerTwo = 1;
  1542.                     shotAttempt.shipNumber = 0;
  1543.                 }
  1544.             }
  1545.         }
  1546.         while (markerTwo == 0);
  1547.  
  1548.         markerTwo = 0;
  1549.     }
  1550.  
  1551.     cout << endl;                                                               //Start map printing function
  1552.     cout << "  || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
  1553.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  1554.     for (int i = 0; i < 10; i++)
  1555.     {
  1556.         cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  1557.         cout << (i + 1);
  1558.         if (i != 9)
  1559.         {
  1560.             cout << " ";
  1561.         }
  1562.         cout << "|";
  1563.         for (int j = 0; j < 10; j++)
  1564.         {
  1565.             int holder = hitMatrix[i][j];
  1566.             cout << "| ";
  1567.             if (holder == 0)
  1568.             {
  1569.                 cout << "-";
  1570.             }
  1571.             else if (holder == 1)
  1572.             {
  1573.                 cout << "/";
  1574.             }
  1575.             else if (holder == 2)
  1576.             {
  1577.                 cout << "H";
  1578.             }
  1579.             cout << " ";
  1580.         }
  1581.         cout << "|" << endl;
  1582.     }
  1583.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  1584.     cout << endl;                                                               //End map printing function
  1585.  
  1586.     cout << "The enemy shot at (" << shotAttempt.x << "," << shotAttempt.y << ")" << endl;
  1587.  
  1588.     if (shipMatrix[shotY][shotX] > 0)                                           //Tell user if its hit or miss
  1589.     {
  1590.         cout << "HIT!" << endl;
  1591.     }
  1592.     else
  1593.     {
  1594.         cout << "MISS!" << endl;
  1595.     }
  1596.  
  1597.     for (int i = 4; i > 0; i--)                                                 //Update shot history
  1598.     {
  1599.         enemyShotHistory[i] = enemyShotHistory[i - 1];
  1600.     }
  1601.  
  1602.     enemyShotHistory[0] = shotAttempt;
  1603.  
  1604.     return shotAttempt;                                                         //Return the coordinate of current shot
  1605. }
  1606.  
  1607. /* boardSweep - function that places misses on the board around a sunken ship
  1608.  * inputs -
  1609.  * sunken:          type - Ship Class       - creates a ship class holder for newly sunken ship
  1610.  * hitMatrix:       type - int** (2d array) - creates a matrix holder for current hits map
  1611.  * output - NONE
  1612.  */
  1613.  
  1614. void boardSweep(Ship &sunken, int** hitMatrix)
  1615. {
  1616.     int currentX = 0;                                                           //Setup variables
  1617.     int currentY = 0;
  1618.  
  1619.     for (int i = 0; i < sunken.length; i++)                                     //From start till end of ship
  1620.     {
  1621.         for (int j = -1; j < 2; j++)
  1622.         {
  1623.             for (int k = -1; k < 2; k++)                                        //For all squares around a ships square
  1624.             {
  1625.                 currentY = sunken.coorY[i] + j;
  1626.                 currentX = sunken.coorX[i] + k;
  1627.                 if (((currentY) >= 0) && ((currentY) < 10) && ((currentX) >= 0) && ((currentX) < 10))
  1628.                 {
  1629.                     //If there is no mark there and it is on the board
  1630.                     if (hitMatrix[currentY][currentX] == 0)
  1631.                     {
  1632.                         hitMatrix[currentY][currentX] = 1;                      //Mark it as a miss
  1633.                     }
  1634.                 }
  1635.             }
  1636.         }
  1637.     }
  1638.  
  1639.     cout << endl;                                                               //Start map printing function
  1640.     cout << "  || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
  1641.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  1642.     for (int i = 0; i < 10; i++)
  1643.     {
  1644.         cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  1645.         cout << (i + 1);
  1646.         if (i != 9)
  1647.         {
  1648.             cout << " ";
  1649.         }
  1650.         cout << "|";
  1651.         for (int j = 0; j < 10; j++)
  1652.         {
  1653.             int holder = hitMatrix[i][j];
  1654.             cout << "| ";
  1655.             if (holder == 0)
  1656.             {
  1657.                 cout << "-";
  1658.             }
  1659.             else if (holder == 1)
  1660.             {
  1661.                 cout << "/";
  1662.             }
  1663.             else if (holder == 2)
  1664.             {
  1665.                 cout << "H";
  1666.             }
  1667.             cout << " ";
  1668.         }
  1669.         cout << "|" << endl;
  1670.     }
  1671.     cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
  1672.     cout << endl;                                                               //End map printing function
  1673. }
  1674.  
  1675. /* modalComparisonFunction - function that analyses shot history pattern
  1676.  * inputs -
  1677.  * enemyShotHistory:type - Coordinate array - holds the previous 5 ai shots
  1678.  * output -
  1679.  * shotAttempt:     type - Coordinate Class - holds the decided shot output
  1680.  */
  1681.  
  1682. Coordinate modalComparisonFunction (Coordinate *enemyShotHistory)
  1683. {
  1684.     int counter = 0;                                                            //Setup variables
  1685.     int modalNumX = 0;
  1686.     int modalCounterX =0;
  1687.     int modalNumY = 0;
  1688.     int modalCounterY =0;
  1689.     int minX = 11;
  1690.     int minY = 11;
  1691.     int maxX = 0;
  1692.     int maxY = 0;
  1693.     int sideMarker = 0;
  1694.     Coordinate shotAttempt;
  1695.  
  1696.     shotAttempt.x = 0;
  1697.     shotAttempt.y = 0;
  1698.     shotAttempt.shipNumber = 0;
  1699.  
  1700.     for (int i = 1; i <= 10; i++)                                               //For 1-10 (board values)
  1701.     {
  1702.         for (int j = 0; j < 5; j++)
  1703.         {
  1704.             if ((enemyShotHistory[j].x == i) && (enemyShotHistory[j].shipNumber != 0))
  1705.             {                                                                   //Check if each x matches the value and is a hit
  1706.                 counter++;
  1707.             }
  1708.         }
  1709.  
  1710.         if (counter > modalCounterX)                                            //Compare for modal value
  1711.         {
  1712.             modalNumX = i;
  1713.             modalCounterX = counter;
  1714.         }
  1715.  
  1716.         if ((counter != 0) && (minX > i))
  1717.         {
  1718.             minX = i;                                                           //Compare for min value
  1719.         }
  1720.  
  1721.         if ((counter != 0) && (maxX < i))
  1722.         {
  1723.             maxX = i;                                                           //Compare for max value
  1724.         }
  1725.  
  1726.         counter = 0;
  1727.     }
  1728.  
  1729.     for (int i = 1; i <= 10; i++)                                               //For 1-10 (board values)
  1730.     {
  1731.         for (int j = 0; j < 5; j++)
  1732.         {
  1733.             if ((enemyShotHistory[j].y == i) && (enemyShotHistory[j].shipNumber != 0))
  1734.             {                                                                   //Check if each y matches the value and is a hit
  1735.                 counter++;
  1736.             }
  1737.         }
  1738.  
  1739.         if (counter > modalCounterY)                                            //Compare for modal value
  1740.         {
  1741.             modalNumY = i;
  1742.             modalCounterY = counter;
  1743.         }
  1744.  
  1745.         if ((counter != 0) && (minY > i))
  1746.         {
  1747.             minY = i;                                                           //Compare for min value
  1748.         }
  1749.  
  1750.         if ((counter != 0) && (maxY < i))
  1751.         {
  1752.             maxY = i;                                                           //Compare for max value
  1753.         }
  1754.  
  1755.         counter = 0;
  1756.     }
  1757.  
  1758.     sideMarker = (rand() % 2) + 1;                                              //Randomly select a side to fire at
  1759.  
  1760.     if (modalCounterX > modalCounterY)
  1761.     {
  1762.         shotAttempt.x = modalNumX;                                              //Set x as modal value
  1763.  
  1764.         if (sideMarker == 1)                                                    //Set y just outside range
  1765.         {
  1766.             shotAttempt.y = maxY + 1;
  1767.         }
  1768.         else
  1769.         {
  1770.             shotAttempt.y = minY - 1;
  1771.         }
  1772.     }
  1773.     else
  1774.     {
  1775.         shotAttempt.y = modalNumY;                                              //Set y as modal value
  1776.  
  1777.         if (sideMarker == 1)                                                    //Set x just outside range
  1778.         {
  1779.             shotAttempt.x = maxX + 1;
  1780.         }
  1781.         else
  1782.         {
  1783.             shotAttempt.x = minX - 1;
  1784.         }
  1785.     }
  1786.  
  1787.     return shotAttempt;
  1788. }
  1789.  
  1790. /* placeShip - public Ship Class function that sets values when ship is placed onto board
  1791.  * inputs -
  1792.  * startX:          type - int              - value of starting x coordinate for the ship
  1793.  * startY:          type - int              - value of starting y coordinate for the ship
  1794.  * directionShip:   type - char             - char of the compass direction the ship goes from starting coordinate
  1795.  * output -
  1796.  * errorNum:        type - int              - returns a numerical value as an error flag
  1797.  */
  1798.  
  1799. int Ship::placeShip(int startX, int startY, char directionShip)
  1800. {
  1801.     int errorNum = 0;                                                           //Setup variables
  1802.     int endX = 0;
  1803.     int endY = 0;
  1804.  
  1805.     startC.setVal(startX, startY);                                              //Set the start coordinate and direction of the ship
  1806.     direction = directionShip;
  1807.  
  1808.     switch (directionShip)                                                      //Calculate end coordinate based on the start and direction of the ship
  1809.     {
  1810.     case ('N'):
  1811.         endY = startY - (length - 1);
  1812.         endX = startX;
  1813.         break;
  1814.     case ('E'):
  1815.         endY = startY;
  1816.         endX = startX + (length - 1);
  1817.         break;
  1818.     case ('S'):
  1819.         endY = startY + (length - 1);
  1820.         endX = startX;
  1821.         break;
  1822.     case ('W'):
  1823.         endY = startY;
  1824.         endX = startX - (length - 1);
  1825.         break;
  1826.     default:
  1827.         break;
  1828.     }
  1829.  
  1830.     if (endX < 1 || endX > 10)                                                  //If ship would go out of x range send error 1 back
  1831.     {
  1832.         errorNum = 1;
  1833.     }
  1834.     else if (endY < 1 || endY > 10)                                             //If ship would go out of y range send error 2 back
  1835.     {
  1836.         errorNum = 2;
  1837.     }
  1838.     else
  1839.     {
  1840.         endC.setVal(endX, endY);                                                //Else set the end coordinates and send non-error (0) back
  1841.         errorNum = 0;
  1842.     }
  1843.  
  1844.     return errorNum;
  1845. }
  1846.  
  1847. /* hitMark - public Ship Class function that marks when the ship takes damage and checks if sunk
  1848.  * inputs -
  1849.  * currentShot:     type - Coordinate Class - coordinate for current user shot attempt
  1850.  * output -
  1851.  * sunkResponse:    type - int              - returns an int as a truth value dependant on if the ship has sunk
  1852.  */
  1853.  
  1854. int Ship::hitMark(Coordinate &currentShot)
  1855. {
  1856.     int hitNumber = 0;                                                          //Setup variables
  1857.     int sunkResponse = 0;
  1858.  
  1859.     for (int i = 0; i < 5; i++)                                                 //For all the ships hit array
  1860.     {
  1861.         if ((coorX[i] == (currentShot.x - 1)) && (coorY[i] == (currentShot.y - 1)))
  1862.         {
  1863.             hit[i] = 1;                                                         //If the shot matches a ship coordinate mark hit array
  1864.         }
  1865.  
  1866.         if (hit[i] == 1)
  1867.         {
  1868.             hitNumber++;                                                        //If the hit array is marked at position add to counter
  1869.         }
  1870.     }
  1871.  
  1872.     if (hitNumber == length)                                                    //If the number of hits in the array matches the ship length
  1873.     {
  1874.         sunkResponse = 1;                                                       //Then ship is sunk mark response for feedback
  1875.         sunkMarker = 1;
  1876.     }
  1877.  
  1878.     return sunkResponse;
  1879. }
  1880.  
  1881. /* setVal - public Coordinate Class function that sets the coordinate values
  1882.  * inputs -
  1883.  * valX:            type - int              - value of x coordinate to be set
  1884.  * valY:            type - int              - value of y coordinate to be set
  1885.  * output - NONE
  1886.  */
  1887.  
  1888. void Coordinate::setVal(int valX, int valY)
  1889. {
  1890.     x = valX;                                                                   //Set the coordinate x value as sent x value
  1891.     y = valY;                                                                   //Set the coordinate y value as sent y value
  1892. }
  1893.  
  1894. /* Fixed Issues:
  1895.  * 1: Following Up -
  1896.  * If the ai hits user ship itll try to fire next to it next
  1897.  * shot if it has hit then miss it should try another position
  1898.  * next to the hit but it goes back to random fire
  1899.  * found - incorrect ai stage flag in place
  1900.  * fix - change ai stage flag
  1901.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement