forked from zaphodgjd/class-chess-123
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.cpp
227 lines (194 loc) · 4.96 KB
/
Application.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
#include "Application.h"
#include "imgui/imgui.h"
#include "classes/Chess.h"
#ifdef DEBUG
#include "tools/Logger.h"
#endif
namespace ClassGame {
Chess *game = nullptr;
bool gameOver = false;
int gameWinner = -1;
// game starting point
// this is called by the main render loop in main.cpp
void GameStartUp() {
game = new Chess();
game->setUpBoard();
}
void drawMoveProber() {
const ImGuiTableFlags flags = ImGuiTableFlags_Borders |
ImGuiTableFlags_RowBg;
ImGui::BeginChild("Moves", ImVec2(0, 0), true);
if (ImGui::BeginTable("Move Prober", 2, flags)) {
ImGui::TableSetupColumn("I", ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_DefaultSort, 12.0f);
ImGui::TableSetupColumn("Moves", ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_DefaultSort);
ImGui::TableHeadersRow();
std::vector<Move> moves = game->getMoves();
std::vector<std::vector<Move>> moveList;
moveList.resize(64);
for (const Move& move : moves) {
moveList[move.getFrom()].emplace_back(move);
}
for(int i = 0; i < 64; i++) {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text("%s", ChessSquare::indexToPosNotation(i).c_str());
ImGui::TableSetColumnIndex(1);
std::string position;
for (const Move& move : moveList[i]) {
position += ChessSquare::indexToPosNotation(move.getTo()) + " ";
}
ImGui::Text("%s", position.c_str());
}
ImGui::EndTable();
}
ImGui::EndChild();
}
const std::map<ChessPiece, char> symbolFromPiece = {
{ChessPiece::Pawn, 'p'},
{ChessPiece::Knight, 'n'},
{ChessPiece::Bishop, 'b'},
{ChessPiece::Rook, 'r'},
{ChessPiece::Queen, 'q'},
{ChessPiece::King, 'k'}
};
void drawState() {
ImGui::BeginChild("State", ImVec2(300, 400), true);
GameState state = game->getState();
uint8_t castling = state.getCastlingRights();
std::string castleString;
if ((castling & (1 << 3)) != 0) {
castleString += 'K';
}
if ((castling & (1 << 2)) != 0) {
castleString += 'Q';
}
if ((castling & (1 << 1)) != 0) {
castleString += 'k';
}
if ((castling & (1 << 0)) != 0) {
castleString += 'q';
}
if (castling == 0) {
castleString += '-';
}
ImGui::Text("Game State");
ImGui::Text("Clock: %d", state.getClock());
ImGui::Text("Half Clock: %d", state.getHalfClock());
ImGui::Text("Black's Turn? %d", state.isBlackTurn());
ImGui::Text("En Passant Square: %s", ChessSquare::indexToPosNotation(state.getEnPassantSquare()).c_str());
ImGui::Text("Castle Rights %s", castleString.c_str());
std::string stateStr = "";
ProtoBoard bitboard = state.getProtoBoard();
int file = 8;
int rank = 0;
for (int k = 0; k < 64; k++) {
if (k % 8 == 0) {
stateStr += '\n';
file--;
rank = 0;
}
ChessPiece piece = bitboard.PieceFromIndex(file * 8 + rank);
if (piece == ChessPiece::NoPiece) {
stateStr += '0';
rank++;
continue;
}
char symbol = symbolFromPiece.at((ChessPiece)(piece & 7));
if ((piece & 8) == 0) {
symbol = toupper(symbol);
}
stateStr += symbol;
rank++;
}
ImGui::Text("%s", stateStr.c_str());
for (int i = 0; i < 12; i++) {
std::string s;
int piece = 0;
if (i < 6) {
s += 'w';
} else {
s += 'b';
piece = 8;
}
int j = i < 6 ? i : i - 6;
switch (j) {
case 0:
s += "P ";
piece |= 0b001;
break;
case 1:
s += "Kn";
piece |= 0b010;
break;
case 2:
s += "B ";
piece |= 0b011;
break;
case 3:
s += "R ";
piece |= 0b100;
break;
case 4:
s += "Q ";
piece |= 0b101;
break;
case 5:
s += "K ";
piece |= 0b110;
break;
}
s += " -- ";
auto pos = bitboard.getBitPositions((ChessPiece)piece);
for (int i : pos) {
s += ChessSquare::indexToPosNotation(i) + " ";
}
ImGui::Text("%s", s.c_str());
}
ImGui::EndChild();
}
// game render loop
// this is called by the main render loop in main.cpp
void RenderGame() {
ImGui::DockSpaceOverViewport(ImGui::GetMainViewport());
//ImGui::ShowDemoWindow();
ImGui::Begin("Settings");
ImGui::Text("Current Player Number: %d", game->getCurrentPlayer()->playerNumber());
ImGui::Text("Current Board State: %s", game->stateString().c_str());
if (gameOver) {
ImGui::Text("\nGame Over!");
if (gameWinner != -1) {
ImGui::Text("Winner: %d", gameWinner);
} else {
ImGui::Text("Stalemate!");
}
if (ImGui::Button("Reset Game")) {
game->stopGame();
game->setUpBoard();
gameOver = false;
gameWinner = -1;
}
}
drawState();
drawMoveProber();
ImGui::End();
ImGui::Begin("GameWindow");
game->drawFrame();
ImGui::End();
#ifdef DEBUG
Loggy.draw();
#endif
}
// end turn is called by the game code at the end of each turn
// this is where we check for a winner
void EndOfTurn() {
Player *winner = game->checkForWinner();
if (winner) {
gameOver = true;
gameWinner = winner->playerNumber();
}
if (game->checkForDraw()) {
gameOver = true;
gameWinner = -1;
}
}
}