-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMosaic.cpp
508 lines (445 loc) · 14.9 KB
/
Mosaic.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include <string>
#include "Mosaic.h"
//Modified for M2
Mosaic::Mosaic(int boardID) {
this->boardID = boardID;
for(int index=0;index<7;index++){
this->brokenTiles.push_back('.');
}
}
Mosaic::Mosaic(int boardID, vector<string> mosaicData){
this->boardID = boardID;
const int mosaicDataLen = 10;
const int leftSideData = 4;
const int rightSideData = 9;
for(string str: mosaicData){
cout << str << " " << str.length();
cout << '\n';
}
//Processing left hand side
int lineIndex = 0;
for(int row = 0; row < mosaicDataLen; row++){
string lineStr = mosaicData[row];
if(row <= leftSideData){ //From line 0 to Line 4
lineIndex = row;
leftParsing(lineIndex, lineStr);
}else if(row > leftSideData && row <= rightSideData){ //From line 5 to line 9
lineIndex = row - 5;
rightParsing(lineIndex, lineStr);
}else if(row==10){ //Last row
brokenTiles = vector<char>(lineStr.begin(), lineStr.end());
}
}
}
void Mosaic::leftParsing(int row, string lineData){
int length = lineData.length();
for(int index=0;index<length;index++){
this->playerBoardLeftSide[row][boundary-index] = lineData[index];
}
}
void Mosaic::rightParsing(int row, string lineData){
int length = lineData.length();
for(int index=0;index<length;index++){
this->playerBoardRightSide[row][index] = lineData[index];
}
}
Mosaic::~Mosaic() {
}
int Mosaic::getNumBrokenTiles() {
return this->brokenTilesSize;
}
string Mosaic::getLeftSideMosaic() {
string leftMosaicStr;
for(int row=0;row<Board_Height;row++){
for(int col=0;col <row+1;col++){
leftMosaicStr += this->playerBoardLeftSide[row][boundary-col];
}
leftMosaicStr += '\n';
}
return leftMosaicStr;
}
string Mosaic::getRightSideMosaic() {
string rightMosaicStr;
for(int row=0;row<Board_Height;row++){
for(int index=0;index<Board_Length;index++){
rightMosaicStr += this->playerBoardRightSide[row][index];
}
rightMosaicStr += '\n';
}
return rightMosaicStr;
}
string Mosaic::getBrokenTiles() {
return string(brokenTiles.begin(), brokenTiles.end());
}
//Modified for M2.
//Current Usage -> return the entire PlayerBoard (Both left and right side)
vector<string> Mosaic::getRightBoard(){
vector<string> rightBoard;
string tempStr;
for(int row=0;row< Board_Height;row++){
tempStr = "";
for(int col=0;col < Board_Length; col++){
tempStr += this->playerBoardRightSide[row][col];
}
rightBoard.push_back(tempStr);
}
return rightBoard;
}
int Mosaic::getBoardID(){
return this->boardID;
}
bool Mosaic::isRightSideRowCompleted(){
int completed = 0;
for(int row=0;row<Board_Height; row++){
completed = 0;
for(int col=0; col <Board_Length; col++){
if(isupper(this->playerBoardRightSide[row][col])){
completed += 1;
}
}
if(completed==5){
return true;
}
}
return false;
}
bool Mosaic::isLeftSideRowFull(int row){
int slotCheck = row+1;
for(int col=0; col < slotCheck; col++){
if(playerBoardLeftSide[row][boundary-col]=='.'){
return false;
}
}
return true;
}
int Mosaic::pointsCalculation(int status){
int points = 0;
int completedTile = 0;
char currentSlot;
char tileColour;
if(status==ROUND_CALCULATION){
//Update the right side of the board first
for(int row=0; row < Board_Height; row++){
if(isLeftSideRowFull(row)==true){
int tileColIndex = 0;
//Find the tileColour of that row and update right side
tileColour = this->playerBoardLeftSide[row][boundary];
for(int col=0; col < Board_Length; col++){
if(this->playerBoardRightSide[row][col]==std::tolower(tileColour)){
this->playerBoardRightSide[row][col] = tileColour;
tileColIndex = col;
}
}
//And then calculates points for that completed row
//check consecutive tiles in the horizontal row;
string colOfMosaic;
string rowOfMosaic;
for(int index=0; index < Board_Length; index++){
colOfMosaic += this->playerBoardRightSide[row][index];
rowOfMosaic += this->playerBoardRightSide[index][tileColIndex];
}
int pointA, pointB;
pointA = pointB = 0;
pointA = consectiveCheck(colOfMosaic, tileColIndex);
pointB = consectiveCheck(rowOfMosaic, row);
if(pointA==1&&pointB==1){ //mean only one and no consective tile
points += 1;
}else if(pointA==1||pointB==1)
{
if(pointA==1){
points += pointB;
}else{
points += pointA;
}
}else{
points += pointA + pointB;
}
}
}
//Deduce points on floor line
int floorlineDeduction[brokenTilesSize] = {-1,-1,-2,-2,-2,-3,-3};
for(int index=0; index < brokenTilesSize; index++){
if(this->brokenTiles[index]!='.'){
points -= floorlineDeduction[index];
}
}
}
if(status== ENDGAME_CALCULATION){
//2 points for each completed row
for(int row=0; row < Board_Height; row++){
completedTile = 0;
for(int col=0; col < Board_Length; col++){
currentSlot = this->playerBoardRightSide[row][col];
if(isupper(currentSlot)){
completedTile += 1;
}
}
if(completedTile==5){
points+=2;
}
}
//7 points for each completed vertical line
for(int col=0; col < Board_Length; col++){
completedTile = 0;
for(int row=0;row < Board_Height; row++){
currentSlot = this->playerBoardRightSide[row][col];
if(isupper(currentSlot)){
completedTile += 1;
}
}
if(completedTile==5){
points+=7;
}
}
//10 points for each color you have placed all 5 tiles on the wall
char colourList[5] = {'B', 'Y', 'R', 'U', 'L'};
for(char colour: colourList){
completedTile = 0;
for(int row=0; row<Board_Height; row++){
for(int col=0; col < Board_Length; col++){
currentSlot = this->playerBoardRightSide[row][col];
if(currentSlot==colour){
completedTile += 1;
}
}
}
if(completedTile==5){
points += 10;
}
}
}
return points;
}
//Return the max number of Consective Capital Tiles on that row
int Mosaic::consectiveCheck(string rowOfMosaic, int needleIndex){
//if the tile is at rightMost
int points = 0;
if(needleIndex==boundary){
for(int col=boundary; col >= 0; col--){
if(isupper(rowOfMosaic[col])){
points += 1;
}else{
return points;
}
}
return points;
}
//if the tile is at leftMost
if(needleIndex==0){
for(int col=0; col <= boundary; col++){
if(isupper(rowOfMosaic[col])){
points +=1;
}else{
return points;
}
}
return points;
}
//if the tile is between rightMost and leftMost
points += 1; //Default 1 point
int index=needleIndex;
bool stop = false;
//check righthand side
index = needleIndex + 1;
while(stop==false && index <= boundary){
if(isupper(rowOfMosaic[index])){
points += 1;
index += 1;;
}else{
stop=true;
}
}
//check lefthand side
stop = false;
index = needleIndex-1;
while(stop==false && index >= 0){
if(isupper(rowOfMosaic[index])){
points += 1;
index += 1;;
}else{
stop=true;
}
}
return points;
}
//After calculating score, clear the board
vector<char> Mosaic::clearTheBoard(){
vector<char> tilesToBoxLid;
int slot = 0; //no of slot on the current row
//left Side -> clear the board
for(int row=0; row < Board_Height; row++){
slot = row+1;
//if full remove all the tiles from the row and pass it to the vector
if(isLeftSideRowFull(row)==true){
for(int col=0; col < slot; col++){
tilesToBoxLid.push_back(this->playerBoardLeftSide[row][boundary-col]);
this->playerBoardLeftSide[row][boundary-col] = '.';
}
}
}
//and for the floor line
for(int index=0; index < brokenTilesSize; index++){
if(this->brokenTiles[index]!='.'){
if(this->brokenTiles[index]!='F'){
tilesToBoxLid.push_back(this->brokenTiles[index]);
}
this->brokenTiles[index] = '.';
}
}
return tilesToBoxLid;
}
vector<string> Mosaic::getFullMosaic() {
vector<string> fullMosaic;
string tempStr;
for(int row=0;row<Board_Height;row++){
tempStr = std::to_string(row+1) + ".:";
//from the left side
for(int col=0;col<Board_Length;col++){
tempStr += this->playerBoardLeftSide[row][col];
}
tempStr += "||";
//to the right side
for(int col=0;col<Board_Length;col++){
tempStr += this->playerBoardRightSide[row][col];
}
fullMosaic.push_back(tempStr);
}
//and the floor line
tempStr = "6 Broken: " + string(this->brokenTiles.begin(), this->brokenTiles.end());
fullMosaic.push_back(tempStr);
return fullMosaic;
}
/*
displaying two players board together
if more than 2 boards, would be display as follows:
[ Baord A ] [ Board B ]
[ Board C ] [ Board D ]
*/
void Mosaic::displayCurrentBoard(vector<vector<string>> otherBoards, vector<string> otherPlayersName) {
string tempStr;
string board1Str, board2Str, board3Str, board4Str;
string player2Name, player3Name, player4Name;
string whiteSpace = " ";
vector<string> ourMosaic = this->getFullMosaic();
vector<string> board2, board3, board4;
int numOfOtherBoards = otherBoards.size();
//for only 2 players
if(numOfOtherBoards >=1){
board2 = otherBoards[0];
player2Name = otherPlayersName[0];
} // 3 players
if(numOfOtherBoards >= 2){
board3 = otherBoards[1];
player3Name = otherPlayersName[1];
} // 4 players
if(numOfOtherBoards >= 3){
player4Name = otherPlayersName[2];
board4 = otherBoards[2];
}
cout << "[Mosaic For You]" << setw(30) << "[Mosaic For " + player2Name + "]"+ "\n";
//Getting every line of Board 1 & 2
for(int index =0; index < Board_Length+1; index++){
board1Str = ourMosaic[index];
board2Str = board2[index];
cout << board1Str << setw(30) << board2Str + "\n";
board1Str = "";
board2Str= "";
}
cout << "\n";
//3 Players
if(numOfOtherBoards == 2){
cout << "\n";
cout << "[Mosaic For " + player3Name + "]"+ "\n";
for(int index =0; index < Board_Length+1; index++){
board3Str = board3[index];
cout << board3Str << setw(30) << "\n";
board3Str = "";
}
}
// 4 Players
if(numOfOtherBoards == 3){
cout << "\n";
cout << "[Mosaic For " + player3Name + "]" << setw(30) << "[Mosaic For " + player4Name + "]"+ "\n";
for(int index =0; index < Board_Length+1; index++){
board3Str = board3[index];
board4Str = board4[index];
cout << board3Str << setw(30) << board4Str + "\n";
board3Str = "";
board4Str= "";
}
}
}
/*
This function passing tilesToPlayerBoard and updates the current board,
if there is excessive tiles left from the floor line, they will be store in a vector and return it
*/
vector<char> Mosaic::updateLeftSideBoard(vector<char> tilesToPlayerBoard, char tileColour, int mosaicRow) {
int rowIndex = mosaicRow -1;
int col = 0;
if(mosaicRow!=6){
while(!tilesToPlayerBoard.empty() && col < mosaicRow){
if(this->playerBoardLeftSide[rowIndex][boundary-col]=='.'){
this->playerBoardLeftSide[rowIndex][boundary-col] = tileColour;
tilesToPlayerBoard.pop_back();
}
col += 1;
}
}
//so the remaining must be excessive and should place it to the floorline
col = 0;
if(!tilesToPlayerBoard.empty()){
while(!tilesToPlayerBoard.empty() && col < this->brokenTilesSize){
if(this->brokenTiles[col]=='.'){
this->brokenTiles[col] = tileColour;
tilesToPlayerBoard.pop_back();
}
col += 1;
}
}
//In a rare case, if the tilesToPlayerBoard still not empty i.e. if the floor line can't hold the exccessive tiles
//we return this vector and they should be placed in the boxLid
return tilesToPlayerBoard;
}
void Mosaic::addTilesToBroken(char tile){
for(int index=0; index < brokenTilesSize; index++){
if(this->brokenTiles[index]=='.'){
brokenTiles[index] = tile;
return;
}
}
}
bool Mosaic::checkRightSideValid(int rowNumber, char tileColour) {
/* for a valid move:
1) right side of the current row is empty
2) or if it is not empty, it must contain the same color tile, excessive tiles moves to the floor line
3) and the left side does not contain the same color tile
if row == 6, means it is passing to the floor line,
then automatically return true
*/
int const boundary = 4;
int rowIndex = rowNumber-1;
int filled = 0;
if(rowNumber==6){
return true;
}else{
//check left side - if the current row is full
for(int col=0; col < rowNumber; col++){
if(playerBoardLeftSide[rowIndex][boundary-col]!='.'){
if(playerBoardLeftSide[rowIndex][boundary-col]!=tileColour){
return false;
}
filled += 1;
}
}
if(filled==rowNumber){
return false;
}
//check right side
for(int col=0; col < Board_Length; col++){
if(playerBoardRightSide[rowIndex][col]==toupper(tileColour)){
return false;
}
}
}
return true;
}