/// <summary> /// When the any button is clicked, this part of the code gets executed /// </summary> /// <param name="param">Button which was clicked</param> private void FieldClickExecute(object param) { //Casting object as Button for easier manipulation Button btn = (Button)param; //Getting row and column of the clicked button int row = Grid.GetRow(btn); int column = Grid.GetColumn(btn); //Corresponding array item is marked as "X" ticTacToeGrid[row - 1, column - 1] = "X"; //Button content is updated to "X" btn.Content = "X"; //Calling method CheckVictory to check if the victory condition is met if (CheckVictory()) { //If the victory condition is met, player get's to chose if they want to play again if (MessageBox.Show("Player won!\nWould you like to play again?", "Congratulations", MessageBoxButton.YesNo, MessageBoxImage.None) == MessageBoxResult.Yes) { //If player wants to play again, a new view is opened while the current one is closed playerWon = true; TicTacToeView newGame = new TicTacToeView(); newGame.Show(); view.Close(); } else { //If the player doesn't want to play again, the program is terminated playerWon = true; view.Close(); } } //Declaring variables for computer's turn int rowO, columnO; //Chosing row and column untill computer finds a pair which isn't already taken do { rowO = rnd.Next(0, 3); columnO = rnd.Next(0, 3); } while (CheckFreeSpot(rowO, columnO)); //Getting information about the button at chosen row and column Button btn1 = (Button)GetGridElement(view.gameGrid, rowO + 1, columnO + 1); //Array item as well as button content are set to "O" ticTacToeGrid[rowO, columnO] = "O"; btn1.Content = "O"; //If the victory condition is met, player get's to chose if they want to play again if (CheckVictory() && playerWon == false) { //If player wants to play again, a new view is opened while the current one is closed if (MessageBox.Show("Computer won!\nWould you like to play again?", "Game Over", MessageBoxButton.YesNo, MessageBoxImage.None) == MessageBoxResult.Yes) { TicTacToeView newGame = new TicTacToeView(); newGame.Show(); view.Close(); } //If the player doesn't want to play again, the program is terminate else { view.Close(); } } }
public TicTacToeViewModel(TicTacToeView tttv) { view = tttv; ticTacToeGrid = new string[3, 3]; }