Skip to content

Commit

Permalink
Fix everything related to undoing an en-passant move
Browse files Browse the repository at this point in the history
  • Loading branch information
Sopel97 committed May 30, 2020
1 parent 0b8e92f commit 3677656
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/app/board/controls/ChessBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,15 @@ public void SetGame(ChessGame game)
SetGame(FenProvider.StartPos, game);
}

public void SetGame(string startpos, ChessGame game)
public void SetGame(string startpos, ChessGame game, int offset = 0)
{
SetPosition(startpos);
int i = 0;
foreach (var move in game.Moves)
{
++i;
if (i <= offset)
continue;
DoMove(move.SAN, true);
}
SetSelection(FirstPly);
Expand Down
11 changes: 9 additions & 2 deletions src/app/forms/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1802,9 +1802,16 @@ private void retractionsGridView_CellContentDoubleClick(object sender, DataGridV
var fen = chessBoard.GetFen();
var game = new ChessGame(fen);
var prevGame = reverseMove.AppliedTo(game);
var prevFen = prevGame.GetFen();
string prevFen = prevGame.GetFen();
prevGame.MakeMove(reverseMove.Move, true);
chessBoard.SetGame(prevFen, prevGame);
// YOU WOULD EXPECT THAT prevGame NOW HAS ONLY ONE MOVE MADE
// BUT YOU WOULD BE FUCKING WRONG BECAUSE
// THIS FUCKING LIBRARY ADDS A PAWN MOVE BEFORE EN PASSANT
// SO THEN prevFen DOESN'T MATCH THE ACTUAL FIRST POSITION IN prevGame
// (DID I MENTION THAT THE LIBRARY DOESN'T PROVIDE A WAY TO UNDO MOVES
// NOR GET THE START FEN IN A GAME?)
// SO WE PASS AN OFFSET TO INDICATE WHERE TO START READING MOVES
chessBoard.SetGame(prevFen, prevGame, prevGame.Moves.Count - 1);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/chess/Eran.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static string MakeFromBoardAndMove(
ChessGame before,
DetailedMove move)
{
var ep = before.GetGameCreationData().EnPassant;
var ep = before.GetFen().Split(' ')[3];
string eran = "";

var pieceChar = Char.ToUpper(move.Piece.GetFenCharacter());
Expand Down Expand Up @@ -195,9 +195,9 @@ public static string MakeFromBoardAndMove(

eran += ' ';

if (ep != null)
if (ep != "-")
{
eran += ep.ToString().ToLower();
eran += ep;
}
else
{
Expand Down
15 changes: 13 additions & 2 deletions src/chess/ReverseMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,23 @@ public ChessGame AppliedTo(ChessGame game)
movedPiece = game.GetPieceAt(Move.NewPosition);
}

creationData.Board[8 - Move.NewPosition.Rank][(int)Move.NewPosition.File] = CapturedPiece;
creationData.Board[8 - Move.OriginalPosition.Rank][(int)Move.OriginalPosition.File] = movedPiece;
if (movedPiece is Pawn && OldEpSquare == Move.NewPosition)
{
int offset = prevPlayer == Player.White ? 1 : -1;
creationData.Board[8 - Move.NewPosition.Rank + offset][(int)Move.NewPosition.File] = CapturedPiece;
creationData.Board[8 - Move.NewPosition.Rank][(int)Move.NewPosition.File] = null;
creationData.Board[8 - Move.OriginalPosition.Rank][(int)Move.OriginalPosition.File] = movedPiece;
}
else
{
creationData.Board[8 - Move.NewPosition.Rank][(int)Move.NewPosition.File] = CapturedPiece;
creationData.Board[8 - Move.OriginalPosition.Rank][(int)Move.OriginalPosition.File] = movedPiece;
}
}

creationData.Moves = new DetailedMove[0];

// IT ADDS A PAWN MOVE IF EnPassant IS SET... WTF IS THIS LIBRARY...
return new ChessGame(creationData);
}

Expand Down

0 comments on commit 3677656

Please sign in to comment.