public bool IsEquivalent(GameMove other) { return (From.IsEquivalent(other.From) && To.IsEquivalent(other.To) && Who == other.Who); }
/// <summary> /// Serializes move into its algebraic notation representation /// </summary> /// <param name="move">Move to serialize</param> /// <returns>Textual representation of move</returns> private static string SerializeMoveToAlgebraicNotation(GameMove move) { char separator = move.ToWhom == null ? '-' : 'x'; return ($"{GetCharacterForFigure(move.Who)}{GetCharacterForPosition(move.From.X)}{move.From.Y + 1}{separator}{GetCharacterForPosition(move.To.X)}{move.To.Y + 1}"); }
/// <summary> /// Validates and performs given move /// </summary> /// <param name="game">Description of game</param> /// <returns>Result of validation</returns> private static ValidationResult ValidateAndPerform(GameData game, GameMove myMove) { if (myMove == null) { return(new ValidationResult(false, null)); } var myMoves = GetAllMoves(game.Chessboard, game.PlayerOnMove); // move is in possible moves if (myMoves.Any(x => x.IsEquivalent(myMove))) { PerformMove(game, myMove); } else { return(new ValidationResult(false, null)); } // move can't end in beeing checked if (PlayerIsChecked(game, game.PlayerOnMove)) { return(new ValidationResult(false, null)); } game.RecordOfGame.Add(SerializeMoveToAlgebraicNotation(myMove)); // chech whether opponent in in checkmate or stalemate if (PlayerHasNoPossibleMoves(game, GetOppositeColor(game.PlayerOnMove))) { if (PlayerIsChecked(game, GetOppositeColor(game.PlayerOnMove))) { game.EndState = GetWinStateFromPlayerColor(game.PlayerOnMove); } else { game.EndState = GameState.Draw; } } // alternate playing player game.PlayerOnMove = GetOppositeColor(game.PlayerOnMove); return(new ValidationResult(true, game)); }
/// <summary> /// Reverts given move /// </summary> /// <param name="game">Description of game</param> /// <param name="move">Move to revert</param> /// <param name="tempSavedTakenFigure">Saved figure in case, that move was capture</param> private static void RevertMove(GameData game, GameMove move, Figure tempSavedTakenFigure) { game.Chessboard.MoveTo(move.To, move.From); game.Chessboard.AddFigure(tempSavedTakenFigure, move.To); }
/// <summary> /// Performs given move on chessboard /// </summary> /// <param name="game">Description of game</param> /// <param name="move">Move to perform</param> private static void PerformMove(GameData game, GameMove move) { game.Chessboard.GetFigureOnPosition(move.From).Moved = true; game.Chessboard.MoveTo(move.From, move.To); }