-
Notifications
You must be signed in to change notification settings - Fork 1
/
GamePlay.cpp
344 lines (291 loc) · 8.8 KB
/
GamePlay.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
// Ryan Hutton
// Final Project - Farkle Game
// SNHU IT 312 - Software Development with C++
// 2/23/2020
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include "GamePlay.h"
using namespace std;
GamePlay::GamePlay()
{
int dice[6];
std::string* players[5];
bool rollAgainPos[6] = { false };
int score, totalDice, rerollDice, firstScore, totalScore;
}
GamePlay::~GamePlay()
{
}
// Accessors and modifiers for private variables
/*void GamePlay::setDice(int newDice[])
{
dice[] = newDice[];
}
*/
int GamePlay::getDice(int index)
{
return dice[index];
}
//void GamePlay::setPlayers(string newPlayers[]);
std::string GamePlay::getPlayers(int index)
{
return players[index];
}
//void setRollAgainPos(bool newPosition[])
bool GamePlay::getRollAgainPos(int index)
{
return rollAgainPos[index];
}
void GamePlay::setScore(int newScore)
{
score = newScore;
}
int GamePlay::getScore()
{
return score;
}
void GamePlay::setTotalDice(int newTotalDice)
{
totalDice = newTotalDice;
}
int GamePlay::getTotalDice()
{
return totalDice;
}
void GamePlay::setFirstScore(int newFirstScore)
{
firstScore = newFirstScore;
}
int GamePlay::getFirstScore()
{
return firstScore;
}
void GamePlay::setTotalScore(int newTotalScore)
{
totalScore = newTotalScore;
}
int GamePlay::getTotalScore()
{
return totalScore;
}
int* GamePlay::rollDice() //Dice roll function, restricts possible numbers between 1 and 6
{
for (int i = 0; i < 6; i++)
{
dice[i] = (rand() % 6 + 1); //Numbers restricted to between 1 and 6 to simulate dice
}
//return dice[6]; //Returns roll values
return dice;
}
int* GamePlay::rollAgain() // Reroll function, usees rerollPosition bool array to only reroll the positions that don't score the first time
{
for (int i = 0; i < 6; i++)
{
if (rollAgainPos[i] == true)
{
dice[i] = (rand() % 6 + 1);
}
}
//return dice[6];
return dice;
}
void GamePlay::displayRoll()
{
for (int i = 0; i <= 5; i++) // Prints dice values from the roll
{
cout << dice[i] << " ";
}
}
std::string* GamePlay::collectNames(int numPlayers) //Function to establish player name array, set it's size and take name input
{
for (int i = 0; i <= (numPlayers - 1); i++) //Loop to get the names of all players
{
std::cout << "Enter the name of player " << (i + 1) << ": ";
std::cin >> players[i];
}
return players;
}
int GamePlay::calculateScore(int* playerScore) //Scoring function
{
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0; //Variable counts for each roll possibility
setScore(0); //Reset score each time the function is run
rerollDice = 0; // Reset the number of dice to reroll each time the function is run
for (int i = 0; i < 6; i++) // check for 1's and 5's as well as increment the individual number counters
{
switch (dice[i])
{
case 1:
score += 100;
count1++;
break;
case 2:
count2++;
rerollDice++;
rollAgainPos[i] = true;
break;
case 3:
count3++;
rerollDice++;
rollAgainPos[i] = true;
break;
case 4:
count4++;
rerollDice++;
rollAgainPos[i] = true;
break;
case 5:
score += 50;
count5++;
break;
case 6:
count6++;
rerollDice++;
rollAgainPos[i] = true;
break;
}
}
if (rerollDice == 6)
{
cout << "Farkle! You didn't roll any scoring dice, any score gained this turn is lost" << endl;
score = 0;
}
else
{
if (count1 >= 3) // If statements to check for triple rolls and add to the score accordingly
{
score = (score - 300) + 1000; //Removes original 300 score before adding 1000
}
else if (count2 >= 3)
{
score += 200;
for (int i = 0; i < 6; i++)
{
if (dice[i] == 2) { rollAgainPos[i] = false; }
}
rerollDice -= 3;
}
else if (count3 >= 3)
{
score += 300;
for (int i = 0; i < 6; i++)
{
if (dice[i] == 3) { rollAgainPos[i] = false; }
}
rerollDice -= 3;
}
else if (count4 >= 3)
{
score += 400;
for (int i = 0; i < 6; i++)
{
if (dice[i] == 4) { rollAgainPos[i] = false; }
}
rerollDice -= 3;
}
else if (count5 >= 3)
{
score = (score - 150) + 500; //Removes 150 from score before adding 500
}
else if (count6 >= 3)
{
score += 600;
for (int i = 0; i < 6; i++)
{
if (dice[i] == 6) { rollAgainPos[i] = false; }
}
rerollDice -= 3;
}
}
firstScore = score; // Initial turn variable that has to exceed 1000 to continue in the game
cout << "Score from that roll: " << score << endl;
return rerollDice, * playerScore;
}
int GamePlay::eachPlayerTurn(int numPlayers, int* playerScore) //Function that handles individual player turns
{
char rerollCheck; // Variable for Y or N reroll input
for (int i = 0; i < numPlayers; i++) // Repeats the turn based on how many players are playing
{
cout << players[i] << " let's roll the dice: " << endl; //Says which player is currently rolling
rollDice(); //Dice roll function
displayRoll(); //Prints dice roll
calculateScore(playerScore); //Scores the roll
if (rerollDice == 0)
{
std::cout << "All dice are scoring dice, you get to roll all 6 again for more score";
playerScore[i] += score;
rollDice();
displayRoll();
calculateScore(playerScore);
}
else if (rerollDice != 6)
{
cout << endl << "Do you want to reroll the " << rerollDice << " non scoring dice? (Y/N)" << endl; //Gives the option to reroll the non scoring dice or not
cin >> rerollCheck;
if (rerollCheck == 'y' || rerollCheck == 'Y') //reroll is performed based on the number of non scoring dice
{
rollAgain(); //Dice roll function
displayRoll(); //Print dice roll
calculateScore(playerScore); //Score reroll
}
else if (rerollCheck == 'n' || rerollCheck == 'N') //If user chooses no their turn is over and rerollDice is set to 0 so that the dice total will be reset properl
{
std::cout << "Your turn is now over" << endl;
rerollDice = 0;
}
}
for (int i = 0; i < 6; i++)
{
rollAgainPos[i] = false;
}
std::cout << players[i] << " your score from that round: " << score << endl;
playerScore[i] += score; //Adds the score total to the individual player score array
totalScore += score; //Built the game to work single player to check functionality
std::cout << "Your total score after that round: " << getTotalScore() << endl << endl;
}
return *playerScore;
}
int GamePlay::initialTurn(int* playerScore) // First turn that handles the rolls before the initial 1000+ roll, executes on it's own until it happens
{
rollDice();
displayRoll();
calculateScore(playerScore);
rerollDice = 0;
return *playerScore;
}
int GamePlay::RunGame()
{
srand(time(0)); //Seeds the random number generator
int numPlayers;
cout << endl << endl << "How many players are there?" << endl;
while (!(cin >> numPlayers))//Number of players input
{
cin.clear();
cin.ignore();
cout << "Please enter a whole number" << std::endl;
}
collectNames(numPlayers); // Get the names of the players
int* playerScore = new int[numPlayers]; // Scoring array to hold individual player scores
for (int i = 0; i < numPlayers; i++)
{
playerScore[i] = 0;
}//Initializes score arry to a 0 value after it's size is determined
while (firstScore < 1000)
{
initialTurn(playerScore);
} //First turn won't end until player rolls over 1000 on a single roll
cout << endl; // Basic separation from the first round spam
while (totalScore < 10000) //Win condition, game lasts until a player hits or exceeds 10,000 points
{
std::cout << std::endl;
eachPlayerTurn(numPlayers, playerScore);
if (totalScore >= 10000)
{
cout << "Congratulations, you win!" << endl << endl;
break;
}
}
system("PAUSE");
return 0;
}