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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#include "gameboard.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cctype>
using std::cout;
using std::endl;
using std::cin;
using std::setw;
using std::toupper;
/*****************************************************************
Function: set_Gameboard()
Use: Initializes the elements of the array to an empty state.
Arguments: None.
*****************************************************************/
void Gameboard::set_Gameboard()
{
int water[GRIDSIZE][GRIDSIZE];
// Function to initialize the grid to empty space.
for (int row=0; row <GRIDSIZE; row++)
{
for (int col=0; col <GRIDSIZE; col++)
{
water[row][col] = EMPTY;
}
}
}
/*****************************************************************
Function:set_randomPieces()
Use: Randomly places the pieces for the computer
Arguments: None
******************************************************************/
int Gameboard::set_randomPieces()
{
srand(time(0));
int row, col;
int water[GRIDSIZE][GRIDSIZE];
//Randomly placing ships within grid.
for(int shipNum = 0; shipNum < NUM_SHIPS; shipNum++)
{
bool randomset = true;
while (randomset)
{
row = rand() % GRIDSIZE;
col = rand() % GRIDSIZE;
if (water[row][col] == EMPTY)
{
water[row][col] = SHIPS;
randomset = false;
}
else
randomset = true;
}
}
}
/*****************************************************************
Functions:set_playerPieces()
Use: Method that allows the player to manually set his/her pieces
Arguments: None
*****************************************************************/
int Gameboard::set_playerPiece()
{
int lrow, ncol;
char letter = 'A';
int plyrPiece;
cout <<"It is now time to place your ships within the grid." <<endl;
cout <<"You will be able to place " <<NUM_SHIPS <<"within the grid." <<endl;
cout <<"Enter the row number first(0-7):" <<endl;
cin >> lrow;
cout <<"Enter the column letter next(A-H):" <<endl;
cin >> letter;
//Placing the players ship in the row
for (int i = 0; i < NUM_SHIPS; i++)
{
bool plyrPiece = true;
while (plyrPiece)
{
if (lrow < 0 && lrow > GRIDSIZE)
{
cout <<"No, No, No, you can't do that" <<endl;
plyrPiece = false;
}
else
plyrPiece = true;
}
}
//Ship placement loop
for (int j = 0; j < NUM_SHIPS; j++)
{
while (plyrPiece)
{
if (ncol < 0 && ncol > GRIDSIZE)
{
cout <<"No, No, No, you can't do that" <<endl;
plyrPiece = false;
}
else
plyrPiece = true;
}
}
//Letter to number conversion
ncol = toupper(letter);
}
/****************************************************************
Functions:printGrid()
Use: Method that prints out the grids and bombs dropped
Arguments: None
*****************************************************************/
void Gameboard::printGrid(const char gameBoard& computerBoard) const
{
cout <<setw(5) <<"Your Bomb" << setw(5) << "Your Navy" <<endl;
//prints out the column letters on top.
for (int j = 0, j <1, j++)
{
for (int column ='A', column-'A'<GRIDSIZE, column++)
{
cout <<column <<endl;
// loop row
for (int x=0, x<GRIDSIZE, x++)
{
if (computerBoard.water[row][col] == SHIPS)
cout <<EMPTY;
else
cout <<computerBoard.water[row][col] <<endl;
// loop column
for (int y=0, y<GRIDSIZE, y++)
{
if (computerBoard.water[row][col] == SHIPS)
cout <<EMPTY;
else
cout <<computerBoard.water[row][col] <<endl;
}
}
}
}
cout <<endl <<endl;
}
/****************************************************************
Functions:player_bombsDropped()
Use: Method that allows the player to enter coordinates
to drop bombs on the computer opponent.
Arguments: None.
****************************************************************/
int Gameboard::player_bombsDropped()
{
int row,col;
char letter;
cout <<"Bombing coordinates please(Row,Column): " <<endl;
cin >> row;
cout <<endl;
cin >> letter;
cout <<endl;
//Loop to see if row was a hit or miss
if (row == SHIPS && row > 0 && row <GRIDSIZE)
cout <<"Nice shot! That was a hit!" <<endl;
else if (row == EMPTY && row > 0 && row <GRIDSIZE)
cout <<"That was a miss, sir" <<endl;
else
cout <<"Can not place coordinate outside the grid" <<endl;
//Loop to see if column was a hit or miss
if (col == SHIPS && col > 'A' && col <GRIDSIZE)
cout <<"Nice shot! That was a hit!" <<endl;
else if (col == EMPTY && col > 'A' && col <GRIDSIZE)
cout <<"That was a miss, sir" <<endl;
else
cout <<"Can not place coordinate outside the grid" <<endl;
//Letter to number conversion
}
/****************************************************************
Functions:computer_bombsDropped()
Use: Method that allows the computer to chose coordinates to
drop bombs on his/her opponent.
Arguments: None.
*****************************************************************/
int Gameboard::computer_bombsDropped()
{
int row, col;
char letter;
cout <<"Now it is computer's Turn!" <<endl;
//Loop for random row pick
if (row == SHIPS && row > 0 && row <GRIDSIZE)
cout <<"The computer has hit one of our ships!" <<endl;
else if (col == EMPTY && col > 'A' && col <GRIDSIZE)
cout <<"The computer has missed!" <<endl;
else
cout <<"The computer has chosen coordinates outside the grid" <<endl;
//Letter to number conversion
//Random generator
row = rand() % GRIDSIZE;
col = rand() % GRIDSIZE;
};
|