-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
502 lines (447 loc) · 11.6 KB
/
Copy pathgame.cpp
File metadata and controls
502 lines (447 loc) · 11.6 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include "game.h"
Game::Game()//initiates game by deterimining # of players, and creating player objects accordingly
{
string choice="0";
isTwoPlayers=false;
cout << "\033[1;31mWelcome to Battleship!\033[0m" << endl;
cout << "Please choose the number of player(s). (1 or 2): ";
//add code for that here
while (choice!="1" && choice!="2")
{
cin >> choice;//(MJarvis1997) Added input to establish the # of players with bool isTwoPlayers
if (choice == "1")
{
isTwoPlayers = false;
}
else if (choice == "2")
{
isTwoPlayers = true;
}
else
{
cout<<"Invalid input. Please only type in 1 or 2 to specify number of players: ";
}
}
cout<<"The game will now start."<<endl;
makePlayers(isTwoPlayers);
}
//deconstructor?
void Game::printBoard(bool isP1)
{
string temp = "";
if (isP1)
{
temp = p1->getBoard(true);
temp = temp + "\n\n";
temp = temp + p2->getBoard(false);
}
else if (!isP1 && isTwoPlayers)
{
temp = p1->getBoard(false);
temp = temp + "\n\n";
temp = temp + p2->getBoard(true);
}
else if (!isP1 && !isTwoPlayers)
{
cout << "Battlebot is thinking." << endl;
}
cout << temp << endl;
}
void Game::fire(bool isP1)//determines coordinates of fire position(whether from a realPlayer or an AI) and then uses checkHit and updateBoard to make the according changes
//(Mjarvis1997) added bool parameter so that game.fire can be used for both real and computer players
{
int temp;
int temp2;
if (isP1)//(Mjarvis1997) fires for first player
{
cout << "Please choose a location to strike. What are the x and y coordinates? " << endl;
cin >> temp >>temp2;
while(cin.fail())
{
cin.clear();
cin.ignore(256,'\n');
cout<<"Please only input whole numbers for both x and y coordinates."<<endl;
cin >> temp >> temp2;
}
cout << checkHit(temp, temp2, isP1) << endl;
//cout << cleanInput(temp) << endl;
//cout << cleanInput(temp2) << endl;
}
if ((!isP1) && isTwoPlayers)//(Mjarvis1997) fires for real second player
{
cout << "Please choose a location to strike. What are the x and y coordinates? " << endl;
cin >> temp >>temp2;
while(cin.fail())
{
cin.clear();
cin.ignore(256,'\n');
cout<<"Please only input whole numbers for both x and y coordinates."<<endl;
cin >> temp >> temp2;
}
cout << checkHit(temp, temp2, isP1) << endl;
}
else if((!isP1) && !isTwoPlayers)
{
//Randomly picks coordinates
if (searching==false)
{
srand (time(NULL));
temp = rand() % 10 + 1;
srand (time(NULL));
temp2 = rand() % 10 + 1;
if (checkHit(temp, temp2, isP1).compare("hit") == 0)
{
cout << "Battlebot Hit" << endl;
searching = true;
AIhit[0] = temp;
AIhit[1] = temp2;
//Adds surrounding spaces to aiTargets vector
aiSearch();
searching=false;
}
else
{
cout<< "Battlebot Missed"<< endl;
searching = false;
}
}
//
else
{
//choose hits from vector aiTargets
temp = AItargetsX.front();//first value of vector x position
temp2 = AItargetsY.front();//second value of vector y position
AItargetsX.erase(AItargetsX.begin());
AItargetsY.erase(AItargetsY.begin());
if (checkHit(temp, temp2, isP1).compare("hit") == 0)
{
searching = true;
AIhit[0] = temp;
AIhit[1] = temp2;
//Adds surrounding spaces to aiTargets vector
aiSearch();
searching=false;
}
if(AItargetsX.empty() && AItargetsY.empty())
{
searching = false;
}
}
}
}
//(Mjarvis1997) changed function type to string, so it can return the results of the hit, added currentPlayer parameter
//added all the if statements to update the board based on the check
string Game::checkHit(int loc1, int loc2, bool isP1)//(Mjarvis1997) determines if the chosen location is a hit/miss or has been already chosen
{
//used by fire method
if (isP1)
{
if (p2->getBoardValue(loc1, loc2) == 0)//hit water
{
p2->updateBoard(loc1, loc2, 2, 0);//marks missed ship with value of 2
return "miss";
}
if (p2->getBoardValue(loc1, loc2) == 1)//already hit ship here
{
return "already hit";
}
if (p2->getBoardValue(loc1, loc2) == 2)//already missed here
{
return "already miss";
}
if (p2->getBoardValue(loc1, loc2) >= 5)//hit a ship
{
p2->updateBoard(loc1, loc2, 1, p2->getBoardValue(loc1, loc2));//marks hit ship with value of 1
return "hit";
}
}
else
{
if (p1->getBoardValue(loc1, loc2) == 0)//hit water
{
p1->updateBoard(loc1, loc2, 2, 0);//marks missed ship with value of 2
return "miss";
}
if (p1->getBoardValue(loc1, loc2) == 1)//already hit ship here
{
return "already hit";
}
if (p1->getBoardValue(loc1, loc2) == 2)//already missed here
{
return "already miss";
}
if (p1->getBoardValue(loc1, loc2) >= 5)//hit a ship
{
p1->updateBoard(loc1, loc2, 1, p1->getBoardValue(loc1, loc2));//marks hit ship with value of 1
return "hit";
}
}
return "error bool isP1 not assigned properly";
}
//uses set method in player class modifyShips((int x, int y, string orientation, int shipType)
//if parameter "player" is true, then player 1's board is being modified
//otherwise if "player" is false, then player 2's board is being modified
//assumes that player is inputting anchorpoints (x,y) in form "x y". Does not assume valid input
void Game::placeShips(bool player, bool isTwoPlayers)
{
int anchorPointX;
int anchorPointY;
int shipsPlacedCounter = 0;
string orientation;
bool validPlacement = false;
int temp;
if(isTwoPlayers)
{
if (player)
{
cout<<"Player 1, please place your ship."<<endl;
while (shipsPlacedCounter < 5)
{
validPlacement = false;
printBoard(true);
while (!validPlacement)
{
cout << "Place your ship of length " << p1->getPlayerShips(shipsPlacedCounter) << ". What are the x and y coordinates? " << endl;
cin >> anchorPointX >> anchorPointY;
while(cin.fail())
{
cin.clear();
cin.ignore(256,'\n');
cout<<"Please only input whole numbers for both x and y coordinates."<<endl;
cin >> anchorPointX >> anchorPointY;
}
cout << "What is the orientation of the ship? (up, down, left, right, lower case only)" << endl;
cin >> orientation;
validPlacement = p1->modifyShips(anchorPointX, anchorPointY, orientation, p1->getPlayerShipTypes(shipsPlacedCounter), false);
}
shipsPlacedCounter++;
}
}
else
{
cout<<"Player 2, please place your ship."<<endl;
while (shipsPlacedCounter < 5)
{
validPlacement = false;
printBoard(false);
while (!validPlacement)
{
cout << "Place your ship of length " << p2->getPlayerShips(shipsPlacedCounter) << ". What are the x and y coordinates? " << endl;
cin >> anchorPointX >> anchorPointY;
while(cin.fail())
{
cin.clear();
cin.ignore(256,'\n');
cout<<"Please only input whole numbers for both x and y coordinates."<<endl;
cin >> anchorPointX >> anchorPointY;
}
cout << "What is the orientation of the ship? (up, down, left, right, lower case only)" << endl;
cin >> orientation;
validPlacement = p2->modifyShips(anchorPointX, anchorPointY, orientation, p2->getPlayerShipTypes(shipsPlacedCounter), false);
}
shipsPlacedCounter++;
}
}
}
else if(!isTwoPlayers)
{
if (player)
{
cout<<"Player 1, please place your ship."<<endl;
while (shipsPlacedCounter < 5)
{
validPlacement = false;
printBoard(true);
while (!validPlacement)
{
cout << "Place your ship of length " << p1->getPlayerShips(shipsPlacedCounter) << ". What are the x and y coordinates? " << endl;
cin >> anchorPointX >> anchorPointY;
while(cin.fail())
{
cin.clear();
cin.ignore(256,'\n');
cout<<"Please only input whole numbers for both x and y coordinates."<<endl;
cin >> anchorPointX >> anchorPointY;
};
cout << "What is the orientation of the ship? (up, down, left, right, lower case only)" << endl;
cin >> orientation;
validPlacement = p1->modifyShips(anchorPointX, anchorPointY, orientation, p1->getPlayerShipTypes(shipsPlacedCounter), false);
}
shipsPlacedCounter++;
}
}
else
{
while(shipsPlacedCounter < 5)
{
validPlacement = false;
while(!validPlacement)
{
srand (time(NULL));
anchorPointX = rand() % 10 + 1;
srand (time(NULL));
anchorPointY = rand() % 10 + 1;
srand (time(NULL));
temp = rand() % 4;
switch(temp)
{
case 0: orientation = "up";
break;
case 1: orientation = "down";
break;
case 2: orientation = "left";
break;
case 3: orientation = "right";
break;
}
validPlacement = p2->modifyShips(anchorPointX, anchorPointY, orientation, p2->getPlayerShipTypes(shipsPlacedCounter), true);
}
shipsPlacedCounter++;
}
}
}
}
void Game::makePlayers(bool isTwoPlayers)//(Mjarvis1997)changed pType to isTwoPlayers
{
//for making comp player
//(Mjarvis1997) moved this here from game function to consolidate all the player creation to this makePlayer function
if (isTwoPlayers) // 2 players, else is 1 and comp
{
string temp;
cout << "Player 1 enter name: " << endl;
cin >> temp;
p1 = new Player(temp);
temp = "";
cout << "Player 2 enter name: " << endl;
cin >> temp;
p2 = new Player(temp);
}
else//(MJarvis1997)Filled in this else statement to create 1 player of inputted name, and also to create an AI player object
{
string temp;
cout << "Player 1 enter name:" << endl;
cin >> temp;
p1 = new Player(temp);
temp = "";
cout << "A 'BattleBot' has been created for you to test your skills against!";
p2 = new Player("BattleBot");
}
}
//everytime taking in cord info as string
int Game::cleanInput(string input, int range)
{
int t1;
int t2;
//does not yet check if string num is num 1-10
t1 = input[0];
t1 -= 48;
if (input.size() > 1)
{
t2 = input[1];
t2 -= 48;
t1 *= 10;
t1 += t2;
}
if (t1 < 1 || t1 > range)
t1 = -1;
return t1;
}
//used by bship to run game
void Game::gRound(bool isP1) //true = p1
{
//clear board method
//cout << "\033[2J\033[1;1H";?
if(isP1)
cout << "\033[1;32mPlayer 1's turn!\033[0m" << endl;
else
cout << "\033[1;34mPlayer 2's turn!\033[0m" << endl;
if(isTwoPlayers)
{
cout << "Input any key when ready!" << endl;
string x;
cin >> x;//maybe make not sucky
printBoard(isP1);
fire(isP1);
}
//else for a.i.
else
{
cout << "Input any key when ready!" << endl;
string x;
cin >> x;//maybe make not sucky
printBoard(isP1);
fire(isP1);
}
}
void Game::gControl() //true = p1
{
bool playerBool = true;
//some ship place method here
//for 2players
if(isTwoPlayers)
{
placeShips(true,true);
placeShips(false,true);
}
//vs AI
else
{
placeShips(true,false);
placeShips(false,false);
}
while (p1->isWin() == false && p2->isWin() == false)
{
gRound(playerBool);
if (playerBool == true)
playerBool = false;
else
playerBool = true;
}
if (p1->isWin() == true) //true means lose
{
cout << "player 2 wins!\n";
}
else
{
cout << "player 1 wins!\n";
}
delete p1;
delete p2;
}
void Game::aiSearch()
{
int x = AIhit [0];
int y = AIhit [1];
if (x+1<11 && x+1>0)
{
if(p1 ->getBoardValue(x+1,y) == 0 || p1 ->getBoardValue(x+1,y))
{
AItargetsX.push_back(x+1);
AItargetsY.push_back(y);
}
}
if (x-1<11 && x-1>0)
{
if(p1 ->getBoardValue(x-1,y) == 0 || p1 ->getBoardValue(x-1,y) >= 5)
{
AItargetsX.push_back(x-1);
AItargetsY.push_back(y);
}
}
if (y+1<11 && y+1>0)
{
if(p1 ->getBoardValue(x,y+1) == 0 || p1 ->getBoardValue(x,y+1) >= 5)
{
AItargetsX.push_back(x);
AItargetsY.push_back(y+1);
}
}
if (y-1<11 && y-1>0)
{
if(p1 ->getBoardValue(x,y-1) == 0 || p1 ->getBoardValue(x,y-1) >= 5)
{
AItargetsX.push_back(x);
AItargetsY.push_back(y-1);
}
}
}