From 8a7d7a11cf6f5c8d306cf578c7622fdd4c382111 Mon Sep 17 00:00:00 2001 From: Alex Clink Date: Sat, 2 Oct 2021 19:12:06 -0400 Subject: [PATCH] Update tests --- spec/lx_chess/computer_spec.cr | 17 ++++++++++ spec/lx_chess/game/castling_spec.cr | 10 +++--- spec/lx_chess/game/checkmate_spec.cr | 2 +- spec/lx_chess/game/move_to_san_spec.cr | 18 +++++----- spec/lx_chess/game/parse_san_spec.cr | 2 +- spec/lx_chess/game_spec.cr | 47 +++++++++++--------------- spec/spec_helper.cr | 10 +++--- 7 files changed, 57 insertions(+), 49 deletions(-) create mode 100644 spec/lx_chess/computer_spec.cr diff --git a/spec/lx_chess/computer_spec.cr b/spec/lx_chess/computer_spec.cr new file mode 100644 index 0000000..964a049 --- /dev/null +++ b/spec/lx_chess/computer_spec.cr @@ -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 diff --git a/spec/lx_chess/game/castling_spec.cr b/spec/lx_chess/game/castling_spec.cr index 00c161f..7eb9b14 100644 --- a/spec/lx_chess/game/castling_spec.cr +++ b/spec/lx_chess/game/castling_spec.cr @@ -14,7 +14,7 @@ describe "Castling" do game = Game.new(board: fen.board, players: [Player.new, Player.new]) game.make_move(from: "e1", to: "g1") - debug_board(game, ["e1", "g1"]) + debug_board(game.board, ["e1", "g1"]) fen.update(game) fen.to_s.should eq("8/8/8/8/8/8/8/R4RK1 b kq - 1 1") end @@ -24,7 +24,7 @@ describe "Castling" do game = Game.new(board: fen.board, players: [Player.new, Player.new]) game.make_move(from: "e1", to: "c1") - debug_board(game, ["e1", "c1"]) + debug_board(game.board, ["e1", "c1"]) fen.update(game) fen.to_s.should eq("8/8/8/8/8/8/8/2KR3R b kq - 1 1") end @@ -36,7 +36,7 @@ describe "Castling" do game = Game.new(board: fen.board, players: [Player.new, Player.new]) game.make_move(from: "e8", to: "g8") - debug_board(game, ["e8", "g8"]) + debug_board(game.board, ["e8", "g8"]) fen.update(game) fen.to_s.should eq("r4rk1/8/8/8/8/8/8/8 b kq - 1 1") end @@ -46,7 +46,7 @@ describe "Castling" do game = Game.new(board: fen.board, players: [Player.new, Player.new]) game.make_move(from: "e8", to: "c8") - debug_board(game, ["e8", "c8"]) + debug_board(game.board, ["e8", "c8"]) fen.update(game) fen.to_s.should eq("2kr3r/8/8/8/8/8/8/8 b kq - 1 1") end @@ -58,7 +58,7 @@ describe "Castling" do game = Game.new(board: fen.board, players: [Player.new, Player.new]) game.make_move(from: "c1", to: "e1") - debug_board(game, ["c1", "e1"]) + debug_board(game.board, ["c1", "e1"]) fen.update(game) fen.to_s.should eq("8/8/8/8/8/8/8/R2RK3 b kq - 1 1") end diff --git a/spec/lx_chess/game/checkmate_spec.cr b/spec/lx_chess/game/checkmate_spec.cr index 1d46119..bc4f570 100644 --- a/spec/lx_chess/game/checkmate_spec.cr +++ b/spec/lx_chess/game/checkmate_spec.cr @@ -15,7 +15,7 @@ describe Game do game = Game.new(board: fen.board, players: [Player.new, Player.new]) game.checkmate?(1).should eq(false) san = game.make_move("h5", "f7") - debug_board(game, ["h5", "f7"]) + debug_board(game.board, ["h5", "f7"]) game.checkmate?(1).should eq(true) end end diff --git a/spec/lx_chess/game/move_to_san_spec.cr b/spec/lx_chess/game/move_to_san_spec.cr index 1dc4f80..436c3d5 100644 --- a/spec/lx_chess/game/move_to_san_spec.cr +++ b/spec/lx_chess/game/move_to_san_spec.cr @@ -10,7 +10,7 @@ describe Game do "e4" => '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) puts san.to_s san.to_s.should eq("exd5") @@ -19,7 +19,7 @@ describe Game do it "converts pawn moves" do game = Game.new players: [Player.new, Player.new] 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) puts san.to_s san.to_s.should eq("e4") @@ -32,11 +32,11 @@ describe Game do "e4" => '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) puts san.to_s 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) puts san.to_s san.to_s.should eq("cxd5") @@ -48,7 +48,7 @@ describe Game do "e4" => '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) puts san.to_s san.to_s.should eq("Ree5") @@ -60,7 +60,7 @@ describe Game do "f3" => '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) puts san.to_s san.to_s.should eq("Ned4") @@ -72,7 +72,7 @@ describe Game do "e4" => '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) puts san.to_s san.to_s.should eq("Ne2c3") @@ -82,7 +82,7 @@ describe Game do game = Game.new players: [Player.new, Player.new] game.board["e1"] = Piece.from_fen('K') 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) puts san.to_s 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") game = Game.new(board: fen.board, players: [Player.new, Player.new]) 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 san.to_s.should eq("Qxf7#") end diff --git a/spec/lx_chess/game/parse_san_spec.cr b/spec/lx_chess/game/parse_san_spec.cr index 7f3d6f1..3ae8d8e 100644 --- a/spec/lx_chess/game/parse_san_spec.cr +++ b/spec/lx_chess/game/parse_san_spec.cr @@ -26,7 +26,7 @@ describe Game do }) 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) }) end end diff --git a/spec/lx_chess/game_spec.cr b/spec/lx_chess/game_spec.cr index 378556a..600ed73 100644 --- a/spec/lx_chess/game_spec.cr +++ b/spec/lx_chess/game_spec.cr @@ -9,15 +9,6 @@ require "../../src/lx_chess/player" include LxChess 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 it "removes illegal moves" do game = Game.new players: [Player.new, Player.new] @@ -27,7 +18,7 @@ describe Game do if move_set = game.moves("e4") 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]) else raise "move set was nil" @@ -79,7 +70,7 @@ describe Game do game = Game.new players: [Player.new, Player.new] game.board["e1"] = Piece.from_fen('K') game.board["e8"] = Piece.from_fen('r') - debug_board(game) + debug_board(game.board) game.in_check?(0).should eq(true) end end @@ -134,7 +125,7 @@ describe Game do game = Game.new game.board["e2"] = Piece.from_fen('P') 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]) else raise "no moves" @@ -145,7 +136,7 @@ describe Game do game = Game.new game.board["e7"] = Piece.from_fen('p') 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]) else raise "no moves" @@ -156,7 +147,7 @@ describe Game do game = Game.new game.board["e3"] = Piece.from_fen('P') 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]) else raise "no moves" @@ -167,7 +158,7 @@ describe Game do game = Game.new game.board["e6"] = Piece.from_fen('p') 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]) else raise "no moves" @@ -180,7 +171,7 @@ describe Game do game.board["f5"] = Piece.from_fen('p') game.board["d5"] = Piece.from_fen('p') 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]) else raise "no moves" @@ -193,7 +184,7 @@ describe Game do game.board["f5"] = Piece.from_fen('P') game.board["d5"] = Piece.from_fen('P') 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]) else raise "no moves" @@ -206,7 +197,7 @@ describe Game do game.board["d5"] = Piece.from_fen('p') game.en_passant_target = "d6" 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]) else raise "no moves" @@ -217,7 +208,7 @@ describe Game do game = Game.new game.board["c3"] = Piece.from_fen('N') 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]) else raise "no moves" @@ -228,7 +219,7 @@ describe Game do game = Game.new game.board["a1"] = Piece.from_fen('N') 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]) else raise "no moves" @@ -239,7 +230,7 @@ describe Game do game = Game.new game.board["h1"] = Piece.from_fen('N') 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]) else raise "no moves" @@ -250,7 +241,7 @@ describe Game do game = Game.new game.board["e4"] = Piece.from_fen('R') 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[ d4 c4 b4 a4 e5 e6 e7 e8 @@ -266,7 +257,7 @@ describe Game do game = Game.new game.board["e4"] = Piece.from_fen('B') 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[ d5 c6 b7 a8 f5 g6 h7 @@ -282,7 +273,7 @@ describe Game do game = Game.new game.board["e4"] = Piece.from_fen('K') 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[ d4 d5 e5 f5 f4 f3 e3 d3 ]) @@ -295,7 +286,7 @@ describe Game do game = Game.new game.board["h4"] = Piece.from_fen('K') 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[ g4 g5 h5 h3 g3 ]) @@ -308,7 +299,7 @@ describe Game do game = Game.new game.board["e4"] = Piece.from_fen('Q') 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[ d5 c6 b7 a8 f5 g6 h7 @@ -329,7 +320,7 @@ describe Game do game.board["e4"] = Piece.from_fen('B') game.board["f5"] = Piece.from_fen('P') 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[ d5 c6 b7 a8 f3 g2 h1 @@ -345,7 +336,7 @@ describe Game do game.board["e4"] = Piece.from_fen('B') game.board["f5"] = Piece.from_fen('p') 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[ d5 c6 b7 a8 f5 diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index d0d32e4..dd2199c 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -4,14 +4,14 @@ require "../src/lx_chess/game" require "../src/lx_chess/board" require "../src/lx_chess/term_board" -def debug_board(game : LxChess::Game, moves : Array(String)) - moves = moves.map { |m| game.board.index_of(m) } - debug_board(game, moves) +def debug_board(board : LxChess::Board, moves : Array(String)) + moves = moves.map { |m| board.index_of(m) } + debug_board(board, moves) end -def debug_board(game : LxChess::Game, moves : Array(Int16) = [] of Int16) +def debug_board(board : LxChess::Board, moves : Array(Int16) = [] of Int16) puts - gb = LxChess::TermBoard.new(game.board) + gb = LxChess::TermBoard.new(board) gb.highlight(moves) gb.draw puts