private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (gameOver)
            {
                MessageBox.Show("The game is over.");
                playAgainMessage();
                return;
            }
            //Creates a new button
            Button btn = sender as Button;

            Coordinates location = new Coordinates(Grid.GetRow(btn), Grid.GetColumn(btn));

            // if (btn.Content != null)


            if (gameBoard.isValidMove(location, mode))
            {
                moveCounter++;
                // MessageBox.Show("Move counter is: " + moveCounter);
                if (mode == "Nokato Tic-tac-toe")
                {
                    gameBoard.makeMove(location, 'X', mode);
                    btn.Content = "X";
                }
                else if (mode == "Wild Tic-tac-toe (Your Choice Tic-tac-toe)" || mode == "Devil's Tic-tac-toe") //This else-if statement is used for player choice variants of tic-tac-toe
                {
                    btn.Content = playerChoiceMessage();
                }
                else
                {
                    gameBoard.makeMove(location, playerTurn ? 'X' : 'O', mode);
                    btn.Content = playerTurn ? "X" : "O";
                }
                if (mode == "Misere Tic-tac-toe (Avoidance Tic-tac-toe)")
                {
                    if (gameBoard.isWinner(playerTurn ? 'X' : 'O', mode))
                    {
                        MessageBox.Show((playerTurn ? "X" : "O") + " Lost!");
                        gameOver = true;
                        playAgainMessage();
                        return;
                    }
                }
                else if (mode == "Nokato Tic-tac-toe")
                {
                    if (gameBoard.isWinner('X', mode))
                    {
                        MessageBox.Show((playerTurn ? "Player 1" : "Player 2") + " Lost!");
                        gameOver = true;
                        playAgainMessage();
                        return;
                    }
                }
                else
                {
                    if (gameBoard.isWinner(playerTurn ? 'X' : 'O', mode))
                    {
                        if (mode == "Revenge Tic-tac-toe")
                        {
                            if (oneMoreTurn)
                            {
                                MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                                gameOver = true;
                                playAgainMessage();
                                return;
                            }
                            if (moveCounter == 9)
                            {
                                MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                                gameOver = true;
                                playAgainMessage();
                                return;
                            }
                            else
                            {
                                MessageBox.Show(playerTurn ? "X" : "O" + " has one more chance to win or else they lose.");
                                playerTurn  = !playerTurn;
                                oneMoreTurn = true;
                                return;
                            }
                        }
                        else
                        {
                            if (moveCounter == (int)Application.Current.Properties["BoardSize"] * (int)Application.Current.Properties["BoardSize"])
                            {
                                MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                                gameOver = true;
                                playAgainMessage();
                                return;
                            }
                            else
                            {
                                MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                                gameOver = true;
                                playAgainMessage();
                                return;
                            }
                        }
                    }
                    else if (oneMoreTurn)
                    {
                        MessageBox.Show((!playerTurn ? "X" : "O") + " Won!");
                        gameOver = true;
                        playAgainMessage();
                        return;
                    }
                }
                if ((int)Application.Current.Properties["BoardSize"] * (int)Application.Current.Properties["BoardSize"] == moveCounter)
                {
                    MessageBox.Show("Cats game, nobody won!");
                    gameOver = true;
                    playAgainMessage();
                }
            }
            else
            {
                MessageBox.Show("That is not a valid move!");
                return;
            }
            //MessageBox.Show(location.ToString());

            //If btn already has an X or O in it then it will just return

            //Prints an X or O to the button that is pressed



            //TODO
            //Use this to get exact coordinates of users play and return that for logistics
            //This section of code can be used to obtain the coordinates of the where the user has placed an X or O


            //Flips this bool so that it switches between X and O

            if (mode == "Random Tic-tac-toe" && !gameOver)
            {
                Random random = new Random();
                playerTurn = Convert.ToBoolean(random.Next(0, 2));
                MessageBox.Show("It is " + (playerTurn ? "X" : "O") + "'s turn.");
            }
            else
            {
                playerTurn = !playerTurn;
            }
        }
