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

View file

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

View file

@ -3,9 +3,26 @@ require "./piece"
module LxChess
class Fen
class Error < Exception
end
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+/)
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('/')
width = ranks.first.size
height = ranks.size
@ -31,19 +48,23 @@ module LxChess
rank -= 1
end
Fen.new(board: board)
board
end
property board : Board
property turn : String
property castling : String
property en_passant : String
property halfmove_clock : Int16
property fullmove_counter : Int16
def initialize(
@board : Board
# @turn : Int,
# @castling : String,
# @en_passant : String,
# @halfmove_clock : Int,
# @fullmove_counter : Int
@board : Board,
@turn : String,
@castling : String,
@en_passant : String,
@halfmove_clock : Int16,
@fullmove_counter : Int16
)
end
end