# HG changeset patch # User Mike Becker # Date 1535464593 -7200 # Node ID 564af8a16828533dcc1db0aff3d62157eb4341c6 # Parent e3a1a794351e87c4671523250ff03c4b776d28a7 fixes move validation working on old king's position, when the king moves diff -r e3a1a794351e -r 564af8a16828 Fischer-Spassky-1992.pgn --- a/Fischer-Spassky-1992.pgn Tue Aug 28 15:45:44 2018 +0200 +++ b/Fischer-Spassky-1992.pgn Tue Aug 28 15:56:33 2018 +0200 @@ -13,5 +13,5 @@ 23. Ne5 Rae8 24. Bxf7+ Rxf7 25. Nxf7 Rxe1+ 26. Qxe1 Kxf7 27. Qe3 Qg5 28. Qxg5 hxg5 29. b3 Ke6 30. a3 Kd6 31. axb4 cxb4 32. Ra5 Nd5 33. f3 Bc8 34. Kf2 Bf5 35. Ra7 g6 36. Ra6+ Kc5 37. Ke1 Nf4 38. g3 Nxh3 39. Kd2 Kb5 40. Rd6 Kc5 41. Ra6 -Nf2 42. g4 Bd3 43. Re6 1/2-1/2 +Nf2 42. g4 Bd3 43. Re6 diff -r e3a1a794351e -r 564af8a16828 src/chess/rules.c --- a/src/chess/rules.c Tue Aug 28 15:45:44 2018 +0200 +++ b/src/chess/rules.c Tue Aug 28 15:56:33 2018 +0200 @@ -327,17 +327,22 @@ return result; } + /* simulate move for check validation */ + GameState simulation = gamestate_copy_sim(gamestate); + Move simmove = *move; + apply_move_impl(&simulation, &simmove, 1); + /* find kings for check validation */ uint8_t piececolor = (move->piece & COLOR_MASK); uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 0; for (uint8_t row = 0 ; row < 8 ; row++) { for (uint8_t file = 0 ; file < 8 ; file++) { - if (gamestate->board[row][file] == + if (simulation.board[row][file] == (piececolor == WHITE?WKING:BKING)) { mykingfile = file; mykingrow = row; - } else if (gamestate->board[row][file] == + } else if (simulation.board[row][file] == (piececolor == WHITE?BKING:WKING)) { opkingfile = file; opkingrow = row; @@ -345,11 +350,6 @@ } } - /* simulate move for check validation */ - GameState simulation = gamestate_copy_sim(gamestate); - Move simmove = *move; - apply_move_impl(&simulation, &simmove, 1); - /* don't move into or stay in check position */ if (is_covered(&simulation, mykingrow, mykingfile, opponent_color(piececolor))) { @@ -495,19 +495,20 @@ _Bool is_pinned(GameState *gamestate, Move *move) { uint8_t color = move->piece & COLOR_MASK; + GameState simulation = gamestate_copy_sim(gamestate); + Move simmove = *move; + apply_move(&simulation, &simmove); + uint8_t kingfile = 0, kingrow = 0; for (uint8_t row = 0 ; row < 8 ; row++) { for (uint8_t file = 0 ; file < 8 ; file++) { - if (gamestate->board[row][file] == (color|KING)) { + if (simulation.board[row][file] == (color|KING)) { kingfile = file; kingrow = row; } } } - - GameState simulation = gamestate_copy_sim(gamestate); - Move simmove = *move; - apply_move(&simulation, &simmove); + _Bool covered = is_covered(&simulation, kingrow, kingfile, opponent_color(color)); gamestate_cleanup(&simulation); @@ -570,7 +571,7 @@ int reason = INVALID_POSITION; // find threats for the specified position - for (uint8_t i = 0 ; i < threatcount ; i++) { + for (uint8_t i = 0 ; i < threatcount ; i++) { if ((threats[i].piece & (PIECE_MASK | COLOR_MASK)) == move->piece && (move->fromrow == POS_UNSPECIFIED ||