Exemplo n.º 1
0
        public JokerPoint betterPlanMove(JokerGame game)
        {
            if (orderMap == null)
            {
                initOrderMap(game.getHeight());
            }

            int n = game.getHeight();

            JokerMove prevMove;

            if ((prevMove = game.getLastMove()) != null)
            {
                int row = prevMove.getLocation().x;
                int col = prevMove.getLocation().y;

                updateOrderMap(row, col, n);
            }

            char[,] tmpBoard = game.getBoardCopy();
            int best = int.MinValue;
            List <JokerPoint> bestMoves = new List <JokerPoint>();

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (tmpBoard[i, j] != '*')
                    {
                        continue;
                    }

                    tmpBoard[i, j] = color;
                    int heuristic = AiUtil.getLiberties(getColor(), tmpBoard);
                    heuristic -= (AiUtil.getLiberties(getOpponentColor(), tmpBoard) * 2);
                    heuristic += orderMap[i, j];

                    if (heuristic > best)
                    {
                        best = heuristic;
                        bestMoves.Clear();
                        bestMoves.Add(new JokerPoint(i, j));
                    }
                    else if (heuristic == best)
                    {
                        bestMoves.Add(new JokerPoint(i, j));
                    }

                    tmpBoard[i, j] = '*';
                }
            }


            JokerPoint bestMove = bestMoves[Randomizer.Next(bestMoves.Count)];

            updateOrderMap(bestMove.x, bestMove.y, n);
            return(bestMove);
        }
Exemplo n.º 2
0
        private int alphabeta(int depth, int alpha, int beta, char turn)
        {
            // Takes 95% CPU
            int me   = GameEngine.Rules.findCaptured(getColor(), getOpponentColor(), boardCopy, boardCopy.GetLength(0)).Count;
            int oppo = GameEngine.Rules.findCaptured(getOpponentColor(), getColor(), boardCopy, boardCopy.GetLength(0)).Count;

            if (oppo > 0 && me > 0)
            {
                if (turn == getColor())
                {
                    return(-1000000000);
                }
                else
                {
                    return(1000000000);
                }
            }
            else if (oppo + me > 0)
            {
                if (oppo > 0)
                {
                    return(-1000000000);
                }
                else if (me > 0)
                {
                    return(1000000000);
                }
            }

            if (depth == 0)
            {
                return(AiUtil.getLiberties(getColor(), boardCopy) - AiUtil.getLiberties(getOpponentColor(), boardCopy));
            }
            //both player have the same next moves
            //List<Point> nextMoves = AiUtil.getNextMoves(boardCopy, influence, 10); //hard coded horizontal pruning



            IEnumerable <JokerPoint> nextMoves = AiUtil.getNextMoves(boardCopy, 3);

            if (turn == getColor())
            {
                foreach (JokerPoint next in nextMoves)
                {
                    int priorInf = influence[next.x, next.y];
                    updateInfluenceMap(next.x, next.y, boardCopy.GetLength(0));
                    boardCopy[next.x, next.y] = getColor();

                    int temp = alphabeta(depth - 1, alpha, beta, getOpponentColor());

                    boardCopy[next.x, next.y] = '*';
                    restoreInfluenceMap(next.x, next.y, priorInf, boardCopy.GetLength(0));

                    if (alpha < temp)
                    {
                        alpha = temp;
                        if (depth == this._maxDepth)
                        {
                            bestMove = next;
                        }
                    }

                    if (beta <= alpha)
                    {
                        return(alpha);
                    }
                }

                return(alpha);
            }
            else
            {
                foreach (JokerPoint next in nextMoves)
                {
                    int priorInf = influence[next.x, next.y];
                    updateInfluenceMap(next.x, next.y, boardCopy.GetLength(0));
                    boardCopy[next.x, next.y] = getOpponentColor();

                    int temp = alphabeta(depth - 1, alpha, beta, getColor());

                    boardCopy[next.x, next.y] = '*';
                    restoreInfluenceMap(next.x, next.y, priorInf, boardCopy.GetLength(0));

                    if (beta > temp)
                    {
                        beta = temp;
                        if (depth == this._maxDepth)
                        {
                            bestMove = next;
                        }
                    }

                    if (beta <= alpha)
                    {
                        return(beta);
                    }
                }

                return(beta);
            }
        }