Esempio n. 2
0
        //Method that handles the event of a button click
        //This whole window is a game board and it uses buttons for where players can choose to put either an X or O
        //  The game board is a 3X3 board
        //This method is for the game board
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (gameOver)
            {
                MessageBox.Show("The game is over.");
                playAgainMessage();
                return;
            }
            //Creates a new button
            Button btn = sender as Button;

            Coordinates location = new Coordinates(Grid.GetRow(btn), Grid.GetColumn(btn), Int32.Parse(btn.Name.Substring(2, 1)));


            if (gameBoard.isValidMove(location, mode))
            {
                moveCounter++;
                // MessageBox.Show("Move counter is: " + moveCounter);

                gameBoard.makeMove(location, playerTurn ? 'X' : 'O', mode);
                btn.Content = playerTurn ? "X" : "O";


                if (gameBoard.isWinner(playerTurn ? 'X' : 'O', mode))
                {
                    if (moveCounter == 27)
                    {
                        MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                        gameOver = true;
                        playAgainMessage();
                        return;
                    }
                    else
                    {
                        MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                        gameOver = true;
                        playAgainMessage();
                        return;
                    }
                }

                if (3 * 3 * 3 == moveCounter)
                {
                    MessageBox.Show("Cats game, nobody won!");
                    gameOver = true;
                    playAgainMessage();
                    return;
                }
            }
            else
            {
                MessageBox.Show("That is not a valid move!");
                return;
            }
            //MessageBox.Show(location.ToString());

            //If btn already has an X or O in it then it will just return

            //Prints an X or O to the button that is pressed



            //TODO
            //Use this to get exact coordinates of users play and return that for logistics
            //This section of code can be used to obtain the coordinates of the where the user has placed an X or O


            //Flips this bool so that it switches between X and O

            playerTurn = !playerTurn;
        }
        //Method that handles when one of the buttons in the grid is clicked
        //Outcome of the button press is an X or O will display on the button
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (gameOver)
            {
                MessageBox.Show("The game is over.");
                return;
            }
            //Creates a new button
            Button btn = sender as Button;

            Coordinates location = new Coordinates(Grid.GetRow(btn), Grid.GetColumn(btn));

            // if (btn.Content != null)


            if (gameBoard.isValidMove(location, mode, lastMove))
            {
                moveCounter++;
                // MessageBox.Show("Move counter is: " + moveCounter);
                gameBoard.makeMove(location, playerTurn ? 'X' : 'O', mode);
                theBoards[location.row % 3, location.col % 3].makeMove(new Coordinates(location.row % 3, location.col % 3), playerTurn ? 'X' : 'O', mode);
                btn.Content = playerTurn ? "X" : "O";

                switch (location.row % 3 + " " + location.col % 3)
                {
                case "0 0":


                    if (theBoards[0, 0].isWinner(playerTurn ? 'X' : 'O', mode))
                    {
                        theBoards[0, 0].makeMove(new Coordinates(location.row % 3, location.col % 3), playerTurn ? 'X' : 'O', mode);
                        if (gameBoard.isWinner(playerTurn ? 'X' : 'O', mode))
                        {
                            MessageBox.Show((playerTurn ? "X" : "O") + " Won this board!");
                            gameOver = true;
                        }
                    }
                    break;

                case "1 0":
                    //case "0 1":
                    if (theBoards[1, 0].isWinner(playerTurn ? 'X' : 'O', mode))
                    {
                        gameBoard.makeMove(location, playerTurn ? 'X' : 'O', mode);
                        if (gameBoard.isWinner(playerTurn ? 'X' : 'O', mode))
                        {
                            MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                            gameOver = true;
                        }
                    }
                    break;

                case "2 0":
                    // case "0 2":
                    if (theBoards[2, 0].isWinner(playerTurn ? 'X' : 'O', mode))
                    {
                        gameBoard.makeMove(location, playerTurn ? 'X' : 'O', mode);
                        if (gameBoard.isWinner(playerTurn ? 'X' : 'O', mode))
                        {
                            MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                            gameOver = true;
                        }
                    }
                    break;

                case "0 1":
                    //case "1 0":
                    if (theBoards[0, 1].isWinner(playerTurn ? 'X' : 'O', mode))
                    {
                        gameBoard.makeMove(location, playerTurn ? 'X' : 'O', mode);
                        if (gameBoard.isWinner(playerTurn ? 'X' : 'O', mode))
                        {
                            MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                            gameOver = true;
                        }
                    }
                    break;

                case "1 1":
                    if (theBoards[1, 1].isWinner(playerTurn ? 'X' : 'O', mode))
                    {
                        gameBoard.makeMove(location, playerTurn ? 'X' : 'O', mode);
                        if (gameBoard.isWinner(playerTurn ? 'X' : 'O', mode))
                        {
                            MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                            gameOver = true;
                        }
                    }
                    break;

                case "2 1":
                    // case "1 2":
                    if (theBoards[2, 1].isWinner(playerTurn ? 'X' : 'O', mode))
                    {
                        gameBoard.makeMove(location, playerTurn ? 'X' : 'O', mode);
                        if (gameBoard.isWinner(playerTurn ? 'X' : 'O', mode))
                        {
                            MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                            gameOver = true;
                        }
                    }
                    break;

                case "0 2":
                    // case "2 0":
                    if (theBoards[0, 2].isWinner(playerTurn ? 'X' : 'O', mode))
                    {
                        gameBoard.makeMove(location, playerTurn ? 'X' : 'O', mode);
                        if (gameBoard.isWinner(playerTurn ? 'X' : 'O', mode))
                        {
                            MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                            gameOver = true;
                        }
                    }
                    break;

                case "1 2":
                    // case "2 1":
                    if (theBoards[1, 2].isWinner(playerTurn ? 'X' : 'O', mode))
                    {
                        gameBoard.makeMove(location, playerTurn ? 'X' : 'O', mode);
                        if (gameBoard.isWinner(playerTurn ? 'X' : 'O', mode))
                        {
                            MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                            gameOver = true;
                        }
                    }
                    break;

                case "2 2":
                    if (theBoards[2, 2].isWinner(playerTurn ? 'X' : 'O', mode))
                    {
                        gameBoard.makeMove(location, playerTurn ? 'X' : 'O', mode);
                        if (gameBoard.isWinner(playerTurn ? 'X' : 'O', mode))
                        {
                            MessageBox.Show((playerTurn ? "X" : "O") + " Won!");
                            gameOver = true;
                        }
                    }
                    break;
                }
            }
            else
            {
                MessageBox.Show("That is not a valid move!");
                return;
            }
            //MessageBox.Show(location.ToString());

            //If btn already has an X or O in it then it will just return

            //Prints an X or O to the button that is pressed

            lastMove = location;

            //TODO
            //Use this to get exact coordinates of users play and return that for logistics
            //This section of code can be used to obtain the coordinates of the where the user has placed an X or O


            //Flips this bool so that it switches between X and O
            playerTurn = !playerTurn;
        }