Refactor placement to own method

This commit is contained in:
Alex Clink 2021-09-04 19:48:12 -04:00
parent f19fb93e5b
commit 3f2d89e0ad
3 changed files with 40 additions and 19 deletions

View file

@ -4,7 +4,7 @@ A chess program written in crystal
## Installation ## Installation
`shards build src/lx_chess.cr` `shards build --release --no-debug`
## Usage ## Usage
@ -14,15 +14,15 @@ Usage example:
```sh ```sh
╰─$ ./bin/lx_chess --fen "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" ╰─$ ./bin/lx_chess --fen "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
8: ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ 8: ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
7: ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ 7: ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
6: 6:
5: 5:
4: 4:
3: 3:
2: ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ 2: ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
1: ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ 1: ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
a b c d e f g h a b c d e f g h
``` ```
## Contributing ## Contributing

View file

@ -8,6 +8,6 @@ targets:
lx_chess: lx_chess:
main: src/lx_chess.cr main: src/lx_chess.cr
crystal: 1.0.0 crystal: 1.1.1
license: MIT license: MIT

View file

@ -3,9 +3,26 @@ require "./piece"
module LxChess module LxChess
class Fen class Fen
class Error < Exception
end
def self.parse(fen : String) def self.parse(fen : String)
raise "Invalid FEN" unless fen =~ /[rnbqkp\d\/]+\s+[a-z]+\s[a-z\-]+\s+[a-z\-]+\s\d+\s\d+/i
placement, turn, castling, en_passant, halfmove_clock, fullmove_counter = fen.split(/\s+/) placement, turn, castling, en_passant, halfmove_clock, fullmove_counter = fen.split(/\s+/)
board = self.parse_placement(placement)
Fen.new(
board: board,
turn: turn,
castling: castling,
en_passant: en_passant,
halfmove_clock: halfmove_clock.to_i16,
fullmove_counter: fullmove_counter.to_i16
)
end
def self.parse_placement(placement)
ranks = placement.split('/') ranks = placement.split('/')
width = ranks.first.size width = ranks.first.size
height = ranks.size height = ranks.size
@ -31,19 +48,23 @@ module LxChess
rank -= 1 rank -= 1
end end
board
Fen.new(board: board)
end end
property board : Board property board : Board
property turn : String
property castling : String
property en_passant : String
property halfmove_clock : Int16
property fullmove_counter : Int16
def initialize( def initialize(
@board : Board @board : Board,
# @turn : Int, @turn : String,
# @castling : String, @castling : String,
# @en_passant : String, @en_passant : String,
# @halfmove_clock : Int, @halfmove_clock : Int16,
# @fullmove_counter : Int @fullmove_counter : Int16
) )
end end
end end