Beispiel #1
0
        private void AIMove()
        {
            int botMove = AI.AIMove();

            zonesBttn[botMove].Content    = Player2Symbol;
            zonesBttn[botMove].Foreground = Player2Symbol == 'X' ? Brushes.Red : Brushes.Blue;
            int[] winPos = ProgrammLogics.CheckWin(Zones(), botMove);

            if (winPos != null)
            {
                Brush prevColor = zonesBttn[0].Background;

                foreach (int w in winPos)
                {
                    zonesBttn[w].Background = Brushes.Green;
                }

                MessageBox.Show("Computer Win!", "Game Over");
                IncrementComputerWins();

                foreach (int w in winPos)
                {
                    zonesBttn[w].Background = prevColor;
                }

                ClearPlayField();

                if (!IsPlayer1FirstMove)
                {
                    AIMove();
                }

                return;
            }
            else if (ProgrammLogics.CheckDraw(Zones()))
            {
                MessageBox.Show("Draw!", "Game Over");
                ClearPlayField();
                IncrementDraws();

                if (!IsPlayer1FirstMove)
                {
                    AIMove();
                }
            }
        }
Beispiel #2
0
        private static ZonePoints CheckZonePoints(char[] playField, char symbol, int depthLevel = 0)
        {
            ZonePoints[] posibleMoves = GetEmptyIndexes(playField);
            ZonePoints?  bestZone     = null;

            depthLevel++;

            foreach (ZonePoints zone in posibleMoves)
            {
                char[] newBoard = (char[])playField.Clone();
                newBoard[zone.Zone] = symbol;

                ZonePoints newZone = zone;

                if (ProgrammLogics.CheckWin(newBoard, zone.Zone) != null && symbol == MainWindow.Player2Symbol)
                {
                    newZone.Points = depthLevel;
                }
                else if (ProgrammLogics.CheckWin(newBoard, zone.Zone) != null && symbol == MainWindow.Player1Symbol)
                {
                    newZone.Points = -depthLevel;
                }
                else if (!ProgrammLogics.CheckDraw(newBoard))
                {
                    newZone.Points = CheckZonePoints(newBoard, symbol == 'X' ? 'O' : 'X', depthLevel).Points;
                }

                if (bestZone == null ||
                    (symbol == MainWindow.Player2Symbol && newZone.Points > ((ZonePoints)bestZone).Points) ||
                    (symbol == MainWindow.Player1Symbol && newZone.Points < ((ZonePoints)bestZone).Points))
                {
                    bestZone = newZone;
                }
            }

            return((ZonePoints)bestZone);
        }
Beispiel #3
0
        private void ZoneClick(object sender, RoutedEventArgs e)
        {
            if ((char)((Button)sender).Content != ' ')
            {
                return;
            }

            ((Button)sender).Content    = currentSymbol;
            ((Button)sender).Foreground = currentSymbol == 'X' ? Brushes.Red : Brushes.Blue;

            int[] winPos = ProgrammLogics.CheckWin(Zones(), Array.FindIndex(zonesBttn, zone => zone.Name == ((Button)sender).Name));


            if (winPos != null)
            {
                Brush prevColor = zonesBttn[0].Background;

                foreach (int w in winPos)
                {
                    zonesBttn[w].Background = Brushes.Green;
                }

                if (currentSymbol == Player1Symbol)
                {
                    if (IsSingleplayer)
                    {
                        MessageBox.Show("You win!", "Game Over");
                    }
                    else
                    {
                        MessageBox.Show("Player 1 Win!", "Game Over");
                    }
                    IncrementPlayer1Wins();
                }
                else
                {
                    MessageBox.Show("Player 2 Win!", "Game Over");
                    IncrementPlayer2Wins();
                }

                foreach (int w in winPos)
                {
                    zonesBttn[w].Background = prevColor;
                }

                ClearPlayField();

                if (!IsSingleplayer)
                {
                    currentSymbol = IsPlayer1FirstMove ? Player1Symbol : Player2Symbol;
                }
                else if (IsSingleplayer && !IsPlayer1FirstMove)
                {
                    AIMove();
                }

                return;
            }
            else if (ProgrammLogics.CheckDraw(Zones()))
            {
                MessageBox.Show("Draw!", "Game Over");
                ClearPlayField();
                IncrementDraws();

                if (!IsSingleplayer)
                {
                    currentSymbol = IsPlayer1FirstMove ? Player1Symbol : Player2Symbol;
                }
                else if (IsSingleplayer && !IsPlayer1FirstMove)
                {
                    AIMove();
                }

                return;
            }

            if (IsSingleplayer)
            {
                AIMove();
            }
            else
            {
                currentSymbol = currentSymbol == 'X' ? 'O' : 'X';
            }
        }