Пример #1
0
        private void RandomMove()
        {
            int i = 0, j = 0;

            while (true)
            {
                i = random.Next(3);
                j = random.Next(3);
                if (Board[i, j].Equals(' '))
                {
                    Board[i, j] = player2;
                    break;
                }
            }
            int[] drawingCoordinates = GetDrawingCoordinates(i, j);
            Point point = new Point(drawingCoordinates[0], drawingCoordinates[1]);

            if (player2.Equals('X'))
            {
                Plays.Add(new PlayX(point, i, j, ColorX));
            }
            else
            {
                Plays.Add(new PlayO(point, i, j, ColorO));
            }
            XTurn = !XTurn;
        }
Пример #2
0
        public void AddPlay(int x, int y)
        {
            Point point = GetDrawingPoint(x, y);

            if (point != Point.Empty)//Clicked on board (valid click)
            {
                int[] coordinates = GetBoardCoordinates(x, y);
                if (!Over)                                                 //Game is not over
                {
                    if (Board[coordinates[0], coordinates[1]].Equals(' ')) //Clicked spot is empty
                    {
                        if (XTurn)
                        {
                            Plays.Add(new PlayX(point, coordinates[0], coordinates[1], ColorX));
                        }
                        else
                        {
                            Plays.Add(new PlayO(point, coordinates[0], coordinates[1], ColorO));
                        }

                        AddToBoard(coordinates[0], coordinates[1]);
                        WinCheck();
                        if (!PVP && !Over)
                        {
                            if (Difficullty < 2)
                            {
                                RandomMove();
                            }
                            else
                            {
                                AIMove();
                            }
                        }
                        WinCheck();
                        if (!Over)
                        {
                            XTurn = !XTurn;
                        }
                    }
                }
            }
        }
Пример #3
0
        private void AIMove()
        {
            float bestScore = float.NegativeInfinity;
            int   moveI = -1, moveJ = -1;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (Board[i, j].Equals(' '))
                    {
                        Board[i, j] = player2;
                        float score = Minimax(Board, 0, false, float.NegativeInfinity, float.PositiveInfinity);

                        Board[i, j] = ' ';
                        if (score > bestScore)
                        {
                            bestScore = score;
                            moveI     = i;
                            moveJ     = j;
                        }
                    }
                }
            }
            int[] drawingCoordinates = GetDrawingCoordinates(moveI, moveJ);
            Point point              = new Point(drawingCoordinates[0], drawingCoordinates[1]);

            Board[moveI, moveJ] = player2;

            if (player2.Equals('X'))
            {
                Plays.Add(new PlayX(point, moveI, moveJ, ColorX));
            }
            else
            {
                Plays.Add(new PlayO(point, moveI, moveJ, ColorO));
            }
            XTurn = !XTurn;
        }