Render full fen string

This commit is contained in:
Alex Clink 2021-09-15 22:46:34 -04:00
parent 322249c6b3
commit 9b727ad3d5
4 changed files with 49 additions and 23 deletions

View file

@ -59,7 +59,7 @@ term.clear_scroll
loop do
term.move 0, 0
print fen.placement
print fen.to_s
term.trunc
puts
puts
@ -137,6 +137,8 @@ loop do
end
end
end
fen.update(game)
rescue e : LxChess::Notation::InvalidNotation | LxChess::Game::SanError | LxChess::Game::IllegalMove
if msg = e.message
log.unshift msg

View file

@ -18,7 +18,7 @@ module LxChess
turn: turn,
castling: castling,
en_passant: en_passant,
halfmove_clock: halfmove_clock.to_i16,
halfmove_clock: halfmove_clock.to_i8,
fullmove_counter: fullmove_counter.to_i16
)
end
@ -60,24 +60,40 @@ module LxChess
property turn : String
property castling : String
property en_passant : String
property halfmove_clock : Int16
property halfmove_clock : Int8
property fullmove_counter : Int16
def initialize(
@board : Board,
@turn : String,
@castling : String,
@en_passant : String,
@halfmove_clock : Int16,
@fullmove_counter : Int16
@board,
@turn,
@castling,
@en_passant,
@halfmove_clock,
@fullmove_counter
)
end
def update(game : Game)
if index = game.en_passant_target
@en_passant = @board.cord(index)
else
@en_passant = "-"
end
@castling = game.castling
@halfmove_clock = game.fifty_move_rule
@fullmove_counter = game.full_moves + 1
end
def placement
rows = @board.map { |piece| piece ? piece.fen_symbol : nil }
.each_slice(@board.width)
.map { |row| row.chunks { |r| r.nil? }.map { |chunked, values| chunked ? values.size : values.join }.join }
.to_a.reverse.join('/')
end
def to_s
[placement, @turn, @castling, @en_passant, @halfmove_clock, @fullmove_counter].join(' ')
end
end
end

View file

@ -20,6 +20,16 @@ module LxChess
def initialize(@board : Board = Board.new, @players = [] of Player)
end
def castling
io = IO::Memory.new
@players.map_with_index do |p, i|
io << (i == 0 ? 'K' : 'k') if p.castle_king
io << (i == 0 ? 'Q' : 'q') if p.castle_queen
end
c_stirng = io.to_s
c_stirng.size == 0 ? "-" : c_stirng
end
def current_player
@players[@turn]
end
@ -59,10 +69,6 @@ module LxChess
end
end
if index
puts @board.cord(index)
end
fen_symbol = notation.fen_symbol(@turn == 0 ? "w" : "b")
pieces = @board.select do |piece|
@ -182,10 +188,10 @@ module LxChess
{x: -1, y: -1}, # down left
])
# TODO: castling
if can_castle_right?(piece)
if can_castle_king?(piece)
set.add_offset(x: 2, y: 0)
end
if can_castle_left?(piece)
if can_castle_queen?(piece)
set.add_offset(x: -2, y: 0)
end
end
@ -193,20 +199,20 @@ module LxChess
end
end
def can_castle_right?(piece)
def can_castle_king?(piece)
return false if @players.empty?
player = piece.white? ? @players[0] : @players[1]
return false unless player.castle_right
return false unless player.castle_king
return false unless index = piece.index
return false unless @board.border_right(index) >= 2
# TODO: figure out if castling crosses checks
@board[index + 1].nil? && @board[index + 2].nil?
end
def can_castle_left?(piece)
def can_castle_queen?(piece)
return false if @players.empty?
player = piece.white? ? @players[0] : @players[1]
return false unless player.castle_left
return false unless player.castle_queen
return false unless index = piece.index
return false unless @board.border_left(index) <= 2
# TODO: figure out if castling crosses checks
@ -272,6 +278,8 @@ module LxChess
@en_passant_target = nil
end
else
@en_passant_target = nil
end
@board.move(from, to)

View file

@ -1,11 +1,11 @@
module LxChess
class Player
property castle_left : Bool = true
property castle_right : Bool = true
property castle_king : Bool = true
property castle_queen : Bool = true
def no_castling!
@castle_right = false
@castle_left = false
@castle_king = false
@castle_queen = false
end
end
end