-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
236 lines (216 loc) · 6.11 KB
/
Player.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
/* Player Class Description
*Author:Sara Imamura Sanjay Sajeesh Krishnan Kee Chew Huang
*The Player class is used to setup a player and to validate
*scores and possible identify deductions in the score.
*/
#include "Player.h"
Player::Player(std::string name)
{
this->name = name;
patternLine = new TileSet * [WALL_DIM];
for (int i = 0; i < WALL_DIM; ++i)
{
patternLine[i] = new TileSet(i + 1);
for (int j = 0; j < WALL_DIM; ++j)
{
wall[i][j] = NO_TILE;
}
}
this->brokenLength = 0;
this->score = 0;
this->firstPlayer = false;
}
Player::~Player()
{
for(int i = 0; i < WALL_DIM; ++i){
delete this->patternLine[i];
}
delete [] this->patternLine;
}
// return the score of the player
int Player::getScore()
{
return this->score;
}
// add a tile to the pattern line or broken tile when the player collects
// a tile from the Factory
bool Player::factoryOffer(Tile tile, int row)
{
bool success = true;
//first player tile goes to the broken line directly
if(tile == FIRST_PLAYER){
brokenTile[brokenLength] = tile;
++brokenLength;
firstPlayer = true;
}else{
// the player has the option to place the tiles in broken line if they have no possible move
if (row == 0 )
{
if(brokenLength < MAX_FLOORLINE){
brokenTile[brokenLength] = tile;
++brokenLength;
}else {
success = false;
}
}
else
{
//place the tile on desired patternLine if the patternLine is not full, else goes to the broken line
TileSet *tiles = this->patternLine[row - 1];
if (!tiles->isFull())
{
tiles->addTile(tile);
}
else
{
brokenTile[brokenLength] = tile;
++brokenLength;
}
}
}
return success;
}
// Push completed patternLine to the mosaic, calculate the score at the end of each round, return discarded tiles
std::vector<Tile> Player::wallTiling()
{
std::vector<Tile> discardedTiles;
for (int i = 0; i < WALL_DIM; ++i)
{
TileSet *tiles = this->patternLine[i];
Tile tile = tiles->getTile();
//if the patternLine is full, push to the mosaic
if (tiles->isFull())
{
for(int i = 0; i < tiles->getCurrentSize() - 1; ++i){
discardedTiles.push_back(tile);
}
int col = tiles->pushTiles(i);
this->wall[i][col] = tile;
addScore(i,col);
}
}
//deduct points from tiles in brokenLine
score -= deductBrokenPoints(discardedTiles);
// move tiles from the pattern line to the wall
// add unused tiles to the tilebag
// calculate score
return discardedTiles;
}
//return a string containing details of the Player's mosaic
std::string Player::printMosaic()
{
//TODO
std::string mosaic = "";
for (int i = 0; i < WALL_DIM; ++i)
{
mosaic = mosaic + std::to_string(i + 1) + ": " + this->patternLine[i]->print() + "|| ";
for (int j = 0; j < WALL_DIM; ++j)
{
mosaic = mosaic + enumToChar(wall[i][j]) + " ";
}
mosaic += "\n";
}
mosaic += "broken: ";
for (int i = 0; i < brokenLength; ++i)
{
mosaic = mosaic + enumToChar(brokenTile[i]) + " ";
}
return mosaic;
}
//return if the move is valid
bool Player::checkValid(Tile tile, int row)
{
bool valid = false;
if (row == 0)
{
valid = true;
}
else
{
valid = this->patternLine[row - 1]->checkValid(tile);
}
return valid;
}
std::string Player::getName()
{
return this->name;
}
// this is a recursive function which calculate the score in vertical line
int Player::calcScoreHorizontal(int row, int col, int colS){
int returnVal = 0;
// base case
if(this->wall[row][col] == NO_TILE){
returnVal = 0;
}else{
returnVal ++;
if(col > 0 && col <= colS ){
// go left
returnVal += calcScoreHorizontal(row, col-1, colS);
}
if (col < WALL_DIM-1 && col >= colS ){
// go right
returnVal +=calcScoreHorizontal(row, col+1,colS);
}
}
return returnVal;
// recursive case
}
// this is a recursive function which calculate the score in vertical line
int Player::calcScoreVertical(int row, int col, int rowS){
int returnVal = 0;
// base case
if(this->wall[row][col] == NO_TILE){
returnVal = 0;
}else{
returnVal ++;
if(row > 0 && row <= rowS){
// go up
returnVal += calcScoreVertical(row-1, col, rowS);
}
if (row < WALL_DIM-1 && row >= rowS){
// go down
returnVal +=calcScoreVertical(row+1, col, rowS);
}
}
return returnVal;
// recursive case
}
void Player::addScore(int row, int col){
int horizontalPoint = calcScoreHorizontal(row, col, col);
int verticalPoint = calcScoreVertical(row, col, row);
// if there is no connected tiles add 1 to score
if(horizontalPoint == 1 && verticalPoint == 1){
score += 1;
}
// if there is only connected tiles in vertical line
else if(horizontalPoint == 1){
score += verticalPoint;
}
// if there is only connected tiles in horizontal line
else if(verticalPoint == 1){
score += horizontalPoint;
}
// if there is connected tiles in both direction
else{
score += horizontalPoint + verticalPoint;
}
}
//return how many points shall the player be deducted
int Player::deductBrokenPoints(std::vector<Tile> & discardedTiles){
int returnValue = 0;
for(int i = 0; i< this->brokenLength; ++ i){
if(i < 3){
returnValue += 1;
}else if(i < 6){
returnValue += 2;
}else{
returnValue += 3;
}
if(brokenTile[i] != FIRST_PLAYER){
discardedTiles.push_back(brokenTile[i]);
}
brokenTile[i] = NO_TILE;
}
brokenLength = 0;
return returnValue;
}