public void NewGame() { // delete cache of last game if (File.Exists(SESSION_CACHE_FILE)) { File.Delete(SESSION_CACHE_FILE); } // show new game dialog var dlg = new NewGameView(); var dlgContext = new NewGameViewModel(dlg); dlg.DataContext = dlgContext; var result = dlg.ShowDialog(); if (result == true) { // init new game session with user inputs from dialog var humanPlayer = new UIChessPlayer(dlgContext.DrawingSide); var artificialPlayer = new ArtificialChessPlayer(dlgContext.DrawingSide.Opponent(), dlgContext.Difficulty); _session = dlgContext.DrawingSide == ChessColor.White ? new ChessGameSession(humanPlayer, artificialPlayer) : new ChessGameSession(artificialPlayer, humanPlayer); _player = humanPlayer; _session.BoardChanged += boardChanged; Board.UpdatePieces(ChessBitboard.StartFormation); // tell the player which side he draws if he choose random side if (dlgContext.SelectedDrawingSideMode == DrawingSideMode.Random) { MessageBox.Show($"You draw the { dlgContext.DrawingSide.ToString() } pieces!", "Random Side Selection", MessageBoxButton.OK, MessageBoxImage.Information); } // update status bar Status.InitNewGame(); // start the game session in a background thread (async) Task.Run(() => { // play game (until one player loses) var final = _session.ExecuteGame(); // update status bar var finalStatus = ChessDrawSimulator.Instance.GetCheckGameStatus(_session.Board, _session.Game.LastDraw); Status.FinishGame(finalStatus); // reset game session Board.UpdateIsEnabled(false); }); } // make the chess board inactive unless a game session is active updateIsBoardActive(); }
public void ReloadLastGame() { // check if a session cache is existing if (File.Exists(SESSION_CACHE_FILE)) { // reload last session from cache var session = ChessGameSessionSerializer.Instance.Deserialize(SESSION_CACHE_FILE); var humanPlayer = new List <UIChessPlayer>() { session.WhitePlayer as UIChessPlayer, session.BlackPlayer as UIChessPlayer }.First(x => x != null); _session = session; _player = humanPlayer; _session.BoardChanged += boardChanged; updateIsBoardActive(); } }