Exemplo n.º 1
0
        public void cuttingposibilities()
        {
            // take the x best values
            int takenumber        = this.maxwide;
            List <Playfield> temp = new List <Playfield>();

            posmoves.Sort((a, b) => - (botBase.getPlayfieldValue(a)).CompareTo(botBase.getPlayfieldValue(b)));//want to keep the best

            if (this.useComparison)
            {
                int i   = 0;
                int max = Math.Min(posmoves.Count, this.maxwide);

                Playfield p  = null;
                Playfield pp = null;
                //foreach (Playfield p in posmoves)
                for (i = 0; i < max; i++)
                {
                    p = posmoves[i];
                    int hash = p.GetHashCode();
                    p.hashcode = hash;
                    bool found = false;
                    //foreach (Playfield pp in temp)
                    for (int j = 0; j < temp.Count; j++)
                    {
                        pp = temp[j];
                        if (pp.hashcode == p.hashcode)
                        {
                            if (pp.isEqualf(p))
                            {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found)
                    {
                        temp.Add(p);
                    }
                    //i++;
                    //if (i >= this.maxwide) break;
                }
            }
            else
            {
                temp.AddRange(posmoves);
            }
            posmoves.Clear();
            posmoves.AddRange(temp.GetRange(0, Math.Min(takenumber, temp.Count)));
            //posmoves.Clear();
            //posmoves.AddRange(Helpfunctions.TakeList(temp, takenumber));
        }
Exemplo n.º 2
0
        public void cuttingPosibilitiesET()
        {
            Dictionary <Int64, Playfield> tempDict = new Dictionary <Int64, Playfield>(); //todo sepefeets - consider whether playfield.GetHashCode() should be int64 or if this should be int
            Playfield p   = null;
            int       max = posmoves.Count;

            for (int i = 0; i < max; i++)
            {
                p = posmoves[i];
                Int64 hash = p.GetHashCode();
                if (!tempDict.ContainsKey(hash))
                {
                    tempDict.Add(hash, p);
                }
            }
            posmoves.Clear();
            foreach (KeyValuePair <Int64, Playfield> d in tempDict)
            {
                posmoves.Add(d.Value);
            }
            tempDict.Clear();

            //Helpfunctions.Instance.logg(max + " enemy boards cut to " + this.posmoves.Count); //lots of spam just for debugging
        }
Exemplo n.º 3
0
        private List <Action> getBestPlayfield(bool lethalCheck)
        {
            this.currentRandom = (GameManager.getRNG().NextDouble() > this.randomCut) ? true : false;
            this.currentRandom = false;
            this.turnPlayed   += 1;

            bool turn = this.board.isOwnTurn;

            List <Action> actionList = new List <Action>();

            List <Playfield> temp = new List <Playfield>(7000);
            bool             havedonesomething = true;

            Playfield currentState = new Playfield(this.board);
            double    probCut      = this.useNNEval ? 1.0 : 0.0;
            //if (this.playerSide == false)
            //{
            //    for (int i = 0; i < currentState.getCurrentPlayer(true).owncards.Count; i++)
            //    {
            //        Console.WriteLine("idx:" + i);
            //        Console.WriteLine(currentState.getCurrentPlayer(true).owncards[i].card.name);
            //    }

            //    string s = Console.ReadLine();
            //    if (s.Length != 0)
            //    {
            //        string[] sList = s.Split(',');
            //        foreach (string idxStr in sList)
            //        {
            //            int idx = int.Parse(idxStr);
            //            currentState.getCurrentPlayer(true).owncards[idx].playProb = 0.0;
            //        }
            //    }
            //}

            List <Playfield> posmoves = new List <Playfield>();

            posmoves.Add(currentState);

            if (this.useNNEval)
            {
                DNNEval.Instance.getNNActionPolicy(currentState, this.playerSide);
                Player mPlayer = currentState.getCurrentPlayer(true);

                foreach (Handmanager.Handcard hc in mPlayer.owncards)
                {
                    Console.WriteLine(hc.card.name + ":" + hc.playProb);
                }
            }

            int boardcount = 0;
            int deep       = 0;
            //float bestoldval = float.MinValue;

            Dictionary <int, Playfield> seenStates = new Dictionary <int, Playfield>();
            List <int> levelCount = new List <int>();
            //List<Playfield> candidateStates = new List<Playfield>();
            Dictionary <int, List <Playfield> > candidateStates = new Dictionary <int, List <Playfield> >();

            while (havedonesomething)
            {
                debug = 0;

                temp.Clear();
                levelCount.Add(posmoves.Count);
                temp.AddRange(posmoves);
                havedonesomething = false;

                int count = temp.Count;
                for (int i = 0; i < count; i++)
                {
                    Playfield p = temp[i];
                    if (turn != p.isOwnTurn)
                    {
                        continue;
                    }

                    List <Action> actions = new List <Action>(Movegenerator.Instance.getMoveList(p, false, true, true, probCut));

                    foreach (Action a in actions)
                    {
                        havedonesomething = true;
                        Playfield pf = new Playfield(p);
                        pf.doAction(a);
                        if (!seenStates.ContainsKey(pf.GetHashCode()))
                        {
                            seenStates[pf.GetHashCode()] = pf;
                            posmoves.Add(pf);
                        }
                        else
                        {
                            continue;
                        }
                    }

                    int actionHash = BoardHash.Instance.getActionHash(p);

                    bool followPolicy = true;
                    if (this.useNNEval)
                    {
                        int violated = 0;
                        for (int j = 0; j < p.getCurrentPlayer(true).owncards.Count; j++)
                        {
                            Handmanager.Handcard hc = p.getCurrentPlayer(true).owncards[j];
                            //Helpfunctions.Instance.logg(hc.card.name + ":" + hc.playProb);
                            //Helpfunctions.Instance.logg("Entity:" + hc.entity);
                            violated = hc.entity;
                            if (p.getCurrentPlayer(true).owncards[j].playProb > 0)
                            {
                                followPolicy = false;
                                break;
                            }
                        }
                        //if (p.getCurrentPlayer(true).playactions.Count > 0 && violated != 1041)
                        //{
                        //    int ddbug = 1;
                        //}
                    }

                    p.endTurn(false, false);
                    p.drawTurnStartCard();
                    //float value;
                    //if (lethalCheck)
                    //{
                    //    Player ePlayer;
                    //    ePlayer = playerSide ? p.playerSecond : p.playerFirst;
                    //    if (ePlayer.ownHero.Hp < 0)
                    //    {
                    //        value = 1.0f;
                    //    }
                    //    else
                    //    {
                    //        value = 0.0f;
                    //    }
                    //}
                    //else
                    //{
                    //    value = getValue(p);
                    //}
                    //if (value > bestoldval) // want the best enemy-play-> worst for us
                    //{
                    //    bestoldval = value;
                    //    bestold = p;
                    //}

                    if (followPolicy)
                    {
                        if (!candidateStates.ContainsKey(actionHash))
                        {
                            candidateStates[actionHash] = new List <Playfield>();
                        }
                        float stateValue = getStateValue(p);
                        p.value = stateValue;
                        candidateStates[actionHash].Add(p);
                        boardcount++;

                        //candidateStates.Add(p);
                        posmoves.Remove(p);

                        if (boardcount >= maxWide)
                        {
                            break;
                        }
                    }
                }

                //if (bestoldval <= 10000 && bestold != null)
                //{
                //    posmoves.Add(bestold);
                //}

                deep++;
                if (boardcount >= maxWide)
                {
                    Helpfunctions.Instance.logg("reach maxwide");
                    break;
                }
            }


            List <Playfield> statesList = new List <Playfield>();

            Helpfunctions.Instance.logg("Diff card play policy: " + candidateStates.Count);
            foreach (List <Playfield> value in candidateStates.Values)
            {
                if (value.Count > 0)
                {
                    //Helpfunctions.Instance.logg("Actions:" + lethalCheck);
                    //value[0].printLastTurnActions();
                    //Helpfunctions.Instance.logg("Has " + value.Count + " children");
                    //if (value.Count > 100)
                    //{
                    //    this.board.printBoard();
                    //    debug = 1;
                    //}
                    //Helpfunctions.Instance.logg("Count: " + value.Count);
                    //Helpfunctions.Instance.logg("Top 10: ");

                    var mySortedList = value.OrderByDescending(l => l.value)
                                       .Take(10);
                    //foreach (Playfield pf in mySortedList)
                    //{
                    //    Helpfunctions.Instance.logg("state value:" + pf.value);
                    //    pf.printLastTurnActions();
                    //}
                    statesList.AddRange(value);
                }
            }

            if (statesList.Count > 10000)
            {
                debug = 1;
            }
            //Playfield bestState = getBestCandidate(candidateStates, lethalCheck);
            Playfield bestState = null;

            if (!lethalCheck && this.currentRandom)
            {
                bestState = statesList[GameManager.getRNG().Next(statesList.Count)];
            }
            else
            {
                bestState = getBestCandidate(statesList, lethalCheck);
            }

            if (lethalCheck)
            {
                if (bestState != null)
                {
                    actionList.AddRange(bestState.getLastTurnAction());
                }
                else
                {
                    actionList.Add(null);
                }
            }
            else
            {
                if (bestState != null)
                {
                    bestState.printLastTurnMacros();
                    actionList.AddRange(bestState.getLastTurnAction());
                    //Helpfunctions.Instance.logg("value = " + bestoldval);
                }

                actionList.Add(null);
            }

            this.avgBoardCount = (this.avgBoardCount + boardcount) / this.turnPlayed;
            Helpfunctions.Instance.logg("Use DNN:" + this.useNNEval + ", BC = " + boardcount);
            Helpfunctions.Instance.logg("Use DNN:" + this.useNNEval + ", avgBoardCount = " + this.avgBoardCount);
            return(actionList);
        }