Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply Clippy to the source code #12

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions chusst-gen/src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,15 +549,14 @@ fn format_board(board: &impl Board, f: &mut fmt::Formatter) -> fmt::Result {

rows.push(" a b c d e f g h ".to_owned());
for rank in (0..8).rev() {
let mut row_str = String::from(format!("{} ", rank + 1));
let mut row_str = format!("{} ", rank + 1);
for file in 0..8 {
let piece = match board.at(&pos!(rank, file)) {
Some(square_value) => Some((
let piece = board.at(&pos!(rank, file)).map(|square_value| {
(
get_unicode_piece(square_value.piece, square_value.player),
square_value.player,
)),
None => None,
};
)
});

let is_dark_square = square_dark(rank, file);

Expand Down
8 changes: 3 additions & 5 deletions chusst-gen/src/board/bitboards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl PlayerBitboards {

impl ModifiableBoard<Position, Option<PieceType>> for PlayerBitboards {
fn at(&self, index: &Position) -> Option<PieceType> {
let position_mask = bitboard_from_position(&index);
let position_mask = bitboard_from_position(index);
if self.pawns & position_mask != 0 {
Some(PieceType::Pawn)
} else if self.knights & position_mask != 0 {
Expand Down Expand Up @@ -332,13 +332,11 @@ impl ModifiableBoard<Position, Option<Piece>> for Bitboards {
player: Player::White,
piece,
})
} else if let Some(piece) = self.black.at(pos) {
Some(Piece {
} else {
self.black.at(pos).map(|piece| Piece {
player: Player::Black,
piece,
})
} else {
None
}
}

Expand Down
19 changes: 9 additions & 10 deletions chusst-gen/src/board/bitboards/in_between.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4258,17 +4258,16 @@ mod tests {
#[test]
fn check_in_between_table() {
let inbetween_table = super::IN_BETWEEN_TABLE;
for source_index in 0..64 {
for target_index in 0..64 {
for (source_index, in_between_targets) in inbetween_table.iter().enumerate() {
for (target_index, in_between) in in_between_targets.iter().enumerate() {
let source = bitboard_index_to_position(source_index);
let target = bitboard_index_to_position(target_index);
let in_between = inbetween_table[source_index][target_index];

let rank_distance = ((source.rank as i8) - (target.rank as i8)).abs();
let file_distance = ((source.file as i8) - (target.file as i8)).abs();

if rank_distance <= 1 && file_distance <= 1 {
assert_eq!(in_between, 0);
assert_eq!(*in_between, 0);
continue;
}

Expand All @@ -4287,13 +4286,13 @@ mod tests {
1 << rank_and_file_to_bitboard_index(source.rank, file as usize);
}
assert_eq!(
in_between,
*in_between,
in_between_rank,
"source: {}, target: {}, expected:\n{}\nactual:\n{}",
source,
target,
format_bitboard(in_between_rank),
format_bitboard(in_between),
format_bitboard(*in_between),
);
} else if same_file {
let mut in_between_file = 0;
Expand All @@ -4305,7 +4304,7 @@ mod tests {
in_between_file |=
1 << rank_and_file_to_bitboard_index(rank as usize, source.file);
}
assert_eq!(in_between, in_between_file);
assert_eq!(*in_between, in_between_file);
} else if same_diagonal {
let mut in_between_diagonal = 0;
let rank_increment = if source.rank < target.rank { 1i8 } else { -1i8 };
Expand All @@ -4320,16 +4319,16 @@ mod tests {
}

assert_eq!(
in_between,
*in_between,
in_between_diagonal,
"source: {}, target: {}, expected:\n{}\nactual:\n{}",
source,
target,
format_bitboard(in_between_diagonal),
format_bitboard(in_between),
format_bitboard(*in_between),
);
} else {
assert_eq!(in_between, 0);
assert_eq!(*in_between, 0);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion chusst-gen/src/board/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
};
use crate::p;

#[derive(Copy, Clone, Debug, Default, PartialEq)]
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SimpleBoard {
ranks: Ranks<Option<Piece>>,
}
Expand Down
109 changes: 56 additions & 53 deletions chusst-gen/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ impl From<i32> for Score {
}
}

impl Into<i32> for Score {
fn into(self) -> i32 {
self.0
impl From<Score> for i32 {
fn from(val: Score) -> Self {
val.0
}
}

Expand Down Expand Up @@ -139,6 +139,22 @@ struct SearchResult {
stopped: bool,
}

struct SearchScores {
parent: Score,
alpha: Score,
beta: Score,
}

impl Default for SearchScores {
fn default() -> Self {
Self {
parent: Score::from(0),
alpha: Score::MIN,
beta: Score::MAX,
}
}
}

// Value in centipawns
fn get_piece_value(piece: PieceType) -> Score {
match piece {
Expand Down Expand Up @@ -187,7 +203,7 @@ trait GamePrivate<B: Board + SafetyChecks>: PlayableGame<B> {
// Before moving, check if it is a castling and it is valid
if is_king && mv.source.file.abs_diff(mv.target.file) == 2 {
let is_valid_castling_square = |direction: &Direction| {
only_empty_and_safe(self.board(), try_move(&mv.source, &direction), &player)
only_empty_and_safe(self.board(), try_move(&mv.source, direction), &player)
.is_some()
};
let can_castle = match mv.target.file {
Expand Down Expand Up @@ -221,7 +237,7 @@ trait GamePrivate<B: Board + SafetyChecks>: PlayableGame<B> {
if new_game
.as_ref()
.board()
.is_piece_unsafe(&current_king_position)
.is_piece_unsafe(current_king_position)
{
return None;
}
Expand Down Expand Up @@ -303,9 +319,9 @@ trait GamePrivate<B: Board + SafetyChecks>: PlayableGame<B> {
let mut move_names = vec![];

for mv in moves {
move_names.push(Game::move_name(&new_game, &mv).unwrap());
move_names.push(Game::move_name(&new_game, mv).unwrap());

assert!(Game::do_move(&mut new_game, *mv).is_some());
assert!(Game::do_move(&mut new_game, mv).is_some());
}

move_names
Expand All @@ -316,9 +332,7 @@ trait GamePrivate<B: Board + SafetyChecks>: PlayableGame<B> {
&self,
current_depth: u32,
max_depth: u32,
alpha: Score,
beta: Score,
parent_score: Score,
scores: SearchScores,
stop_signal: &mut impl HasStopSignal,
feedback: &mut impl SearchFeedback,
) -> SearchResult {
Expand All @@ -334,7 +348,7 @@ trait GamePrivate<B: Board + SafetyChecks>: PlayableGame<B> {

let king_position = self.board().find_king(&player);

let mut local_alpha = alpha;
let mut local_alpha = scores.alpha;

let is_leaf_node = current_depth == max_depth;
let mut stopped = false;
Expand Down Expand Up @@ -384,7 +398,7 @@ trait GamePrivate<B: Board + SafetyChecks>: PlayableGame<B> {
score: local_score,
}],
// Negamax: negate score from previous move
score: local_score - parent_score,
score: local_score - scores.parent,
searched: 0,
};

Expand All @@ -411,9 +425,11 @@ trait GamePrivate<B: Board + SafetyChecks>: PlayableGame<B> {
current_depth + 1,
max_depth,
// beta becomes the alpha of the other player, and viceversa
-beta,
-local_alpha,
branch.score,
SearchScores {
parent: branch.score,
alpha: -scores.beta,
beta: -local_alpha,
},
stop_signal,
feedback,
);
Expand Down Expand Up @@ -471,7 +487,7 @@ trait GamePrivate<B: Board + SafetyChecks>: PlayableGame<B> {
let _ = writeln!(feedback, "{}}}", indent(current_depth));
}

if branch.score >= beta && branch.score < Score::MAX {
if branch.score >= scores.beta && branch.score < Score::MAX {
// Fail hard beta cutoff

#[cfg(feature = "verbose-search")]
Expand All @@ -489,7 +505,7 @@ trait GamePrivate<B: Board + SafetyChecks>: PlayableGame<B> {
best_move = Some(branch);
}

best_move.as_mut().unwrap().score = beta;
best_move.as_mut().unwrap().score = scores.beta;

break 'main_loop;
}
Expand Down Expand Up @@ -535,9 +551,7 @@ trait GamePrivate<B: Board + SafetyChecks>: PlayableGame<B> {
self.get_best_move_recursive_alpha_beta(
0,
0,
Score::MIN,
Score::MAX,
Score::from(0),
SearchScores::default(),
&mut (),
&mut SilentSearchFeedback::default(),
)
Expand All @@ -547,27 +561,24 @@ trait GamePrivate<B: Board + SafetyChecks>: PlayableGame<B> {
fn get_possible_captures_of_position(&self, position: &Position) -> Vec<Position> {
let mut captures: Vec<Position> = Vec::new();

match self.board().at(position) {
Some(square) => {
for possible_position in self.get_possible_moves_iter(*position) {
let is_capture = self.board().at(&possible_position).is_some();
if let Some(square) = self.board().at(position) {
for possible_position in self.get_possible_moves_iter(*position) {
let is_capture = self.board().at(&possible_position).is_some();

if is_capture {
captures.push(possible_position);
} else if !is_capture
&& square.piece == PieceType::Pawn
&& position.file.abs_diff(position.file) != 0
{
let passed_rank = usize::try_from(
i8::try_from(position.rank).unwrap()
- B::pawn_progress_direction(&square.player),
)
.unwrap();
captures.push(pos!(passed_rank, possible_position.file));
}
if is_capture {
captures.push(possible_position);
} else if !is_capture
&& square.piece == PieceType::Pawn
&& position.file.abs_diff(position.file) != 0
{
let passed_rank = usize::try_from(
i8::try_from(position.rank).unwrap()
- B::pawn_progress_direction(&square.player),
)
.unwrap();
captures.push(pos!(passed_rank, possible_position.file));
}
}
None => (),
}

captures
Expand Down Expand Up @@ -639,9 +650,8 @@ pub trait Game<B: Board + SafetyChecks>: GamePrivate<B> {
let tgt_piece_opt = board.at(&mv.target);
let pieces_iter = player_pieces_iter!(board: board, player: player);

match piece_char(&src_piece.piece) {
Some(piece_char_value) => name.push(piece_char_value),
None => (),
if let Some(piece_char_value) = piece_char(&src_piece.piece) {
name.push(piece_char_value);
}

let is_pawn = src_piece.piece == PieceType::Pawn;
Expand All @@ -658,19 +668,15 @@ pub trait Game<B: Board + SafetyChecks>: GamePrivate<B> {
continue;
}

match board.at(&player_piece_position) {
Some(player_piece) => {
if player_piece.piece != src_piece.piece {
continue;
}
if let Some(player_piece) = board.at(&player_piece_position) {
if player_piece.piece != src_piece.piece {
continue;
}
None => {}
}

if self
.get_possible_moves_iter(player_piece_position)
.find(|possible_position| *possible_position == mv.target)
.is_some()
.any(|possible_position| possible_position == mv.target)
{
ambiguous_piece_exists = true;
if player_piece_position.rank == mv.source.rank {
Expand Down Expand Up @@ -746,9 +752,7 @@ pub trait Game<B: Board + SafetyChecks>: GamePrivate<B> {
self.get_best_move_recursive_alpha_beta(
0,
search_depth,
Score::MIN,
Score::MAX,
Score::from(0),
SearchScores::default(),
stop_signal,
feedback,
)
Expand Down Expand Up @@ -895,7 +899,6 @@ pub trait Game<B: Board + SafetyChecks>: GamePrivate<B> {
Some(
enemy_army
.keys()
.into_iter()
.map(|piece| Piece {
piece: *piece,
player: *enemy_player,
Expand Down
Loading