forked from worobec/Yahtzee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yahtzee1.cpp
209 lines (199 loc) · 5.78 KB
/
yahtzee1.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
//A program that plays and scores one hand of Yahtzee
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int rollDie();
int maxOfAKindFound(int []);
int maxStraightFound(int []);
bool fullHouseFound(int []);
int totalAllDice(int []);
void sortArray(int [], int);
int main()
{
const int DICE_IN_PLAY = 5;
int hand[DICE_IN_PLAY];
srand(time(0));
char playAgain = 'y';
while (playAgain == 'y')
{
string keep = "nnnnn"; //setup to roll all dice in the first roll
int turn = 1;
while (turn < 4 && keep != "yyyyy")
{
//roll dice not kept
for (int dieNumber = 0; dieNumber < DICE_IN_PLAY; dieNumber++)
{
if (keep[dieNumber] != 'y')
hand[dieNumber] = rollDie();
}
//output roll
cout << "Your roll was: ";
for (int dieNumber = 0; dieNumber < DICE_IN_PLAY; dieNumber++)
{
cout << hand[dieNumber] << " ";
}
cout << endl;
//if not the last roll of the hand prompt the user for dice to keep
if (turn < 3)
{
cout << "enter dice to keep (y or n) ";
cin >> keep;
}
turn++;
}
//start scoring
//hand need to be sorted to check for straights
sortArray(hand, DICE_IN_PLAY);
cout << "Here is your sorted hand : ";
for (int dieNumber = 0; dieNumber < DICE_IN_PLAY; dieNumber++)
{
cout << hand[dieNumber] << " ";
}
cout << endl;
//upper scorecard
for (int dieValue = 1; dieValue <=6; dieValue++)
{
int currentCount = 0;
for (int diePosition = 0; diePosition < 5; diePosition++)
{
if (hand[diePosition] == dieValue)
currentCount++;
}
cout << "Score " << dieValue * currentCount << " on the ";
cout << dieValue << " line" << endl;
}
//lower scorecard
if (maxOfAKindFound(hand) >= 3)
{
cout << "Score " << totalAllDice(hand) << " on the ";
cout << "3 of a Kind line" << endl;
}
else cout << "Score 0 on the 3 of a Kind line" << endl;
if (maxOfAKindFound(hand) >= 4)
{
cout << "Score " << totalAllDice(hand) << " on the ";
cout << "4 of a Kind line" << endl;
}
else cout << "Score 0 on the 4 of a Kind line" << endl;
if (fullHouseFound(hand))
cout << "Score 25 on the Full House line" << endl;
else
cout << "Score 0 on the Full House line" << endl;
if (maxStraightFound(hand) >= 4)
cout << "Score 30 on the Small Straight line" << endl;
else
cout << "Score 0 on the Small Straight line" << endl;
if (maxStraightFound(hand) >= 5)
cout << "Score 40 on the Large Straight line" << endl;
else
cout << "Score 0 on the Large Straight line" << endl;
if (maxOfAKindFound(hand) >= 5)
cout << "Score 50 on the Yahtzee line" << endl;
else
cout << "Score 0 on the Yahtzee line" << endl;
cout << "Score " << totalAllDice(hand) << " on the ";
cout << "Chance line" << endl;
cout << "\nEnter 'y' to play again ";
cin >> playAgain;
}
return 0;
}
int rollDie()
//this function simulates the rolling of a single die
{
int roll = rand() % 6 + 1;
return roll;
}
int maxOfAKindFound(int hand[])
//this function returns the count of the die value occurring most in the hand
//but not the value itself
{
int maxCount = 0;
int currentCount ;
for (int dieValue = 1; dieValue <=6; dieValue++)
{
currentCount = 0;
for (int diePosition = 0; diePosition < 5; diePosition++)
{
if (hand[diePosition] == dieValue)
currentCount++;
}
if (currentCount > maxCount)
maxCount = currentCount;
}
return maxCount;
}
int totalAllDice(int hand[])
//this function returns the total value of all dice in a hand
{
int total = 0;
for (int diePosition = 0; diePosition < 5; diePosition++)
{
total += hand[diePosition];
}
return total;
}
void sortArray(int array[], int size)
//bubble sort from Gaddis chapter 8
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
int maxStraightFound(int hand[])
//this function returns the length of the longest
//straight found in a hand
{
int maxLength = 1;
int curLength = 1;
for(int counter = 0; counter < 4; counter++)
{
if (hand[counter] + 1 == hand[counter + 1] ) //jump of 1
curLength++;
else if (hand[counter] + 1 < hand[counter + 1]) //jump of >= 2
curLength = 1;
if (curLength > maxLength)
maxLength = curLength;
}
return maxLength;
}
bool fullHouseFound(int hand[])
//this function returns true if the hand is a full house
//or false if it does not
{
bool foundFH = false;
bool found3K = false;
bool found2K = false;
int currentCount ;
for (int dieValue = 1; dieValue <=6; dieValue++)
{
currentCount = 0;
for (int diePosition = 0; diePosition < 5; diePosition++)
{
if (hand[diePosition] == dieValue)
currentCount++;
}
if (currentCount == 2)
found2K = true;
if (currentCount == 3)
found3K = true;
}
if (found2K && found3K)
foundFH = true;
return foundFH;
}