Skip to content

Commit

Permalink
Fix: queenside castling was not evaluated
Browse files Browse the repository at this point in the history
  • Loading branch information
oriolarcas committed Sep 7, 2023
1 parent 96bfedf commit 4c266d6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 17 deletions.
12 changes: 5 additions & 7 deletions COPYING.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# License
# Chusst, a simple chess engine in Rust.

Chusst, a simple chess engine in Rust.

## License of the software
## Copyright of the software

Copyright (C) 2023 Oriol Arcas

Expand All @@ -16,9 +14,9 @@ but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
You should have received a [copy of the GNU General Public License](LICENSE.txt)
along with this program. If not, see <https://www.gnu.org/licenses/>.

## License of the chess piece artwork
## Copyright of the chess piece artwork

The icon and the chess piece set used in this software have been created by [Colin M.L. Burnett](https://en.wikipedia.org/wiki/User:Cburnett).
The icon and the chess piece set used in this software have been created by [Colin M. L. Burnett](https://en.wikipedia.org/wiki/User:Cburnett).
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Simple project to learn about chess programming, Rust and Tauri.

Run `cargo tauri dev` to build and run the UI, which will allow you to play against the engine.

# License
# License and copyright

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License.

The icon and the chess piece set used in this software have been created by Colin M. L. Burnett.

See [COPYING](COPYING.md).
75 changes: 75 additions & 0 deletions src-tauri/src/moves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,22 @@ mod tests {
mv: mv!(e1 => g1),
checks: vec![pp!(kw @ g1), pp!(rw @ f1)],
},
// Queenside castling
TestBoard {
board: None,
initial_moves: vec![
mv!(d2 => d4),
mv!(a7 => a6),
mv!(d1 => d3),
mv!(b7 => b6),
mv!(c1 => d2),
mv!(c7 => c6),
mv!(b1 => c3),
mv!(d7 => d6),
],
mv: mv!(e1 => c1),
checks: vec![pp!(kw @ c1), pp!(rw @ d1)],
},
];

for test_board in &test_boards {
Expand Down Expand Up @@ -954,4 +970,63 @@ mod tests {
);
}
}

// Template to quickly test a specific board/move
#[test]
#[ignore]
fn quick_test() {
// White: ♙ ♘ ♗ ♖ ♕ ♔
// Black: ♟ ♞ ♝ ♜ ♛ ♚
let test_boards = [
TestBoard {
board: Some(
" a b c d e f g h \n\
8 [♜][♞][ ][♛][♚][♝][♞][ ]\n\
7 [ ][♝][♟][♟][♟][♟][♟][♜]\n\
6 [ ][♟][ ][ ][ ][ ][ ][ ]\n\
5 [♟][ ][ ][ ][ ][ ][ ][ ]\n\
4 [♙][ ][ ][♙][♙][ ][ ][♙]\n\
3 [ ][♙][♘][ ][♗][♘][♙][ ]\n\
2 [ ][ ][♙][♕][ ][ ][ ][ ]\n\
1 [♖][ ][ ][ ][♔][ ][ ][♖]",
),
initial_moves: vec![],
mv: mv!(e1 => c1),
checks: vec![],
},
];

for test_board in test_boards {
// Prepare board
let mut game = Game {
board: custom_board(&test_board.board),
player: Player::White,
last_move: None,
info: Default::default(),
};

game.info.disable_castle_kingside(&Player::White);
game.info.disable_castle_kingside(&Player::Black);

// Do setup moves
for mv in &test_board.initial_moves {
assert!(
do_move(&mut game, &mv).is_some(),
"move {} failed:\n{}",
mv,
game.board
);
}

// Do move
let mut rev_game = <ReversableGame as PlayableGame>::from_game(&mut game);

assert!(
rev_game.do_move(&test_board.mv),
"invalid move {}:\n{}",
test_board.mv,
rev_game.as_ref().board
);
}
}
}
10 changes: 4 additions & 6 deletions src-tauri/src/moves/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,8 @@ impl<'a> Iterator for KingIter<'a> {
KingIterStates::KingIterKingsideCastle => {
self.state = KingIterStates::KingIterQueensideCastle;
if !self.board_state.game_info.can_castle_kingside(player) {
return None;
}
if only_empty(
None
} else if only_empty(
&self.board_state.board,
try_move(&self.board_state.position, &dir!(0, 1)),
)
Expand All @@ -654,9 +653,8 @@ impl<'a> Iterator for KingIter<'a> {
KingIterStates::KingIterQueensideCastle => {
self.state = KingIterStates::KingIterEnd;
if !self.board_state.game_info.can_castle_queenside(player) {
return None;
}
if only_empty(
None
} else if only_empty(
&self.board_state.board,
try_move(&self.board_state.position, &dir!(0, -1)),
)
Expand Down
4 changes: 1 addition & 3 deletions src-tauri/src/moves/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,17 @@ pub struct ReversableGame<'a> {
moves: Vec<ReversableMove>,
last_move: Option<MoveInfo>,
info: Option<GameInfo>,
move_player: Player,
}

impl<'a> PlayableGame<'a> for ReversableGame<'a> {
fn from_game(game: &'a mut Game) -> Self {
let last_move = game.last_move;
let game_info = game.info;
let player = game.player;
ReversableGame {
game,
moves: vec![],
last_move,
info: Some(game_info),
move_player: player,
}
}

Expand All @@ -222,6 +219,7 @@ impl<'a> PlayableGame<'a> for ReversableGame<'a> {
}

impl<'a> ReversableGame<'a> {
#[allow(dead_code)]
pub fn undo(&mut self) {
assert!(!self.moves.is_empty());

Expand Down

0 comments on commit 4c266d6

Please sign in to comment.