forked from itsJonnyJyo/Chicken-Feet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
310 lines (295 loc) · 7.27 KB
/
game.cpp
File metadata and controls
310 lines (295 loc) · 7.27 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
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <vector>
#include "game.h"
#include "yard.h"
#include "player.h"
#include "hand.h"
#include "field.h"
using namespace std;
const int MAXBONES = 7;
Game::Game(): turn(0), nPlayers(0), rounds(1), currTurn(0), doublePlayed(false), doublePlays(0){
}
Game::~Game()
{
for(auto it = players.begin(); it != players.end(); it++){
delete *it;
}
}
int Game::nextTurn(){
currTurn++;
currTurn = currTurn % nPlayers;
return currTurn;
}
void Game::run(){
char ch;
cout << "Welcome to Chicken-Feet the game!!" << endl << endl;
cout << "Please enter the number of players between 2 and 4: ";
cin >> nPlayers;
while(nPlayers < 2 || nPlayers > 4)
{
cin.clear();
cin.ignore();
cout << endl << "Invalid number! Please enter a number between 2 and 4: ";
cin >> nPlayers;
}
for(int i = 0; i < nPlayers; i++)
{
players.push_back(new Player());
}
cout << endl << "Creating " << nPlayers << " players and dealing " << MAXBONES << " bones each!" << endl << endl;
Yard * deck = new Yard(1);
Field *field = new Field();
deck->shuffleDeck();
for(int i=0; i < MAXBONES; i++){
for(int j = 0; j < nPlayers; j++){
players.at(j)->drawBone(deck);
}
}
while(!firstMove(field)){
cout << "No one has a double!" << endl;
cout << "Everybody draws a bone!" << endl;
for(int i = 0; i < nPlayers; i++){
players.at(i)->drawBone(deck);
}
}
do{
cout << "Player " << getTurn() + 1 << ", it's your turn. Your hand is:" << endl << endl;
players.at(getTurn())->getHand();
display();
cout << endl << "Make a selection from the menu above: ";
cin >> ch;
options(ch, deck, field);
if(toupper(ch) == 'P' || toupper(ch) == 'D')
nextTurn();
}while(toupper(ch) != 'Q' || gameOver());
}
int Game::getTurn(){
return currTurn;
}
void Game::display(){
cout << "---------------------------" << endl;
cout << "Press (H) to show hand" << endl;
cout << "Press (P) to play a Bone" << endl;
cout << "Press (D) to draw a Bone" << endl;
cout << "Press (S) to see your Score" << endl;
cout << "Press (F) to see the field" << endl;
cout << "Press (Q) to quit" << endl;
cout << "---------------------------" << endl;
}
void Game::options(char ch, Yard * deck, Field *field){
switch(toupper(ch)){
case 'H':
cout << endl << "Bones in hand of player " << currTurn + 1 << " are:" << endl << endl;
players.at(currTurn)->getHand();
break;
case 'P':
gamePlay(field);
break;
case 'D':
cout << "you drew a ";
players.at(currTurn)->drawBone(deck);
players.at(currTurn)->getDraw();
break;
case 'S':
//cout << "your current score is " << endl;
players.at(currTurn)->getScore();
break;
case 'F':
field->print();
break;
case 'Q':
cout << "Thank you for playing!" << endl;
break;
default:
cout << endl << "Please enter a valid command." << endl << endl;
}
}
void Game::gamePlay(Field * field)
{
int domino, side;
Bone* aBone = new Bone();
if(doublePlayed)
{
cout << "The last domino played was a double, the only available move is: "
<< doubleValue
<< endl;
}
else
{
field->print();
}
cout << "Enter domino you would like to play." << endl;
cin >> domino;
cout << "Enter 1 for left and 2 for right" << endl;
if(cin >> side){}
else{
cin.clear();
cin.ignore();
}
if(domino > players.at(currTurn)->getSize() || domino < 1)
{
cout << "Domino entered is out of range. Please try again.";
currTurn = (currTurn - 1) % nPlayers;//it should still be their turn
}
else
{
players.at(currTurn)->playBone((domino-1), aBone);
if(findMove(side, aBone, field))
{
field->addBone(aBone);
}
else
{
players.at(currTurn)->addBone(aBone);
currTurn = (currTurn-1)%nPlayers;
cout << "invalid move" << endl;
}
}
gameOver();
}
bool Game::findMove(int side, Bone* aBone, Field *field)
{
Bone *fieldBone = new Bone();
int pos = 0;
while(field->eachBone(pos, fieldBone)) //this will loop through the field and end when it's out of range.
{
//double play
if(doublePlayed)
{
if(side == 1) //if they want to play the top
{
if(aBone->getTop() != doubleValue)//the bone played does not match the double
return false;
//find the double that is being played off of
if(fieldBone->getIsDouble() && (fieldBone->getTop() == doubleValue))
{
if(doublePlays <= 1) //check to see if it's the last side of the double
{
doublePlayed = false;
fieldBone->setBottomUsed(true);
fieldBone->setTopUsed(true);
}
aBone->setTopUsed(true); //set the side of the bone played
doublePlays--;
return true;
}
}
if(side == 2)//if they play the bottom
{
if(aBone->getBottom() != doubleValue)
return false;
if(fieldBone->getIsDouble() && (fieldBone->getTop() == doubleValue))
{
if(doublePlays <= 1)
{
doublePlayed = false;
fieldBone->setBottomUsed(true);
fieldBone->setTopUsed(true);
}
aBone->setBottomUsed(true);
doublePlays--;
return true;
}
}
}
//not a double, should this be in an else?
if(side==1)
{
if((fieldBone->getTop() == aBone->getTop()) && !fieldBone->getTopUsed())
{
fieldBone->setTopUsed(true);
aBone->setTopUsed(true);
if(aBone->getIsDouble())
setForDouble(aBone);
return true;
}
else if((fieldBone->getBottom() == aBone->getTop()) && !fieldBone->getBottomUsed())
{
fieldBone->setBottomUsed(true);
aBone->setTopUsed(true);
if(aBone->getIsDouble())
setForDouble(aBone);
return true;
}
}
if(side == 2)
{
if((fieldBone->getBottom() == aBone->getBottom()) && !fieldBone->getBottomUsed())
{
fieldBone->setBottomUsed(true);
aBone->setBottomUsed(true);
if(aBone->getIsDouble())
setForDouble(aBone);
return true;
}
else if((fieldBone->getTop() == aBone->getBottom()) && !fieldBone->getTopUsed())
{
fieldBone->setTopUsed(true);
aBone->setBottomUsed(true);
if(aBone->getIsDouble())
setForDouble(aBone);
return true;
}
}
pos++;//controls the loop
}
return false;
}
void Game::setForDouble(Bone*& aBone)
{
doublePlayed = true;
doubleValue = aBone->getTop();
doublePlays = 3;
aBone->setTopUsed(false);
aBone->setBottomUsed(false);
}
bool Game::firstMove(Field *field)
{
Bone *highestBone = new Bone();
Bone *aBone = new Bone();
int highest = -1, player, pos = 0, highestPos = 0;
for(int i=0; i < nPlayers; i++) //begin the game with highest double
{
if(players.at(i)->highestDouble(pos, aBone))
{
if(highest < aBone->getTop())
{
highest = aBone->getTop();
player = i;
highestBone = aBone;
highestPos = pos;
}
}
}
if(highest < -1)
return false;
cout << "Player "
<< player+1
<< " had the highest double. Bone";
highestBone->print();
cout << "was automatically played." << endl;
players.at(player)->playBone(highestPos, highestBone);
field->addBone(highestBone);
currTurn = (player + 1)% nPlayers;
doublePlayed = true;
doublePlays = 3;
doubleValue = highest;
cout << endl << "Player " << player + 1 << ", your hand is now:" << endl << endl;
players.at(player)->getHand();
return true;
}
bool Game::gameOver()
{
if(players.at(currTurn)->getSize() < 1)
{
cout << "Player "
<< currTurn+1
<< " has won!"
<< endl;
return true;
}
else
return false;
}