예제 #1
0
파일: MCTSPlayer.cs 프로젝트: shuyi3/AIPJ
        //public int expand(Node p)
        //{

        //    GameManager.Instance.moveCount++;
        //    if (GameManager.Instance.moveCount == 225)
        //    {
        //         int debug = 1;
        //    }

        //    int state = 0;
        //    Playfield afterState = new Playfield(p.state);

        //    tt = new TranspositionTable();
        //    endTurnTt = new HandHashTable();
        //    List<Playfield> moves = new List<Playfield>();
        //    Movegenerator.Instance.getMoveListForPlayfield(afterState, null, false);

        //    int counter = GameManager.Instance.moveCount;

        //    getAllpossibleStates(afterState, ref moves);

        //    if (GameManager.Instance.moveCount - counter > 1000)
        //    {
        //        Helpfunctions.Instance.logg("MOVES = " + (GameManager.Instance.moveCount - counter));
        //        int debug = 1;
        //    }

        //    foreach (Playfield pf in moves)
        //    {
        //        if (pf.moveList.Count != 0)
        //        {
        //            int debug = 1;
        //        }
        //    }

        //    Helpfunctions.Instance.logg("try size = " + endTurnTt.TTable.Count);

        //    //while (endTurnTt.TTable.Count == 1)
        //    //{ // no moves available, change side
        //    //    tt.clearTable();
        //    //    endTurnTt.clearTable();
        //    //    afterState.endTurn(false, false);
        //    //    Movegenerator.Instance.getMoveListForPlayfield(afterState, null, false);
        //    //    getAllpossibleStates(afterState, ref moves);
        //    //    state = 1;
        //    //}

        //    if (endTurnTt.TTable.Count == 1)
        //        state = 1;

        //    //afterState.endTurn(false, false);
        //    //Node originalNode = new Node(new Playfield(afterState), null, p.depth + 1);

        //    //p.children.Add(originalNode);


        //    foreach (KeyValuePair<float, List<Playfield>> move in endTurnTt.TTable)
        //    {
        //        Node afterNode = new Node(new Playfield(move.Value[0]), null, p.depth + 1);
        //        //afterNode.state.doAction(move);
        //        p.children.Add(afterNode);
        //    }

        //    Helpfunctions.Instance.logg("expand size = " + endTurnTt.TTable.Count);
        //    //if (moves.Count > 100)
        //    //{
        //    //    foreach (Playfield pf in moves)
        //    //    {
        //    //        Helpfunctions.Instance.logg("key:" + endTurnTt.getHashkey(pf));
        //    //        //pf.debugHand();
        //    //    }
        //    //}
        //    return state;
        //}

        public void getAllpossibleStates(Playfield state, ref List<Playfield> statesList)
        {
            GameManager.Instance.moveCount++;
            if (state.moveList.Count == 0)
            {
                Player mPlayer;
                String turn;
                //if (state.isOwnTurn)
                //{
                //    mPlayer = state.playerSecond;
                //    turn = "second";
                //}
                //else
                //{
                //    mPlayer = state.playerFirst;
                //    turn = "first";
                //}

                //Helpfunctions.Instance.logg(turn + ": pre mana = " + mPlayer.ownMaxMana);
                Playfield afterState = new Playfield(state);
                afterState.endTurn(false, false);
                if (endTurnTt.addToMap(afterState) == false)
                {
                    //GameManager.Instance.moveCount++;
                    //Helpfunctions.Instance.logg("move = " + GameManager.Instance.moveCount +
                    //    ",mana = " + mPlayer.ownMaxMana);
                    //Helpfunctions.Instance.logg("MOVES ===============================");
                    //state.printActions();
                    //Helpfunctions.Instance.logg("count: " + statesList.Count);
                    //statesList.Add(afterState);
                }
                return;
            }
            foreach (Action action in state.moveList)
            {
                Playfield afterState = new Playfield(state);
                afterState.doAction(action);
                Movegenerator.Instance.getMoveListForPlayfield(afterState, false);
                
                //if (action.actionType == actionEnum.playcard && action.card.card.name == CardDB.cardName.arcaneintellect)
                //{
                //    if (tt.addToMap(state) == false)
                //    {
                //        state.endTurn(false, false);
                //        statesList.Add(state);
                //    }
                //    continue;
                //}
                if (tt.addToMap(afterState) == false)
                {
                    //    statesList.Add(afterState);

                    //Helpfunctions.Instance.logg("Turn: " + afterState.isOwnTurn);
                    getAllpossibleStates(afterState, ref statesList);
                }
                else
                {
                    Helpfunctions.Instance.logg("pruned");
                }
            }
        }
