Skip to content

Commit

Permalink
fix: fixed some bugs about move searching + broader move encoding (fr…
Browse files Browse the repository at this point in the history
…om 16 bit to 32)
  • Loading branch information
KrinjMaster committed Apr 18, 2024
1 parent cd90848 commit c24a9e3
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 226 deletions.
350 changes: 196 additions & 154 deletions src/board.rs

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use crate::{
piece_parsing::parse_bitboards,
};

pub const WHITE_CHECKMATE: i32 = 10_000;
pub const DRAW: i32 = 0;
pub const BLACK_CHECKMATE: i32 = -10_000;
pub const CHECKMATE: i32 = 10_000;

pub const PAWN: i32 = 100;
pub const KNIGHT: i32 = 310;
Expand Down
3 changes: 2 additions & 1 deletion src/magic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// i decided not to leave magics generationg
// i decided not to leave magics generationg fns here
// dunno if you did want to see it here tho

use crate::{
board::Bitboard,
constants::{BISHOP_MOVES, ROOK_MOVES},
Expand Down
20 changes: 9 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ mod utils;
use board::BoardState;
use constants::DEFAULT_FEN_STRING;

use crate::board::Color;
// use search::negamax;
// use utils::print_bitboard;
use search::negamax;

fn main() {
// let _board = BoardState::from_fen(DEFAULT_FEN_STRING).unwrap_or_else(|err| {
// println!("{}", err);
// exit(1);
// });
let board =
BoardState::from_fen("rnbqkbnr/ppppp1pp/8/5p1Q/4P3/8/PPPP1PPP/RNB1KBNR w KQkq f6 0 1")
.expect("Fail during board setup");
println!("{}", board.is_in_check(Color::Black));
let mut board = BoardState::from_fen(DEFAULT_FEN_STRING).unwrap_or_else(|err| {
println!("{}", err);
exit(1);
});

let score = negamax(&mut board, 3);

println!("{}", score);
}

#[cfg(test)]
Expand Down
12 changes: 6 additions & 6 deletions src/move_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::constants::{
use crate::magic::{get_bishop_move, get_rook_move};

pub fn generate_pawn_moves(
pawns: Vec<(u32, u32)>,
pawns: Vec<(u8, u8)>,
bb_friendly_pieces: Bitboard,
bb_enemy_pieces: Bitboard,
bb_en_passant: Bitboard,
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn generate_pawn_moves(
}

pub fn generate_king_moves(
kings: Vec<(u32, u32)>,
kings: Vec<(u8, u8)>,
bb_friendly_pieces: Bitboard,
) -> Vec<(Bitboard, Bitboard)> {
let mut bb_moves_vec: Vec<(Bitboard, Bitboard)> = vec![];
Expand All @@ -90,7 +90,7 @@ pub fn generate_king_moves(
}

pub fn generate_knight_moves(
knights: Vec<(u32, u32)>,
knights: Vec<(u8, u8)>,
bb_friendly_pieces: Bitboard,
) -> Vec<(Bitboard, Bitboard)> {
let mut bb_moves_vec: Vec<(Bitboard, Bitboard)> = vec![];
Expand All @@ -108,7 +108,7 @@ pub fn generate_knight_moves(
}

pub fn generate_rook_moves(
rooks: Vec<(u32, u32)>,
rooks: Vec<(u8, u8)>,
bb_friendly_pieces: Bitboard,
bb_fullboard: Bitboard,
) -> Vec<(Bitboard, Bitboard)> {
Expand All @@ -125,7 +125,7 @@ pub fn generate_rook_moves(
}

pub fn generate_bishop_moves(
bishops: Vec<(u32, u32)>,
bishops: Vec<(u8, u8)>,
bb_friendly_pieces: Bitboard,
bb_fullboard: Bitboard,
) -> Vec<(Bitboard, Bitboard)> {
Expand All @@ -142,7 +142,7 @@ pub fn generate_bishop_moves(
}

pub fn generate_queen_moves(
queens: Vec<(u32, u32)>,
queens: Vec<(u8, u8)>,
bb_fullboard: Bitboard,
bb_friendly_pieces: Bitboard,
) -> Vec<(Bitboard, Bitboard)> {
Expand Down
6 changes: 3 additions & 3 deletions src/piece_parsing.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use crate::board::{Bitboard, Color};
use crate::constants::BOARD_SQUARES;

pub fn parse_bitboards(color: Color, bitboards: Bitboard) -> Vec<(u32, u32)> {
pub fn parse_bitboards(color: Color, bitboards: Bitboard) -> Vec<(u8, u8)> {
let piece_color = match color {
Color::White => 0,
Color::Black => 1,
};

let mut pieces_vector: Vec<(u32, u32)> = vec![];
let mut pieces_vector: Vec<(u8, u8)> = vec![];

let mut bb_pieces: u64 = bitboards.clone();

while bb_pieces != 0 {
let square_index: u32 = bb_pieces.trailing_zeros();

pieces_vector.push((piece_color, square_index));
pieces_vector.push((piece_color, square_index as u8));

bb_pieces ^= BOARD_SQUARES[square_index as usize];
}
Expand Down
49 changes: 26 additions & 23 deletions src/search.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
use crate::{
board::{BoardState, Color, Move},
eval::evaluate,
board::BoardState,
eval::{evaluate, CHECKMATE, DRAW},
};
use std::process::exit;

pub fn negamax(board: &mut BoardState, depth: u8, moves: Vec<Move>) -> (i32, Move) {
let mut max: i32 = -100_000;
let mut best_move: Move = 0;
pub fn negamax(board: &mut BoardState, depth: u8) -> i32 {
let mut max: i32 = -100_000_000;

if depth == 0 {
let score = evaluate(board);

return (score, best_move);
return score;
}

let moves = board.generate_moves_by_color(&board.to_move);

if moves.is_empty() {
if board.is_in_check(&board.to_move) {
return CHECKMATE;
}

return DRAW;
}

for piece_move in moves.iter() {
let (_, _, _, color, _, _) = board.decode_move(*piece_move, false);
board.make_move(*piece_move);
let (_, _, _, color, _, _) = board.decode_move(*piece_move).unwrap_or_else(|err| {
println!("{}", err);
exit(1);
});

let opposite_color: Color = match color {
Color::White => Color::Black,
Color::Black => Color::White,
};
board.make_move(*piece_move);

if board.clone().is_in_check(opposite_color) {
if board.is_in_check(&color) {
let _ = board.undo_move();
continue;
}

let score = -(negamax(
board,
depth - 1,
board.generate_moves_by_color(&board.to_move),
)
.0);
let score = negamax(board, depth - 1);

if score > max {
best_move = *piece_move;
max = score;
if -score > max {
max = -score;
}

let _ = board.undo_move();
}

(max, best_move)
max
}
26 changes: 1 addition & 25 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,7 @@ fn in_check() {
let board =
BoardState::from_fen("rnbqkbnr/ppppp1pp/8/5p1Q/4P3/8/PPPP1PPP/RNB1KBNR w KQkq f6 0 1")
.expect("Fail during board setup");
assert!(board.is_in_check(Color::Black))
}

#[test]
fn encoding_test() {
let board = BoardState::from_fen(DEFAULT_FEN_STRING).expect("Fail during board setup");

let moves = board.encode_move(62, 52, Piece::None);
// choose a white right knight move

assert_eq!(moves, 27966);
}

#[test]
fn decoding_test() {
let board = BoardState::from_fen(DEFAULT_FEN_STRING).expect("Fail during board setup");

let moves = board.decode_move(board.generate_moves_by_color(&Color::White)[18], false);
// choose a white right knight move

assert_eq!(moves.0, BOARD_SQUARES[62]);
assert_eq!(moves.1, BOARD_SQUARES[45]);
assert!(matches!(moves.2, Piece::Knight));
assert!(matches!(moves.3, Color::White));
assert!(matches!(moves.4, Piece::None));
assert!(board.is_in_check(&Color::Black))
}

#[test]
Expand Down
23 changes: 22 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::board::{Bitboard, Color};
use crate::board::{Bitboard, Color, Piece};

pub fn print_bitboard(bb: Bitboard) {
let formatted_bb: String = format!("{:064b}", bb);
Expand Down Expand Up @@ -36,3 +36,24 @@ pub fn opposite_color(color: Color) -> Color {
Color::Black => Color::White,
}
}

pub fn number_to_piece(piece: u32) -> Result<Piece, &'static str> {
match piece {
0 => Ok(Piece::Pawn),
1 => Ok(Piece::Knight),
2 => Ok(Piece::Bishop),
3 => Ok(Piece::Rook),
4 => Ok(Piece::Queen),
5 => Ok(Piece::King),
6 => Ok(Piece::None),
_ => Err("Piece wasn't found when converting!"),
}
}

pub fn number_to_color(color: u32) -> Result<Color, &'static str> {
match color {
0 => Ok(Color::White),
1 => Ok(Color::Black),
_ => Err("Color wasn't found when converting!"),
}
}

0 comments on commit c24a9e3

Please sign in to comment.