1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
//This takes in a row and a col
//it then checks to see if the guess is a hit or not
//if there is not a zero at that place it has to be
//a 1 or a 2, I will consider that to be a hit
//my version of the game does not take into account
//whether the user hits the boat at the same place
//more then once
bool isHit(int row, int col)
{
if(board[row][col] = 1)
{
board[row][col] = 2;
return true;
}
else
{
return false;
}
}
//Checks the entire board
//if there are any 1's there is boat still on the board that is not yet hit
//so return false if it finds a 1, otherwise the game is over
bool isGameWon()
{
for(int row = 0; row < 10; row++)
for(int col = 0; col < 10; col++)
if(board[row][col] = 1)
{
return false; //game is not yet won
}
return true;
}
//Checks to see if this particular ship is sunk by checking to see
//if the board at this ship's position is all 2s.
bool isShipSunk(Ship ship, int startRow, int startCol, int size, ShipDirection shipDir)
{
if(shipDir == VERTICAL)
{
if(startRow + size > 10)
{
return false;
}
for(int row = startRow; row < startRow+size; row++)
{
if(board[row][startCol] !=2)
{
return false;
}
}
}
else if(shipDir==HORIZONTAL)
{
if(startCol + size > 10)
{
return false;
}
for(int col = startCol; col < startCol+size; col++)
{
if(board[startRow][col] !=2)
{
return false;
}
}
}
return true;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
//Seed the random number generator with the current time
srand((unsigned int) time(0));
char guessRow;
int guessCol;
char playAgain();
char keepPlaying;
bool isGameWon;
bool isHit;
//While the player intends to keep playing
do
{
//Create each of the five ships
Ship carrier = Ship(5, "Carrier");
Ship battleship = Ship(4, "Battleship");
Ship submarine = Ship(3, "Submarine");
Ship destroyer = Ship(3, "Destroyer");
Ship patrolboat = Ship(2, "Patrol boat");
//Create the board
Board board = Board();
//Place the ships on the board
board.placeShip(carrier);
board.placeShip(battleship);
board.placeShip(submarine);
board.placeShip(destroyer);
board.placeShip(patrolboat);
//print the board with the ships on it temporarilly for testing, when the game is finished I will take this out.
board.printBoard();
do
{
//Asks User for Coordinates of their choice.
cout << "\n\nPlease enter your coordinates: \n";
cout << "Enter row: ";
cin >> guessRow;
guessRow = toupper(guessRow);
cout << "Enter column: ";
cin >> guessCol;
cout << endl;
isHit=board.isHit(guessRow-'A',guessCol);
if (isHit)
{
cout << "Its a hit \n";
}
else
{
cout << "It was a miss! \n";
}
//check each ship to see if it was sunk.
if(board.isShipSunk(carrier,carrier.getStartRow(),carrier.getStartCol(),carrier.getNumberOfHits(),carrier.getShipDir()))
{ cout << "You sunk my Carrier!"; }
if(board.isShipSunk(battleship,battleship.getStartRow(),battleship.getStartCol(),battleship.getNumberOfHits(),battleship.getShipDir()))
{ cout << "You sunk my Battleship!"; }
if(board.isShipSunk(submarine,submarine.getStartRow(),submarine.getStartCol(),submarine.getNumberOfHits(),submarine.getShipDir()))
{ cout << "You sunk my Submarine!"; }
if(board.isShipSunk(destroyer,destroyer.getStartRow(),destroyer.getStartCol(),destroyer.getNumberOfHits(),destroyer.getShipDir()))
{ cout << "You sunk my Destroyer!"; }
if(board.isShipSunk(patrolboat,patrolboat.getStartRow(),patrolboat.getStartCol(),patrolboat.getNumberOfHits(),patrolboat.getShipDir()))
{ cout << "You sunk my Partrol boat!"; }
//do that on all 5 ships
isGameWon=board.isGameWon();
}
while (!isGameWon);
//ask the player if he or she intends to keep playing
keepPlaying = playAgain();
if ( keepPlaying == 'N')
{
cout << "Goodbye \n";
system("pause");
return 0;
}
}
while (keepPlaying == 'Y');
}
//play again
char playAgain()
{
char keepPlaying;
cout << "Would you like to play again? (y or n) ";
do
{
cin >> keepPlaying;
keepPlaying = toupper(keepPlaying);
if (keepPlaying!='Y' && keepPlaying != 'N')
cout << "Enter only a 'Y' or an 'N', please!" << endl << endl;
} while ( keepPlaying !='Y' && keepPlaying != 'N');
return keepPlaying;
}
|