/// <summary> /// Stops the clock, or queues a pause if it is the AI's move /// in an AI vs. Human game. /// </summary> public void Stop() { if (!Timer.IsEnabled) { return; } // If it is the computer's turn in an AI vs. Human game, queue the // pause so that it will take effect when it is the human's turn. if (!GameViewModel.IsGameOver && !GameViewModel.IsGameAiVersusAi && GameViewModel.IsCurrentPlayerAi) { // Pause (in the Switch method) when the computer finishes its move. _pauseQueued = true; return; } // If it is an AI vs. AI game, halt it. If any players are human, // then the game will pause automatically when it is a human's turn. if (GameViewModel.IsGameAiVersusAi) { GameViewModel.Stop(); } Timer.Stop(); UpdateViews(); PlayerOneMoveTimeStopwatch.Stop(); PlayerOneTotalTimeStopwatch.Stop(); PlayerTwoMoveTimeStopwatch.Stop(); PlayerTwoTotalTimeStopwatch.Stop(); }
/// <summary> /// Starts or resumes the clock. /// </summary> public void Start() { if (Timer.IsEnabled || GameViewModel.IsGameOver) { if (_pauseQueued) { _pauseQueued = false; } return; } Timer.Start(); if (GameViewModel.IsGameAiVersusAi) { GameViewModel.Start(); } UpdateViews(); if (GameViewModel.CurrentPlayer == State.One) { PlayerOneMoveTimeStopwatch.Start(); PlayerOneTotalTimeStopwatch.Start(); } else { PlayerTwoMoveTimeStopwatch.Start(); PlayerTwoTotalTimeStopwatch.Start(); } }
/// <summary> /// Called when a player makes a move, stopping that player's /// clock and starting the opponent's clock (or stopping both /// player's clocks if the game has been paused). /// </summary> private void Switch() { // If the game is not the main view, and it is now the human's turn // in a human vs. AI game, then pause the game. if (!Window.Current.Visible && !GameViewModel.IsCurrentPlayerAi && !GameViewModel.IsGameHumanVersusHuman) { Stop(); } // Retain the pause queue state only if it is the computer's turn. This is // necessary because the pause can occur after the human's turn but // before the GameViewModel.CurrentPlayer changes (which calls this method). if (_pauseQueued && !GameViewModel.IsCurrentPlayerAi) { Stop(); _pauseQueued = false; } if (GameViewModel.CurrentPlayer == State.One) { PlayerTwoMoveTimeStopwatch.Stop(); PlayerTwoTotalTimeStopwatch.Stop(); if (Timer.IsEnabled) { PlayerOneMoveTimeStopwatch.Restart(); PlayerOneTotalTimeStopwatch.Start(); } else { PlayerOneMoveTimeStopwatch.Reset(); } } else { PlayerOneMoveTimeStopwatch.Stop(); PlayerOneTotalTimeStopwatch.Stop(); if (Timer.IsEnabled) { PlayerTwoMoveTimeStopwatch.Restart(); PlayerTwoTotalTimeStopwatch.Start(); } else { PlayerTwoMoveTimeStopwatch.Reset(); } } }