-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.hpp
More file actions
327 lines (252 loc) · 12.5 KB
/
Board.hpp
File metadata and controls
327 lines (252 loc) · 12.5 KB
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
#ifndef BOARD_HPP
#define BOARD_HPP
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <cstdint>
#include <vector>
#include <bitset>
#include "Math.hpp"
#include "Move.hpp"
#include "Utils.hpp"
#include "Utils.hpp"
#define TILE_WIDTH 75
#define PIECE_WIDTH 40
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 720
enum class BitboardPieceType : uint8_t {
White = 0, Black, Pawn, Knight, Bishop, Queen, King, Rook
};
class Board{
public:
void render(){
drawBoard();
drawPieces();
}
//Sets the renderer
void setRenderer(SDL_Renderer* in_rend){
this->renderer = in_rend;
}
void setFenPosition(string in_fen){
this->fen = in_fen;
}
//Currently does not do anything lol
void load_from_fen(string fen){
int row = 0; int col = 0;
for (char ch : fen){
if (ch ==' ')
break;
if (isalpha(ch)){
this->squares[row][col] = ch;
++col;
}else if (isnumber(ch)){
col += ch - '0';
}else if (ch == '/'){
++row;
col = 0;
}
}
}
//Initializes the bitboards. Could initialize from fen later
void init_board(){
bitboards.resize(8);
this->squares.resize(8, vector<char>(8, ' '));
load_from_fen(fen); // Starting position
//("4k3/p7/8/8/5R2/8/8/4K3 b - - 0 3");
//load_from_fen("4k3/6N1/5b2/3R4/8/8/8/4K3 b - - 0 3"); // Double check example
bitset<64> first_bit_string; first_bit_string.set(63, true); // Something like 10000000000... for the top left corner
for (int row = 0; row < 8; ++row){
for (int col = 0; col < 8; ++col){
char current_piece = this->squares[row][col];
bitset<64> curr_piece_bit_string = first_bit_string >> (row*8+col);
switch (tolower(current_piece))
{
case 'p':
bitboards[(int)BitboardPieceType::Pawn] = bitboards[(int)BitboardPieceType::Pawn] | curr_piece_bit_string;
break;
case 'r':
bitboards[(int)BitboardPieceType::Rook] = bitboards[(int)BitboardPieceType::Rook] | curr_piece_bit_string;
break;
case 'n':
bitboards[(int)BitboardPieceType::Knight] = bitboards[(int)BitboardPieceType::Knight] | curr_piece_bit_string;
break;
case 'b':
bitboards[(int)BitboardPieceType::Bishop] = bitboards[(int)BitboardPieceType::Bishop] | curr_piece_bit_string;
break;
case 'q':
bitboards[(int)BitboardPieceType::Queen] = bitboards[(int)BitboardPieceType::Queen] | curr_piece_bit_string;
break;
case 'k':
bitboards[(int)BitboardPieceType::King] = bitboards[(int)BitboardPieceType::King] | curr_piece_bit_string;
break;
default:
break;
}
if (current_piece != ' '){
if (tolower(current_piece) == current_piece){
//it is black
bitboards[(int)BitboardPieceType::Black] = bitboards[(int)BitboardPieceType::Black] | curr_piece_bit_string;
}else{
//it is white
bitboards[(int)BitboardPieceType::White] = bitboards[(int)BitboardPieceType::White] | curr_piece_bit_string;
}
}
}
}
filenames = {"orangePawn.png", "orangeKnight.png", "orangeBishop.png", "orangeQueen.png", "orangeKing.png", "orangeRook.png",
"bluePawn.png", "blueKnight.png", "blueBishop.png", "blueQueen.png", "blueKing.png", "blueRook.png"};
}
Position getTilePos(SDL_Point mouse_pos){
this->mouse_pos = mouse_pos;
this->is_dragging = true;
int start_x = (SCREEN_WIDTH/2) - 4*TILE_WIDTH; int start_y = (SCREEN_HEIGHT/2) - 4*TILE_WIDTH;
mouse_pos.x -= start_x;
mouse_pos.y -= start_y;
int row_remainder = mouse_pos.y % TILE_WIDTH;
int row_ind = (int)(mouse_pos.y - row_remainder) / TILE_WIDTH;
int col_remainder = mouse_pos.x % TILE_WIDTH;
int col_ind = (int)(mouse_pos.x - col_remainder) / TILE_WIDTH;
// cout << "From --> Row: " << row_ind << " , Col: " << col_ind << endl;
drag_from = {row_ind, col_ind};
return drag_from;
}
void updateMouse(SDL_Point mouse_pos) {
this->mouse_pos = mouse_pos;
}
void stopDragging(){
this->is_dragging = false;
}
void makeMove(Move* move){
int from_i = 63 - (move->from.row*8 + move->from.col);
int to_i = 63 - (move->to.row*8 + move->to.col);
bool is_white = bitboards[(int)BitboardPieceType::White][from_i] == 1;
for (int i = 2; i < 8; ++i){ // Basically untested
bitboards[i][to_i] = 0;
if (bitboards[i][from_i] == 1){
bitboards[i][from_i] = 0;
bitboards[i][to_i] = 1;
}
}
if (is_white){
bitboards[0][from_i] = 0;
bitboards[0][to_i] = 1;
bitboards[1][from_i] = 0;
bitboards[1][to_i] = 0;
}else{
bitboards[1][from_i] = 0;
bitboards[1][to_i] = 1;
bitboards[0][from_i] = 0;
bitboards[0][to_i] = 0;
}
bitboards[(int)!is_white][to_i] = 1;
}
//*********************************//
//***** Class Level Variables *****//
//*********************************//
SDL_Renderer* renderer;
bool player_side;
bool first = true;
Position drag_from;
SDL_Point mouse_pos;
bool is_dragging;
vector<bitset<64> > bitboards;
uint64_t empty_board = 0;
uint64_t full_board;
vector<vector<char> > squares;
private:
// DRAWING AND RENDERING SECTION
void drawPieces(){
bitset<64> top_left; top_left.set(63, true);
int start_x = (SCREEN_WIDTH/2) - 4*TILE_WIDTH; int start_y = (SCREEN_HEIGHT/2) - 4*TILE_WIDTH;
for (int row = 0; row < 8; ++row){
for (int col = 0; col < 8; ++col){
int new_x = start_x + col * TILE_WIDTH;
int new_y = start_y + row * TILE_WIDTH;
//Put in center of tile
new_x += TILE_WIDTH/2 - PIECE_WIDTH/2;
new_y += TILE_WIDTH/2 - PIECE_WIDTH/2;
Vector2i new_pos(new_x, new_y);
//If this is the piece that is being dragged...
if (is_dragging && row == drag_from.row && col == drag_from.col){
new_pos = {mouse_pos.x - PIECE_WIDTH/2, mouse_pos.y - PIECE_WIDTH/2};
}
bitset<64> current_spot = top_left >> (row*8 + col);
if ( (current_spot & bitboards[(int)BitboardPieceType::White]) != 0){ //It is a white piece
//cout << "Trying to draw a piece!!" << endl;
//drawTile(new_pos, PIECE_WIDTH, this->lightPieceColor, true);
if ( (current_spot & bitboards[(int)BitboardPieceType::Pawn]) != 0){
drawPiece(new_pos, PIECE_WIDTH, PieceType::WhitePawn);
} else if ( (current_spot & bitboards[(int)BitboardPieceType::Rook]) != 0){
drawPiece(new_pos, PIECE_WIDTH, PieceType::WhiteRook);
} else if ( (current_spot & bitboards[(int)BitboardPieceType::Knight]) != 0){
drawPiece(new_pos, PIECE_WIDTH, PieceType::WhiteKnight);
}else if ( (current_spot & bitboards[(int)BitboardPieceType::Bishop]) != 0){
drawPiece(new_pos, PIECE_WIDTH, PieceType::WhiteBishop);
}else if ( (current_spot & bitboards[(int)BitboardPieceType::Queen]) != 0){
drawPiece(new_pos, PIECE_WIDTH, PieceType::WhiteQueen);
}else if ( (current_spot & bitboards[(int)BitboardPieceType::King]) != 0){
drawPiece(new_pos, PIECE_WIDTH, PieceType::WhiteKing);
}
}
if ( (current_spot & bitboards[(int)BitboardPieceType::Black]) != 0){ //It is a Black piece
//drawTile(new_pos, PIECE_WIDTH, this->darkPieceColor, true);
if ( (current_spot & bitboards[(int)BitboardPieceType::Pawn]) != 0){
drawPiece(new_pos, PIECE_WIDTH, PieceType::BlackPawn);
} else if ( (current_spot & bitboards[(int)BitboardPieceType::Rook]) != 0){
drawPiece(new_pos, PIECE_WIDTH, PieceType::BlackRook);
} else if ( (current_spot & bitboards[(int)BitboardPieceType::Knight]) != 0){
drawPiece(new_pos, PIECE_WIDTH, PieceType::BlackKnight);
}else if ( (current_spot & bitboards[(int)BitboardPieceType::Bishop]) != 0){
drawPiece(new_pos, PIECE_WIDTH, PieceType::BlackBishop);
}else if ( (current_spot & bitboards[(int)BitboardPieceType::Queen]) != 0){
drawPiece(new_pos, PIECE_WIDTH, PieceType::BlackQueen);
}else if ( (current_spot & bitboards[(int)BitboardPieceType::King]) != 0){
drawPiece(new_pos, PIECE_WIDTH, PieceType::BlackKing);
}
}
}
}
}
void drawBoard(){
//background color
SDL_SetRenderDrawColor(renderer, 172, 221, 214, 120);
SDL_RenderClear(renderer);
int start_x = (SCREEN_WIDTH/2) - 4*TILE_WIDTH; int start_y = (SCREEN_HEIGHT/2) - 4*TILE_WIDTH;
for (int row = 0; row < 8; ++row){
for (int col = 0; col < 8; ++col){
int new_x = start_x + col * TILE_WIDTH;
int new_y = start_y + row * TILE_WIDTH;
Vector2i new_pos(new_x, new_y);
if (abs(row - col) % 2 == 0){
//Draw a light tile
drawTile(new_pos, TILE_WIDTH, this->lightTileColor);
}else{
//Draw a dark tile
drawTile(new_pos, TILE_WIDTH, this->darkTileColor);
}
}
}
}
void drawTile(Vector2i& pos, int width, Color color, bool is_piece = false){
SDL_Rect* rect = new SDL_Rect();
rect->x = pos.x; rect->y = pos.y; rect->w = width; rect->h = width;
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
SDL_RenderFillRect(renderer, rect);
}
void drawPiece(Vector2i& pos, int width, PieceType piece_type){
SDL_Rect* dest = new SDL_Rect();
dest->x = pos.x; dest->y = pos.y; dest->w = width; dest->h = width;
string file = "./res/gfx/" + filenames[(int) piece_type];
const char* file_cstr = file.c_str();
SDL_Texture* texture = IMG_LoadTexture(renderer, file_cstr);
SDL_RenderCopy(renderer, texture, NULL, dest);
SDL_DestroyTexture(texture);
}
Color lightTileColor = {230, 220, 186, 255};
Color darkTileColor = {202, 167, 132, 255};
Color lightPieceColor = {255, 191, 134, 255};
Color darkPieceColor = {51, 153, 255, 255};
int i = 0;
string fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
vector<string> filenames;
};
#endif