-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShatranjBoard.cpp
163 lines (155 loc) · 3.45 KB
/
ShatranjBoard.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
#include "ShatranjBoard.h"
ShatranjBoard::ShatranjBoard()
{
_width = 8;
_height = 8;
ShatranjBoard::Initialize();
}
ShatranjBoard::~ShatranjBoard()
{
}
Board* ShatranjBoard::Clone()
{
ShatranjBoard* cb = new ShatranjBoard();
for (int i = 0; i < GetWidth(); i++)
{
for (int j = 0; j < GetHeight(); j++)
{
const Piece* p = GetData(i, j);
cb->SetData(i, j, p != nullptr ? cb->CreatePiece(p->GetType(), p->GetColour()) : nullptr);
}
}
return cb;
}
void ShatranjBoard::Initialize()
{
_moveCount = 0;
_pgn = "";
for (int i = 0; i < _width; i++)
{
for (int j = 0; j < _height; j++)
{
if (_initialSetup[j][i] != None)
{
_data[i][j] = new ShatranjPiece(_initialSetup[j][i], j < 5 ? Black : White);
}
else
{
_data[i][j] = nullptr;
}
}
}
}
Piece* ShatranjBoard::CreatePiece(PieceType pieceType, PieceColour pieceColour)
{
return new ShatranjPiece(pieceType, pieceColour);
}
void ShatranjBoard::GetMoves(Piece* piece, int x, int y)
{
_moves.clear();
switch (piece->GetType())
{
case King:
CheckMove(piece, x + 1, y + 1);
CheckMove(piece, x + 1, y);
CheckMove(piece, x + 1, y - 1);
CheckMove(piece, x, y + 1);
CheckMove(piece, x, y - 1);
CheckMove(piece, x - 1, y + 1);
CheckMove(piece, x - 1, y);
CheckMove(piece, x - 1, y - 1);
break;
case Queen:
CheckMove(piece, x + 1, y + 1);
CheckMove(piece, x + 1, y - 1);
CheckMove(piece, x - 1, y + 1);
CheckMove(piece, x - 1, y - 1);
break;
case Rook:
CheckDirection(piece, x, y, North);
CheckDirection(piece, x, y, East);
CheckDirection(piece, x, y, West);
CheckDirection(piece, x, y, South);
break;
case Bishop:
CheckMove(piece, x + 2, y + 2);
CheckMove(piece, x + 2, y - 2);
CheckMove(piece, x - 2, y + 2);
CheckMove(piece, x - 2, y - 2);
break;
case Pawn:
if (piece->GetColour() == Black)
{
if (y + 1 < _height && _data[x][y + 1] == nullptr)
{
CheckMove(piece, x, y + 1);
}
if (y + 1 < _height && x + 1 < _width && _data[x + 1][y + 1] != nullptr)
{
CheckMove(piece, x + 1, y + 1);
}
if (y + 1 < _height && x - 1 >= 0 && _data[x - 1][y + 1] != nullptr)
{
CheckMove(piece, x - 1, y + 1);
}
}
else
{
if (y - 1 >= 0 && _data[x][y - 1] == nullptr)
{
CheckMove(piece, x, y - 1);
}
if (y - 1 >= 0 && x + 1 < _width && _data[x + 1][y - 1] != nullptr)
{
CheckMove(piece, x + 1, y - 1);
}
if (y - 1 >= 0 && x - 1 >= 0 && _data[x - 1][y - 1] != nullptr)
{
CheckMove(piece, x - 1, y - 1);
}
}
break;
case Knight:
CheckMove(piece, x + 1, y + 2);
CheckMove(piece, x - 1, y + 2);
CheckMove(piece, x + 2, y + 1);
CheckMove(piece, x + 2, y - 1);
CheckMove(piece, x - 2, y + 1);
CheckMove(piece, x - 2, y - 1);
CheckMove(piece, x + 1, y - 2);
CheckMove(piece, x - 1, y - 2);
break;
default:
break;
}
}
void ShatranjBoard::WriteMove(PieceType pieceType, int x1, int y1, int x2, int y2, char promotion, bool capture)
{
if (_moveCount % 2 == 0)
{
_pgn += std::to_string((_moveCount / 2) + 1) + ". "; // Add move number for white
}
if (pieceType != Pawn)
{
_pgn += _pieceToPGN.at(pieceType);
}
_pgn.push_back(static_cast<char>(x1 + 97));
_pgn += std::to_string(_height - y1);
if (capture)
{
_pgn += "x";
}
_pgn.push_back(static_cast<char>(x2 + 97));
_pgn += std::to_string(y2);
if (promotion != ' ')
{
_pgn += "=";
_pgn += static_cast<char>(std::toupper(promotion));
}
_pgn += " ";
_moveCount++;
}
std::string ShatranjBoard::GetPGN()
{
return _pgn;
}