/// <summary> /// Tries to finalize the game. /// Should be called when ever a player makes a move. /// Will update the Wins or tie if any of the conditions meet. /// The winning stratagy positions will be stored. /// </summary> /// <returns>true if the game has been finalized.</returns> public bool TryFinalize(out TicTacToeWin whoWon) { if (TryFinalizePlayer(TicTacToeToken.XToken) == TicTacToeToken.XToken) { Player1Wins++; ResetBoard(); whoWon = TicTacToeWin.Player1; return(true); } else if (TryFinalizePlayer(TicTacToeToken.OToken) == TicTacToeToken.OToken) { Player2Wins++; ResetBoard(); whoWon = TicTacToeWin.Player2; return(true); } else if (CheckForTie()) { Ties++; ResetBoard(); whoWon = TicTacToeWin.Tie; return(true); } whoWon = TicTacToeWin.Nobody; return(false); }
private void Cell_Tapped(object sender, EventArgs e) { //do nothing if the game is done if (CurrentGame != GameState.ONGOING) { return; } Cell cell = sender as Cell; if (cell.State != Cell.CellState.EMPTY) { return; } if (CurrentTurn == Turn.X) { cell.State = Cell.CellState.X; } else { cell.State = Cell.CellState.O; } TicTacToeWin state = CheckWin(); CurrentGame = state.gameState; switch (state.winLine) { case TicTacToeWin.WinLine.NONE: break; case TicTacToeWin.WinLine.TOP_HORIZONTAL: th_line.IsVisible = true; break; case TicTacToeWin.WinLine.MIDDLE_HORIZONTAL: mh_line.IsVisible = true; break; case TicTacToeWin.WinLine.BOTTOM_HORIZONTAL: bh_line.IsVisible = true; break; case TicTacToeWin.WinLine.LEFT_VERTICAL: lv_line.IsVisible = true; break; case TicTacToeWin.WinLine.MIDDLE_VERTICAL: mv_line.IsVisible = true; break; case TicTacToeWin.WinLine.RIGHT_VERTICAL: rv_line.IsVisible = true; break; case TicTacToeWin.WinLine.BACK_DIAGONAL: backDiag_line.IsVisible = true; break; case TicTacToeWin.WinLine.FRONT_DIAGONAL: frontDiag_line.IsVisible = true; break; } switch (CurrentGame) { case GameState.ONGOING: break; case GameState.TIE: OnEnd?.Invoke(this, new EventArgs()); break; case GameState.O_WIN: OnEnd?.Invoke(this, new EventArgs()); break; case GameState.X_WIN: OnEnd?.Invoke(this, new EventArgs()); break; } SwitchTurn(); }