A chess library for Gleam
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Generate castling moves

+42 -1
+42 -1
src/starfish/internal/move.gleam
··· 137 137 moves: List(Move(Legal)), 138 138 directions: List(Direction), 139 139 ) -> List(Move(Legal)) { 140 + let moves = regular_king_moves(game, position, moves, directions) 141 + 142 + let is_empty = fn(position) { 143 + iv.get_or_default(game.board, position, board.Empty) == board.Empty 144 + } 145 + 146 + let moves = case game.to_move { 147 + board.Black if game.castling.black_kingside -> 148 + case is_empty(61) && is_empty(62) { 149 + True -> [Castle(from: position, to: 62), ..moves] 150 + False -> moves 151 + } 152 + board.White if game.castling.white_kingside -> 153 + case is_empty(5) && is_empty(6) { 154 + True -> [Castle(from: position, to: 6), ..moves] 155 + False -> moves 156 + } 157 + _ -> moves 158 + } 159 + 160 + case game.to_move { 161 + board.Black if game.castling.black_queenside -> 162 + case is_empty(59) && is_empty(58) && is_empty(57) { 163 + True -> [Castle(from: position, to: 58), ..moves] 164 + False -> moves 165 + } 166 + board.White if game.castling.white_queenside -> 167 + case is_empty(3) && is_empty(2) && is_empty(1) { 168 + True -> [Castle(from: position, to: 2), ..moves] 169 + False -> moves 170 + } 171 + _ -> moves 172 + } 173 + } 174 + 175 + fn regular_king_moves( 176 + game: Game, 177 + position: Int, 178 + moves: List(Move(Legal)), 179 + directions: List(Direction), 180 + ) -> List(Move(Legal)) { 140 181 case directions { 141 182 [] -> moves 142 183 [direction, ..directions] -> { ··· 150 191 Ok(board.Occupied(_, _)) | Error(_) -> moves 151 192 } 152 193 153 - king_moves(game, position, moves, directions) 194 + regular_king_moves(game, position, moves, directions) 154 195 } 155 196 } 156 197 }