forked from Wenfei134/chess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChessGame.cpp
88 lines (76 loc) · 2.27 KB
/
ChessGame.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
#include "ChessGame.hpp"
#include "Board.hpp"
#include "Move.hpp"
#include "Pieces/Piece.hpp"
#include <iostream>
#include <stdexcept>
// --------------------------------------------------------------------------------------------------------------------
ChessGame::ChessGame()
{
mBoard = new Board();
}
// --------------------------------------------------------------------------------------------------------------------
ChessGame::~ChessGame()
{
delete mBoard;
}
// --------------------------------------------------------------------------------------------------------------------
void ChessGame::TakeTurn(Move mv)
{
if (mState != ACTIVE)
{
throw std::logic_error("Game is already done");
}
mBoard->DoMove(mv);
// white if black, black if white
mPlayerInTurn = mPlayerInTurn == PieceColour::kWhite ? PieceColour::kBlack : PieceColour::kWhite;
if (mBoard->IsCheckmated(mPlayerInTurn))
{
if (mPlayerInTurn == PieceColour::kWhite)
{
mState = BLACK_WIN;
std::cout << "BLACK WIN";
}
else
{
mState = WHITE_WIN;
std::cout << "WHITE WIN";
}
}
else if (mBoard->IsStalemated(mPlayerInTurn))
{
mState = STALEMATE;
}
}
// --------------------------------------------------------------------------------------------------------------------
std::vector<std::vector<std::vector<Move>>> ChessGame::GetLegalMoves()
{
std::vector<std::vector<std::vector<Move>>> ret;
for (int i = 0; i < 8; i++)
{
std::vector<std::vector<Move>> row;
for (int j = 0; j < 8; j++)
{
row.emplace_back(std::vector<Move>{});
}
ret.push_back(row);
}
std::vector<Move> legalMoves = mBoard->ListLegalMoves(mPlayerInTurn);
for (auto move : legalMoves)
{
int r = move.mStart->GetRow();
int c = move.mStart->GetCol();
ret[r][c].push_back(move);
}
return ret;
}
// --------------------------------------------------------------------------------------------------------------------
int ChessGame::GetState()
{
return mState;
}
// --------------------------------------------------------------------------------------------------------------------
Board *ChessGame::GetBoard() {
return mBoard;
}
// --------------------------------------------------------------------------------------------------------------------