-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
390 lines (345 loc) · 10.3 KB
/
Game.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "Game.h"
#include "Board.h"
#include "Player.h"
#include "globals.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <cctype>
#include <algorithm> //is this ok to add?
using namespace std;
struct Ship {
int len;
char sym;
string name;
};
class GameImpl
{
public:
GameImpl(int nRows, int nCols);
int rows() const;
int cols() const;
bool isValid(Point p) const;
Point randomPoint() const;
bool addShip(int length, char symbol, string name);
int nShips() const;
int shipLength(int shipId) const;
char shipSymbol(int shipId) const;
string shipName(int shipId) const;
Player* play(Player* p1, Player* p2, Board& b1, Board& b2, bool shouldPause);
vector<Ship> getShip()
{
return m_addShips;
}
bool attackHelp(Board& bTurn, Board& bNot, Player* pTurn, Player* pNot, bool shouldPause, Player*& winner);
private:
vector<Ship> m_addShips;
vector<char> m_shipSym;
vector<char>::iterator it;
int m_r;
int m_c;
int shipId; //do I need this
//Player* winner = nullptr;
};
void waitForEnter()
{
cout << "Press enter to continue: ";
cin.ignore(10000, '\n');
}
GameImpl::GameImpl(int nRows, int nCols)
{
//set rows and columns
m_r = nRows;
m_c = nCols;
// This compiles but may not be correct
}
int GameImpl::rows() const
{
if (m_r >= 0 && m_r <= MAXROWS)
return m_r;
return -1; // This compiles but may not be correct
}
int GameImpl::cols() const
{
if (m_c >= 0 && m_c <= MAXCOLS)
return m_c;
return -1; // This compiles but may not be correct
}
bool GameImpl::isValid(Point p) const
{
return p.r >= 0 && p.r < rows() && p.c >= 0 && p.c < cols();
}
Point GameImpl::randomPoint() const
{
return Point(randInt(rows()), randInt(cols()));
}
bool GameImpl::addShip(int length, char symbol, string name)
{
//EDIT
if ((length <= m_c || length <= m_r) && length >= 0) //make sure all parameters are valid// n
//symbol cannot be X, o, .
//symbol cannot be previously used
{
if (symbol != 'X' && symbol != 'o' && symbol != '.')
{
Ship shipadded = { length, symbol, name };
//check if symbol is already in symbol vector
it = find(m_shipSym.begin(), m_shipSym.end(), symbol);
if (it == m_shipSym.end())
{
//symbol does not already exist
m_shipSym.push_back(symbol);
m_addShips.push_back(shipadded);
return true;
}
}
}
return false; // This compiles but may not be correct
}
int GameImpl::nShips() const
{
return m_addShips.size();
//return -1; // This compiles but may not be correct
}
int GameImpl::shipLength(int shipId) const
{
if (shipId >= 0 && shipId < m_addShips.size())
return m_addShips[shipId].len;
else
return -1; // This compiles but may not be correct
}
char GameImpl::shipSymbol(int shipId) const
{
if (shipId >= 0 && shipId < m_addShips.size())
return m_addShips[shipId].sym;
else
return '?'; // This compiles but may not be correct
}
string GameImpl::shipName(int shipId) const
{
if (shipId >= 0 && shipId < m_addShips.size())
return m_addShips[shipId].name;
else
return ""; // This compiles but may not be correct
}
bool GameImpl::attackHelp(Board& bTurn, Board& bNot,Player* pTurn, Player* pNot, bool shouldPause, Player*& winner)
{
bool shotHit = false, shipDestroyed = false, wasted = false, validShot = false;
int shipId;
cout << pTurn->name() << "'s turn. Board for " << pNot->name() << ": " << endl;
if (pTurn->isHuman())
bNot.display(true);
else
bNot.display(false);
//Place an attack
//If the attack is valid,
//If shotHit is true, cout << (Player whose turn it is) attacked (point just attacked
Point p = pTurn->recommendAttack();
if (validShot=bNot.attack(p, shotHit, shipDestroyed, shipId) == true)
{
if (shotHit && shipDestroyed)
{
cout << pTurn->name() << " attacked (" << p.r << "," << p.c << "), and destroyed a " << shipName(shipId) << ", resulting in : ";
}
if (shotHit && !shipDestroyed)
{
cout << pTurn->name() << " attacked (" << p.r << "," << p.c << ") and hit something, resulting in: ";
}
if (!shotHit && !shipDestroyed)
{
cout << pTurn->name() << " attacked (" << p.r << "," << p.c << ") and missed, resulting in: ";
}
cout << endl;
}
else
{
wasted = true;
cout << pTurn->name() << " wasted a shot at (" << p.r << "," << p.c << ")." << endl;
if (shouldPause)
{
cout << "Press enter to continue: ";
cin.ignore(100000, '\n');
}
}
if (pTurn->isHuman())
bNot.display(true);
else
bNot.display(false);
if (shouldPause)
{
cout << "Press enter to continue: ";
cin.clear();
cin.ignore(100000, '\n');
//cout << endl << "ignore was run" << endl;
}
/*if (shotHit) {
cout << "shotHit!" << endl;
}*/
pTurn->recordAttackResult(p, validShot, shotHit, shipDestroyed, shipId);
/*if (shouldPause && !wasted)
{
cout << "Press enter to continue: ";
cin.ignore(1000, '\n');
}*/
if (bTurn.allShipsDestroyed())
{
cout << pNot->name() << " is the winner!" << endl;
winner = pNot;
return true;
}
if (bNot.allShipsDestroyed())
{
cout << pTurn->name() << " is the winner!" << endl;
winner = pTurn;
return true;
}
return false;
//code above will loop while allshipsDestroyed for both boards is false
//once breaks out of while loop in play function, if b1 allshipsDestroyed is true, player 2 is winner and vice versa.
}
Player* GameImpl::play(Player* p1, Player* p2, Board& b1, Board& b2, bool shouldPause)
{
//this will be made up of calls from player and board class
// temp variables for p1 and p2
//
// once in while loop, swap vars and this will switch them for each call of while loop
//call placeShips for player 1 and 2
if (p1->placeShips(b1) == false)
{
cout << p1->name() << " was unable to place all their ships.";
return nullptr;
}
if (p2->placeShips(b2) == false)
{
cout << p2->name() << " was unable to place all their ships.";
return nullptr;
}
//once ships are successfully placed, gameplay begins
//use a helper function in a while loop
Player* winner = nullptr;
while (!b1.allShipsDestroyed() && !b2.allShipsDestroyed()) //or while attackHelp == false;
{
if (attackHelp(b1, b2, p1, p2, shouldPause, winner))
break;
attackHelp(b2, b1, p2, p1, shouldPause, winner);
}
return winner;
}
//MAKE SURE TO DELETE
//void Game::dump()
//{
// for (int i = 0; i < nShips(); i++)
// {
// cout << m_impl->getShip().at(i).len << endl;
// cout << m_impl->getShip().at(i).sym << endl;
// cout << m_impl->getShip().at(i).name << endl;
// cout << endl;
// }
//}
//******************** Game functions *******************************
// These functions for the most part simply delegate to GameImpl's functions.
// You probably don't want to change any of the code from this point down.
Game::Game(int nRows, int nCols)
{
if (nRows < 1 || nRows > MAXROWS)
{
cout << "Number of rows must be >= 1 and <= " << MAXROWS << endl;
exit(1);
}
if (nCols < 1 || nCols > MAXCOLS)
{
cout << "Number of columns must be >= 1 and <= " << MAXCOLS << endl;
exit(1);
}
m_impl = new GameImpl(nRows, nCols);
}
Game::~Game()
{
delete m_impl;
}
int Game::rows() const
{
return m_impl->rows();
}
int Game::cols() const
{
return m_impl->cols();
}
bool Game::isValid(Point p) const
{
return m_impl->isValid(p);
}
Point Game::randomPoint() const
{
return m_impl->randomPoint();
}
bool Game::addShip(int length, char symbol, string name)
{
if (length < 1)
{
cout << "Bad ship length " << length << "; it must be >= 1" << endl;
return false;
}
if (length > rows() && length > cols())
{
cout << "Bad ship length " << length << "; it won't fit on the board"
<< endl;
return false;
}
if (!isascii(symbol) || !isprint(symbol))
{
cout << "Unprintable character with decimal value " << symbol
<< " must not be used as a ship symbol" << endl;
return false;
}
if (symbol == 'X' || symbol == '.' || symbol == 'o')
{
cout << "Character " << symbol << " must not be used as a ship symbol"
<< endl;
return false;
}
int totalOfLengths = 0;
for (int s = 0; s < nShips(); s++)
{
totalOfLengths += shipLength(s);
if (shipSymbol(s) == symbol)
{
cout << "Ship symbol " << symbol
<< " must not be used for more than one ship" << endl;
return false;
}
}
if (totalOfLengths + length > rows() * cols())
{
cout << "Board is too small to fit all ships" << endl;
return false;
}
return m_impl->addShip(length, symbol, name);
}
int Game::nShips() const
{
return m_impl->nShips();
}
int Game::shipLength(int shipId) const
{
assert(shipId >= 0 && shipId < nShips());
return m_impl->shipLength(shipId);
}
char Game::shipSymbol(int shipId) const
{
assert(shipId >= 0 && shipId < nShips());
return m_impl->shipSymbol(shipId);
}
string Game::shipName(int shipId) const
{
assert(shipId >= 0 && shipId < nShips());
return m_impl->shipName(shipId);
}
Player* Game::play(Player* p1, Player* p2, bool shouldPause)
{
if (p1 == nullptr || p2 == nullptr || nShips() == 0)
return nullptr;
Board b1(*this);
Board b2(*this);
return m_impl->play(p1, p2, b1, b2, shouldPause);
}