mirror of
https://github.com/SleepingInsomniac/lx_chess_cr
synced 2024-11-16 19:49:34 +01:00
Refactor placement to own method
This commit is contained in:
parent
f19fb93e5b
commit
3f2d89e0ad
3 changed files with 40 additions and 19 deletions
|
@ -4,7 +4,7 @@ A chess program written in crystal
|
|||
|
||||
## Installation
|
||||
|
||||
`shards build src/lx_chess.cr`
|
||||
`shards build --release --no-debug`
|
||||
|
||||
## Usage
|
||||
|
||||
|
|
|
@ -8,6 +8,6 @@ targets:
|
|||
lx_chess:
|
||||
main: src/lx_chess.cr
|
||||
|
||||
crystal: 1.0.0
|
||||
crystal: 1.1.1
|
||||
|
||||
license: MIT
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue