Ejemplo n.º 1
0
        private void CheckWinner()
        {
            int xCount = 0;
            int oCount = 0;

            void CheckCounts()
            {
                Console.WriteLine($"x: {xCount}, o: {oCount}");
                MessageBoxResult playAgain;

                void PlayAgain()
                {
                    if (playAgain == MessageBoxResult.Yes)
                    {
                        frmTicTacToe frmTicTacToe = new frmTicTacToe();
                        frmTicTacToe.Show();
                        this.Close();
                    }
                    else if (playAgain == MessageBoxResult.No)
                    {
                        this.Close();
                    }
                }

                if (xCount == 3)
                {
                    lblText.Content = "Player 1 has won!";
                    playAgain       = MessageBox.Show("Player 1 has won! Would you like to play again?", "Tic Tac Toe", MessageBoxButton.YesNoCancel);
                    PlayAgain();
                }
                else if (oCount == 3)
                {
                    lblText.Content = "Player 2 has won!";
                    playAgain       = MessageBox.Show("Player 2 has won! Would you like to play again?", "Tic Tac Toe", MessageBoxButton.YesNoCancel);
                    PlayAgain();
                }
                else if (turns >= 9)
                {
                    lblText.Content = "It's a cat's game!";
                    playAgain       = MessageBox.Show("It's a cat's game! Would you like to play again?", "Tic Tac Toe", MessageBoxButton.YesNoCancel);
                    PlayAgain();
                }
            }

            //check rows
            Console.WriteLine($"check rows");
            for (int y = 0; y < 3; y++)
            {
                for (int x = 0; x < 3; x++)
                {
                    if (spots[x, y] != '\0' && spots[x, y] == 'X')
                    {
                        xCount++;
                    }

                    if (spots[x, y] != '\0' && spots[x, y] == 'O')
                    {
                        oCount++;
                    }

                    CheckCounts();
                }
                xCount = 0;
                oCount = 0;
            }

            //check columns
            Console.WriteLine($"check columns");
            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    if (spots[x, y] != '\0' && spots[x, y] == 'X')
                    {
                        xCount++;
                    }

                    if (spots[x, y] != '\0' && spots[x, y] == 'O')
                    {
                        oCount++;
                    }

                    CheckCounts();
                }
                xCount = 0;
                oCount = 0;
            }

            //check topleft to botright
            Console.WriteLine($"check topleft to botright");
            for (int xy = 0; xy < 3; xy++)
            {
                if (spots[xy, xy] != '\0' && spots[xy, xy] == 'X')
                {
                    xCount++;
                }

                if (spots[xy, xy] != '\0' && spots[xy, xy] == 'O')
                {
                    oCount++;
                }

                CheckCounts();
            }
            xCount = 0;
            oCount = 0;

            //check topright to botleft
            Console.WriteLine($"check topright to botleft");
            for (int xy = 2; xy > 0; xy--)
            {
                if (spots[xy, xy] != '\0' && spots[xy, xy] == 'X')
                {
                    xCount++;
                }

                if (spots[xy, xy] != '\0' && spots[xy, xy] == 'O')
                {
                    oCount++;
                }

                CheckCounts();
            }
        }
Ejemplo n.º 2
0
        private void btnTicTacToe_Click(object sender, RoutedEventArgs e)
        {
            frmTicTacToe frmTicTacToe = new frmTicTacToe();

            frmTicTacToe.Show();
        }