Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************************/
- /* Controlled Projectile Release Simulation Software
- * Codename: Battleships
- * Version 3.0
- *
- * Dev Log:
- * Version | Author | Comments
- * 3.0 | BMS | user vs ai
- * 3.1 | BMS | following up fix
- *
- * 1 current issues:
- * 2: Mid-Game Crash
- * Game runs fine on occasion and not on others, unsure as to
- * reason. Keeping game log for crashes to attempt to recognise
- * any sort of pattern but looks entirely random.
- */
- /***********************************************************/
- #include <iostream>
- #include <time.h>
- using namespace std;
- /**Class Definitions**/
- /*Coordinate - used to combine two ints in coordinate style
- *x and y are the coordinate values
- *setVal was used to set the x and y values
- *shipNumber used to mark a ship value at the coordinate
- */
- class Coordinate
- {
- public:
- int x, y;
- void setVal(int, int);
- int shipNumber;
- };
- /*Ship - used to define multiple parameters to do with ship objects
- *direction is the compass direction used from start to map the ship
- *coorX and coorY are the arrays on coordinate values for the ship
- *hit array used to mark the parts of the ship that have sustained damage
- *startC and endC coordinates at either end of the ship
- *length marks the length of the ship in no of spaces
- *sunkMarker marks whether or not the ship is still afloat
- *placeShip function sets most ship values based on user input
- *hitMark function sets ship damage and sunkMarker if ship is at critical
- */
- class Ship
- {
- char direction;
- public:
- int coorX[5];
- int coorY[5];
- int hit[5];
- Coordinate startC, endC;
- int length;
- int sunkMarker;
- int placeShip(int, int, char);
- int hitMark(Coordinate&);
- };
- /**Prototype Functions**/
- void setShip (Ship&, int **, int);
- Coordinate takeShot (int **, int **, Coordinate&);
- void enemySetShip (Ship&, int **, int);
- Coordinate enemyTakeShot (int **, int **, Coordinate&, int&, Coordinate *);
- void boardSweep (Ship&, int **);
- Coordinate modalComparisonFunction (Coordinate *);
- /**Functions**/
- /* main - main code
- * inputs - NONE
- * output - int
- */
- int main(void)
- {
- int rowCount = 10; //Setup variables
- int colCount = 10;
- int winMarker = 0;
- int sunkResponse = 0;
- int sunkCounter = 0;
- int enemySunkCounter = 0;
- Coordinate previousShot;
- Coordinate currentShot;
- Coordinate enemyPreviousShot;
- Coordinate enemyCurrentShot;
- int enemyShotOuput = 0;
- Coordinate enemyShotHistory[5];
- int turnCounter = 0;
- previousShot.x = 0;
- previousShot.y = 0;
- previousShot.shipNumber = 0;
- enemyPreviousShot.x = 0;
- enemyPreviousShot.y = 0;
- enemyPreviousShot.shipNumber = 0;
- for (int i = 0; i < 5; i++)
- {
- enemyShotHistory[i].x = 0;
- enemyShotHistory[i].y = 0;
- enemyShotHistory[i].shipNumber = 0;
- }
- int** shipMatrix = new int*[rowCount]; //Create matrix for ship map
- for(int i = 0; i < rowCount; ++i)
- {
- shipMatrix[i] = new int[colCount];
- for(int j = 0; j < colCount; j++)
- {
- shipMatrix[i][j] = 0;
- }
- }
- int** hitMatrix = new int*[rowCount]; //Create matrix for hit map
- for(int i = 0; i < rowCount; ++i)
- {
- hitMatrix[i] = new int[colCount];
- for(int j = 0; j < colCount; j++)
- {
- hitMatrix[i][j] = 0;
- }
- }
- int** enemyMatrix = new int*[rowCount]; //Create matrix for enemy ship map
- for(int i = 0; i < rowCount; ++i)
- {
- enemyMatrix[i] = new int[colCount];
- for(int j = 0; j < colCount; j++)
- {
- enemyMatrix[i][j] = 0;
- }
- }
- int** enemyHitMatrix = new int*[rowCount]; //Create matrix for enemy hit map
- for(int i = 0; i < rowCount; ++i)
- {
- enemyHitMatrix[i] = new int[colCount];
- for(int j = 0; j < colCount; j++)
- {
- enemyHitMatrix[i][j] = 0;
- }
- }
- cout << "BATTLESHIPS!" << endl;
- cout << endl; //Start of map printing function
- cout << " || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- for (int i = 0; i < 10; i++)
- {
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << (i + 1);
- if (i != 9)
- {
- cout << " ";
- }
- cout << "|";
- for (int j = 0; j < 10; j++)
- {
- int holder = hitMatrix[i][j];
- cout << "| ";
- if (holder == 0)
- {
- cout << "-";
- }
- else if (holder == 1)
- {
- cout << "/";
- }
- else if (holder == 2)
- {
- cout << "H";
- }
- cout << " ";
- }
- cout << "|" << endl;
- }
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << endl; //End of map printing function
- cout << "The position of your ships is set by:" << endl; //Give user instruction
- cout << "- An X coordinate (1-10)" << endl;
- cout << "- Then a Y coordinate (1-10)" << endl;
- cout << "- Then the direction from the coordinate (N/E/S/W)" << endl;
- cout << "- Enter X on direction to start placement again" << endl;
- Ship Carrier; //Create carrier, ship class
- Carrier.length = 5;
- Carrier.sunkMarker = 0;
- cout << endl;
- cout << "The first ship to be placed is the aircraft carrier" << endl;
- setShip(Carrier, shipMatrix, 0); //Place carrier
- Ship Battleship; //Create battleship, ship class
- Battleship.length = 4;
- Battleship.sunkMarker = 0;
- cout << endl;
- cout << "The next ship to be placed is the battleship" << endl;
- setShip(Battleship, shipMatrix, 0); //Place battleship
- Ship Destroyer; //Create destroyer, ship class
- Destroyer.length = 3;
- Destroyer.sunkMarker = 0;
- cout << endl;
- cout << "The next ship to be placed is the destroyer" << endl;
- setShip(Destroyer, shipMatrix, 0); //Place destroyer
- Ship Submarine; //Create submarine, ship class
- Submarine.length = 3;
- Submarine.sunkMarker = 0;
- cout << endl;
- cout << "The next ship to be placed is the submarine" << endl;
- setShip(Submarine, shipMatrix, 1); //Place submarine
- Ship Patrol; //Create patrol boat, ship class
- Patrol.length = 2;
- Patrol.sunkMarker = 0;
- cout << endl;
- cout << "The final ship to be placed is the patrol boat" << endl;
- setShip(Patrol, shipMatrix, 0); //Place patrol boat
- cout << endl;
- srand(time(NULL)); //Enemy setup
- Ship enemyCarrier; //Create carrier, ship class
- enemyCarrier.length = 5;
- enemyCarrier.sunkMarker = 0;
- enemySetShip(enemyCarrier, enemyMatrix, 0); //Place carrier
- Ship enemyBattleship; //Create battleship, ship class
- enemyBattleship.length = 4;
- enemyBattleship.sunkMarker = 0;
- enemySetShip(enemyBattleship, enemyMatrix, 0); //Place battleship
- Ship enemyDestroyer; //Create destroyer, ship class
- enemyDestroyer.length = 3;
- enemyDestroyer.sunkMarker = 0;
- enemySetShip(enemyDestroyer, enemyMatrix, 0); //Place destroyer
- Ship enemySubmarine; //Create submarine, ship class
- enemySubmarine.length = 3;
- enemySubmarine.sunkMarker = 0;
- enemySetShip(enemySubmarine, enemyMatrix, 1); //Place submarine
- Ship enemyPatrol; //Create patrol boat, ship class
- enemyPatrol.length = 2;
- enemyPatrol.sunkMarker = 0;
- enemySetShip(enemyPatrol, enemyMatrix, 0); //Place patrol boat
- for (int i = 0; i < 5; i++) //Set all boats hit markers off
- {
- Carrier.hit[i] = 0;
- Battleship.hit[i] = 0;
- Destroyer.hit[i] = 0;
- Submarine.hit[i] = 0;
- Patrol.hit[i] = 0;
- enemyCarrier.hit[i] = 0;
- enemyBattleship.hit[i] = 0;
- enemyDestroyer.hit[i] = 0;
- enemySubmarine.hit[i] = 0;
- enemyPatrol.hit[i] = 0;
- }
- cout << "The game will now begin!" << endl;
- for (int i = 0; i <= 32767; i++) //Manually created delay (delay and sleep wouldnt work XD)
- {
- for (int j = 0; j <= 32767; j++);
- }
- cout << endl; //Start of map printing function
- cout << " || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- for (int i = 0; i < 10; i++)
- {
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << (i + 1);
- if (i != 9)
- {
- cout << " ";
- }
- cout << "|";
- for (int j = 0; j < 10; j++)
- {
- int holder = hitMatrix[i][j];
- cout << "| ";
- if (holder == 0)
- {
- cout << "-";
- }
- else if (holder == 1)
- {
- cout << "/";
- }
- else if (holder == 2)
- {
- cout << "H";
- }
- cout << " ";
- }
- cout << "|" << endl;
- }
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << endl; //End of map printing function
- do //Start of play loop
- {
- turnCounter++;
- cout << "Turn " << turnCounter << endl << endl;
- currentShot = takeShot(enemyMatrix, hitMatrix, previousShot); //Send to takeShot function
- switch (currentShot.shipNumber) //Check returned ship number on space that was fired at
- {
- case 0:
- break;
- case 2:
- sunkResponse = enemyPatrol.hitMark(currentShot); //Patrol boat hit and sunk checker
- if (sunkResponse == 1)
- {
- cout << "You sunk the enemy Patrol Boat!" << endl;
- cout << endl;
- boardSweep(enemyPatrol, hitMatrix);
- sunkCounter++;
- }
- break;
- case 3:
- sunkResponse = enemyDestroyer.hitMark(currentShot); //Destroyer hit and sunk checker
- if (sunkResponse == 1)
- {
- cout << "You sunk the enemy Destroyer!" << endl;
- cout << endl;
- boardSweep(enemyDestroyer, hitMatrix);
- sunkCounter++;
- }
- break;
- case 4:
- sunkResponse = enemyBattleship.hitMark(currentShot); //Battleship hit and sunk checker
- if (sunkResponse == 1)
- {
- cout << "You sunk the enemy Battleship!" << endl;
- cout << endl;
- boardSweep(enemyBattleship, hitMatrix);
- sunkCounter++;
- }
- break;
- case 5:
- sunkResponse = enemyCarrier.hitMark(currentShot); //Carrier hit and sunk checker
- if (sunkResponse == 1)
- {
- cout << "You sunk the enemy Aircraft Carrier!" << endl;
- cout << endl;
- boardSweep(enemyCarrier, hitMatrix);
- sunkCounter++;
- }
- break;
- case 6:
- sunkResponse = enemySubmarine.hitMark(currentShot); //Submarine hit and sunk checker
- if (sunkResponse == 1)
- {
- cout << "You sunk the enemy Submarine!" << endl;
- cout << endl;
- boardSweep(enemySubmarine, hitMatrix);
- sunkCounter++;
- }
- break;
- default:
- break;
- }
- previousShot = currentShot;
- if (sunkCounter == 5) //If all boats hit break play loop
- {
- winMarker = 1;
- }
- cout << endl;
- enemyCurrentShot = enemyTakeShot(shipMatrix, enemyHitMatrix, enemyPreviousShot, enemyShotOuput, enemyShotHistory);
- /*Send to enemyTakeShot function*/
- switch (enemyCurrentShot.shipNumber) //Check returned ship number on space that was fired at
- {
- case 0:
- break;
- case 2:
- sunkResponse = Patrol.hitMark(enemyCurrentShot); //Patrol boat hit and sunk checker
- if (sunkResponse == 1)
- {
- cout << "The enemy sunk your Patrol Boat!" << endl;
- cout << endl;
- boardSweep(Patrol, enemyHitMatrix);
- enemySunkCounter++;
- enemyShotOuput = 0;
- }
- break;
- case 3:
- sunkResponse = Destroyer.hitMark(enemyCurrentShot); //Destroyer hit and sunk checker
- if (sunkResponse == 1)
- {
- cout << "The enemy sunk your Destroyer!" << endl;
- cout << endl;
- boardSweep(Destroyer, enemyHitMatrix);
- enemySunkCounter++;
- enemyShotOuput = 0;
- }
- break;
- case 4:
- sunkResponse = Battleship.hitMark(enemyCurrentShot); //Battleship hit and sunk checker
- if (sunkResponse == 1)
- {
- cout << "The enemy sunk your Battleship!" << endl;
- cout << endl;
- boardSweep(Battleship, enemyHitMatrix);
- enemySunkCounter++;
- enemyShotOuput = 0;
- }
- break;
- case 5:
- sunkResponse = Carrier.hitMark(enemyCurrentShot); //Carrier hit and sunk checker
- if (sunkResponse == 1)
- {
- cout << "The enemy sunk your Aircraft Carrier!" << endl;
- cout << endl;
- boardSweep(Carrier, enemyHitMatrix);
- enemySunkCounter++;
- enemyShotOuput = 0;
- }
- break;
- case 6:
- sunkResponse = Submarine.hitMark(enemyCurrentShot); //Submarine hit and sunk checker
- if (sunkResponse == 1)
- {
- cout << "The enemy sunk your Submarine!" << endl;
- cout << endl;
- boardSweep(Submarine, enemyHitMatrix);
- enemySunkCounter++;
- enemyShotOuput = 0;
- }
- break;
- default:
- break;
- }
- enemyPreviousShot = enemyCurrentShot;
- if ((enemySunkCounter == 5) && (winMarker == 0)) //If all boats hit break play loop
- {
- winMarker = 2;
- }
- else if ((enemySunkCounter == 5) && (winMarker == 1))
- {
- winMarker = 3;
- }
- cout << endl;
- }
- while (winMarker == 0);
- cout << endl; //Start of map printing function
- cout << " || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- for (int i = 0; i < 10; i++)
- {
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << (i + 1);
- if (i != 9)
- {
- cout << " ";
- }
- cout << "|";
- for (int j = 0; j < 10; j++)
- {
- int holder = hitMatrix[i][j];
- cout << "| ";
- if (holder == 0)
- {
- cout << "-";
- }
- else if (holder == 1)
- {
- cout << "/";
- }
- else if (holder == 2)
- {
- cout << "H";
- }
- cout << " ";
- }
- cout << "|" << endl;
- }
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << endl; //End of map printing function
- switch (winMarker)
- {
- case 1:
- cout << "YOU WIN! You have sunk all enemy ships on turn " << turnCounter << "!" << endl;
- break; //Victory message
- case 2:
- cout << "YOU LOSE! The enemy has sunk all your ships on turn " << turnCounter << "!" << endl;
- break; //Defeat message
- case 3:
- cout << "DRAW! You sunk each others ships on the same turn! (Turn " << turnCounter << ")" << endl;
- break; //Draw message
- default:
- cout << "ERROR - Winner could not be determined" << endl; //Default message
- break;
- }
- }
- /* setShip - setup function setting ships position from user input
- * inputs -
- * placement: type - Ship Class - creates a ship class holder for current passed ship
- * shipMatrix: type - int** (2d array) - creates a matrix holder for current ship map
- * multiMark: type - int - value to check if the ship is destroyer or submarine
- * output - NONE
- */
- void setShip(Ship &placement, int** shipMatrix, int multiMark)
- {
- int markerOne = 0; //Setup variables
- int markerTwo = 0;
- int markerThree = 0;
- int markerFour = 0;
- int startX = 0;
- int startY = 0;
- char direction = ' ';
- int errorNum = 0;
- int placeX = 0;
- int placeY = 0;
- cout << "The ship is " << placement.length << " spaces long" << endl; //Tell user ship length
- cout << endl;
- do
- {
- do
- {
- cout << "Please enter the X position for your ship" << endl; //Get ship X coordinate
- cin >> startX;
- cin.clear();
- cin.ignore();
- if (0 < startX && startX <= 10) //Range check X
- {
- markerOne = 1;
- }
- else
- {
- cout << "X must be a number between 1-10" << endl;
- }
- }
- while (markerOne == 0);
- markerOne = 0;
- do
- {
- cout << "Please enter the Y position for your ship" << endl; //Get ship Y coordinate
- cin >> startY;
- cin.clear();
- cin.ignore();
- if (0 < startY && startY <= 10) //Range check Y
- {
- markerOne = 1;
- }
- else
- {
- cout << "Y must be a number between 1-10" << endl;
- }
- }
- while (markerOne == 0);
- markerOne = 0;
- do
- {
- cout << "Please enter the direction or X to reset" << endl; //Get compass direction of ship from start
- cin >> direction;
- cin.clear();
- cin.ignore();
- cout << endl;
- switch (direction) //Check input value for direction or reset key
- {
- case 'N':
- markerOne = 1;
- markerTwo = 1;
- break;
- case 'E':
- markerOne = 1;
- markerTwo = 1;
- break;
- case 'S':
- markerOne = 1;
- markerTwo = 1;
- break;
- case 'W':
- markerOne = 1;
- markerTwo = 1;
- break;
- case 'n':
- direction = 'N';
- markerOne = 1;
- markerTwo = 1;
- break;
- case 'e':
- direction = 'E';
- markerOne = 1;
- markerTwo = 1;
- break;
- case 's':
- direction = 'S';
- markerOne = 1;
- markerTwo = 1;
- break;
- case 'w':
- direction = 'W';
- markerOne = 1;
- markerTwo = 1;
- break;
- case 'X':
- markerOne = 1;
- cout << "Resetting entered values" << endl;
- break;
- default:
- cout << "Please enter N/E/S/W or X to reset" << endl;
- break;
- }
- }
- while (markerOne == 0);
- markerOne = 0; //Reset placement values
- placement.startC.x = 0;
- placement.startC.y = 0;
- errorNum = placement.placeShip(startX, startY, direction); //Send to ship placement function
- if (errorNum == 1) //Error - 1 edge of ship goes out of x range
- {
- cout << "The ship will not fit on the X-axis with these values" << endl;
- markerTwo = 0;
- errorNum = 0;
- }
- else if (errorNum == 2) //Error - 1 edge of ship goes out of y range
- {
- cout << "The ship will not fit on the Y-axis with these values" << endl;
- markerTwo = 0;
- errorNum = 0;
- }
- else
- {
- if (multiMark == 1)
- {
- placement.length = 6;
- }
- if (placement.startC.x == placement.endC.x) //Equivalent of direction checker (N)
- {
- if(placement.startC.y > placement.endC.y)
- {
- for (int i = placement.startC.y; i >= placement.endC.y; i--)
- {
- //For all squares check before placement
- placeX = placement.startC.x - 1;
- placeY = i - 1;
- /*Checks to see if any of the places will collide or touch another ship*/
- for (int j = -1; j < 2; j++)
- {
- for (int k = -1; k < 2; k++)
- {
- if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
- {
- if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
- {
- markerTwo = 0;
- cout << "At X: " << (placeX + k) << " Y: " << (placeY + j) << " Against Mark: " << shipMatrix[placeY + j][placeX + k] << endl;
- cout << "This placement will collide with another ship!" << endl;
- markerFour = 1;
- }
- }
- }
- }
- }
- placeX = 0;
- placeY = 0;
- if (markerFour == 0) //If there are no collision spaces
- {
- /*Place the ship onto the ship board*/
- for (int i = placement.startC.y; i >= placement.endC.y; i--)
- {
- placeX = placement.startC.x - 1;
- placeY = i - 1;
- placement.coorX[markerThree] = placeX;
- placement.coorY[markerThree] = placeY;
- placement.hit[markerThree] = 0;
- markerThree++;
- shipMatrix[placeY][placeX] = placement.length;
- }
- }
- }
- else if(placement.startC.y < placement.endC.y) //Same as previous section (S)
- {
- for (int i = placement.startC.y; i <= placement.endC.y; i++)
- {
- placeX = placement.startC.x - 1;
- placeY = i - 1;
- for (int j = -1; j < 2; j++)
- {
- for (int k = -1; k < 2; k++)
- {
- if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
- {
- if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
- {
- markerTwo = 0;
- cout << "At X: " << (placeX + k) << " Y: " << (placeY + j) << " Against Mark: " << shipMatrix[placeY + j][placeX + k] << endl;
- cout << "This placement will collide with another ship!" << endl;
- markerFour = 1;
- }
- }
- }
- }
- }
- placeX = 0;
- placeY = 0;
- if (markerFour == 0)
- {
- for (int i = placement.startC.y; i <= placement.endC.y; i++)
- {
- placeX = placement.startC.x - 1;
- placeY = i - 1;
- placement.coorX[markerThree] = placeX;
- placement.coorY[markerThree] = placeY;
- placement.hit[markerThree] = 0;
- markerThree++;
- shipMatrix[placeY][placeX] = placement.length;
- }
- }
- }
- }
- else if (placement.startC.y == placement.endC.y) //Same as previous section (W)
- {
- if(placement.startC.x > placement.endC.x)
- {
- for (int i = placement.startC.x; i >= placement.endC.x; i--)
- {
- placeX = i - 1;
- placeY = placement.startC.y - 1;
- for (int j = -1; j < 2; j++)
- {
- for (int k = -1; k < 2; k++)
- {
- if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
- {
- if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
- {
- markerTwo = 0;
- cout << "At X: " << (placeX + k) << " Y: " << (placeY + j) << " Against Mark: " << shipMatrix[placeY + j][placeX + k] << endl;
- cout << "This placement will collide with another ship!" << endl;
- markerFour = 1;
- }
- }
- }
- }
- }
- placeX = 0;
- placeY = 0;
- if (markerFour == 0)
- {
- for (int i = placement.startC.x; i >= placement.endC.x; i--)
- {
- placeX = i - 1;
- placeY = placement.startC.y - 1;
- placement.coorX[markerThree] = placeX;
- placement.coorY[markerThree] = placeY;
- placement.hit[markerThree] = 0;
- markerThree++;
- shipMatrix[placeY][placeX] = placement.length;
- }
- }
- }
- else if(placement.startC.x < placement.endC.x) //Same as previous section(E)
- {
- for (int i = placement.startC.x; i <= placement.endC.x; i++)
- {
- placeX = i - 1;
- placeY = placement.startC.y - 1;
- for (int j = -1; j < 2; j++)
- {
- for (int k = -1; k < 2; k++)
- {
- if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
- {
- if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
- {
- markerTwo = 0;
- cout << "At X: " << (placeX + k) << " Y: " << (placeY + j) << " Against Mark: " << shipMatrix[placeY + j][placeX + k] << endl;
- cout << "This placement will collide with another ship!" << endl;
- markerFour = 1;
- }
- }
- }
- }
- }
- placeX = 0;
- placeY = 0;
- if (markerFour == 0)
- {
- for (int i = placement.startC.x; i <= placement.endC.x; i++)
- {
- placeX = i - 1;
- placeY = placement.startC.y - 1;
- placement.coorX[markerThree] = placeX;
- placement.coorY[markerThree] = placeY;
- placement.hit[markerThree] = 0;
- markerThree++;
- shipMatrix[placeY][placeX] = placement.length;
- }
- }
- }
- }
- }
- if (multiMark == 1)
- {
- placement.length = 3;
- }
- markerFour = 0;
- direction = ' ';
- startX = 0;
- startY = 0;
- placeX = 0;
- placeY = 0;
- }
- while (markerTwo == 0);
- markerTwo = 0;
- markerThree = 0;
- cout << endl; //Start of map printing function for ship map
- cout << " || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- for (int i = 0; i < 10; i++)
- {
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << (i + 1);
- if (i != 9)
- {
- cout << " ";
- }
- cout << "|";
- for (int j = 0; j < 10; j++)
- {
- int holder = shipMatrix[i][j];
- cout << "| ";
- if (holder == 0)
- {
- cout << "-";
- }
- else
- {
- cout << holder;
- }
- cout << " ";
- }
- cout << "|" << endl;
- }
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << endl; //End of map printing function
- }
- /* enemySetShip - setup function setting ships position from random "ai" input
- * inputs -
- * placement: type - Ship Class - creates a ship class holder for current passed ship
- * shipMatrix: type - int** (2d array) - creates a matrix holder for current ship map
- * multiMark: type - int - value to check if the ship is destroyer or submarine
- * output - NONE
- */
- void enemySetShip(Ship &placement, int** shipMatrix, int multiMark)
- {
- int markerOne = 0; //Setup variables
- int markerTwo = 0;
- int markerThree = 0;
- int markerFour = 0;
- int startX = 0;
- int startY = 0;
- int dirNum = 0;
- char direction = ' ';
- int errorNum = 0;
- int placeX = 0;
- int placeY = 0;
- do
- {
- do
- {
- startX = (rand() % 10) + 1; //Get ship X coordinate
- if (0 < startX && startX <= 10) //Range check X
- {
- markerOne = 1;
- }
- }
- while (markerOne == 0);
- markerOne = 0;
- do
- {
- startY = (rand() % 10) + 1; //Get ship Y coordinate
- if (0 < startY && startY <= 10) //Range check Y
- {
- markerOne = 1;
- }
- }
- while (markerOne == 0);
- markerOne = 0;
- do
- {
- dirNum = (rand() % 4); //Get ship X coordinate
- switch (dirNum) //Check input value for direction or reset key
- {
- case 0:
- direction = 'N';
- markerOne = 1;
- markerTwo = 1;
- break;
- case 1:
- direction = 'E';
- markerOne = 1;
- markerTwo = 1;
- break;
- case 2:
- direction = 'S';
- markerOne = 1;
- markerTwo = 1;
- break;
- case 3:
- direction = 'W';
- markerOne = 1;
- markerTwo = 1;
- break;
- default:
- break;
- }
- }
- while (markerOne == 0);
- markerOne = 0; //Reset placement values
- placement.startC.x = 0;
- placement.startC.y = 0;
- errorNum = placement.placeShip(startX, startY, direction); //Send to ship placement function
- if (errorNum == 1) //Error - 1 edge of ship goes out of x range
- {
- markerTwo = 0;
- errorNum = 0;
- }
- else if (errorNum == 2) //Error - 1 edge of ship goes out of y range
- {
- markerTwo = 0;
- errorNum = 0;
- }
- else
- {
- if (multiMark == 1)
- {
- placement.length = 6; //To separate the destroyer and the submarine
- }
- if (placement.startC.x == placement.endC.x) //Equivalent of direction checker (N)
- {
- if(placement.startC.y > placement.endC.y)
- {
- for (int i = placement.startC.y; i >= placement.endC.y; i--)
- {
- //For all squares check before placement
- placeX = placement.startC.x - 1;
- placeY = i - 1;
- /*Checks to see if any of the places will collide or touch another ship*/
- for (int j = -1; j < 2; j++)
- {
- for (int k = -1; k < 2; k++)
- {
- if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
- {
- if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
- {
- markerTwo = 0;
- markerFour = 1;
- }
- }
- }
- }
- }
- placeX = 0;
- placeY = 0;
- if (markerFour == 0) //If there are no collision spaces
- {
- /*Place the ship onto the ship board*/
- for (int i = placement.startC.y; i >= placement.endC.y; i--)
- {
- placeX = placement.startC.x - 1;
- placeY = i - 1;
- placement.coorX[markerThree] = placeX;
- placement.coorY[markerThree] = placeY;
- placement.hit[markerThree] = 0;
- markerThree++;
- shipMatrix[placeY][placeX] = placement.length;
- }
- }
- }
- else if(placement.startC.y < placement.endC.y) //Same as previous section (S)
- {
- for (int i = placement.startC.y; i <= placement.endC.y; i++)
- {
- placeX = placement.startC.x - 1;
- placeY = i - 1;
- for (int j = -1; j < 2; j++)
- {
- for (int k = -1; k < 2; k++)
- {
- if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
- {
- if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
- {
- markerTwo = 0;
- markerFour = 1;
- }
- }
- }
- }
- }
- placeX = 0;
- placeY = 0;
- if (markerFour == 0)
- {
- for (int i = placement.startC.y; i <= placement.endC.y; i++)
- {
- placeX = placement.startC.x - 1;
- placeY = i - 1;
- placement.coorX[markerThree] = placeX;
- placement.coorY[markerThree] = placeY;
- placement.hit[markerThree] = 0;
- markerThree++;
- shipMatrix[placeY][placeX] = placement.length;
- }
- }
- }
- }
- else if (placement.startC.y == placement.endC.y) //Same as previous section (W)
- {
- if(placement.startC.x > placement.endC.x)
- {
- for (int i = placement.startC.x; i >= placement.endC.x; i--)
- {
- placeX = i - 1;
- placeY = placement.startC.y - 1;
- for (int j = -1; j < 2; j++)
- {
- for (int k = -1; k < 2; k++)
- {
- if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
- {
- if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
- {
- markerTwo = 0;
- markerFour = 1;
- }
- }
- }
- }
- }
- placeX = 0;
- placeY = 0;
- if (markerFour == 0)
- {
- for (int i = placement.startC.x; i >= placement.endC.x; i--)
- {
- placeX = i - 1;
- placeY = placement.startC.y - 1;
- placement.coorX[markerThree] = placeX;
- placement.coorY[markerThree] = placeY;
- placement.hit[markerThree] = 0;
- markerThree++;
- shipMatrix[placeY][placeX] = placement.length;
- }
- }
- }
- else if(placement.startC.x < placement.endC.x) //Same as previous section(E)
- {
- for (int i = placement.startC.x; i <= placement.endC.x; i++)
- {
- placeX = i - 1;
- placeY = placement.startC.y - 1;
- for (int j = -1; j < 2; j++)
- {
- for (int k = -1; k < 2; k++)
- {
- if (((placeY + j) >= 0) && ((placeY + j) < 10) && ((placeX + k) >= 0) && ((placeX + k) < 10))
- {
- if (shipMatrix[placeY + j][placeX + k] > 0 && shipMatrix[placeY + j][placeX + k] != placement.length)
- {
- markerTwo = 0;
- markerFour = 1;
- }
- }
- }
- }
- }
- placeX = 0;
- placeY = 0;
- if (markerFour == 0)
- {
- for (int i = placement.startC.x; i <= placement.endC.x; i++)
- {
- placeX = i - 1;
- placeY = placement.startC.y - 1;
- placement.coorX[markerThree] = placeX;
- placement.coorY[markerThree] = placeY;
- placement.hit[markerThree] = 0;
- markerThree++;
- shipMatrix[placeY][placeX] = placement.length;
- }
- }
- }
- }
- }
- if (multiMark == 1)
- {
- placement.length = 3; //To separate the destroyer and the submarine
- }
- markerFour = 0;
- direction = ' ';
- startX = 0;
- startY = 0;
- placeX = 0;
- placeY = 0;
- }
- while (markerTwo == 0);
- markerTwo = 0;
- markerThree = 0;
- }
- /* takeShot - function to get shot values from user and apply the results
- * inputs -
- * shipMatrix: type - int** (2d array) - creates a matrix holder for current ship map
- * hitMatrix: type - int** (2d array) - creates a matrix holder for current hits map
- * previousShot: type - Coordinate Class - creates a coordinate class holder for users previous shot
- * output -
- * shotAttempt: type - Coordinate Class - returns a coordinate for the users current shot
- */
- Coordinate takeShot(int** shipMatrix, int** hitMatrix, Coordinate &previousShot)
- {
- Coordinate shotAttempt; //Setup variables
- int markerOne = 0;
- int markerTwo = 0;
- int shotX = 0;
- int shotY = 0;
- cout << "Your previous shot was at (" << previousShot.x << "," << previousShot.y << ")" << endl;
- cout << endl;
- do
- {
- cout << "Your turn!" << endl;
- cout << "Enter X shot coordinate:" << endl; //Get user x coordinate input
- do
- {
- cin >> shotAttempt.x;
- cin.clear();
- cin.ignore();
- if (shotAttempt.x < 1 || shotAttempt.x > 10) //Range check x
- {
- cout << "X must be between 1-10" << endl;
- }
- else
- {
- markerOne = 1;
- }
- }
- while (markerOne == 0);
- markerOne = 0;
- cout << "Enter Y shot coordinate:" << endl; //Get user y coordinate input
- do
- {
- cin >> shotAttempt.y;
- cin.clear();
- cin.ignore();
- if (shotAttempt.y < 1 || shotAttempt.y > 10) //Range check y
- {
- cout << "Y must be between 1-10" << endl;
- }
- else
- {
- markerOne = 1;
- }
- }
- while (markerOne == 0);
- markerOne = 0;
- shotX = shotAttempt.x - 1;
- shotY = shotAttempt.y - 1;
- if (hitMatrix[shotY][shotX] == 0) //Check to see if there is already a shot a given position
- {
- if (shipMatrix[shotY][shotX] > 0) //Check to see if its a hit...
- {
- hitMatrix[shotY][shotX] = 2;
- markerTwo = 1;
- shotAttempt.shipNumber = shipMatrix[shotY][shotX];
- }
- else //...or a miss
- {
- hitMatrix[shotY][shotX] = 1;
- markerTwo = 1;
- shotAttempt.shipNumber = 0;
- }
- }
- else //If shot already at given position then notify the user
- {
- cout << "You have already shot there!" << endl;
- }
- }
- while (markerTwo == 0);
- markerTwo = 0;
- cout << endl; //Start map printing function
- cout << " || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- for (int i = 0; i < 10; i++)
- {
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << (i + 1);
- if (i != 9)
- {
- cout << " ";
- }
- cout << "|";
- for (int j = 0; j < 10; j++)
- {
- int holder = hitMatrix[i][j];
- cout << "| ";
- if (holder == 0)
- {
- cout << "-";
- }
- else if (holder == 1)
- {
- cout << "/";
- }
- else if (holder == 2)
- {
- cout << "H";
- }
- cout << " ";
- }
- cout << "|" << endl;
- }
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << endl; //End map printing function
- if (shipMatrix[shotY][shotX] > 0) //Tell user if its hit or miss
- {
- cout << "HIT!" << endl;
- }
- else
- {
- cout << "MISS!" << endl;
- }
- return shotAttempt; //Return the coordinate of current shot
- }
- /* takeShot - function to get shot values from ai and apply the results
- * inputs -
- * shipMatrix: type - int** (2d array) - creates a matrix holder for current ship map
- * hitMatrix: type - int** (2d array) - creates a matrix holder for current hits map
- * previousShot: type - Coordinate Class - creates a coordinate class holder for ais previous shot
- * enemyShotOutput: type - int & - marker for output of ai previous shot type
- * enemyShotHistory:type - Coordinate* array- holds the array of previous 5 ai
- * output -
- * shotAttempt: type - Coordinate Class - returns a coordinate for the users current shot
- */
- Coordinate enemyTakeShot(int** shipMatrix, int** hitMatrix, Coordinate &previousShot, int &enemyShotOutput, Coordinate* enemyShotHistory)
- {
- Coordinate shotAttempt; //Setup variables
- Coordinate previousHit;
- int markerOne = 0;
- int markerTwo = 0;
- int shotX = 0;
- int shotY = 0;
- int directionNum = 0;
- int markerHit = 0;
- previousHit.x = 0;
- previousHit.y = 0;
- previousHit.shipNumber = 0;
- cout << "The enemy previous shot was at (" << previousShot.x << "," << previousShot.y << ")" << endl;
- cout << endl;
- cout << "Enemy turn!" << endl;
- if (enemyShotOutput == 0)
- {
- do
- {
- do
- {
- shotAttempt.x = (rand() % 10) + 1; //Get ship X coordinate
- if (shotAttempt.x > 1 && shotAttempt.x <= 10) //Range check x
- {
- markerOne = 1;
- }
- }
- while (markerOne == 0);
- markerOne = 0;
- do
- {
- shotAttempt.y = (rand() % 10) + 1; //Get ship Y coordinate
- if (shotAttempt.y > 1 && shotAttempt.y <= 10) //Range check y
- {
- markerOne = 1;
- }
- }
- while (markerOne == 0);
- markerOne = 0;
- shotX = shotAttempt.x - 1;
- shotY = shotAttempt.y - 1;
- if (hitMatrix[shotY][shotX] == 0) //Check to see if there is already a shot a given position
- {
- if (shipMatrix[shotY][shotX] > 0) //Check to see if its a hit...
- {
- hitMatrix[shotY][shotX] = 2;
- markerTwo = 1;
- shotAttempt.shipNumber = shipMatrix[shotY][shotX];
- enemyShotOutput = 1;
- }
- else //...or a miss
- {
- hitMatrix[shotY][shotX] = 1;
- markerTwo = 1;
- shotAttempt.shipNumber = 0;
- }
- }
- }
- while (markerTwo == 0);
- markerTwo = 0;
- }
- else if (enemyShotOutput == 1) //If previous shot was a hit with misses before it
- {
- for (int i = 0; i < 5; i++)
- {
- if ((enemyShotHistory[i].shipNumber != 0) && (markerHit == 0))
- {
- markerHit = 1;
- previousHit = enemyShotHistory[i];
- }
- }
- do
- {
- do
- {
- directionNum = (rand() % 4);
- switch (directionNum) //Randomly choose a direction for next shot
- {
- case 0: //North
- shotAttempt.x = previousHit.x;
- shotAttempt.y = previousHit.y - 1;
- break;
- case 1: //East
- shotAttempt.x = previousHit.x + 1;
- shotAttempt.y = previousHit.y;
- break;
- case 2: //South
- shotAttempt.x = previousHit.x;
- shotAttempt.y = previousHit.y + 1;
- break;
- case 3: //West
- shotAttempt.x = previousHit.x - 1;
- shotAttempt.y = previousHit.y;
- break;
- }
- if (shotAttempt.x > 1 && shotAttempt.x <= 10) //Range check x
- {
- markerOne = 1;
- }
- if (shotAttempt.y > 1 && shotAttempt.y <= 10) //Range check y
- {
- markerOne = 1;
- }
- }
- while (markerOne == 0);
- markerOne = 0;
- shotX = shotAttempt.x - 1;
- shotY = shotAttempt.y - 1;
- if (hitMatrix[shotY][shotX] == 0) //Check to see if there is already a shot a given position
- {
- if (shipMatrix[shotY][shotX] > 0) //Check to see if its a hit...
- {
- hitMatrix[shotY][shotX] = 2;
- markerTwo = 1;
- shotAttempt.shipNumber = shipMatrix[shotY][shotX];
- enemyShotOutput = 2;
- }
- else //...or a miss
- {
- hitMatrix[shotY][shotX] = 1;
- markerTwo = 1;
- shotAttempt.shipNumber = 0;
- }
- }
- }
- while (markerTwo == 0);
- markerTwo = 0;
- }
- else if (enemyShotOutput == 2) //If multiple previous shots were hits
- {
- do
- {
- do
- {
- shotAttempt = modalComparisonFunction(enemyShotHistory); //Find next shot
- if (shotAttempt.x > 1 && shotAttempt.x <= 10) //Range check x
- {
- markerOne = 1;
- }
- if (shotAttempt.y > 1 && shotAttempt.y <= 10) //Range check y
- {
- markerOne = 1;
- }
- }
- while (markerOne == 0);
- markerOne = 0;
- shotX = shotAttempt.x - 1;
- shotY = shotAttempt.y - 1;
- if (hitMatrix[shotY][shotX] == 0) //Check to see if there is already a shot a given position
- {
- if (shipMatrix[shotY][shotX] > 0) //Check to see if its a hit...
- {
- hitMatrix[shotY][shotX] = 2;
- markerTwo = 1;
- shotAttempt.shipNumber = shipMatrix[shotY][shotX];
- }
- else //...or a miss
- {
- hitMatrix[shotY][shotX] = 1;
- markerTwo = 1;
- shotAttempt.shipNumber = 0;
- }
- }
- }
- while (markerTwo == 0);
- markerTwo = 0;
- }
- cout << endl; //Start map printing function
- cout << " || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- for (int i = 0; i < 10; i++)
- {
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << (i + 1);
- if (i != 9)
- {
- cout << " ";
- }
- cout << "|";
- for (int j = 0; j < 10; j++)
- {
- int holder = hitMatrix[i][j];
- cout << "| ";
- if (holder == 0)
- {
- cout << "-";
- }
- else if (holder == 1)
- {
- cout << "/";
- }
- else if (holder == 2)
- {
- cout << "H";
- }
- cout << " ";
- }
- cout << "|" << endl;
- }
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << endl; //End map printing function
- cout << "The enemy shot at (" << shotAttempt.x << "," << shotAttempt.y << ")" << endl;
- if (shipMatrix[shotY][shotX] > 0) //Tell user if its hit or miss
- {
- cout << "HIT!" << endl;
- }
- else
- {
- cout << "MISS!" << endl;
- }
- for (int i = 4; i > 0; i--) //Update shot history
- {
- enemyShotHistory[i] = enemyShotHistory[i - 1];
- }
- enemyShotHistory[0] = shotAttempt;
- return shotAttempt; //Return the coordinate of current shot
- }
- /* boardSweep - function that places misses on the board around a sunken ship
- * inputs -
- * sunken: type - Ship Class - creates a ship class holder for newly sunken ship
- * hitMatrix: type - int** (2d array) - creates a matrix holder for current hits map
- * output - NONE
- */
- void boardSweep(Ship &sunken, int** hitMatrix)
- {
- int currentX = 0; //Setup variables
- int currentY = 0;
- for (int i = 0; i < sunken.length; i++) //From start till end of ship
- {
- for (int j = -1; j < 2; j++)
- {
- for (int k = -1; k < 2; k++) //For all squares around a ships square
- {
- currentY = sunken.coorY[i] + j;
- currentX = sunken.coorX[i] + k;
- if (((currentY) >= 0) && ((currentY) < 10) && ((currentX) >= 0) && ((currentX) < 10))
- {
- //If there is no mark there and it is on the board
- if (hitMatrix[currentY][currentX] == 0)
- {
- hitMatrix[currentY][currentX] = 1; //Mark it as a miss
- }
- }
- }
- }
- }
- cout << endl; //Start map printing function
- cout << " || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|" << endl;
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- for (int i = 0; i < 10; i++)
- {
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << (i + 1);
- if (i != 9)
- {
- cout << " ";
- }
- cout << "|";
- for (int j = 0; j < 10; j++)
- {
- int holder = hitMatrix[i][j];
- cout << "| ";
- if (holder == 0)
- {
- cout << "-";
- }
- else if (holder == 1)
- {
- cout << "/";
- }
- else if (holder == 2)
- {
- cout << "H";
- }
- cout << " ";
- }
- cout << "|" << endl;
- }
- cout << "--++---+---+---+---+---+---+---+---+---+---+" << endl;
- cout << endl; //End map printing function
- }
- /* modalComparisonFunction - function that analyses shot history pattern
- * inputs -
- * enemyShotHistory:type - Coordinate array - holds the previous 5 ai shots
- * output -
- * shotAttempt: type - Coordinate Class - holds the decided shot output
- */
- Coordinate modalComparisonFunction (Coordinate *enemyShotHistory)
- {
- int counter = 0; //Setup variables
- int modalNumX = 0;
- int modalCounterX =0;
- int modalNumY = 0;
- int modalCounterY =0;
- int minX = 11;
- int minY = 11;
- int maxX = 0;
- int maxY = 0;
- int sideMarker = 0;
- Coordinate shotAttempt;
- shotAttempt.x = 0;
- shotAttempt.y = 0;
- shotAttempt.shipNumber = 0;
- for (int i = 1; i <= 10; i++) //For 1-10 (board values)
- {
- for (int j = 0; j < 5; j++)
- {
- if ((enemyShotHistory[j].x == i) && (enemyShotHistory[j].shipNumber != 0))
- { //Check if each x matches the value and is a hit
- counter++;
- }
- }
- if (counter > modalCounterX) //Compare for modal value
- {
- modalNumX = i;
- modalCounterX = counter;
- }
- if ((counter != 0) && (minX > i))
- {
- minX = i; //Compare for min value
- }
- if ((counter != 0) && (maxX < i))
- {
- maxX = i; //Compare for max value
- }
- counter = 0;
- }
- for (int i = 1; i <= 10; i++) //For 1-10 (board values)
- {
- for (int j = 0; j < 5; j++)
- {
- if ((enemyShotHistory[j].y == i) && (enemyShotHistory[j].shipNumber != 0))
- { //Check if each y matches the value and is a hit
- counter++;
- }
- }
- if (counter > modalCounterY) //Compare for modal value
- {
- modalNumY = i;
- modalCounterY = counter;
- }
- if ((counter != 0) && (minY > i))
- {
- minY = i; //Compare for min value
- }
- if ((counter != 0) && (maxY < i))
- {
- maxY = i; //Compare for max value
- }
- counter = 0;
- }
- sideMarker = (rand() % 2) + 1; //Randomly select a side to fire at
- if (modalCounterX > modalCounterY)
- {
- shotAttempt.x = modalNumX; //Set x as modal value
- if (sideMarker == 1) //Set y just outside range
- {
- shotAttempt.y = maxY + 1;
- }
- else
- {
- shotAttempt.y = minY - 1;
- }
- }
- else
- {
- shotAttempt.y = modalNumY; //Set y as modal value
- if (sideMarker == 1) //Set x just outside range
- {
- shotAttempt.x = maxX + 1;
- }
- else
- {
- shotAttempt.x = minX - 1;
- }
- }
- return shotAttempt;
- }
- /* placeShip - public Ship Class function that sets values when ship is placed onto board
- * inputs -
- * startX: type - int - value of starting x coordinate for the ship
- * startY: type - int - value of starting y coordinate for the ship
- * directionShip: type - char - char of the compass direction the ship goes from starting coordinate
- * output -
- * errorNum: type - int - returns a numerical value as an error flag
- */
- int Ship::placeShip(int startX, int startY, char directionShip)
- {
- int errorNum = 0; //Setup variables
- int endX = 0;
- int endY = 0;
- startC.setVal(startX, startY); //Set the start coordinate and direction of the ship
- direction = directionShip;
- switch (directionShip) //Calculate end coordinate based on the start and direction of the ship
- {
- case ('N'):
- endY = startY - (length - 1);
- endX = startX;
- break;
- case ('E'):
- endY = startY;
- endX = startX + (length - 1);
- break;
- case ('S'):
- endY = startY + (length - 1);
- endX = startX;
- break;
- case ('W'):
- endY = startY;
- endX = startX - (length - 1);
- break;
- default:
- break;
- }
- if (endX < 1 || endX > 10) //If ship would go out of x range send error 1 back
- {
- errorNum = 1;
- }
- else if (endY < 1 || endY > 10) //If ship would go out of y range send error 2 back
- {
- errorNum = 2;
- }
- else
- {
- endC.setVal(endX, endY); //Else set the end coordinates and send non-error (0) back
- errorNum = 0;
- }
- return errorNum;
- }
- /* hitMark - public Ship Class function that marks when the ship takes damage and checks if sunk
- * inputs -
- * currentShot: type - Coordinate Class - coordinate for current user shot attempt
- * output -
- * sunkResponse: type - int - returns an int as a truth value dependant on if the ship has sunk
- */
- int Ship::hitMark(Coordinate ¤tShot)
- {
- int hitNumber = 0; //Setup variables
- int sunkResponse = 0;
- for (int i = 0; i < 5; i++) //For all the ships hit array
- {
- if ((coorX[i] == (currentShot.x - 1)) && (coorY[i] == (currentShot.y - 1)))
- {
- hit[i] = 1; //If the shot matches a ship coordinate mark hit array
- }
- if (hit[i] == 1)
- {
- hitNumber++; //If the hit array is marked at position add to counter
- }
- }
- if (hitNumber == length) //If the number of hits in the array matches the ship length
- {
- sunkResponse = 1; //Then ship is sunk mark response for feedback
- sunkMarker = 1;
- }
- return sunkResponse;
- }
- /* setVal - public Coordinate Class function that sets the coordinate values
- * inputs -
- * valX: type - int - value of x coordinate to be set
- * valY: type - int - value of y coordinate to be set
- * output - NONE
- */
- void Coordinate::setVal(int valX, int valY)
- {
- x = valX; //Set the coordinate x value as sent x value
- y = valY; //Set the coordinate y value as sent y value
- }
- /* Fixed Issues:
- * 1: Following Up -
- * If the ai hits user ship itll try to fire next to it next
- * shot if it has hit then miss it should try another position
- * next to the hit but it goes back to random fire
- * found - incorrect ai stage flag in place
- * fix - change ai stage flag
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement