Refactor some clippy-pedantic errors

This commit is contained in:
stefiosif
2024-09-06 20:30:57 +03:00
parent 6b9650a6f0
commit 2093d91dfa
12 changed files with 42 additions and 47 deletions

View File

@@ -1,7 +1,6 @@
use u64 as Bitboard; use u64 as Bitboard;
pub const EMPTY: Bitboard = 0x0; pub const EMPTY: Bitboard = 0x0;
pub const FULL: Bitboard = 0xffffffffffffffff;
pub const NOT_FILE_A: Bitboard = 0xfefefefefefefefe; pub const NOT_FILE_A: Bitboard = 0xfefefefefefefefe;
pub const NOT_FILE_H: Bitboard = 0x7f7f7f7f7f7f7f7f; pub const NOT_FILE_H: Bitboard = 0x7f7f7f7f7f7f7f7f;
pub const NOT_FILE_AB: Bitboard = 0xfcfcfcfcfcfcfcfc; pub const NOT_FILE_AB: Bitboard = 0xfcfcfcfcfcfcfcfc;

View File

@@ -153,7 +153,7 @@ impl Board {
} }
} }
pub fn set_piece(&mut self, piece: Piece) { pub fn set_piece(&mut self, piece: &Piece) {
match piece.color { match piece.color {
Color::Black => self.black_pieces[piece.piece_type].bitboard |= piece.bitboard, Color::Black => self.black_pieces[piece.piece_type].bitboard |= piece.bitboard,
Color::White => self.white_pieces[piece.piece_type].bitboard |= piece.bitboard, Color::White => self.white_pieces[piece.piece_type].bitboard |= piece.bitboard,
@@ -211,7 +211,7 @@ pub enum PieceType {
} }
impl PieceType { impl PieceType {
const fn idx(&self) -> usize { const fn idx(self) -> usize {
match self { match self {
Self::Pawn => 0, Self::Pawn => 0,
Self::Knight => 1, Self::Knight => 1,
@@ -247,7 +247,7 @@ pub enum Color {
} }
impl Color { impl Color {
pub const fn opponent(&self) -> Self { pub const fn opponent(self) -> Self {
match self { match self {
Self::White => Self::Black, Self::White => Self::Black,
Self::Black => Self::White, Self::Black => Self::White,

View File

@@ -62,7 +62,7 @@ pub fn piece_placement(pieces: &str) -> Result<Board, FenError> {
)) ))
} }
} { } {
board.set_piece(Piece::new(1 << square, piece_type, color)); board.set_piece(&Piece::new(1 << square, piece_type, color));
file += 1; file += 1;
}; };
} }

View File

@@ -19,7 +19,7 @@ impl Game {
from_fen(fen) from_fen(fen)
} }
pub const fn run(&self) { pub const fn run() {
Board::new(); Board::new();
} }

View File

@@ -17,7 +17,7 @@ impl History {
} }
pub fn push_move_parameters(&mut self, move_parameters: MoveParameters) { pub fn push_move_parameters(&mut self, move_parameters: MoveParameters) {
self.move_parameters.push(move_parameters) self.move_parameters.push(move_parameters);
} }
pub fn pop_move_parameters(&mut self) -> Option<MoveParameters> { pub fn pop_move_parameters(&mut self) -> Option<MoveParameters> {
@@ -56,7 +56,7 @@ impl MoveParameters {
} }
fn add_move(&mut self, mv: Move) { fn add_move(&mut self, mv: Move) {
self.mv = Some(mv) self.mv = Some(mv);
} }
fn add_captured_piece(&mut self, board: &Board, dst: usize, color: Color) { fn add_captured_piece(&mut self, board: &Board, dst: usize, color: Color) {
@@ -64,13 +64,13 @@ impl MoveParameters {
} }
fn add_promoted_piece(&mut self, promote: Promote) { fn add_promoted_piece(&mut self, promote: Promote) {
self.promoted_piece = Some(promote.into_piece_type()) self.promoted_piece = Some(promote.into_piece_type());
} }
pub fn add_irreversible_parameters(&mut self, state: State) { pub fn add_irreversible_parameters(&mut self, state: State) {
self.castling_ability = Some(state.castling_ability); self.castling_ability = Some(state.castling_ability);
self.en_passant_square = state.en_passant_square; self.en_passant_square = state.en_passant_square;
self.halfmove_clock = Some(state.halfmove_clock) self.halfmove_clock = Some(state.halfmove_clock);
} }
pub fn add_capture_and_promotion_piece(&mut self, board: &Board, mv: Move, color: Color) { pub fn add_capture_and_promotion_piece(&mut self, board: &Board, mv: Move, color: Color) {
@@ -107,7 +107,7 @@ mod tests {
move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player()); move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player());
game.board.make_move(&mv); game.board.make_move(&mv);
game.board.unmake_move(move_parameters); game.board.unmake_move(&move_parameters);
assert_eq!(board_before_make, game.board); assert_eq!(board_before_make, game.board);
@@ -118,7 +118,7 @@ mod tests {
move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player()); move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player());
game.board.make_move(&mv); game.board.make_move(&mv);
game.board.unmake_move(move_parameters); game.board.unmake_move(&move_parameters);
assert_eq!(board_before_make, game.board); assert_eq!(board_before_make, game.board);
@@ -137,7 +137,7 @@ mod tests {
move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player()); move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player());
game.board.make_move(&mv); game.board.make_move(&mv);
game.board.unmake_move(move_parameters); game.board.unmake_move(&move_parameters);
assert_eq!(board_before_make, game.board); assert_eq!(board_before_make, game.board);
@@ -148,7 +148,7 @@ mod tests {
move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player()); move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player());
game.board.make_move(&mv); game.board.make_move(&mv);
game.board.unmake_move(move_parameters); game.board.unmake_move(&move_parameters);
assert_eq!(board_before_make, game.board); assert_eq!(board_before_make, game.board);
@@ -163,7 +163,7 @@ mod tests {
move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player()); move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player());
game.board.make_move(&mv); game.board.make_move(&mv);
game.board.unmake_move(move_parameters); game.board.unmake_move(&move_parameters);
assert_eq!(board_before_make, game.board); assert_eq!(board_before_make, game.board);
@@ -184,7 +184,7 @@ mod tests {
move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player()); move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player());
game.board.make_move(&mv); game.board.make_move(&mv);
game.board.unmake_move(move_parameters); game.board.unmake_move(&move_parameters);
assert_eq!(board_before_make, game.board); assert_eq!(board_before_make, game.board);
@@ -203,7 +203,7 @@ mod tests {
move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player()); move_parameters.add_capture_and_promotion_piece(&game.board, mv, game.current_player());
game.board.make_move(&mv); game.board.make_move(&mv);
game.board.unmake_move(move_parameters); game.board.unmake_move(&move_parameters);
assert_eq!(board_before_make, game.board); assert_eq!(board_before_make, game.board);

View File

@@ -62,8 +62,9 @@ impl State {
self.update_castling_state_capture(src, color); self.update_castling_state_capture(src, color);
match (src, color) { match (src, color) {
(4, Color::White) => self.set_castling_ability(color, Castle::None), (4, Color::White) | (60, Color::Black) => {
(60, Color::Black) => self.set_castling_ability(color, Castle::None), self.set_castling_ability(color, Castle::None)
}
_ => (), _ => (),
} }
} }

View File

@@ -39,8 +39,8 @@ pub enum Response {
Info(String), Info(String),
} }
fn write_response(handle_out: &mut impl Write, response: Response) -> Result<(), String> { fn write_response(handle_out: &mut impl Write, response: &Response) -> Result<(), String> {
writeln!(handle_out, "{}", response).map_err(|e| e.to_string())?; writeln!(handle_out, "{response}").map_err(|e| e.to_string())?;
handle_out.flush().map_err(|e| e.to_string())?; handle_out.flush().map_err(|e| e.to_string())?;
Ok(()) Ok(())
} }
@@ -52,8 +52,8 @@ impl fmt::Display for Response {
match self { match self {
Self::UciOk => write!(f, "id name ippos\nid author stefiosif\nuciok"), Self::UciOk => write!(f, "id name ippos\nid author stefiosif\nuciok"),
Self::ReadyOk => write!(f, "readyok"), Self::ReadyOk => write!(f, "readyok"),
Self::BestMove(best_move) => write!(f, "bestmove {}", best_move), Self::BestMove(best_move) => write!(f, "bestmove {best_move}"),
Self::Info(info) => write!(f, "{}", info), Self::Info(info) => write!(f, "{info}"),
} }
} }
} }
@@ -174,7 +174,7 @@ pub fn uci_loop<R: BufRead, W: Write>(input: R, mut output: W) -> Result<(), Str
Command::Quit => break, Command::Quit => break,
}; };
write_response(&mut output, response)?; write_response(&mut output, &response)?;
output.flush().map_err(|e| e.to_string())?; output.flush().map_err(|e| e.to_string())?;
} }

View File

@@ -30,7 +30,7 @@ fn main() {
Mode::Uci => { Mode::Uci => {
let input = io::stdin().lock(); let input = io::stdin().lock();
let output = io::stdout().lock(); let output = io::stdout().lock();
uci::uci_loop(input, output).unwrap_or_else(|e| println!("{}", e)); uci::uci_loop(input, output).unwrap_or_else(|e| println!("{e}"));
} }
} }
} }

View File

@@ -61,7 +61,7 @@ impl fmt::Debug for Move {
Promote::Rook => 'r', Promote::Rook => 'r',
Promote::Queen => 'q', Promote::Queen => 'q',
}; };
write!(f, "{}", promote_char)?; write!(f, "{promote_char}")?;
} }
Ok(()) Ok(())
@@ -115,7 +115,7 @@ impl Move {
} }
pub fn parse_into_str(&self) -> String { pub fn parse_into_str(&self) -> String {
format!("{:?}", self) format!("{self:?}")
} }
} }
@@ -156,22 +156,22 @@ impl Board {
} }
MoveType::Promotion(promote) => { MoveType::Promotion(promote) => {
Self::remove_piece(mv.src, own_pieces); Self::remove_piece(mv.src, own_pieces);
Self::promote_piece(mv.dst, own_pieces, promote); Self::promote_piece(mv.dst, own_pieces, *promote);
} }
MoveType::PromotionCapture(promote) => { MoveType::PromotionCapture(promote) => {
Self::remove_piece(mv.src, own_pieces); Self::remove_piece(mv.src, own_pieces);
Self::remove_piece(mv.dst, opponent_pieces); Self::remove_piece(mv.dst, opponent_pieces);
Self::promote_piece(mv.dst, own_pieces, promote); Self::promote_piece(mv.dst, own_pieces, *promote);
} }
MoveType::Castle => { MoveType::Castle => {
Self::move_piece(mv.src, mv.dst, own_pieces); Self::move_piece(mv.src, mv.dst, own_pieces);
Self::move_rook_castle(mv.dst, own_pieces); Self::move_rook_castle(mv.dst, own_pieces);
self.state.set_castling_ability(color, Castle::None) self.state.set_castling_ability(color, Castle::None);
} }
} }
} }
pub fn unmake_move(&mut self, move_parameters: MoveParameters) { pub fn unmake_move(&mut self, move_parameters: &MoveParameters) {
let color_before_move = self.state.change_side(); let color_before_move = self.state.change_side();
self.state.revert_full_move(color_before_move); self.state.revert_full_move(color_before_move);
self.state.en_passant_square = move_parameters.en_passant_square; self.state.en_passant_square = move_parameters.en_passant_square;
@@ -216,7 +216,7 @@ impl Board {
Color::White => mv.dst - 8, Color::White => mv.dst - 8,
Color::Black => mv.dst + 8, Color::Black => mv.dst + 8,
}; };
opponent_pieces[PieceType::Pawn].bitboard |= square_to_bitboard(enemy_pawn_square) opponent_pieces[PieceType::Pawn].bitboard |= square_to_bitboard(enemy_pawn_square);
} }
MoveType::Castle => { MoveType::Castle => {
Self::move_piece(mv.dst, mv.src, own_pieces); Self::move_piece(mv.dst, mv.src, own_pieces);
@@ -260,7 +260,7 @@ impl Board {
Self::move_piece(rook_src, rook_dst, pieces); Self::move_piece(rook_src, rook_dst, pieces);
} }
fn promote_piece(square: usize, pieces: &mut [Piece; 6], promote: &Promote) { fn promote_piece(square: usize, pieces: &mut [Piece; 6], promote: Promote) {
match promote { match promote {
Promote::Knight => pieces[PieceType::Knight].bitboard |= square_to_bitboard(square), Promote::Knight => pieces[PieceType::Knight].bitboard |= square_to_bitboard(square),
Promote::Bishop => pieces[PieceType::Bishop].bitboard |= square_to_bitboard(square), Promote::Bishop => pieces[PieceType::Bishop].bitboard |= square_to_bitboard(square),

View File

@@ -349,7 +349,7 @@ fn king_castling_moves(board: &Board, color: Color, all_occupancies: Bitboard) -
let mut add_move_if_empty_path = |path_mask, king_dst| { let mut add_move_if_empty_path = |path_mask, king_dst| {
if !have_common_bit(all_occupancies, path_mask) { if !have_common_bit(all_occupancies, path_mask) {
moves.push(Move::new_with_type(king_src, king_dst, MoveType::Castle)) moves.push(Move::new_with_type(king_src, king_dst, MoveType::Castle));
} }
}; };
@@ -362,14 +362,10 @@ fn king_castling_moves(board: &Board, color: Color, all_occupancies: Bitboard) -
add_move_if_empty_path(path_long, king_dst_long); add_move_if_empty_path(path_long, king_dst_long);
} }
(Castle::Both, Castle::Short) (Castle::Both | Castle::Short, Castle::Short) | (Castle::Short, Castle::Both) => {
| (Castle::Short, Castle::Short)
| (Castle::Short, Castle::Both) => {
add_move_if_empty_path(path_short, king_dst_short); add_move_if_empty_path(path_short, king_dst_short);
} }
(Castle::Both, Castle::Long) (Castle::Both | Castle::Long, Castle::Long) | (Castle::Long, Castle::Both) => {
| (Castle::Long, Castle::Long)
| (Castle::Long, Castle::Both) => {
add_move_if_empty_path(path_long, king_dst_long); add_move_if_empty_path(path_long, king_dst_long);
} }
_ => (), _ => (),

View File

@@ -27,17 +27,17 @@ pub fn negamax(
game.board.make_move(&mv); game.board.make_move(&mv);
if game.board.king_under_check(color) { if game.board.king_under_check(color) {
game.board.unmake_move(move_parameters); game.board.unmake_move(&move_parameters);
continue; continue;
} }
legal_moves += 1; legal_moves += 1;
let move_score = -negamax(game, -beta, -alpha, depth - 1, plies + 1).1; let move_score = -negamax(game, -beta, -alpha, depth - 1, plies + 1).1;
game.board.unmake_move(move_parameters); game.board.unmake_move(&move_parameters);
if move_score > best_score { if move_score > best_score {
best_score = move_score; best_score = move_score;
best_move = Some(mv) best_move = Some(mv);
} }
if move_score >= beta { if move_score >= beta {
@@ -50,9 +50,8 @@ pub fn negamax(
if legal_moves == 0 { if legal_moves == 0 {
if game.board.king_under_check(color) { if game.board.king_under_check(color) {
return (None, mate_score); return (None, mate_score);
} else {
return (None, 0);
} }
return (None, 0);
} }
(best_move, best_score) (best_move, best_score)
} }

View File

@@ -27,12 +27,12 @@ pub fn quiescence(game: &mut Game, mut alpha: i32, beta: i32) -> (Option<Move>,
game.board.make_move(&mv); game.board.make_move(&mv);
if game.board.king_under_check(color) { if game.board.king_under_check(color) {
game.board.unmake_move(move_parameters); game.board.unmake_move(&move_parameters);
continue; continue;
} }
let move_score = -quiescence(game, -beta, -alpha).1; let move_score = -quiescence(game, -beta, -alpha).1;
game.board.unmake_move(move_parameters); game.board.unmake_move(&move_parameters);
if move_score >= beta { if move_score >= beta {
return (Some(mv), beta); return (Some(mv), beta);