Skip to content

Commit

Permalink
Use 'arrayvec' crate for our MoveList implementation
Browse files Browse the repository at this point in the history
Elo   | 1.05 +- 3.35 (95%)
SPRT  | 8.0+0.08s Threads=1 Hash=16MB
LLR   | 2.95 (-2.94, 2.94) [-5.00, 0.00]
Games | N: 17274 W: 4955 L: 4903 D: 7416
Penta | [437, 1838, 4036, 1888, 438]
  • Loading branch information
jgilchrist committed Oct 29, 2024
1 parent 5662e05 commit e24fa90
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 92 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rust-version = "1.81"
release = []

[dependencies]
arrayvec = "0.7.6"
colored = "2.1.0"
nom = "7.1.1"
rand = "0.8.5"
Expand Down
4 changes: 2 additions & 2 deletions src/chess/game.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::chess::bitboard::bitboards;
use crate::chess::movelist::MoveList;
use crate::chess::moves::MoveList;
use crate::chess::piece::Piece;
use crate::chess::square::squares;
use crate::chess::zobrist::ZobristHash;
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Game {
if self.halfmove_clock >= 100 {
let mut movelist = MoveList::new();
generate_legal_moves(self, &mut movelist);
return movelist.has_moves();
return !movelist.is_empty();
}

false
Expand Down
1 change: 0 additions & 1 deletion src/chess/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub mod direction;
pub mod fen;
pub mod game;
pub mod movegen;
pub mod movelist;
pub mod moves;
pub mod perft;
pub mod piece;
Expand Down
2 changes: 1 addition & 1 deletion src/chess/movegen/gen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::chess::bitboard::{bitboards, Bitboard};
use crate::chess::movegen::{attackers, pins, tables};
use crate::chess::movelist::MoveList;
use crate::chess::moves::MoveList;
use crate::chess::square::{squares, Square};
use crate::chess::{game::Game, moves::Move, piece::PromotionPieceKind};

Expand Down
77 changes: 0 additions & 77 deletions src/chess/movelist.rs

This file was deleted.

5 changes: 5 additions & 0 deletions src/chess/moves.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::chess::{piece::PromotionPieceKind, square::Square};
use arrayvec::ArrayVec;

const MAX_LEGAL_MOVES: usize = 218;

pub type MoveList = ArrayVec<Move, MAX_LEGAL_MOVES>;

#[derive(PartialEq, Eq, Clone, Copy)]
pub struct Move {
Expand Down
15 changes: 9 additions & 6 deletions src/engine/search/move_picker.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::chess::game::Game;
use crate::chess::movegen;
use crate::chess::movegen::MovegenCache;
use crate::chess::movelist::MoveList;
use crate::chess::moves::Move;
use crate::chess::moves::{Move, MoveList};
use crate::engine::search::move_ordering::score_move;
use crate::engine::search::{move_ordering, PersistentState, SearchState};

Expand Down Expand Up @@ -135,7 +134,7 @@ impl MovePicker {

if let Some(killer1) = state.killer_moves.get_0(plies) {
for i in self.first_quiet..self.moves.len() {
if self.moves.get(i) == killer1 {
if self.moves.get(i).map_or(false, |m| *m == killer1) {
self.moves.swap(self.first_quiet, i);
self.first_quiet += 1;

Expand All @@ -161,7 +160,7 @@ impl MovePicker {

if let Some(killer2) = state.killer_moves.get_1(plies) {
for i in self.first_quiet..self.moves.len() {
if self.moves.get(i) == killer2 {
if self.moves.get(i).map_or(false, |m| *m == killer2) {
self.moves.swap(self.first_quiet, i);
self.first_quiet += 1;

Expand Down Expand Up @@ -226,7 +225,7 @@ impl MovePicker {
}
}

let best_move = self.moves.get(best_move_idx);
let best_move = *self.moves.get(best_move_idx).unwrap();

// Move our best move to the start of the moves we haven't tried
self.moves.swap(self.idx, best_move_idx);
Expand All @@ -252,7 +251,11 @@ impl MovePicker {
persistent_state: &PersistentState,
) {
for i in start..end {
self.scores[i] = score_move(game, self.moves.get(i), &persistent_state.history_table);
self.scores[i] = score_move(
game,
*self.moves.get(i).unwrap(),
&persistent_state.history_table,
);
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/tests/perft_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::chess::fen::START_POS;
use crate::chess::game::Game;
use crate::chess::movegen;
use crate::chess::movegen::MovegenCache;
use crate::chess::movelist::MoveList;
use crate::chess::moves::MoveList;
use crate::chess::perft::perft;
use crate::engine::search::move_picker::MovePicker;
use crate::engine::search::{PersistentState, SearchState};
Expand Down Expand Up @@ -107,17 +107,21 @@ fn movepicker_perft(
// To get good coverage, we use the length of the move list to determine whether to try captures/quiets
// in best move/killers.
if captures_movelist.len() >= 3 {
best_move = Some(captures_movelist.get(0));
best_move = Some(*captures_movelist.first().unwrap());
} else if quiets_movelist.len() >= 3 {
best_move = Some(quiets_movelist.get(0));
best_move = Some(*quiets_movelist.first().unwrap());
}

if quiets_movelist.len() >= 3 {
state.killer_moves.try_push(depth, quiets_movelist.get(2));
state
.killer_moves
.try_push(depth, *quiets_movelist.get(2).unwrap());
}

if quiets_movelist.len() >= 4 {
state.killer_moves.try_push(depth, quiets_movelist.get(3));
state
.killer_moves
.try_push(depth, *quiets_movelist.get(3).unwrap());
}
}

Expand Down

0 comments on commit e24fa90

Please sign in to comment.