/// <summary> /// Loads game of chess from incoming stream /// </summary> /// <param name="stream">Record of loaded game</param> /// <returns>Description state of game</returns> public static LoadingResult LoadGame(StreamReader stream) { var game = NewGame(); while (!stream.EndOfStream) { if (game.EndState == GameState.StillPlaying) { ValidationResult validationResult; try { validationResult = GameValidator.ValidateAndPerform(game.DeepClone(), stream.ReadLine()); // get from validator } catch (Exception) { return(new LoadingResult(game, false)); } if (validationResult.IsValid) { game = validationResult.NewGameState; } else { return(new LoadingResult(game, false)); } if (game.EndState != GameState.StillPlaying) { if (game.EndState == GameState.BlackWin) { game.RecordOfGame.Add(ProgramLocalization.BlackWonRecord); } if (game.EndState == GameState.WhiteWin) { game.RecordOfGame.Add(ProgramLocalization.WhiteWonRecord); } if (game.EndState == GameState.Draw) { game.RecordOfGame.Add(ProgramLocalization.ItsDrawRecord); } } } else { break; } } return(new LoadingResult(game, true)); }
public void TryChangeChessboardState(TrackingState trackingState) { if (Game.EndState == GameState.StillPlaying) { var validationResult = GameValidator.ValidateAndPerform(Game.DeepClone(), trackingState); // get from validator if (validationResult.IsValid) { Game = validationResult.NewGameState; } if (trackingState.IsEquivalentTo(Game.Chessboard.GetTrackingStates())) { OutputFacade.UpdateValidationState(true); } else { OutputFacade.UpdateValidationState(validationResult.IsValid); } if (Game.EndState != GameState.StillPlaying) { ProgramState.GameFinished(); // do some stopping of everything} OutputFacade.AddToTrackingLog(ProgramLocalization.GameEnded); if (Game.EndState == GameState.BlackWin) { OutputFacade.AddToTrackingLog(ProgramLocalization.BlackWon); Game.RecordOfGame.Add(ProgramLocalization.BlackWonRecord); } if (Game.EndState == GameState.WhiteWin) { OutputFacade.AddToTrackingLog(ProgramLocalization.WhiteWon); Game.RecordOfGame.Add(ProgramLocalization.WhiteWonRecord); } if (Game.EndState == GameState.Draw) { OutputFacade.AddToTrackingLog(ProgramLocalization.ItsDraw); Game.RecordOfGame.Add(ProgramLocalization.ItsDrawRecord); } } OutputFacade.UpdateRecordState(Game.RecordOfGame); OutputFacade.UpdateBoardState(RenderGameState()); OutputFacade.UpdateWhosPlaying(Game.PlayerOnMove); } }