-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
770 lines (701 loc) · 17.5 KB
/
board.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#include "board.h"
#include <cstring>
#include <QtDebug>
#include <QtConcurrent/QtConcurrentRun>
#include <QCoreApplication>
#include <QSound>
#include "minimax.h"
const int cornerPoints = 50; /**< TODO */
const int edgePoints = 2; /**< TODO */
/**
* Constructor style (0=default, 1=Future, 2=Super Mario)
*
* @param size
* @param style
*/
Board::Board(int size, int style) :
QObject(0),boardSize(size), boardStyle(style), board(0), whitePoints(0), blackPoints(0), boolGameOver(false), whiteCornerPoints(0),
blackCornerPoints(0), whiteEdgePoints(0),blackEdgePoints(0)
{
this->initializeBoard();
}
/**
* Copy constructor
*
* @param other
*/
Board::Board(const Board & other) :
QObject(0)
{
this->boardSize = other.getBoardSize();
this->whoseNext = other.getWhoIsNext();
this->whitePoints = other.getWhitePoints();
this->blackPoints = other.getBlackPoints();
this->boolGameOver = other.isGameOver();
this->whiteCornerPoints = other.getWhiteCornerPoints();
this->blackCornerPoints = other.getBlackCornerPoints();
this->whiteEdgePoints = other.getWhiteEdgePoints();
this->blackEdgePoints = other.getBlackEdgePoints();
const int cells = this->boardSize*this->boardSize;
this->board = new CELL_STATE[cells];
memcpy(this->board,other.board,cells*sizeof(CELL_STATE));
}
/**
* Destructor
*
*/
Board::~Board()
{
if (this->board != 0) {
delete this->board;
}
}
/**
* Get point of WHITE player
*
* @return int
*/
int Board::getWhitePoints() const
{
return this->whitePoints;
}
/**
* Get point of BLACK player
*
* @return int
*/
int Board::getBlackPoints() const
{
return this->blackPoints;
}
/**
* Get points of WHITE corner count
*
* @return int
*/
int Board::getWhiteCornerPoints() const
{
return this->whiteCornerPoints;
}
/**
* Get points of BLACK corner count
*
* @return int
*/
int Board::getBlackCornerPoints() const
{
return this->blackCornerPoints;
}
/**
* Get points of WHITE edge count
*
* @return int
*/
int Board::getWhiteEdgePoints() const
{
return this->whiteEdgePoints;
}
/**
* Get points of BLACK edge count
*
* @return int
*/
int Board::getBlackEdgePoints() const
{
return this->blackEdgePoints;
}
/**
* Get overall score, calculated from edge, base and corner count
* Was meant to be used in highscore view
*
* @return int
*/
int Board::getScore() const
{
const int basic = this->getWhitePoints() - this->getBlackPoints();
const int edges = this->getWhiteEdgePoints() * edgePoints - this->getBlackEdgePoints() * edgePoints;
const int corners = (cornerPoints*this->getWhiteCornerPoints()) - (cornerPoints*this->getBlackCornerPoints());
const int score = basic + corners + edges;
return corners;
}
/**
* Get a list of valid moves by player
*
* @param player
* @return QList<BoardPosition>
*/
QList<BoardPosition> Board::getValidMoves(CELL_STATE player) const
{
QList<BoardPosition> validMoves;
for (int x = 0; x < this->getBoardSize(); x++)
{
for (int y = 0; y < this->getBoardSize(); y++)
{
BoardPosition position = {x,y};
if (this->isValidMove(position, player))
validMoves.append(position);
}
}
return validMoves;
}
/**
* Checks if the move at a specific position by an specific player is valid
*
* @param position
* @param player
* @param flips
* @return bool
*/
bool Board::isValidMove(BoardPosition position,CELL_STATE player, QList<BoardPosition> * flips) const
{
Q_UNUSED(flips);
if (player == EMPTY) {
return false;
}
if (this->isCellOccupied(position)) {
return false;
}
const CELL_STATE enemy = this->getEnemyOf(player);
bool valid = false;
//TODO: Improve algorithm
//Check move left
{
bool gotEnemy = false;
for (int x = position.x-1; x >= 0; x--)
{
const BoardPosition consider = {x,position.y};
const CELL_STATE considerCell = this->getCell(consider);
//Found empty cell
if (considerCell == EMPTY)
break;
//Found enemy cell
else if (considerCell == enemy)
gotEnemy = true;
//Found friendly cell
else
{
if (gotEnemy)
{
valid = true;
if (flips)
flips->append(this->getCellsBetween(consider,position));
}
break;
}
}
}
//Check move right
{
bool gotEnemy = false;
for (int x = position.x+1; x < this->getBoardSize(); x++)
{
const BoardPosition consider = {x,position.y};
const CELL_STATE considerCell = this->getCell(consider);
if (considerCell == EMPTY)
break;
else if (considerCell == enemy)
gotEnemy = true;
else
{
if (gotEnemy)
{
valid = true;
if (flips)
flips->append(this->getCellsBetween(consider,position));
}
break;
}
}
}
//Check move up
{
bool gotEnemy = false;
for (int y = position.y+1; y < this->getBoardSize(); y++)
{
const BoardPosition consider = {position.x,y};
const CELL_STATE considerCell = this->getCell(consider);
if (considerCell == EMPTY)
break;
else if (considerCell == enemy)
gotEnemy = true;
else
{
if (gotEnemy)
{
valid = true;
if (flips)
flips->append(this->getCellsBetween(consider,position));
}
break;
}
}
}
//Check move down
{
bool gotEnemy = false;
for (int y = position.y-1; y >= 0; y--)
{
const BoardPosition consider = {position.x,y};
const CELL_STATE considerCell = this->getCell(consider);
if (considerCell == EMPTY)
break;
else if (considerCell == enemy)
gotEnemy = true;
else
{
if (gotEnemy)
{
valid = true;
if (flips)
flips->append(this->getCellsBetween(consider,position));
}
break;
}
}
}
//Diagonal up-right check
{
bool gotEnemy = false;
int x = position.x+1;
int y = position.y+1;
while (x < this->getBoardSize() && y < this->getBoardSize())
{
const BoardPosition consider = {x,y};
const CELL_STATE considerCell = this->getCell(consider);
if (considerCell == EMPTY)
break;
else if (considerCell == enemy)
gotEnemy = true;
else
{
if (gotEnemy)
{
valid = true;
if (flips)
flips->append(this->getCellsBetween(consider,position));
}
break;
}
++x;
++y;
}
}
//Diagonal up-left check
{
bool gotEnemy = false;
int x = position.x-1;
int y = position.y+1;
while (x >= 0 && y < this->getBoardSize())
{
const BoardPosition consider = {x,y};
const CELL_STATE considerCell = this->getCell(consider);
if (considerCell == EMPTY)
break;
else if (considerCell == enemy)
gotEnemy = true;
else
{
if (gotEnemy)
{
valid = true;
if (flips)
flips->append(this->getCellsBetween(consider,position));
}
break;
}
--x;
++y;
}
}
//Diagonal down-right check
{
bool gotEnemy = false;
int x = position.x+1;
int y = position.y-1;
while (x < this->getBoardSize()&& y >= 0)
{
const BoardPosition consider = {x,y};
const CELL_STATE considerCell = this->getCell(consider);
if (considerCell == EMPTY)
break;
else if (considerCell == enemy)
gotEnemy = true;
else
{
if (gotEnemy)
{
valid = true;
if (flips)
flips->append(this->getCellsBetween(consider,position));
}
break;
}
++x;
--y;
}
}
//Diagonal down-left check
{
bool gotEnemy = false;
int x = position.x-1;
int y = position.y-1;
while (x >= 0 && y >= 0)
{
const BoardPosition consider = {x,y};
const CELL_STATE considerCell = this->getCell(consider);
if (considerCell == EMPTY)
break;
else if (considerCell == enemy)
gotEnemy = true;
else
{
if (gotEnemy)
{
valid = true;
if (flips)
flips->append(this->getCellsBetween(consider,position));
}
break;
}
--x;
--y;
}
}
return valid;
}
/**
* Does a move to a specific position by player
*
* @param position
* @param player
* @return bool
*/
bool Board::makeMove(BoardPosition position, CELL_STATE player)
{
if (player != this->getWhoIsNext())
{
return false;
}
QList<BoardPosition> flips;
if (!this->isValidMove(position,player,&flips)) {
QSound::play(":/sound/doh.wav");
return false;
}
const int boardSize = this->getBoardSize();
//Update corner counts
if ((position.x == 0 && position.y == 0) || (position.x == 0 && position.y == boardSize-1)
|| (position.x == boardSize-1 && position.y == 0) || (position.x == boardSize-1 && position.y == boardSize-1))
{
if (player == WHITE)
this->whiteCornerPoints++;
else
this->blackCornerPoints++;
}
// the move with flips
foreach(BoardPosition theirPos, flips)
{
this->setCell(theirPos,player);
this->incrementCount(player);
this->decrementCount(this->getEnemyOf(player));
}
this->setCell(position,player);
this->incrementCount(player);
this->whoseNext = this->getEnemyOf(this->getWhoIsNext());
if (this->getValidMoves(this->getWhoIsNext()).isEmpty())
{
this->whoseNext = this->getEnemyOf(this->getWhoIsNext());
if (this->getValidMoves(this->getWhoIsNext()).isEmpty())
{
this->whoseNext = EMPTY;
this->boolGameOver = true;
this->gameOver(this->getWinningColor(), this->getWhitePoints(), this->getBlackPoints());
}
}
//Update edge counts
int wCount = 0;
int bCount = 0;
for (int x = 0; x < boardSize; x++)
{
for (int y = 0; y < boardSize; y++)
{
BoardPosition position = {x,y};
if (position.x == 0 || position.y == 0 || position.x == boardSize-1 || position.y == boardSize-1)
{
const CELL_STATE owner = this->getCell(position);
if (owner == WHITE)
{
++wCount;
//qDebug() << position.x << position.y << "is white";
}
else if (owner == BLACK)
{
++bCount;
//qDebug() << position.x << position.y << "is black";
}
}
}
}
this->whiteEdgePoints = wCount;
this->blackEdgePoints = bCount;
this->lastMove = position;
this->moveMade(player,this->getWhoIsNext());
return true;
}
/**
* Get board size dimension
*
* @return int
*/
int Board::getBoardSize() const
{
return this->boardSize;
}
/**
* Get board style (0=Default, 1=Future, 2=Super Mario)
*
* @return int
*/
int Board::getBoardStyle()
{
return this->boardStyle;
}
/**
* Checks if cell is occupied by a player at a specific position
*
* @param position
* @return bool
*/
bool Board::isCellOccupied(BoardPosition position) const
{
return (this->getCell(position) != EMPTY);
}
/**
* Get the CELL_STATE (0=EMPTY, 1=WHITE, 2=BLACK) at a specific position
*
* @param position
* @return CELL_STATE
*/
CELL_STATE Board::getCell(BoardPosition position) const
{
return this->board[this->xy2index(position)];
}
/**
* Get who's turn is next
*
* @return CELL_STATE
*/
CELL_STATE Board::getWhoIsNext() const
{
return this->whoseNext;
}
/**
* Is the game over?
*
* @return bool
*/
bool Board::isGameOver() const
{
return this->boolGameOver;
}
/**
* Who won?
*
* @return CELL_STATE
*/
CELL_STATE Board::getWinningColor() const
{
if (this->getWhitePoints() > this->getBlackPoints())
return BLACK;
else if (this->getWhitePoints() < this->getBlackPoints())
return WHITE;
else
return EMPTY;
}
/**
* A getter for the actual calculated best move
*
* @return BoardPosition
*/
BoardPosition Board::getBestMove() const
{
return this->bestMove;
}
/**
* A getter for the last made move
*
* @return BoardPosition
*/
BoardPosition Board::getLastMove() const
{
return this->lastMove;
}
/**
* Calculates best move by using minimax algorithm for a specific player and ai difficulty
*
* @param player
* @param difficulty
*/
void Board::calculateBestMove(CELL_STATE player,int difficulty)
{
QSharedPointer<Board> board(new Board(*this));
Minimax minimax(board, difficulty);
minimax.search();
this->bestMove = minimax.getBestMove();
}
/**
* Initialize a new board
*
*/
void Board::initializeBoard()
{
if (this->board != 0)
{
delete this->board;
this->board = 0;
}
const int cells = this->boardSize*this->boardSize;
this->board = new CELL_STATE[cells];
for (int x = 0; x < this->boardSize; x++)
{
for (int y = 0; y < this->boardSize; y++)
{
const BoardPosition position = {x,y};
this->board[this->xy2index(position)] = EMPTY;
}
}
int size = this->getBoardSize();
int style = this->getBoardStyle();
//Initialize board positions
const BoardPosition ul = {size/2-1,size/2};
const BoardPosition ur = {size/2,size/2};
const BoardPosition bl = {size/2-1,size/2-1};
const BoardPosition br = {size/2,size/2-1};
//const BoardPosition temp = {5,3};
//Start positions
this->board[this->xy2index(ul)] = WHITE;
this->board[this->xy2index(ur)] = BLACK;
this->board[this->xy2index(bl)] = BLACK;
this->board[this->xy2index(br)] = WHITE;
//this->board[this->xy2index(temp)] = WHITE;
this->whoseNext = BLACK;
this->whitePoints = 2;
this->blackPoints = 2;
this->boolGameOver = false;
this->boardChanged();
this->scoreChanged(this->getWhitePoints(),this->getBlackPoints());
//this->calculateBestMove(this->getWhoIsNext());
}
/**
* Gets the absolut postion by BoardPosition
*
* @param position
* @return int
*/
int Board::xy2index(BoardPosition position) const
{
return position.y*this->getBoardSize() + position.x;
}
/**
* Gets the absolut postion by x and y coordinates
*
* @param x
* @param y
* @return int
*/
int Board::xy2index(int x, int y) const
{
return y*this->getBoardSize() + x;
}
/**
* Get's the enemey of a player
* Need this for valid move and makeMove methods
*
*
* @param color
* @return CELL_STATE
*/
CELL_STATE Board::getEnemyOf(CELL_STATE color) const
{
if (color == WHITE)
return BLACK;
else if (color == BLACK)
return WHITE;
return EMPTY;
}
/**
* Get all cells between two BoardPostions as a list
*
* @param p1
* @param p2
* @return QList<BoardPosition>
*/
QList<BoardPosition> Board::getCellsBetween(BoardPosition p1, BoardPosition p2) const
{
QList<BoardPosition> toRet;
int dx;
int dy;
if (p2.x < p1.x)
dx = 1;
else if (p2.x > p1.x)
dx = -1;
else
dx = 0;
if (p2.y < p1.y)
dy = 1;
else if (p2.y > p1.y)
dy = -1;
else
dy = 0;
BoardPosition start = {p2.x + dx, p2.y + dy};
while (start.x != p1.x || start.y != p1.y)
{
toRet.append(start);
start.x += dx;
start.y += dy;
}
return toRet;
}
/**
* Set a cell at a specific postion to a given color (WHITE or BLACK)
*
* @param position
* @param color
*/
void Board::setCell(BoardPosition position, CELL_STATE color)
{
this->board[this->xy2index(position)] = color;
this->boardChanged();
}
/**
* Increments points for a given player
*
* @param color
*/
void Board::incrementCount(CELL_STATE color)
{
if (color == EMPTY)
return;
else if (color == BLACK)
this->blackPoints++;
else
this->whitePoints++;
this->scoreChanged(this->getWhitePoints(),this->getBlackPoints());
}
/**
* Decrement points for a given player
*
* @param color
*/
void Board::decrementCount(CELL_STATE color)
{
if (color == EMPTY) {
return;
}
else if (color == BLACK) {
this->blackPoints--;
}
else {
this->whitePoints--;
}
this->scoreChanged(this->getWhitePoints(),this->getBlackPoints());
}