Skip to content

Commit b75da70

Browse files
committed
Constans fix
1 parent 9931c46 commit b75da70

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

src/board.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl Board {
144144
_ => panic!("Invalid FEN"),
145145
};
146146

147-
self.add_piece(color, piece, rank * 8 + file);
147+
self.add_piece(color, piece, rank * BOARD_WIDTH + file);
148148
file += 1;
149149
}
150150
}
@@ -237,27 +237,27 @@ impl Board {
237237
fn square_to_index(square: &str) -> usize {
238238
let file = square.chars().nth(0).unwrap() as usize - 'a' as usize;
239239
let rank = square.chars().nth(1).unwrap() as usize - '1' as usize;
240-
rank * 8 + file
240+
rank * BOARD_WIDTH + file
241241
}
242242

243243
fn index_to_square(index: usize) -> String {
244-
let file = (index % 8) as u8 + b'a';
245-
let rank = (index / 8) as u8 + b'1';
244+
let file = (index % BOARD_WIDTH) as u8 + b'a';
245+
let rank = (index / BOARD_WIDTH) as u8 + b'1';
246246
format!("{}{}", file as char, rank as char)
247247
}
248248

249249
fn is_square_empty(&self, index: usize) -> bool {
250250
!self.white_occupancy.is_set(index) && !self.black_occupancy.is_set(index)
251251
}
252252

253-
pub fn is_index_in_bounds(index: i64) -> bool {
254-
index >= 0 && index < 64
253+
pub fn is_index_in_bounds(index: i32) -> bool {
254+
index >= 0 && index < BOARD_SIZE as i32
255255
}
256256

257257
pub fn print(&self) {
258-
for rank in (0..8).rev() {
259-
for file in 0..8 {
260-
let index = rank * 8 + file;
258+
for rank in (0..BOARD_WIDTH).rev() {
259+
for file in 0..BOARD_WIDTH {
260+
let index = rank * BOARD_WIDTH + file;
261261
let piece = if self.white_pieces.pawns.is_set(index) {
262262
'P'
263263
} else if self.white_pieces.knights.is_set(index) {
@@ -295,8 +295,8 @@ impl Board {
295295
pub fn make_move(&mut self, mv: &Move) {
296296
if mv.en_passant {
297297
let direction = match mv.color {
298-
Color::White => 8,
299-
Color::Black => -8,
298+
Color::White => MOVE_UP,
299+
Color::Black => MOVE_DOWN,
300300
};
301301
self.remove_piece(mv.color, Piece::Pawn, mv.from);
302302
self.add_piece(mv.color, Piece::Pawn, mv.to);
@@ -350,10 +350,10 @@ impl Board {
350350

351351
if mv.en_passant {
352352
let direction = match mv.color {
353-
Color::White => 8,
354-
Color::Black => -8,
353+
Color::White => MOVE_UP,
354+
Color::Black => MOVE_DOWN,
355355
};
356-
self.en_passant_square = Some((mv.to as i64 - direction) as usize);
356+
self.en_passant_square = Some((mv.to as i32 - direction) as usize);
357357
}
358358

359359
self.turn = match self.turn {
@@ -398,19 +398,19 @@ impl Board {
398398

399399
// TODO: Validate check
400400

401-
for i in 0..64 {
401+
for i in 0..BOARD_SIZE {
402402
if !pawns.is_set(i) {
403403
continue;
404404
}
405405

406406
let direction = match self.turn {
407-
Color::White => 8,
408-
Color::Black => -8,
407+
Color::White => MOVE_UP,
408+
Color::Black => MOVE_DOWN,
409409
};
410410

411411
let pos = i;
412412
let from = i;
413-
let possible_to = i as i64 + direction;
413+
let possible_to = i as i32 + direction;
414414

415415
if !Board::is_index_in_bounds(possible_to) {
416416
continue;
@@ -419,10 +419,10 @@ impl Board {
419419
let to = possible_to as usize;
420420

421421
// DOUBLE PUSH
422-
if (RANK_2.is_set(pos) && self.turn == Color::White)
423-
|| (RANK_7.is_set(pos) && self.turn == Color::Black)
422+
if (ROW_2.is_set(pos) && self.turn == Color::White)
423+
|| (ROW_7.is_set(pos) && self.turn == Color::Black)
424424
{
425-
let double = to as i64 + direction;
425+
let double = to as i32 + direction;
426426
if self.is_square_empty(to) && self.is_square_empty(double as usize) {
427427
moves.push(Move {
428428
from,
@@ -465,8 +465,8 @@ impl Board {
465465
}
466466

467467
// PROMOTION
468-
if (self.turn == Color::White && RANK_7.is_set(pos) && self.is_square_empty(to))
469-
|| (self.turn == Color::Black && RANK_2.is_set(pos) && self.is_square_empty(to))
468+
if (self.turn == Color::White && ROW_7.is_set(pos) && self.is_square_empty(to))
469+
|| (self.turn == Color::Black && ROW_2.is_set(pos) && self.is_square_empty(to))
470470
{
471471
moves.push(Move {
472472
from,

src/constans.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
use crate::bitboard::Bitboard;
22

3-
pub const RANK_1: Bitboard = Bitboard(0x00000000000000FF);
4-
pub const RANK_2: Bitboard = Bitboard(0x000000000000FF00);
5-
pub const RANK_3: Bitboard = Bitboard(0x0000000000FF0000);
6-
pub const RANK_4: Bitboard = Bitboard(0x00000000FF000000);
7-
pub const RANK_5: Bitboard = Bitboard(0x000000FF00000000);
8-
pub const RANK_6: Bitboard = Bitboard(0x0000FF0000000000);
9-
pub const RANK_7: Bitboard = Bitboard(0x00FF000000000000);
10-
pub const RANK_8: Bitboard = Bitboard(0xFF00000000000000);
3+
pub const BOARD_SIZE: usize = 64;
4+
pub const BOARD_WIDTH: usize = 8;
5+
pub const MOVE_UP: i32 = 8;
6+
pub const MOVE_DOWN: i32 = -8;
7+
pub const MOVE_LEFT: i32 = -1;
8+
pub const MOVE_RIGHT: i32 = 1;
9+
pub const EMPTY: Bitboard = Bitboard(0x0000000000000000);
10+
pub const ROW_1: Bitboard = Bitboard(0x00000000000000FF);
11+
pub const ROW_2: Bitboard = Bitboard(0x000000000000FF00);
12+
pub const ROW_3: Bitboard = Bitboard(0x0000000000FF0000);
13+
pub const ROW_4: Bitboard = Bitboard(0x00000000FF000000);
14+
pub const ROW_5: Bitboard = Bitboard(0x000000FF00000000);
15+
pub const ROW_6: Bitboard = Bitboard(0x0000FF0000000000);
16+
pub const ROW_7: Bitboard = Bitboard(0x00FF000000000000);
17+
pub const ROW_8: Bitboard = Bitboard(0xFF00000000000000);
1118

12-
pub const FILE_A: Bitboard = Bitboard(0x0101010101010101);
13-
pub const FILE_B: Bitboard = Bitboard(0x0202020202020202);
14-
pub const FILE_C: Bitboard = Bitboard(0x0404040404040404);
15-
pub const FILE_D: Bitboard = Bitboard(0x0808080808080808);
16-
pub const FILE_E: Bitboard = Bitboard(0x1010101010101010);
17-
pub const FILE_F: Bitboard = Bitboard(0x2020202020202020);
18-
pub const FILE_G: Bitboard = Bitboard(0x4040404040404040);
19-
pub const FILE_H: Bitboard = Bitboard(0x8080808080808080);
19+
pub const COL_A: Bitboard = Bitboard(0x0101010101010101);
20+
pub const COL_B: Bitboard = Bitboard(0x0202020202020202);
21+
pub const COL_C: Bitboard = Bitboard(0x0404040404040404);
22+
pub const COL_D: Bitboard = Bitboard(0x0808080808080808);
23+
pub const COL_E: Bitboard = Bitboard(0x1010101010101010);
24+
pub const COL_F: Bitboard = Bitboard(0x2020202020202020);
25+
pub const COL_G: Bitboard = Bitboard(0x4040404040404040);
26+
pub const COL_H: Bitboard = Bitboard(0x8080808080808080);
2027

2128
pub const STARTING_POSITION: &str = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
2229

0 commit comments

Comments
 (0)