예제 #2
0
파일: MCTSPlayer.cs 프로젝트: shuyi3/AIPJ
        public float sample(Node p)
        {
            Playfield startState = new Playfield(p.state);
            Action move = null;
            int turn = p.depth;

            //Helpfunctions.Instance.logg("turn: " + turn);

            int score = startState.getGameResult();
            while (score == -1)
            {
                //List<Action> moves = Movegenerator.Instance.getMoveList(startState, false, false, true);
                //if (move != null)
                //{
                //GameManager.Instance.moveCount++;
                //if (GameManager.Instance.moveCount == 562)
                //{
                //    int debug = 1;
                //}
                    //var milliseconds = (DateTime.Now - DateTime.MinValue).TotalMilliseconds;
                    Movegenerator.Instance.getMoveListForPlayfield(startState, false);
                    //double time = (DateTime.Now - DateTime.MinValue).TotalMilliseconds - milliseconds;
                    //GameManager.Instance.myTimer += time;
                    //Helpfunctions.Instance.logg("my:" + time + " total:" + GameManager.Instance.myTimer);


                    //milliseconds = (DateTime.Now - DateTime.MinValue).TotalMilliseconds;
                    //List<Action> bruteForceMoves = Movegenerator.Instance.getMoveList(startState, false, true, true);
                    //time = (DateTime.Now - DateTime.MinValue).TotalMilliseconds - milliseconds;
                    //GameManager.Instance.sfTimer += time;
                    //Helpfunctions.Instance.logg("sf:" + time + " total:" + GameManager.Instance.sfTimer);

                    //if (bruteForceMoves.Count != startState.moveList.Count) {
                    //    startState.printBoard();
                    //    int debug = 1;
                    //    Helpfunctions.Instance.logg("BF Move List:------------------------------------");
                    //    foreach (Action action in bruteForceMoves)
                    //    {
                    //        action.print();
                    //    }
                    //    startState.printMoveList();
                    //}
                //}
                //Helpfunctions.Instance.logg("Count: " + startState.moveList.Count);
                if (startState.moveList.Count == 0)
                {
                    startState.endTurn(false, false);
                    //Helpfunctions.Instance.logg("Turn = " + startState.isOwnTurn);
                    if (!isEndReached)
                    {
                        //if (startState.isOwnTurn && this.playerSide == 1 || !startState.isOwnTurn && this.playerSide == 0)
                        //{
                        //    turn++;
                        //    //Helpfunctions.Instance.logg("Turn++");
                        //}
                        turn++;
                        move = null;
                        if (turn == rolloutDepth) //evaluate at deapth == 5
                        {
                            //startState.printBoard();
                            float value = startState.getBoardValue();
                            //Helpfunctions.Instance.logg("value = " + value);
                            if (value > bestValue)
                            {
                                bestBoard = new Playfield(startState);
                                bestValue = value;
                            }
                            return value;
                        }
                    }
                }
                else
                {
                    move = startState.moveList[GameManager.getRNG().Next(startState.moveList.Count)];
                    startState.doAction(move);
                }
                score = startState.getGameResult();
            }

            isEndReached = true;
            if (playerSide == score)
            {
                return 1;
            }
            return 0;
        }
