-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQwintoScoreSheet.cpp
More file actions
152 lines (129 loc) · 3.31 KB
/
QwintoScoreSheet.cpp
File metadata and controls
152 lines (129 loc) · 3.31 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
#include "QwintoScoreSheet.h"
QwintoScoreSheet::QwintoScoreSheet(const std::string _name) : ScoreSheet(_name)
{
d_scoreSheetRows.reserve(3);
d_scoreSheetRows.push_back( new QwintoRow<Color::RED>() );
d_scoreSheetRows.push_back( new QwintoRow<Color::YELLOW>() );
d_scoreSheetRows.push_back( new QwintoRow<Color::BLUE>() );
}
QwintoScoreSheet::~QwintoScoreSheet()
{
for(auto& row : d_scoreSheetRows)
{
delete row;
}
}
bool QwintoScoreSheet::score(RollOfDice _dice, Color _color, int _pos)
{
if(validate( _dice, _color, _pos ))
{
Row& row = (*this)[_color];
row[_pos] = _dice; // this is probably going to have to be changed when we implement Qwixx because _pos = -1
setTotal();
return true;
}
return false;
}
bool QwintoScoreSheet::validate(const RollOfDice _dice, const Color _color, const int _pos) const
{
bool rowValidate = false;
int red, yellow, blue;
switch (_color)
{
case Color::RED:
//validate function from row
rowValidate = d_scoreSheetRows[0]->validate(_pos, _dice);
//check for stacked same values
if(_pos < 9)
{
rowValidate = rowValidate && (*d_scoreSheetRows[1])[_pos+1] != _dice;
if (_pos < 8)
rowValidate = rowValidate && (*d_scoreSheetRows[2])[_pos+2] != _dice;
}
break;
case Color::YELLOW:
rowValidate = d_scoreSheetRows[1]->validate(_pos, _dice);
if(_pos < 9)
rowValidate = rowValidate && (*d_scoreSheetRows[2])[_pos-1] != _dice;
if (_pos > 0)
rowValidate = rowValidate && (*d_scoreSheetRows[0])[_pos-1] != _dice;
break;
case Color::BLUE:
rowValidate = d_scoreSheetRows[2]->validate(_pos, _dice);
if(_pos > 0)
{
rowValidate = rowValidate && (*d_scoreSheetRows[1])[_pos-1] != _dice;
if (_pos > 1)
rowValidate = rowValidate && (*d_scoreSheetRows[2])[_pos-2] != _dice;
}
break;
}
return rowValidate;
}
int QwintoScoreSheet::calcTotal() const
{
int current_score = 0;
current_score -= d_failedThrows * 5;
for (int i = 0; i < 3; ++i)
{
int filled_cells = 0;
for (int j = 0; j < 10; ++j)
{
if ((*d_scoreSheetRows[i])[j] > 0)
++filled_cells;
}
if (filled_cells == 9)
current_score += (*d_scoreSheetRows[i])[9];
else
current_score += filled_cells;
}
for (int i = 0; i < 8; ++i)
{
if ((*d_scoreSheetRows[0])[i] < 0 && (*d_scoreSheetRows[1])[i+1] < 0 && (*d_scoreSheetRows[2])[i+2] < 0)
{
if (i == 0 || i == 7)
current_score += (*d_scoreSheetRows[2])[i+2];
else if (i == 1 || i == 5)
current_score += (*d_scoreSheetRows[0])[i];
else
current_score += (*d_scoreSheetRows[1])[i+1];
}
}
return current_score;
}
bool QwintoScoreSheet::operator!() const
{
//checking for common fail conditions (failed throws)
if (ScoreSheet::operator!())
return true;
int filled_rows = 0;
for (int i = 0; i < 3; ++i)
{
int filled_cells = 0;
for (int j = 0; j < 10; ++j)
{
if ((*d_scoreSheetRows[i])[j] > 0)
++filled_cells;
}
if (filled_cells == 9)
++filled_rows;
}
if (filled_rows >= 2)
return true;
//if no condition is met the game is not over
return false;
}
Row& QwintoScoreSheet::operator[](const Color _color)
{
switch(_color)
{
case Color::RED :
return *d_scoreSheetRows[0];
case Color::YELLOW :
return *d_scoreSheetRows[1];
case Color::BLUE :
return *d_scoreSheetRows[2];
default:
return *d_scoreSheetRows[0];
}
}