-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
122 lines (102 loc) · 2.22 KB
/
Game.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
#include "Game.h"
#include "Player.h"
#include "Board.h"
#include "move.h"
#include "Spot.h"
#include "Piece.h"
#include "king.h"
void Game::initialize(Player *p1, Player *p2)
{
players[0] = p1;
players[1] = p2;
board->resetBoard();
if (p1->isWhiteSide())
{
this->currentTurn = p1;
}
else
{
this->currentTurn = p2;
}
movesPlayed->clear();
}
bool Game::isEnd()
{
return this->getStatus() != GameStatus::ACTIVE;
}
bool Game::getStatus()
{
return this->status;
}
void Game::setStatus(GameStatus status)
{
this->status = status;
}
bool Game::playerMove(Player *player, int startX, int startY, int endX, int endY)
{
Spot *startBox = board->getBox(startX, startY);
Spot *endBox = board->getBox(startY, endY);
Move *move = new Move(player, startBox, endBox);
delete move;
return this->makeMove(move, player);
}
bool Game::makeMove(Move *move, Player *player)
{
Piece *sourcePiece = move->getStart().getPiece();
if (sourcePiece == nullptr)
{
return false;
}
// valid player
if (player != currentTurn)
{
return false;
}
if (sourcePiece->isWhite() != player->isWhiteSide())
{
return false;
}
// valid move?
if (!sourcePiece->canMove(board, move->getStart(), move->getEnd()))
{
return false;
}
// kill?
Piece *destPiece = move->getStart().getPiece();
if (destPiece != nullptr)
{
destPiece->setKilled(true);
move->setPieceKilled(destPiece);
}
// castling?
if (sourcePiece != nullptr && dynamic_cast<King*>(sourcePiece) != nullptr && sourcePiece->isCastlingMove())
{
move->setCastlingMove(true);
}
// store the move
movesPlayed->add(move);
// move piece from the stat box to end box
move->getEnd().setPiece(move->getStart().getPiece());
move->getStart.setPiece(nullptr);
if (destPiece != nullptr && dynamic_cast<King*>(destPiece) != nullptr)
{
if (player->isWhiteSide())
{
this->setStatus(GameStatus::WHITE_WIN);
}
else
{
this->setStatus(GameStatus::BLACK_WIN);
}
}
// set the current turn to the other player
if (this->currentTurn == players[0])
{
this->currentTurn = players[1];
}
else
{
this->currentTurn = players[0];
}
return true;
}