forked from defnottyler/Team7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.h
545 lines (488 loc) · 11.9 KB
/
Board.h
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//#ifndef
//#def SOMENAME
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <ctime>
#include "Cards.h"
//#include "UnitCard.h"
//#include "SpecialCard.h"
//No longer blows up on compilation.
using namespace std;
class BoardRow
{
public:
//BoardRow(int pos);
BoardRow();
vector<UnitCard*> cards;
void setRow(int pos);
void applyModifier(int effect);
int getRowStr();
private:
void deBuff();
void buff();
void clear();
//void calcStr();
int rowPosition;
bool buffed;
bool deBuffed;
int rowStrength;
};
BoardRow::BoardRow()
{
rowStrength = 0;
}
void BoardRow::setRow(int pos)
{
rowPosition = pos;
}
void BoardRow::applyModifier(int effect)
{
switch (effect)
{
case 0:
clear();
break;
case 1:
buff();
break;
case 2:
deBuff();
break;
}
}
void BoardRow::clear()
{
rowStrength = 0;
for (int i = 0; i < cards.size(); i++)
{
if (!cards.at(i)->isHero)
cards.at(i)->setStrength(cards.at(i)->strength);
rowStrength += cards.at(i)->getStrength();
}
buffed = false;
deBuffed = false;
}
void BoardRow::deBuff()
{
rowStrength = 0;
for (int i = 0; i < cards.size(); i++)
{
if (!cards.at(i)->isHero && !buffed)
cards.at(i)->setStrength(1);
else if (!cards.at(i)->isHero && buffed)
cards.at(i)->setStrength(2);
rowStrength += cards.at(i)->getStrength();
}
deBuffed = true;
}
void BoardRow::buff()
{
rowStrength = 0;
for (int i = 0; i < cards.size(); i++)
{
if (!cards.at(i)->isHero && !deBuffed)
cards.at(i)->setStrength(cards.at(i)->strength * 2);
else if (!cards.at(i)->isHero && deBuffed)
cards.at(i)->setStrength(2);
rowStrength += cards.at(i)->getStrength();
}
}
int BoardRow::getRowStr()
{
return rowStrength;
}
class Board
{
public:
public:
Board(); //Constructor starts entire game and configures board.
void handGenerator();
int chooseTurn();
void printHand(int p);
void printBoard(int p);
void playRound(int *p1Score, int *p2Score);
void playerOneTurn();
void playerTwoTurn();
void playCard(int index, vector<Card*> playerHand); //Puts a card on the field and changes player's turn
void displayTurnOptions();
//void startGame();
//void startRound(); //Called at start of each round
//void endOfRound(); //Called when both players pass/run out of cards. Compares strength
//void endOfGame(); //Called when one or both player's points hit zero
//void printBoard();
private:
BoardRow playerOneRows[3];
BoardRow playerTwoRows[3];
vector<Card*> playerOneDeck;
vector<Card*> playerTwoDeck;
vector<Card*> playerOneHand;
vector<Card*> playerTwoHand;
vector<Card*> playerOneGrave;
vector<Card*> playerTwoGrave;
int p1TotalStrength;
int p2TotalStrength;
int boardMod;
bool isFirstRound;
int firstTurnChoice;
int playerTurn;
int roundCount;
bool p1Pass;
bool p2Pass;
int turnOption;
void initializeDecks(string filename, bool p);
//void pullHand(); //Fills each hand with 10 cards at start of game
//void killCards(); //Places cards in used pile
//void changeModifier();
};
Board::Board()
{
for (int i = 0; i < 3; i++)
{
playerOneRows[i].setRow(i);
playerTwoRows[i].setRow(i);
}
p1TotalStrength = 0;
p2TotalStrength = 0;
boardMod = 0;
isFirstRound = true;
initializeDecks("PlayerOneDeck.txt", true);
initializeDecks("PlayerTwoDeck.txt", false);
cout << "Loading Decks from Cards.txt" << endl;
handGenerator();
}
void Board::initializeDecks(string filename, bool p)
{
ifstream d_one(filename);
string line;
string name;
bool hero = false;
int strength, ability, range;
int index = 5;
while (getline(d_one, line))
{
if (line.substr(0, 4) == "true")
{
index = line.find(" ", 5);
name = line.substr(5, index - 5);
index++;
strength = stoi(line.substr(index, 2));
index = line.find(" ", index) + 1;
range = stoi(line.substr(index, 1));
index = line.find(" ", index) + 1;
ability = stoi(line.substr(index, 1));
index = line.find(" ", index) + 1;
if (line.substr(index, 4) == "true")
hero = true;
//UnitCard card(range, ability, hero, name, strength);
if (p)
playerOneDeck.push_back(new UnitCard(range, ability, hero, name, strength));
else
playerTwoDeck.push_back(new UnitCard(range, ability, hero, name, strength));
//playerOneDeck.push_back(card);
}
else
{
index = line.find(" ", 6);
name = line.substr(6, index - 6);
ability = stoi(line.substr(index + 1, 1));
if (p)
playerOneDeck.push_back(new SpecialCard(ability, name));
else
playerTwoDeck.push_back(new SpecialCard(ability, name));
}
}
d_one.close();
}
void Board::handGenerator() //puts cards from deck into player hand(s)
{
srand(time(NULL));
int deckSize1 = playerOneDeck.size();
int deckSize2 = playerTwoDeck.size();
int index1;
int index2;
for (int i = 0; i<10; i++) {
index1 = rand() % deckSize1;
index2 = rand() % deckSize2;
playerOneHand.push_back(playerOneDeck.at(index1));
playerOneDeck.erase(playerOneDeck.begin() + index1 - 1);
deckSize1--;
playerTwoHand.push_back(playerTwoDeck.at(index2));
playerTwoDeck.erase(playerTwoDeck.begin() + index2 - 1);
deckSize2--;
//hands are now generated
}
}
int Board::chooseTurn()
{
srand(time(NULL));
firstTurnChoice = (rand() % 2) + 1;
return firstTurnChoice;
}
/* JEFF's CODE- REPLACED WITH TYLER's CODe
void Board::play()
{
//cout << "play reached";
if (isFirstTurn)
{
playerTurn = chooseTurn();
}
//doublechecking
p1Points = 0;
p2Points = 0;
int mostPoints; //determines who wins the game
do
{
//BEGINNING OF ROUND
//set player turn
(playerTurn == 2) ? playerTurn = 1 : playerTurn = 2;
//clear board
roundCount += 1;
p1TotalStrength = 0; // This may not be neccessary
p2TotalStrength = 0; // This may not be nececcesary
int currentPlayerTurn;
bool lastTurn = false;
bool passOnTurn = false;
do
{
//BEGINNING OF A TURN
//flip turn
(playerTurn == 2) ? playerTurn = 1 : playerTurn = 2;
if (passOnTurn)
lastTurn = true;
if (playerTurn == 1) {
passOnTurn = playerOneTurn();
}
else if (playerTurn == 2) {
passOnTurn = playerTwoTurn();
}
} while (!lastTurn);
//determine winner of round and award point?
if (p1TotalStrength < p2TotalStrength) {
cout << "Player 2 wins the round" << endl;
p2Points += 1;
}
else if (p1TotalStrength == p2TotalStrength)
{
cout << "\nTie game" << endl;
p1Points += 1;
p2Points += 1;
}
else
{
cout << "Player 1 wins the round" << endl;
p1Points += 1;
}
//determine who has the most Points andh:400:2: error: ‘p1Pass’ was not declared in this scope
p1Pass = false;
^
Board.h:401:2: error: ‘p2Pass’ was not declared in this scope
p2Pass = false;
^
Board.h:402:6: error: ‘isFirstRound’ was not declared in this scope
if (isFirstRound) {
^
Board.h: In member function ‘void Board::playerOneTurn()’:
Board.h:442:9: error: ‘turnOption’ was not declared in this scope
cin >> turnOption;
^
Board.h:449:3: error: ‘p1Pass’ was not declared in this scope
p1Pass = true;
^
Board.h: In member function ‘void Board::playerTwoTurn()’:
Board.h:458:9: error: ‘turnOption’ was not declared in this scope
cin >> turnOption;
^
Board.h:465:3: error: ‘p2Pass’ was not declared in this scope
p2Pass = true;
if this is enough to win the game
(p1Points > p2Points) ? mostPoints = p1Points : mostPoints = p2Points;
} while (mostPoints < POINTSTOWIN);
//game ending criteria has been met. Declare the winner
if (mostPoints == p1Points && mostPoints == p2Points)
{
cout << "Players have tied the game!" << endl;
}
else if (mostPoints == p1Points)
{
cout << "Player 1 has won the game" << endl;
}
else
{
cout << "Player 2 has won the game" << endl;
}
}
//returns 1 if pass, 0 if normal
int Board::playerOneTurn()
{
//print_board();
int playerOneOption;
do {
playerOneOption = -1; //reset the user's choice
cout << "Choose a card or Pass." << endl;
//turnOptions(); //prints out a list of options the player can do during his turn
cin >> playerOneOption; //takes in the option the player chose
if(playerOneOption > 0) { // 1 is play card
//playCard(playerOneOption); // remove card from hand and place onto appropraite position on the board
return 1;
}
else if(playerOneOption == 0) { // 2 is pass
return 0; //Indicates this turn is a pass
}
else {
cout << "Not a valid option!" << endl;
}
} while(playerOneOption < 0 )
}
int Board::playerTwoTurn()
{
print_board();
int playerTwoOption;
do {
playerTwoOption = -1; //reset the user's choice
cout << "Choose a card or Pass." << endl;
//turnOptions(); //prints out a list of options the player can do during his turn
cin >> playerTwoOption; //takes in the option the player chose
if(playerTwoOption > 0) {
//playCard(playerTwoOption);
return 1;
}
else if(playerTwoOption == 0) { // 2 is pass
return 0;
}
else {
cout << "Not a valid option!" << endl;
}
} while(playerTwoOption < 0)
}
*/
void Board::displayTurnOptions()
{
cout << "Choose one of the following options:" << endl;
cout << "1) Play a card" << endl;
cout << "2) Pass turn" << endl;
cout << "Player Option: ";
}
void Board::playRound(int *p1Score, int *p2Score)
{
int roundWinner;
p1Pass = false;
p2Pass = false;
if (isFirstRound) {
playerTurn == chooseTurn();
if (playerTurn == 1) {
playerOneTurn();
playerTurn = 2;
}
else {
playerTwoTurn();
playerTurn = 1;
}
isFirstRound = false;
}
do {
if (playerTurn == 1) {
playerOneTurn();
}
else {
playerTwoTurn();
}
if (p1Pass && !p2Pass) {
while (!p2Pass)
playerTwoTurn();
}
if (p2Pass && !p1Pass) {
while (!p1Pass)
playerOneTurn();
}
} while(!p1Pass && !p2Pass);
if (p1TotalStrength > p2TotalStrength)
*p1Score++;
else
*p2Score++;
}
void Board::playerOneTurn()
{
int cardIndex;
displayTurnOptions();
cin >> turnOption;
if (turnOption == 1) {
cout << "Select Card to play: ";
cin >> cardIndex;
playCard(cardIndex, playerOneHand);
}
else
p1Pass = true;
playerTurn = 2;
}
void Board::playerTwoTurn()
{
int cardIndex;
displayTurnOptions();
cin >> turnOption;
if (turnOption == 1) {
cout << "Select Card to play: ";
cin >> cardIndex;
playCard(cardIndex, playerTwoHand);
}
else
p2Pass = true;
playerTurn = 1;
}
//This method is still getting an error, can someone help!?
//Polymorphism problem...since Player hand elements
void Board::playCard(int index, vector<Card*> playerHand) {
int cardRow;
UnitCard *currentCard;
if(playerHand.at(index) -> isUnit) {
currentCard = playerHand.at(index);
}
}
void Board::printBoard(int p)
{
if(p==1){
cout<<"~~~~~~~~~~~~GAMING BOARD~~~~~~~~~~~~\n";
cout<<"Other player's Siege cards: \n";
//display playerTwoRows[2]
cout<<"Other player's Ranged cards: \n";
//display playerTwoRows[1]
cout<<"Other player's Close Combat cards: \n";
//display playerTwoRows[0]
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"Your Close Combat cards: \n";
//display playerOneRows[0]
cout<<"Your Ranged cards: \n";
//display playerOneRows[1]
cout<<"Your Siege cards: \n";
//display playerOneRows[2]
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"Your hand: (Name/Type/Abilit/isHero/Strength)\n";
for (int i = 0; i < playerOneHand.size(); i++)
{
playerOneHand.at(i)->toString();
}
}
else{
cout<<"~~~~~~~~~~~~GAMING BOARD~~~~~~~~~~~~\n";
cout<<"Other player's Siege cards: \n";
//display playerTwoRows[2]
cout<<"Other player's Ranged cards: \n";
//display playerTwoRows[1]
cout<<"Other player's Close Combat cards: \n";
//display playerTwoRows[0]
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"Your Close Combat cards: \n";
//display playerOneRows[0]
cout<<"Your Ranged cards: \n";
//display playerOneRows[1]
cout<<"Your Siege cards: \n";
//display playerOneRows[2]
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"Your hand: (Name/Type/Abilit/isHero/Strength)\n";
for (int i = 0; i < playerTwoHand.size(); i++)
{
playerTwoHand.at(i)->toString();
}
}
}