Ejemplo n.º 1
0
        public static List <RecRateResult> recRate(State2048 root, int depth)
        {
            List <StateTrans> moves = root.getAllMoveStates();

            //If we can't move, the game is over; worst penalty
            if (moves.Count == 0)
            {
                return new List <RecRateResult> {
                           new RecRateResult(double.MaxValue, MoveDir.Self)
                }
            }
            ;

            //If the depth is zero, return the result from this state
            if (depth == 0)
            {
                return new List <RecRateResult> {
                           new RecRateResult(root.rate(), MoveDir.Self)
                }
            }
            ;

            List <RecRateResult> result = new List <RecRateResult>();

            foreach (StateTrans st in moves)
            {
                double bestOfWorst = double.MaxValue;

                List <State2048> allRandom = st.state.getAllRandom();
                foreach (State2048 rand in allRandom)
                {
                    List <RecRateResult> randRes = recRate(rand, depth - 1);

                    //Take the maximum penalty
                    double worstRate = randRes[0].rating;
                    for (int i = 1; i < randRes.Count; i++)
                    {
                        if (randRes[i].rating > worstRate)
                        {
                            worstRate = randRes[i].rating;
                        }
                    }

                    if (worstRate < bestOfWorst)
                    {
                        bestOfWorst = worstRate;
                    }
                }

                result.Add(new RecRateResult(bestOfWorst, st.dir));
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static double alphabetarate(State2048 root, int depth, double alpha, double beta, bool player)
        {
            if (depth == 0)
            {
                return(root.rate());
            }

            if (player)
            {
                List <StateTrans> moves = root.getAllMoveStates();

                //If we can't move, the game is over; worst penalty
                if (moves.Count == 0)
                {
                    return(double.MaxValue);
                }

                foreach (StateTrans st in moves)
                {
                    alpha = Math.Min(alpha, alphabetarate(st.state, depth - 1, alpha, beta, !player));
                    if (beta >= alpha)
                    {
                        break;
                    }
                }
                return(alpha);
            }
            else
            {
                List <State2048> moves = root.getAllRandom();

                foreach (State2048 st in moves)
                {
                    beta = Math.Max(beta, alphabetarate(st, depth - 1, alpha, beta, !player));
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(beta);
            }
        }