Parse checkmates but don't verify

This commit is contained in:
ekzyis 2024-10-16 20:18:42 +02:00
parent c1ad076828
commit 392cf19ae6
2 changed files with 18 additions and 1 deletions

View File

@ -360,7 +360,7 @@ func (b *Board) Move(move string) error {
} }
// make sure the move is marked as a check if it was // make sure the move is marked as a check if it was
if b.InCheck() && !strings.HasSuffix(move, "+") { if b.InCheck() && !strings.HasSuffix(move, "+") && !strings.HasSuffix(move, "#") {
move += "+" move += "+"
} }
@ -379,6 +379,7 @@ func parseMove(move string) (string, int, int, string, error) {
) )
move = strings.TrimSuffix(move, "+") move = strings.TrimSuffix(move, "+")
move = strings.TrimSuffix(move, "#")
if move == "O-O" { if move == "O-O" {
return "K", 5, 7, "g1", nil return "K", 5, 7, "g1", nil

View File

@ -472,6 +472,22 @@ func TestBoardCheck(t *testing.T) {
assertParse(t, b, "Kxf7") assertParse(t, b, "Kxf7")
} }
func TestBoardCheckmate(t *testing.T) {
t.Parallel()
b := chess.NewBoard()
assert.False(t, b.InCheck())
// fool's mate
assertParse(t, b, "f3 e6 g4 Qh4#")
assert.True(t, b.InCheck())
assert.True(t, strings.HasSuffix(b.Moves[len(b.Moves)-1], "#"), "checkmate move should end with #")
assertMoveError(t, b, "a3", "invalid move a3: king is in check")
}
func TestBoardPin(t *testing.T) { func TestBoardPin(t *testing.T) {
t.Parallel() t.Parallel()