Update tests

This commit is contained in:
Alex Clink 2021-10-02 19:12:06 -04:00
parent 22084ccbe3
commit 8a7d7a11cf
7 changed files with 57 additions and 49 deletions

View file

@ -0,0 +1,17 @@
require "../spec_helper"
require "../../src/lx_chess/computer"
require "../../src/lx_chess/board"
require "../../src/lx_chess/piece"
include LxChess
describe Computer do
describe "#board_score" do
it "scores the board" do
computer = Computer.new
game = Game.new
game.board["e4"] = Piece.from_fen('P')
computer.board_score(game).should eq(100)
end
end
end

View file

@ -14,7 +14,7 @@ describe "Castling" do
game = Game.new(board: fen.board, players: [Player.new, Player.new]) game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "e1", to: "g1") game.make_move(from: "e1", to: "g1")
debug_board(game, ["e1", "g1"]) debug_board(game.board, ["e1", "g1"])
fen.update(game) fen.update(game)
fen.to_s.should eq("8/8/8/8/8/8/8/R4RK1 b kq - 1 1") fen.to_s.should eq("8/8/8/8/8/8/8/R4RK1 b kq - 1 1")
end end
@ -24,7 +24,7 @@ describe "Castling" do
game = Game.new(board: fen.board, players: [Player.new, Player.new]) game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "e1", to: "c1") game.make_move(from: "e1", to: "c1")
debug_board(game, ["e1", "c1"]) debug_board(game.board, ["e1", "c1"])
fen.update(game) fen.update(game)
fen.to_s.should eq("8/8/8/8/8/8/8/2KR3R b kq - 1 1") fen.to_s.should eq("8/8/8/8/8/8/8/2KR3R b kq - 1 1")
end end
@ -36,7 +36,7 @@ describe "Castling" do
game = Game.new(board: fen.board, players: [Player.new, Player.new]) game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "e8", to: "g8") game.make_move(from: "e8", to: "g8")
debug_board(game, ["e8", "g8"]) debug_board(game.board, ["e8", "g8"])
fen.update(game) fen.update(game)
fen.to_s.should eq("r4rk1/8/8/8/8/8/8/8 b kq - 1 1") fen.to_s.should eq("r4rk1/8/8/8/8/8/8/8 b kq - 1 1")
end end
@ -46,7 +46,7 @@ describe "Castling" do
game = Game.new(board: fen.board, players: [Player.new, Player.new]) game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "e8", to: "c8") game.make_move(from: "e8", to: "c8")
debug_board(game, ["e8", "c8"]) debug_board(game.board, ["e8", "c8"])
fen.update(game) fen.update(game)
fen.to_s.should eq("2kr3r/8/8/8/8/8/8/8 b kq - 1 1") fen.to_s.should eq("2kr3r/8/8/8/8/8/8/8 b kq - 1 1")
end end
@ -58,7 +58,7 @@ describe "Castling" do
game = Game.new(board: fen.board, players: [Player.new, Player.new]) game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "c1", to: "e1") game.make_move(from: "c1", to: "e1")
debug_board(game, ["c1", "e1"]) debug_board(game.board, ["c1", "e1"])
fen.update(game) fen.update(game)
fen.to_s.should eq("8/8/8/8/8/8/8/R2RK3 b kq - 1 1") fen.to_s.should eq("8/8/8/8/8/8/8/R2RK3 b kq - 1 1")
end end

View file

@ -15,7 +15,7 @@ describe Game do
game = Game.new(board: fen.board, players: [Player.new, Player.new]) game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.checkmate?(1).should eq(false) game.checkmate?(1).should eq(false)
san = game.make_move("h5", "f7") san = game.make_move("h5", "f7")
debug_board(game, ["h5", "f7"]) debug_board(game.board, ["h5", "f7"])
game.checkmate?(1).should eq(true) game.checkmate?(1).should eq(true)
end end
end end

View file

