Exemplo n.º 1
0
        public static void OnSpotClicked(TileSpot tileSpot)
        {
            if (WinnerChecker.CheckWin(BlackBoard.Spots) == GameResult.GAME_ON)
            {
                if (tileSpot.Owner == OwnerType.NONE)
                {
                    if (Turn % 2 == 0)
                    {
                        tileSpot.Owner = OwnerType.PLAYER;
                        Turn++;
                    }

                    if (WinnerChecker.CheckWin(BlackBoard.Spots) == GameResult.GAME_ON && Turn <= 7)
                    {
                        var bestMove = BrainAI.GetBestMoveAI(BlackBoard.Spots);

                        if (BlackBoard.Spots[bestMove.I, bestMove.J].Owner == OwnerType.NONE)
                        {
                            BlackBoard.Spots[bestMove.I, bestMove.J].Owner = OwnerType.AI;
                        }
                    }

                    if (Turn < 9)
                    {
                        Turn++;
                    }
                }
            }
            switch (WinnerChecker.CheckWin(BlackBoard.Spots))
            {
            case GameResult.AI_WIN:
                MessageBox.Show("AI Won");
                break;

            case GameResult.PLAYER_WIN:
                MessageBox.Show("Player Won");
                break;

            case GameResult.GAME_DRAW:
                MessageBox.Show("Draw");
                break;
            }
        }
Exemplo n.º 2
0
        public static AIMove MiniMax(TileSpot[,] board, OwnerType player)
        {
            var AvailSpots = WinnerChecker.AvailableSpots(board);

            switch (WinnerChecker.CheckWin(board))
            {
            case GameResult.AI_WIN:
                return(new AIMove()
                {
                    Score = 10
                });

            case GameResult.PLAYER_WIN:
                return(new AIMove()
                {
                    Score = -10
                });

            case GameResult.GAME_DRAW:
                return(new AIMove()
                {
                    Score = 0
                });
            }
            List <AIMove> moves = new List <AIMove>();

            for (int i = 0; i < AvailSpots.Count; i++)
            {
                AIMove move = new AIMove();
                move.I = AvailSpots[i].Uid2D.I;
                move.J = AvailSpots[i].Uid2D.J;

                AvailSpots[i].Owner = player;

                if (player == OwnerType.AI)
                {
                    move.Score = MiniMax(board, OwnerType.PLAYER).Score;
                }
                else
                {
                    move.Score = MiniMax(board, OwnerType.AI).Score;
                }

                moves.Add(move);


                AvailSpots[i].Owner = OwnerType.NONE;
            }

            int bestMove = 0;

            if (player == OwnerType.AI)
            {
                int bestMax = -100000;
                for (int i = 0; i < moves.Count; i++)
                {
                    if (moves[i].Score > bestMax)
                    {
                        bestMax  = moves[i].Score;
                        bestMove = i;
                    }
                }
            }
            else
            {
                int bestMin = 100000;
                for (int i = 0; i < moves.Count; i++)
                {
                    if (moves[i].Score < bestMin)
                    {
                        bestMin  = moves[i].Score;
                        bestMove = i;
                    }
                }
            }

            return(bestMove < moves.Count?moves[bestMove]:new AIMove()
            {
                Score = 0
            });
        }