-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDice.cpp
More file actions
198 lines (178 loc) · 3.99 KB
/
Dice.cpp
File metadata and controls
198 lines (178 loc) · 3.99 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
/*********************************************************************
** Author: Melody Reebs
** Date: 06/08/2018
** Title: Yahtzee
** Description: Dice class implementation file
*********************************************************************/
#include <iostream>
#include <string>
#include <cstdlib>
#include "Dice.hpp"
using std::cout;
using std::endl;
using std::string;
int Dice::roll(int index) {
currentValues[index] = rand() % 6 + 1;
return currentValues[index];
}
void Dice::rollAll() {
for (int i = 0; i < 5; i++) {
roll(i);
}
}
int Dice::getValue(int index) {
return currentValues[index];
}
int* Dice::getValArray() {
return currentValues;
}
void Dice::printValues() {
cout << "Your roll: " << endl;
string line;
line.assign(13, '-');
cout << line << endl;
for (int i = 0; i < 5; i++) {
cout << "Dice " << i + 1 << " | " << " " << currentValues[i] << " ";
cout << endl << line << endl;
}
}
int Dice::calcSum() {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += currentValues[i];
}
return sum;
}
int Dice::calcUpper(int val) {
int score = 0;
for (int i = 0; i < 5; i++) {
if (currentValues[i] == val) {
score += val;
}
}
return score;
}
int Dice::calcNumofKind(int num) {
bool flag = false;
int index = 0;
while (!flag && index < (5 - num + 1)) {
int val = currentValues[index];
int numVals = 1;
for (int i = (index + 1); i < 5; i++) {
if (currentValues[i] == val) {
numVals++;
}
}
if (numVals >= num) {
flag = true;
}
index++;
}
if (flag && num == 5) {
return 50;
}
else if (flag) {
return calcSum();
}
else {
return 0;
}
}
int Dice::calcNuminRow(int num) {
int tempArray[5];
for (int i = 0; i < 5; i++) {
tempArray[i] = currentValues[i];
}
// Sort temp array
for (int start = 0; start < 4; start++) {
int minIndex = start;
int minVal = tempArray[start];
for (int i = (start + 1); i < 5; i++) {
if (tempArray[i] < minVal) {
minVal = tempArray[i];
minIndex = i;
}
}
tempArray[minIndex] = tempArray[start];
tempArray[start] = minVal;
}
if (num == 4) {
int count = 0, i = 0;
bool flag = false;
while (i < 4 && !flag) {
if (tempArray[i] == (tempArray[i + 1] - 1)) {
count++;
}
else if ((tempArray[i] < (tempArray[i + 1] - 1)) && i != 0) {
flag = true;
}
i++;
}
if (count >= 3) {
return 30;
}
else {
return 0;
}
}
else if (num == 5) {
bool flag;
flag = (tempArray[0] == (tempArray[1] - 1)) && (tempArray[1] == (tempArray[2] - 1)) && (tempArray[2] == (tempArray[3] - 1)) && (tempArray[3] == (tempArray[4] - 1));
if (flag) {
return 40;
}
}
else {
return 0;
}
}
int Dice::calcFullHouse() {
int val1, val2, numVal1 = 0, numVal2 = 0;
val1 = currentValues[0];
numVal1++;
for (int i = 1; i < 5; i++) {
if (currentValues[i] == val1) {
numVal1++;
}
else {
val2 = currentValues[i];
}
}
for (int i = 0; i < 5; i++) {
if (currentValues[i] == val2) {
numVal2++;
}
}
if ((numVal1 == 3 && numVal2 == 2) || (numVal1 == 2 && numVal2 == 3)) {
return 25;
}
else {
return 0;
}
}
int Dice::getScore(int category) {
switch(category) {
case 1: return calcUpper(1);
case 2: return calcUpper(2);
case 3: return calcUpper(3);
case 4: return calcUpper(4);
case 5: return calcUpper(5);
case 6: return calcUpper(6);
case 7: return calcNumofKind(3);
case 8: return calcNumofKind(4);
case 9: return calcFullHouse();
case 10: return calcNuminRow(4);
case 11: return calcNuminRow(5);
case 12: return calcNumofKind(5);
case 13: return calcSum();
default: return 0;
}
}
// FOR TESTING
void Dice::setValues(int val1, int val2, int val3, int val4, int val5) {
currentValues[0] = val1;
currentValues[1] = val2;
currentValues[2] = val3;
currentValues[3] = val4;
currentValues[4] = val5;
}