private void MakeMove(IGame game, ilf.pgn.Data.Move move) { switch (move.Type) { case ilf.pgn.Data.MoveType.Simple: case ilf.pgn.Data.MoveType.Capture: case ilf.pgn.Data.MoveType.CaptureEnPassant: MakeMoveSimpleOrCapture(game, move); break; case ilf.pgn.Data.MoveType.CastleKingSide: case ilf.pgn.Data.MoveType.CastleQueenSide: BoardLocation targetLocation; if (game.CurrentPlayer == Player.White) { targetLocation = move.Type == ilf.pgn.Data.MoveType.CastleKingSide ? BoardLocation.G1 : BoardLocation.C1; } else { targetLocation = move.Type == ilf.pgn.Data.MoveType.CastleKingSide ? BoardLocation.G8 : BoardLocation.C8; } game.MakeMove( game.CurrentPlayer == Player.White ? BoardLocation.E1 : BoardLocation.E8, targetLocation); break; } }
private void MakeMoveSimpleOrCapture(IGame game, ilf.pgn.Data.Move move) { var availableMoves = game.FindMoves(); var targetBoardLocation = move.TargetSquare.ToBoardLocation(); var chessPieceMoving = move.Piece.Value.ToChessPiece(game.CurrentPlayer); var qualifyingMoves = availableMoves .Where(a => a.Destination == targetBoardLocation && game.Board[a.Source] == chessPieceMoving) .ToList(); if (qualifyingMoves.Count == 0) { throw new Exception("Unexpected"); } else if (qualifyingMoves.Count == 1) { game.MakeMove(qualifyingMoves.Single()); } else { //if we have a pawn promotion, remove all this junk... if (move.PromotedPiece.HasValue) { qualifyingMoves.RemoveAll(a => a.SpecialMoveType != ToSpecialMoveType(move.PromotedPiece.Value)); } if (move.OriginFile.HasValue) { var requiredColumn = move.OriginFile.Value.ToColumn(); qualifyingMoves.RemoveAll(a => a.Source.Column() != requiredColumn); } else if (move.OriginRank.HasValue) { var requiredRow = move.OriginRank.Value - 1;//remove 1 to adjust for zero based goodness qualifyingMoves.RemoveAll(a => a.Source.Row() != requiredRow); } if (qualifyingMoves.Count != 1) { //we need to do more digging, more than one piece can make this move... throw new NotImplementedException("Failed to load game game from PGN file. Unexpected state found."); } game.MakeMove(qualifyingMoves.Single()); } }