@ -10,7 +10,7 @@ describe Game do
"e4" => 'P', "e4" => 'P',
"d5" => 'p', "d5" => 'p',
}) })
debug_board(game, ["e4", "d5"]) debug_board(game.board, ["e4", "d5"])
san = game.move_to_san(from: "e4", to: "d5", turn: 0) san = game.move_to_san(from: "e4", to: "d5", turn: 0)
puts san.to_s puts san.to_s
san.to_s.should eq("exd5") san.to_s.should eq("exd5")
@ -19,7 +19,7 @@ describe Game do
it "converts pawn moves" do it "converts pawn moves" do
game = Game.new players: [Player.new, Player.new] game = Game.new players: [Player.new, Player.new]
place(game.board, {"e2" => 'P'}) place(game.board, {"e2" => 'P'})
debug_board(game, ["e2", "e4"]) debug_board(game.board, ["e2", "e4"])
san = game.move_to_san(from: "e2", to: "e4", turn: 0) san = game.move_to_san(from: "e2", to: "e4", turn: 0)
puts san.to_s puts san.to_s
san.to_s.should eq("e4") san.to_s.should eq("e4")
@ -32,11 +32,11 @@ describe Game do
"e4" => 'P', "e4" => 'P',
"d5" => 'p', "d5" => 'p',
}) })
debug_board(game, ["e4", "d5"]) debug_board(game.board, ["e4", "d5"])
san = game.move_to_san(from: "e4", to: "d5", turn: 0) san = game.move_to_san(from: "e4", to: "d5", turn: 0)
puts san.to_s puts san.to_s
san.to_s.should eq("exd5") san.to_s.should eq("exd5")
debug_board(game, ["c4", "d5"]) debug_board(game.board, ["c4", "d5"])
san = game.move_to_san(from: "c4", to: "d5", turn: 0) san = game.move_to_san(from: "c4", to: "d5", turn: 0)
puts san.to_s puts san.to_s
san.to_s.should eq("cxd5") san.to_s.should eq("cxd5")
@ -48,7 +48,7 @@ describe Game do
"e4" => 'R', "e4" => 'R',
"d5" => 'R', "d5" => 'R',
}) })
debug_board(game, ["e4", "e5"]) debug_board(game.board, ["e4", "e5"])
san = game.move_to_san(from: "e4", to: "e5", turn: 0) san = game.move_to_san(from: "e4", to: "e5", turn: 0)
puts san.to_s puts san.to_s
san.to_s.should eq("Ree5") san.to_s.should eq("Ree5")
@ -60,7 +60,7 @@ describe Game do
"f3" => 'N', "f3" => 'N',
"e2" => 'N', "e2" => 'N',
}) })
debug_board(game, ["e2", "d4"]) debug_board(game.board, ["e2", "d4"])
san = game.move_to_san(from: "e2", to: "d4", turn: 0) san = game.move_to_san(from: "e2", to: "d4", turn: 0)
puts san.to_s puts san.to_s
san.to_s.should eq("Ned4") san.to_s.should eq("Ned4")
@ -72,7 +72,7 @@ describe Game do
"e4" => 'N', "e4" => 'N',
"e2" => 'N', "e2" => 'N',
}) })
debug_board(game, ["e2", "c3"]) debug_board(game.board, ["e2", "c3"])
san = game.move_to_san(from: "e2", to: "c3", turn: 0) san = game.move_to_san(from: "e2", to: "c3", turn: 0)
puts san.to_s puts san.to_s
san.to_s.should eq("Ne2c3") san.to_s.should eq("Ne2c3")
@ -82,7 +82,7 @@ describe Game do
game = Game.new players: [Player.new, Player.new] game = Game.new players: [Player.new, Player.new]
game.board["e1"] = Piece.from_fen('K') game.board["e1"] = Piece.from_fen('K')
game.board["c8"] = Piece.from_fen('r') game.board["c8"] = Piece.from_fen('r')
debug_board(game, ["c8", "e8"]) debug_board(game.board, ["c8", "e8"])
san = game.move_to_san(from: "c8", to: "e8", turn: 1) san = game.move_to_san(from: "c8", to: "e8", turn: 1)
puts san.to_s puts san.to_s
san.to_s.should eq("Re8+") san.to_s.should eq("Re8+")
@ -92,7 +92,7 @@ describe Game do
fen = Fen.parse("r1bqkb1r/pppp1ppp/2n2n2/4p2Q/2B1P3/8/PPPP1PPP/RNB1K1NR w KQkq - 4 4") fen = Fen.parse("r1bqkb1r/pppp1ppp/2n2n2/4p2Q/2B1P3/8/PPPP1PPP/RNB1K1NR w KQkq - 4 4")
game = Game.new(board: fen.board, players: [Player.new, Player.new]) game = Game.new(board: fen.board, players: [Player.new, Player.new])
san = game.move_to_san(from: "h5", to: "f7", turn: 0) san = game.move_to_san(from: "h5", to: "f7", turn: 0)
debug_board(game, ["h5", "f7"]) debug_board(game.board, ["h5", "f7"])
puts san.to_s puts san.to_s
san.to_s.should eq("Qxf7#") san.to_s.should eq("Qxf7#")
end end

View file

