mirror of
https://github.com/SleepingInsomniac/lx_chess_cr
synced 2025-01-31 19:57:54 +01:00
Change Game#index to Game#index_of to unshadow previous def
the method #index is defined by the enumerable module, and caused type confusion
This commit is contained in:
parent
213eaaac2e
commit
a0f65efbb5
7 changed files with 36 additions and 25 deletions
|
@ -26,4 +26,11 @@ describe LxChess::Board do
|
||||||
board.border_right(12).should eq(15)
|
board.border_right(12).should eq(15)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#index" do
|
||||||
|
it "returns the index of a coordinate" do
|
||||||
|
board = LxChess::Board.new
|
||||||
|
board.index_of("e2").should eq(12)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,8 +13,8 @@ describe Game do
|
||||||
game = Game.new
|
game = Game.new
|
||||||
game.board["e2"] = Piece.from_fen('P')
|
game.board["e2"] = Piece.from_fen('P')
|
||||||
from, to = game.parse_san("e4")
|
from, to = game.parse_san("e4")
|
||||||
from.should eq(game.board.index("e2"))
|
from.should eq(game.board.index_of("e2"))
|
||||||
to.should eq(game.board.index("e4"))
|
to.should eq(game.board.index_of("e4"))
|
||||||
end
|
end
|
||||||
|
|
||||||
it "disambiguates the move correctly" do
|
it "disambiguates the move correctly" do
|
||||||
|
@ -27,7 +27,7 @@ describe Game do
|
||||||
|
|
||||||
moves = game.parse_san("dxe5")
|
moves = game.parse_san("dxe5")
|
||||||
debug_board(game, moves)
|
debug_board(game, moves)
|
||||||
moves.should eq(["d4", "e5"].map { |s| game.board.index(s) })
|
moves.should eq(["d4", "e5"].map { |s| game.board.index_of(s) })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,11 +5,11 @@ require "../src/lx_chess/board"
|
||||||
require "../src/lx_chess/term_board"
|
require "../src/lx_chess/term_board"
|
||||||
|
|
||||||
def debug_board(game : LxChess::Game, moves : Array(String))
|
def debug_board(game : LxChess::Game, moves : Array(String))
|
||||||
moves = moves.map { |m| game.board.index(m) }
|
moves = moves.map { |m| game.board.index_of(m) }
|
||||||
debug_board(game, moves)
|
debug_board(game, moves)
|
||||||
end
|
end
|
||||||
|
|
||||||
def debug_board(game : LxChess::Game, moves = [] of Int16)
|
def debug_board(game : LxChess::Game, moves : Array(Int16) = [] of Int16)
|
||||||
puts
|
puts
|
||||||
gb = LxChess::TermBoard.new(game.board)
|
gb = LxChess::TermBoard.new(game.board)
|
||||||
gb.highlight(moves)
|
gb.highlight(moves)
|
||||||
|
|
|
@ -38,12 +38,12 @@ module LxChess
|
||||||
|
|
||||||
# Retrieve a piece at a human cord ex: `a1`
|
# Retrieve a piece at a human cord ex: `a1`
|
||||||
def [](cord : String)
|
def [](cord : String)
|
||||||
self[index(cord)]
|
self[index_of(cord)]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Retrieve a piece at an *x* and *y* cord
|
# Retrieve a piece at an *x* and *y* cord
|
||||||
def at(x : Int, y : Int)
|
def at(x : Int, y : Int)
|
||||||
self[index(x, y)]
|
self[index_of(x, y)]
|
||||||
end
|
end
|
||||||
|
|
||||||
def rel_index(index : Int, x : Int, y : Int)
|
def rel_index(index : Int, x : Int, y : Int)
|
||||||
|
@ -58,21 +58,21 @@ module LxChess
|
||||||
|
|
||||||
# Set a piece on the board at a certain human readable *cord*
|
# Set a piece on the board at a certain human readable *cord*
|
||||||
def []=(cord : String, piece : Piece | Nil)
|
def []=(cord : String, piece : Piece | Nil)
|
||||||
self[index(cord)] = piece
|
self[index_of(cord)] = piece
|
||||||
end
|
end
|
||||||
|
|
||||||
# Convert an *x* and *y* position into an index.
|
# Convert an *x* and *y* position into an index.
|
||||||
# Ex: `4, 4` => `36`
|
# Ex: `4, 4` => `36`
|
||||||
def index(x : Int, y : Int)
|
def index_of(x : Int, y : Int)
|
||||||
((y * @width) + x).to_i16
|
((y * @width) + x).to_i16
|
||||||
end
|
end
|
||||||
|
|
||||||
# Convert human *cord* into an index on the board.
|
# Convert human *cord* into an index on the board.
|
||||||
# Ex: `a1` => `0`
|
# Ex: `a1` => `0`
|
||||||
def index(cord : String)
|
def index_of(cord : String)
|
||||||
x = LETTERS.index(cord[0].downcase) || 0
|
x = LETTERS.index(cord[0].downcase) || 0
|
||||||
y = cord[1].to_i - 1
|
y = cord[1].to_i - 1
|
||||||
index(x, y)
|
index_of(x, y)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Convert an *index* into a human coordinate (ex: `a1`)
|
# Convert an *index* into a human coordinate (ex: `a1`)
|
||||||
|
|
|
@ -52,7 +52,7 @@ module LxChess
|
||||||
|
|
||||||
def en_passant_target=(cord : String)
|
def en_passant_target=(cord : String)
|
||||||
if cord =~ /[a-z]+\d+/
|
if cord =~ /[a-z]+\d+/
|
||||||
@en_passant_target = @board.index(cord)
|
@en_passant_target = @board.index_of(cord)
|
||||||
else
|
else
|
||||||
@en_passant_target = nil
|
@en_passant_target = nil
|
||||||
end
|
end
|
||||||
|
@ -63,7 +63,7 @@ module LxChess
|
||||||
end
|
end
|
||||||
|
|
||||||
def en_passant_target=(cord : String)
|
def en_passant_target=(cord : String)
|
||||||
@en_passant_target = @board.index(cord)
|
@en_passant_target = @board.index_of(cord)
|
||||||
end
|
end
|
||||||
|
|
||||||
def full_moves
|
def full_moves
|
||||||
|
@ -85,7 +85,7 @@ module LxChess
|
||||||
|
|
||||||
# Parse standard algebraic notation
|
# Parse standard algebraic notation
|
||||||
def parse_san(notation : Notation)
|
def parse_san(notation : Notation)
|
||||||
index = @board.index(notation.square)
|
index = nil
|
||||||
|
|
||||||
if notation.castles?
|
if notation.castles?
|
||||||
raise "expected to find a king, but couldn't!" unless king = find_king
|
raise "expected to find a king, but couldn't!" unless king = find_king
|
||||||
|
@ -103,6 +103,12 @@ module LxChess
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if square = notation.square
|
||||||
|
index = @board.index_of(square)
|
||||||
|
end
|
||||||
|
|
||||||
|
raise "Missing index" unless index
|
||||||
|
|
||||||
fen_symbol = notation.fen_symbol(@turn == 0 ? "w" : "b")
|
fen_symbol = notation.fen_symbol(@turn == 0 ? "w" : "b")
|
||||||
|
|
||||||
move_sets = @board.map do |piece|
|
move_sets = @board.map do |piece|
|
||||||
|
@ -134,14 +140,14 @@ module LxChess
|
||||||
raise SanError.new("#{notation.to_s} is ambiguous") if move_sets.size > 1
|
raise SanError.new("#{notation.to_s} is ambiguous") if move_sets.size > 1
|
||||||
if set = move_sets.first?
|
if set = move_sets.first?
|
||||||
# from, to
|
# from, to
|
||||||
[set.piece.index.as(Int16), index.as(Int16)]
|
[set.piece.index, index]
|
||||||
else
|
else
|
||||||
raise SanError.new("no moves matching `#{notation.to_s}`")
|
raise SanError.new("no moves matching `#{notation.to_s}`")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def move_to_san(from : String, to : String, promotion : Char? = nil, turn = @turn)
|
def move_to_san(from : String, to : String, promotion : Char? = nil, turn = @turn)
|
||||||
move_to_san(@board.index(from), @board.index(to), promotion, turn)
|
move_to_san(@board.index_of(from), @board.index_of(to), promotion, turn)
|
||||||
end
|
end
|
||||||
|
|
||||||
def move_to_san(from : Int, to : Int, promotion : Char? = nil, turn = @turn)
|
def move_to_san(from : Int, to : Int, promotion : Char? = nil, turn = @turn)
|
||||||
|
@ -296,7 +302,6 @@ module LxChess
|
||||||
return false unless player.castle_king
|
return false unless player.castle_king
|
||||||
return false unless index = piece.index
|
return false unless index = piece.index
|
||||||
return false unless (index - @board.border_left(index)).abs >= 2
|
return false unless (index - @board.border_left(index)).abs >= 2
|
||||||
# TODO: figure out if castling crosses checks
|
|
||||||
@board[index + 1].nil? && @board[index + 2].nil?
|
@board[index + 1].nil? && @board[index + 2].nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -306,12 +311,11 @@ module LxChess
|
||||||
return false unless player.castle_queen
|
return false unless player.castle_queen
|
||||||
return false unless index = piece.index
|
return false unless index = piece.index
|
||||||
return false unless (index - @board.border_left(index)).abs >= 2
|
return false unless (index - @board.border_left(index)).abs >= 2
|
||||||
# TODO: figure out if castling crosses checks
|
|
||||||
@board[index - 1].nil? && @board[index - 2].nil?
|
@board[index - 1].nil? && @board[index - 2].nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
def tmp_move(from : String, to : String, promotion : Char? = nil)
|
def tmp_move(from : String, to : String, promotion : Char? = nil)
|
||||||
tmp_move(@board.index(from), @board.index(to), promotion) do
|
tmp_move(@board.index_of(from), @board.index_of(to), promotion) do
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -334,7 +338,7 @@ module LxChess
|
||||||
end
|
end
|
||||||
|
|
||||||
def make_move(from : String, to : String, promotion : Char? = nil)
|
def make_move(from : String, to : String, promotion : Char? = nil)
|
||||||
make_move(from: @board.index(from), to: @board.index(to), promotion: promotion)
|
make_move(from: @board.index_of(from), to: @board.index_of(to), promotion: promotion)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Make a move given a set of coordinates
|
# Make a move given a set of coordinates
|
||||||
|
@ -368,7 +372,7 @@ module LxChess
|
||||||
p && p.color == piece.color && p.rook? && p.index.as(Int16) > piece.index.as(Int16)
|
p && p.color == piece.color && p.rook? && p.index.as(Int16) > piece.index.as(Int16)
|
||||||
end
|
end
|
||||||
if rook
|
if rook
|
||||||
@board.move(from: rook.index.as(Int16), to: to - 1)
|
@board.move(from: rook.index, to: to - 1)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
tmp_move(from, to + 1) do
|
tmp_move(from, to + 1) do
|
||||||
|
@ -379,7 +383,7 @@ module LxChess
|
||||||
p && p.color == piece.color && p.rook? && p.index.as(Int16) < piece.index.as(Int16)
|
p && p.color == piece.color && p.rook? && p.index.as(Int16) < piece.index.as(Int16)
|
||||||
end
|
end
|
||||||
if rook
|
if rook
|
||||||
@board.move(from: rook.index.as(Int16), to: to + 1)
|
@board.move(from: rook.index, to: to + 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -73,7 +73,7 @@ module LxChess
|
||||||
io << (y + 1).to_s.rjust(3) << ": "
|
io << (y + 1).to_s.rjust(3) << ": "
|
||||||
|
|
||||||
files.each do |x|
|
files.each do |x|
|
||||||
index = @board.index(x, y)
|
index = @board.index_of(x, y)
|
||||||
piece = @board[index]
|
piece = @board[index]
|
||||||
|
|
||||||
tint = (index + y) % 2 == 0 ? :light : :dark
|
tint = (index + y) % 2 == 0 ? :light : :dark
|
||||||
|
|
|
@ -38,7 +38,7 @@ module LxChess
|
||||||
when /moves\s+([a-z]\d)/i
|
when /moves\s+([a-z]\d)/i
|
||||||
if matches = input.match(/[a-z]\d/i)
|
if matches = input.match(/[a-z]\d/i)
|
||||||
if square = matches[0]?
|
if square = matches[0]?
|
||||||
if index = @game.board.index(square)
|
if index = @game.board.index_of(square)
|
||||||
if set = @game.moves(index)
|
if set = @game.moves(index)
|
||||||
@gb.highlight(set.moves, "blue")
|
@gb.highlight(set.moves, "blue")
|
||||||
from = "#{set.piece.fen_symbol}#{@game.board.cord(index)}: "
|
from = "#{set.piece.fen_symbol}#{@game.board.cord(index)}: "
|
||||||
|
@ -83,7 +83,7 @@ module LxChess
|
||||||
san = @game.move_to_san(from, to, promo)
|
san = @game.move_to_san(from, to, promo)
|
||||||
@game.make_move(from, to, promo)
|
@game.make_move(from, to, promo)
|
||||||
@pgn.history << san
|
@pgn.history << san
|
||||||
@gb.highlight([@game.board.index(from), @game.board.index(to)])
|
@gb.highlight([@game.board.index_of(from), @game.board.index_of(to)])
|
||||||
@log.unshift "#{san.to_s}: #{from} => #{to}"
|
@log.unshift "#{san.to_s}: #{from} => #{to}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue