lx_chess_cr/spec/lx_chess/game/castling_spec.cr

94 lines
3.1 KiB
Crystal
Raw Normal View History

2021-09-22 03:53:15 +02:00
require "../../spec_helper"
require "../../../src/lx_chess/board"
require "../../../src/lx_chess/piece"
require "../../../src/lx_chess/move_set"
require "../../../src/lx_chess/game"
require "../../../src/lx_chess/fen"
include LxChess
describe "Castling" do
describe "white" do
it "moves the king and the rook when castling kingside" do
fen = Fen.parse("8/8/8/8/8/8/8/R3K2R w KQkq - 0 1")
game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "e1", to: "g1")
2021-10-03 01:12:06 +02:00
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
it "moves the king and the rook when castling queenside" do
fen = Fen.parse("8/8/8/8/8/8/8/R3K2R w KQkq - 0 1")
game = Game.new(board: fen.board, players: [Player.new, Player.new])
2021-09-18 05:19:47 +02:00
game.make_move(from: "e1", to: "c1")
2021-10-03 01:12:06 +02:00
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
end
2021-09-18 05:19:47 +02:00
describe "black" do
it "moves the king and the rook when castling kingside" do
fen = Fen.parse("r3k2r/8/8/8/8/8/8/8 w KQkq - 0 1")
game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "e8", to: "g8")
2021-10-03 01:12:06 +02:00
debug_board(game.board, ["e8", "g8"])
2021-09-18 05:19:47 +02:00
fen.update(game)
fen.to_s.should eq("r4rk1/8/8/8/8/8/8/8 b kq - 1 1")
end
it "moves the king and the rook when castling queenside" do
fen = Fen.parse("r3k2r/8/8/8/8/8/8/8 w KQkq - 0 1")
game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "e8", to: "c8")
2021-10-03 01:12:06 +02:00
debug_board(game.board, ["e8", "c8"])
2021-09-18 05:19:47 +02:00
fen.update(game)
fen.to_s.should eq("2kr3r/8/8/8/8/8/8/8 b kq - 1 1")
end
end
2021-09-19 23:34:18 +02:00
it "castles from non-standard positions" do
# For non-standard chess games
fen = Fen.parse("8/8/8/8/8/8/8/R1K4R w KQkq - 0 1")
game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "c1", to: "e1")
2021-10-03 01:12:06 +02:00
debug_board(game.board, ["c1", "e1"])
2021-09-19 23:34:18 +02:00
fen.update(game)
fen.to_s.should eq("8/8/8/8/8/8/8/R2RK3 b kq - 1 1")
end
it "does not allow castling after the king has moved" do
fen = Fen.parse("8/8/8/8/8/8/8/R3K2R w KQkq - 0 1")
game = Game.new(board: fen.board, players: [Player.new, Player.new])
game.make_move(from: "e1", to: "d1")
fen.update(game)
fen.to_s.should eq("8/8/8/8/8/8/8/R2K3R b kq - 1 1")
2021-09-20 00:48:22 +02:00
game.next_turn!
2021-09-19 23:34:18 +02:00
expect_raises(Game::IllegalMove) do
game.make_move(from: "d1", to: "f1")
end
end
it "does not allow castling if the king will move through check" do
fen = Fen.parse("5r2/8/8/8/8/8/8/R3K2R w KQkq - 0 1")
game = Game.new(board: fen.board, players: [Player.new, Player.new])
expect_raises(Game::IllegalMove) do
game.make_move(from: "e1", to: "g1")
end
end
it "does not allow castling if the king is in check" do
fen = Fen.parse("4r3/8/8/8/8/8/8/R3K2R w KQkq - 0 1")
game = Game.new(board: fen.board, players: [Player.new, Player.new])
expect_raises(Game::IllegalMove) do
game.make_move(from: "e1", to: "g1")
end
end
end