Ejemplo n.º 1
0
    /**
     * Simulates all moves with the given deep and first insert position
     * and calculates a rating for this combination
     * @firstInsertPos has to be a valid insert position for the given GameBoard
     * @deep 1 is only the firstInsertPos move, 0 is no move
     * @playersTurn which player makes the next move
     * @playerMe which player is me. PlayerOne or PlayerTwo
     */
    public DeepSearch(GameBoardData board, int firstInsertPos, int deep, int playersTurn, int playerMe, int lowerBound, int upperBound)
    {
        this.deep        = deep;
        insertPosition   = firstInsertPos;
        this.board       = board.clone();
        this.playerMe    = playerMe;
        this.playersTurn = playersTurn;
        this.lowerBound  = lowerBound;
        this.upperBound  = upperBound;

        int singleWeight = 10, doubleWeight = 100, tripleWeight = 10000;

        weights     = new int[7];
        weights [0] = 0;
        weights [1] = singleWeight;
        weights [2] = doubleWeight;
        weights [3] = tripleWeight;
        weights [4] = tripleWeight * 10;
        weights [5] = tripleWeight * 100;
        weights [6] = tripleWeight * 1000;
    }
Ejemplo n.º 2
0
    private int getRating()
    {
        board.insert(insertPosition, playersTurn);
        deep--;

        //Fist check for winner or draw
        int winner = board.calculateWinner(board);

        if (winner == playerMe)
        {
            //Debug.Log ("Me winner");
            return(int.MaxValue);
        }
        else if (winner == GameManager.DRAW)
        {
            //Debug.Log ("Draw");
            return(0);
        }
        else if (winner != GameManager.NONE)
        {
            //enemy wins
            //Debug.Log("enemy winner");
            return(int.MinValue);
        }

        if (deep > 0)
        {
            int newPlayersTurn = playersTurn;

            if (playersTurn == GameManager.FIRSTPLAYER)
            {
                newPlayersTurn = GameManager.SECONDPLAYER;
            }
            else if (playersTurn == GameManager.SECONDPLAYER)
            {
                newPlayersTurn = GameManager.FIRSTPLAYER;
            }

            int        choosenRating = 0;
            List <int> turns         = board.getValidTurns();

            for (int i = 0; i < turns.Count; i++)
            {
                DeepSearch search;

                if (i == 0)
                {
                    search = new DeepSearch(board.clone(), turns[i], deep, newPlayersTurn, playerMe, lowerBound, upperBound);
                }

                if (newPlayersTurn == playerMe)
                {
                    search = new DeepSearch(board.clone(), turns [i], deep, newPlayersTurn, playerMe, choosenRating, upperBound);
                }
                else
                {
                    search = new DeepSearch(board.clone(), turns [i], deep, newPlayersTurn, playerMe, lowerBound, choosenRating);
                }

                int rating = search.getRating();

                if (i == 0)
                {
                    choosenRating = rating;
                }
                else
                {
                    //if this is my move than choose the highest value
                    //if this is not my move than choose the lowest value
                    if (newPlayersTurn == playerMe)
                    {
                        if (rating > choosenRating)
                        {
                            choosenRating = rating;
                            if (choosenRating == int.MaxValue)
                            {
                                //we found one of the best moves now we can exit
                                return(choosenRating);
                            }
                            else if (choosenRating <= lowerBound)
                            {
                                return(lowerBound);
                            }
                        }
                    }
                    else
                    {
                        if (rating < choosenRating)
                        {
                            choosenRating = rating;
                            if (choosenRating == int.MinValue)
                            {
                                //we found one of the best moves now we can exit
                                return(choosenRating);
                            }
                            else if (choosenRating >= upperBound)
                            {
                                return(upperBound);
                            }
                        }
                    }
                }
            }

            return(choosenRating);
        }
        else
        {
            return(calculateRating());
        }
    }