@ -26,7 +26,7 @@ describe Game do
}) })
moves = game.parse_san("dxe5") moves = game.parse_san("dxe5")
debug_board(game, moves) debug_board(game.board, moves)
moves.should eq(["d4", "e5"].map { |s| game.board.index_of(s) }) moves.should eq(["d4", "e5"].map { |s| game.board.index_of(s) })
end end
end end

View file

@ -9,15 +9,6 @@ require "../../src/lx_chess/player"
include LxChess include LxChess
describe Game do describe Game do
describe "#score" do
it "scores the board" do
game = Game.new
game.board["e4"] = Piece.from_fen('P')
debug_board(game)
game.score.should eq(100)
end
end
describe "#remove_illegal_moves" do describe "#remove_illegal_moves" do
it "removes illegal moves" do it "removes illegal moves" do
game = Game.new players: [Player.new, Player.new] game = Game.new players: [Player.new, Player.new]
@ -27,7 +18,7 @@ describe Game do
if move_set = game.moves("e4") if move_set = game.moves("e4")
move_set = game.remove_illegal_moves(move_set) move_set = game.remove_illegal_moves(move_set)
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.should eq([36, 44, 52, 60, 20, 12]) move_set.moves.should eq([36, 44, 52, 60, 20, 12])
else else
raise "move set was nil" raise "move set was nil"
@ -79,7 +70,7 @@ describe Game do
game = Game.new players: [Player.new, Player.new] game = Game.new players: [Player.new, Player.new]
game.board["e1"] = Piece.from_fen('K') game.board["e1"] = Piece.from_fen('K')
game.board["e8"] = Piece.from_fen('r') game.board["e8"] = Piece.from_fen('r')
debug_board(game) debug_board(game.board)
game.in_check?(0).should eq(true) game.in_check?(0).should eq(true)
end end
end end
@ -134,7 +125,7 @@ describe Game do
game = Game.new game = Game.new
game.board["e2"] = Piece.from_fen('P') game.board["e2"] = Piece.from_fen('P')
if move_set = game.moves("e2") if move_set = game.moves("e2")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e3 e4]) move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e3 e4])
else else
raise "no moves" raise "no moves"
@ -145,7 +136,7 @@ describe Game do
game = Game.new game = Game.new
game.board["e7"] = Piece.from_fen('p') game.board["e7"] = Piece.from_fen('p')
if move_set = game.moves("e7") if move_set = game.moves("e7")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e6 e5]) move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e6 e5])
else else
raise "no moves" raise "no moves"
@ -156,7 +147,7 @@ describe Game do
game = Game.new game = Game.new
game.board["e3"] = Piece.from_fen('P') game.board["e3"] = Piece.from_fen('P')
if move_set = game.moves("e3") if move_set = game.moves("e3")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e4]) move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e4])
else else
raise "no moves" raise "no moves"
@ -167,7 +158,7 @@ describe Game do
game = Game.new game = Game.new
game.board["e6"] = Piece.from_fen('p') game.board["e6"] = Piece.from_fen('p')
if move_set = game.moves("e6") if move_set = game.moves("e6")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e5]) move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e5])
else else
raise "no moves" raise "no moves"
@ -180,7 +171,7 @@ describe Game do
game.board["f5"] = Piece.from_fen('p') game.board["f5"] = Piece.from_fen('p')
game.board["d5"] = Piece.from_fen('p') game.board["d5"] = Piece.from_fen('p')
if move_set = game.moves("e4") if move_set = game.moves("e4")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e5 d5 f5]) move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e5 d5 f5])
else else
raise "no moves" raise "no moves"
@ -193,7 +184,7 @@ describe Game do
game.board["f5"] = Piece.from_fen('P') game.board["f5"] = Piece.from_fen('P')
game.board["d5"] = Piece.from_fen('P') game.board["d5"] = Piece.from_fen('P')
if move_set = game.moves("e4") if move_set = game.moves("e4")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e5]) move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e5])
else else
raise "no moves" raise "no moves"
@ -206,7 +197,7 @@ describe Game do
game.board["d5"] = Piece.from_fen('p') game.board["d5"] = Piece.from_fen('p')
game.en_passant_target = "d6" game.en_passant_target = "d6"
if move_set = game.moves("e5") if move_set = game.moves("e5")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e6 d6]) move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[e6 d6])
else else
raise "no moves" raise "no moves"
@ -217,7 +208,7 @@ describe Game do
game = Game.new game = Game.new
game.board["c3"] = Piece.from_fen('N') game.board["c3"] = Piece.from_fen('N')
if move_set = game.moves("c3") if move_set = game.moves("c3")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[a4 b5 d5 e4 e2 d1 b1 a2]) move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[a4 b5 d5 e4 e2 d1 b1 a2])
else else
raise "no moves" raise "no moves"
@ -228,7 +219,7 @@ describe Game do
game = Game.new game = Game.new
game.board["a1"] = Piece.from_fen('N') game.board["a1"] = Piece.from_fen('N')
if move_set = game.moves("a1") if move_set = game.moves("a1")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[b3 c2]) move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[b3 c2])
else else
raise "no moves" raise "no moves"
@ -239,7 +230,7 @@ describe Game do
game = Game.new game = Game.new
game.board["h1"] = Piece.from_fen('N') game.board["h1"] = Piece.from_fen('N')
if move_set = game.moves("h1") if move_set = game.moves("h1")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[f2 g3]) move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[f2 g3])
else else
raise "no moves" raise "no moves"
@ -250,7 +241,7 @@ describe Game do
game = Game.new game = Game.new
game.board["e4"] = Piece.from_fen('R') game.board["e4"] = Piece.from_fen('R')
if move_set = game.moves("e4") if move_set = game.moves("e4")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[ move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[
d4 c4 b4 a4 d4 c4 b4 a4
e5 e6 e7 e8 e5 e6 e7 e8
@ -266,7 +257,7 @@ describe Game do
game = Game.new game = Game.new
game.board["e4"] = Piece.from_fen('B') game.board["e4"] = Piece.from_fen('B')
if move_set = game.moves("e4") if move_set = game.moves("e4")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[ move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[
d5 c6 b7 a8 d5 c6 b7 a8
f5 g6 h7 f5 g6 h7
@ -282,7 +273,7 @@ describe Game do
game = Game.new game = Game.new
game.board["e4"] = Piece.from_fen('K') game.board["e4"] = Piece.from_fen('K')
if move_set = game.moves("e4") if move_set = game.moves("e4")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[ move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[
d4 d5 e5 f5 f4 f3 e3 d3 d4 d5 e5 f5 f4 f3 e3 d3
]) ])
@ -295,7 +286,7 @@ describe Game do
game = Game.new game = Game.new
game.board["h4"] = Piece.from_fen('K') game.board["h4"] = Piece.from_fen('K')
if move_set = game.moves("h4") if move_set = game.moves("h4")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[ move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[
g4 g5 h5 h3 g3 g4 g5 h5 h3 g3
]) ])
@ -308,7 +299,7 @@ describe Game do
game = Game.new game = Game.new
game.board["e4"] = Piece.from_fen('Q') game.board["e4"] = Piece.from_fen('Q')
if move_set = game.moves("e4") if move_set = game.moves("e4")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[ move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[
d5 c6 b7 a8 d5 c6 b7 a8
f5 g6 h7 f5 g6 h7
@ -329,7 +320,7 @@ describe Game do
game.board["e4"] = Piece.from_fen('B') game.board["e4"] = Piece.from_fen('B')
game.board["f5"] = Piece.from_fen('P') game.board["f5"] = Piece.from_fen('P')
if move_set = game.moves("e4") if move_set = game.moves("e4")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[ move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[
d5 c6 b7 a8 d5 c6 b7 a8
f3 g2 h1 f3 g2 h1
@ -345,7 +336,7 @@ describe Game do
game.board["e4"] = Piece.from_fen('B') game.board["e4"] = Piece.from_fen('B')
game.board["f5"] = Piece.from_fen('p') game.board["f5"] = Piece.from_fen('p')
if move_set = game.moves("e4") if move_set = game.moves("e4")
debug_board(game, move_set.moves) debug_board(game.board, move_set.moves)
move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[ move_set.moves.map { |m| game.board.cord(m) }.should eq(%w[
d5 c6 b7 a8 d5 c6 b7 a8
f5 f5

View file

@ -4,14 +4,14 @@ require "../src/lx_chess/game"
require "../src/lx_chess/board" 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(board : LxChess::Board, moves : Array(String))
moves = moves.map { |m| game.board.index_of(m) } moves = moves.map { |m| board.index_of(m) }
debug_board(game, moves) debug_board(board, moves)
end end
def debug_board(game : LxChess::Game, moves : Array(Int16) = [] of Int16) def debug_board(board : LxChess::Board, moves : Array(Int16) = [] of Int16)
puts puts
gb = LxChess::TermBoard.new(game.board) gb = LxChess::TermBoard.new(board)
gb.highlight(moves) gb.highlight(moves)
gb.draw gb.draw
puts puts