예제 #3
0
파일: MCTSPlayer.cs 프로젝트: shuyi3/AIPJ
        public int expand(Node p)
        {
            
            int state = 0;
            Playfield afterState = new Playfield(p.state);
            //List<Action> moves = Movegenerator.Instance.getMoveList(afterState, false, true, true);
            //GameManager.Instance.moveCount++;
            //if (GameManager.Instance.moveCount == 16)
            //{
            //    int debug = 1;
            //}
            Movegenerator.Instance.getMoveListForPlayfield(afterState, false);

            if (afterState.moveList.Count == 0)
            { // no moves available
                afterState.endTurn(false, false);
                Node originalNode = new Node(new Playfield(afterState), null, p.depth + 1);
                p.children.Add(originalNode);
                state = 1;
            }

            foreach (Action move in afterState.moveList)
            {
                Node afterNode = new Node(new Playfield(afterState), move, p.depth);
                afterNode.state.doAction(move);
                //if (move.actionType == actionEnum.attackWithMinion && move.own.entitiyID == 1008 && move.target.entitiyID == 1003)
                //{
                //    GameManager.Instance.moveCount++;
                //    if (GameManager.Instance.moveCount == 3)
                //    {
                //        int debug = 1;
                //    }
                //}
                p.children.Add(afterNode);
            }

            return state;
        }
예제 #4
0
파일: MOMCTS.cs 프로젝트: shuyi3/AIPJ
    public Playfield rollOut()
    {
        //Playfield rollerState = new Playfield(state);
        //int thisDepth = this.depth();
        //int action = 0;
        // while (!rollerState.isTerminal() && action != -1) {


            bool lethalCheck = false;
            //if (m_player.heuristicType == HeuristicType.LethalCheck)
                //lethalCheck = true;

            Playfield startState = new Playfield(state);
            Action move = null;
            int turn = 0;

            int maxMana = startState.playerFirst.ownMaxMana;

            int score = startState.getGameResult();
            while (score == -1)
            {
                //List<Action> moves = Movegenerator.Instance.getMoveList(startState, false, false, true);
                //if (move != null)
                //{
                //GameManager.Instance.moveCount++;
                //if (GameManager.Instance.moveCount == 562)
                //{
                //    int debug = 1;
                //}
                    //var milliseconds = (DateTime.Now - DateTime.MinValue).TotalMilliseconds;
                Movegenerator.Instance.getMoveListForPlayfield(startState, false, lethalCheck);

                //List<Action> testMoves = Movegenerator.Instance.getMoveList(startState, lethalCheck, true, true);

                //if (startState.moveList.Count != testMoves.Count)
                //{
                //    int debug = 1;
                //}
                    //double time = (DateTime.Now - DateTime.MinValue).TotalMilliseconds - milliseconds;
                    //GameManager.Instance.myTimer += time;
                    //Helpfunctions.Instance.logg("my:" + time + " total:" + GameManager.Instance.myTimer);


                    //milliseconds = (DateTime.Now - DateTime.MinValue).TotalMilliseconds;
                    //List<Action> bruteForceMoves = Movegenerator.Instance.getMoveList(startState, false, true, true);
                    //time = (DateTime.Now - DateTime.MinValue).TotalMilliseconds - milliseconds;
                    //GameManager.Instance.sfTimer += time;
                    //Helpfunctions.Instance.logg("sf:" + time + " total:" + GameManager.Instance.sfTimer);

                    //if (bruteForceMoves.Count != startState.moveList.Count) {
                    //    startState.printBoard();
                    //    int debug = 1;
                    //    Helpfunctions.Instance.logg("BF Move List:------------------------------------");
                    //    foreach (Action action in bruteForceMoves)
                    //    {
                    //        action.print();
                    //    }
                    //    startState.printMoveList();
                    //}
                //}
                //Helpfunctions.Instance.logg("Count: " + startState.moveList.Count);
                if (startState.moveList.Count == 0)
                {
                    //this.isTerminal = true;
                    break;
                }
                else
                {
                    move = startState.moveList[GameManager.getRNG().Next(startState.moveList.Count)];
                    startState.doAction(move);
                }
                score = startState.getGameResult();
            }


        //TODO: last turn rollout
        //while (!finishRollout(rollerState,thisDepth,action)) {
        //    action = roller.roll(rollerState);
        //    //rollerState.next(action);
        //    advance(rollerState, action);
        //    m_player.getHeuristic().AddPlayoutInfo(action, rollerState);
        //    thisDepth++;
        //}
            int endMaxMana = startState.playerFirst.ownMaxMana;

            if (endMaxMana != maxMana)
            {
                int debug = 1;
            }
            startState.endTurn(false, false);
            return startState;
            //return m_player.getHeuristic().value(startState);
    }