Add some specs for parsing SAN

This commit is contained in:
Alex Clink 2021-09-18 12:54:04 -04:00
parent 7614001e45
commit 1397a01aaf
4 changed files with 42 additions and 1 deletions

View file

@ -0,0 +1,34 @@
require "../../spec_helper"
require "../../../src/lx_chess/board"
require "../../../src/lx_chess/piece"
require "../../../src/lx_chess/move_set"
require "../../../src/lx_chess/game"
include LxChess
describe Game do
describe "#parse_san" do
context "when SAN is a pawn move" do
it "returns a correct from and to square" do
game = Game.new
game.board["e2"] = Piece.from_fen('P')
from, to = game.parse_san("e4")
from.should eq(game.board.index("e2"))
to.should eq(game.board.index("e4"))
end
it "disambiguates the move correctly" do
game = Game.new
place(game.board, {
"d4" => 'P',
"f4" => 'P',
"e5" => 'p',
})
moves = game.parse_san("dxe5")
debug_board(game, moves)
moves.should eq(["d4", "e5"].map { |s| game.board.index(s) })
end
end
end
end

View file

@ -19,6 +19,6 @@ end
def place(board : LxChess::Board, squares : Hash(String, Char)) def place(board : LxChess::Board, squares : Hash(String, Char))
squares.each do |cord, sym| squares.each do |cord, sym|
game.board[cord] = LxChess::Piece.from_fen(sym) board[cord] = LxChess::Piece.from_fen(sym)
end end
end end

View file

@ -44,6 +44,7 @@ player_white = LxChess::Player.new
player_black = LxChess::Player.new player_black = LxChess::Player.new
game = LxChess::Game.new(board: fen.board, players: [player_white, player_black]) game = LxChess::Game.new(board: fen.board, players: [player_white, player_black])
gb = LxChess::TermBoard.new(game.board) gb = LxChess::TermBoard.new(game.board)
if theme = options["theme"]? if theme = options["theme"]?
if LxChess::TermBoard::THEMES[theme]? if LxChess::TermBoard::THEMES[theme]?
gb.board_theme = theme gb.board_theme = theme

View file

@ -44,6 +44,7 @@ module LxChess
(@move_clock / 2).to_i16 (@move_clock / 2).to_i16
end end
# Return the current player's king
def own_king def own_king
@board.find do |piece| @board.find do |piece|
next unless piece next unless piece
@ -51,6 +52,11 @@ module LxChess
end end
end end
# Parse SAN from a string
def parse_san(notation : String)
parse_san(Notation.new(notation))
end
# Parse standard algebraic notation # Parse standard algebraic notation
def parse_san(notation : Notation) def parse_san(notation : Notation)
index = @board.index(notation.square) index = @board.index(notation.square)