Beispiel #1
0
        /// <summary>
        /// Checking for the conditions of win / draw.
        /// </summary>
        public void Check()
        {
            int size = Convert.ToInt16(Math.Sqrt(mainGrid.Children.Count));

            Button[] buttons = mainGrid.Children.Cast <Button>().ToArray <Button>();
            char[,] arr = new char[size, size];

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    switch (buttons[size * i + j].Tag)
                    {
                    case "button":
                        arr[i, j] = ' ';
                        break;

                    case "x":
                        arr[i, j] = 'x';
                        break;

                    case "o":
                        arr[i, j] = 'o';
                        break;
                    }
                }
            }
            int[,] result = MatrixLogics.checkForWin(arr);
            if (movesCount == mainGrid.Children.Count)
            {
                MessageBox.Show("Koniec", "Game over", MessageBoxButton.OK);
                isPlayable = false;
            }
            if (result is null)
            {
                return;
            }
            isPlayable = false;
            int won = 0;

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (result[i, j] != 0)
                    {
                        buttons[size * i + j].Background = Brushes.LightGreen;
                        won = result[i, j];
                    }
                }
            }
            if (won == 1)
            {
                playerWon(firstPlayer);
            }
            if (won == 2)
            {
                playerWon(secondPlayer);
            }
        }
Beispiel #2
0
        public void Check()
        {
            int size = Convert.ToInt16(Math.Sqrt(mainGrid.Children.Count));

            Button[] buttons = mainGrid.Children.Cast <Button>().ToArray <Button>();
            char[,] arr = new char[size, size];

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    switch (buttons[size * i + j].Tag)
                    {
                    case "button":
                        arr[i, j] = ' ';
                        break;

                    case "x":
                        arr[i, j] = 'x';
                        break;

                    case "o":
                        arr[i, j] = 'o';
                        break;
                    }
                }
            }
            int[,] result = MatrixLogics.checkForWin(arr);
            foreach (int i in result)
            {
                if (i == 1)
                {
                    break;
                }

                if (i == 2)
                {
                    break;
                }
            }
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (result[i, j] != 0)
                    {
                        buttons[size * i + j].Content = result[i, j];
                    }
                }
            }
        }