|
1 | 1 | use super::Board; |
2 | 2 | use crate::{ |
3 | | - lookup::between, |
4 | | - types::{CastlingKind, Color, Piece, PieceType, Square}, |
| 3 | + lookup::{between, ray_pass}, |
| 4 | + types::{CastlingKind, Color, HOME_RANK, KING_TO_FILE, Piece, PieceType, ROOK_TO_FILE, Square}, |
5 | 5 | }; |
6 | 6 |
|
7 | 7 | #[derive(Debug)] |
@@ -67,81 +67,34 @@ impl Board { |
67 | 67 |
|
68 | 68 | fn set_castling(&mut self, rights: &str) { |
69 | 69 | for right in rights.chars() { |
70 | | - match right { |
71 | | - 'K' => { |
72 | | - let mut rook_from = Square::H1; |
73 | | - while self.piece_on(rook_from).piece_type() != PieceType::Rook { |
74 | | - rook_from = rook_from.shift(-1); |
75 | | - } |
76 | | - |
77 | | - let king_from = self.king_square(Color::White); |
78 | | - self.set_castling_for(CastlingKind::WhiteKingside, king_from, Square::G1, rook_from, Square::F1); |
79 | | - } |
80 | | - 'Q' => { |
81 | | - let mut rook_from = Square::A1; |
82 | | - while self.piece_on(rook_from).piece_type() != PieceType::Rook { |
83 | | - rook_from = rook_from.shift(1); |
84 | | - } |
85 | | - |
86 | | - let king_from = self.king_square(Color::White); |
87 | | - self.set_castling_for(CastlingKind::WhiteQueenside, king_from, Square::C1, rook_from, Square::D1); |
88 | | - } |
89 | | - 'k' => { |
90 | | - let mut rook_from = Square::H8; |
91 | | - while self.piece_on(rook_from).piece_type() != PieceType::Rook { |
92 | | - rook_from = rook_from.shift(-1); |
93 | | - } |
94 | | - |
95 | | - let king_from = self.king_square(Color::Black); |
96 | | - self.set_castling_for(CastlingKind::BlackKingside, king_from, Square::G8, rook_from, Square::F8); |
97 | | - } |
98 | | - 'q' => { |
99 | | - let mut rook_from = Square::A8; |
100 | | - while self.piece_on(rook_from).piece_type() != PieceType::Rook { |
101 | | - rook_from = rook_from.shift(1); |
102 | | - } |
103 | | - |
104 | | - let king_from = self.king_square(Color::Black); |
105 | | - self.set_castling_for(CastlingKind::BlackQueenside, king_from, Square::C8, rook_from, Square::D8); |
106 | | - } |
107 | | - token @ 'A'..='H' => { |
108 | | - let king_from = self.king_square(Color::White); |
109 | | - let rook_from = Square::from_rank_file(0, token as u8 - b'A'); |
110 | | - |
111 | | - let kind = if king_from.file() < rook_from.file() { |
112 | | - CastlingKind::WhiteKingside |
113 | | - } else { |
114 | | - CastlingKind::WhiteQueenside |
115 | | - }; |
116 | | - |
117 | | - let (king_to, rook_to) = match kind { |
118 | | - CastlingKind::WhiteKingside => (Square::G1, Square::F1), |
119 | | - CastlingKind::WhiteQueenside => (Square::C1, Square::D1), |
120 | | - _ => unreachable!(), |
121 | | - }; |
122 | | - |
123 | | - self.set_castling_for(kind, king_from, king_to, rook_from, rook_to); |
124 | | - } |
125 | | - token @ 'a'..='h' => { |
126 | | - let king_from = self.king_square(Color::Black); |
127 | | - let rook_from = Square::from_rank_file(7, token as u8 - b'a'); |
128 | | - |
129 | | - let kind = if king_from.file() < rook_from.file() { |
130 | | - CastlingKind::BlackKingside |
131 | | - } else { |
132 | | - CastlingKind::BlackQueenside |
133 | | - }; |
134 | | - |
135 | | - let (king_to, rook_to) = match kind { |
136 | | - CastlingKind::BlackKingside => (Square::G8, Square::F8), |
137 | | - CastlingKind::BlackQueenside => (Square::C8, Square::D8), |
138 | | - _ => unreachable!(), |
139 | | - }; |
140 | | - |
141 | | - self.set_castling_for(kind, king_from, king_to, rook_from, rook_to); |
142 | | - } |
143 | | - _ => continue, |
| 70 | + if !matches!(right.to_ascii_uppercase(), 'A'..='H' | 'K' | 'Q') { |
| 71 | + continue; |
144 | 72 | } |
| 73 | + |
| 74 | + let color = if right.is_uppercase() { Color::White } else { Color::Black }; |
| 75 | + let king_from = self.king_square(color); |
| 76 | + let mut search_step = right.to_ascii_uppercase() as i8 - b'A' as i8 - king_from.file() as i8; |
| 77 | + |
| 78 | + if right.eq_ignore_ascii_case(&'K') { |
| 79 | + search_step = Square::RIGHT; |
| 80 | + } |
| 81 | + if right.eq_ignore_ascii_case(&'Q') { |
| 82 | + search_step = Square::LEFT; |
| 83 | + } |
| 84 | + |
| 85 | + let rook_from = |
| 86 | + (ray_pass(king_from, king_from.shift(search_step)) & self.colored_pieces(color, PieceType::Rook)).lsb(); |
| 87 | + |
| 88 | + let king_side = rook_from > king_from; |
| 89 | + |
| 90 | + let rights = if king_side { CastlingKind::KINGSIDE[color] } else { CastlingKind::QUEENSIDE[color] }; |
| 91 | + |
| 92 | + let king_to = |
| 93 | + Square::from_rank_file(HOME_RANK[color].clone() as u8, KING_TO_FILE[king_side as usize].clone() as u8); |
| 94 | + let rook_to = |
| 95 | + Square::from_rank_file(HOME_RANK[color].clone() as u8, ROOK_TO_FILE[king_side as usize].clone() as u8); |
| 96 | + |
| 97 | + self.set_castling_for(rights, king_from, king_to, rook_from, rook_to); |
145 | 98 | } |
146 | 99 | } |
147 | 100 |
|
|
0 commit comments