示例#1
0
文件: MCTSPlayer.cs 项目: shuyi3/AIPJ
        public override Action getMove()
        {
            bestValue = float.MinValue;
            bestBoard = new Playfield(board);
            currentState = new Node(board, null, 0);

            if (expand(currentState) == 1) return null; // no moves
            bool isEndReachedBefore = isEndReached;
            for (int i = 0; i < 500; i++)
            {
                //Helpfunctions.Instance.logg("try: " + i);
                //counter++;
                //if (counter == 10) {
                //    Helpfunctions.Instance.logg("try: " + i);
                //    counter = 0;
                //}
                //if (i == 752) {
                //    int debug = 1;
                //}
                if (isEndReachedBefore != isEndReached)
                {
                    //Helpfunctions.Instance.logg("try: " + i + " reach");
                    break;
                }
                else
                {
                    //Helpfunctions.Instance.logg("try: " + i + " not reach");
                }
                UCTRun(currentState, 0.7f);
                //currentState.state = new Playfield(board);
            }

            if (isEndReachedBefore != isEndReached)
            {
                currentState = new Node(board, null, 0);

                if (expand(currentState) == 1) return null; // no moves
                for (int i = 0; i < 10000; i++)
                {
                    UCTRun(currentState, 0.7f);
                    currentState.state = new Playfield(board);
                }
            }

            int maxVisit = 0;
            Action selectedMove = null;
            Node selectedChild = null;
            foreach (Node child in currentState.children)
            {
                //child.move.print();
                //Helpfunctions.Instance.logg("count = " + child.numVisited);
                if (child.numVisited > maxVisit)
                {
                    maxVisit = child.numVisited;
                    selectedMove = child.move;
                    selectedChild = child;
                }
            }

            //Helpfunctions.Instance.logg("Turn of child:" + selectedChild.state.isOwnTurn);
            //currentState.printChildren();
            Helpfunctions.Instance.logg("best value:" + bestValue);
            bestBoard.printBoard();

            return selectedMove;
        }