Beispiel #1
0
 private void CheckGameOver()
 {
     Ruler.GameResult result = Ruler.CheckGame(board);
     if (result != Ruler.GameResult.NotYet)
     {
         state       = MainState.Over;
         this.result = result;
     }
 }
Beispiel #2
0
    public static double Evaluate(GameDescriptor descriptor)
    {
        Unit.OwnerEnum   owner  = descriptor.Turn;
        Ruler.GameResult result = Ruler.CheckGame(descriptor);

        if (result != Ruler.GameResult.NotYet)
        {
            return(EvaluateResult(result, owner));
        }

        double value = 0;

        value += EvalPosition(descriptor, owner) * PosFactor;
        value += EvalCard(descriptor, owner) * CardFactor;
        value += EvalDanger(descriptor, owner) * DangerFactor;
        value += EvalSpecial(descriptor, owner) * SpecialFactor;

        return(value);
    }
Beispiel #3
0
 public void NextTurn()
 {
     if (state == MainState.Wait || state == MainState.AI_Running)
     {
         Ruler.GameResult result = Ruler.CheckGame(board);
         if (result == Ruler.GameResult.NotYet)
         {
             turn  = Unit.Opposite(turn);
             state = MainState.Move;
             NewTurn();
         }
         else
         {
             state       = MainState.Over;
             this.result = result;
             OnGameOver();
         }
     }
 }
Beispiel #4
0
    double AlphaBeta(GameDescriptor descriptor, int depth, double alpha, double beta)
    {
        Ruler.GameResult result = Ruler.CheckGame(descriptor);

        if (result != Ruler.GameResult.NotYet)
        {
            return(Evaluator.EvaluateResult(result, descriptor.Turn) - depth);
        }
        if (depth == 0 || nodeCount >= Max_Node)
        {
            nodeCount++;
            return(Evaluator.Evaluate(descriptor) - depth); // the quicker, the better
        }

        List <PlayerAction> actions = descriptor.QueryAllActions();

        Disturb(actions);

        foreach (PlayerAction tryAction in actions)
        {
            descriptor.DoAction(tryAction);
            var tmp = -AlphaBeta(descriptor, depth - 1, -beta, -alpha);
            tryAction.UnDo(descriptor);

            if (tmp >= beta)
            {
                return(beta);
            }
            if (tmp > alpha)
            {
                alpha = tmp;
                if (depth == Max_Depth)
                {
                    action = tryAction;
                }
            }
        }
        return(alpha);
    }
Beispiel #5
0
    double AlphaBeta_Slow(GameDescriptor descriptor, int depth, double alpha, double beta)
    {
        Ruler.GameResult result = Ruler.CheckGame(descriptor);
        if (result != Ruler.GameResult.NotYet)
        {
            return(Evaluator.EvaluateResult(result, descriptor.Turn) - depth);
        }
        if (depth == 0)
        {
            nodeCount++;
            return(Evaluator.Evaluate(descriptor) - depth);
        }

        List <PlayerAction> actions = descriptor.QueryAllActions_Slow();

        Disturb(actions);

        foreach (PlayerAction tryAction in actions)
        {
            GameDescriptor clone = descriptor.Clone() as GameDescriptor;
            clone.DoAction(tryAction);
            var tmp = -AlphaBeta_Slow(clone, depth - 1, -beta, -alpha);

            if (tmp >= beta)
            {
                return(beta);
            }
            if (tmp > alpha)
            {
                alpha = tmp;
                if (depth == Max_Depth)
                {
                    action = tryAction;
                }
            }
        }
        return(alpha);
    }
Beispiel #6
0
 public void NextTurn(bool lastTurnIsMe = false)
 {
     if (lastTurnIsMe)
     {
         DoAction(PlayerAction.CreateComplete());
     }
     _DoTurnOver();
     if (state == MainState.Wait || state == MainState.AgentRunning || state == MainState.AgentEffectWaiting)
     {
         Ruler.GameResult result = Ruler.CheckGame(board);
         if (result == Ruler.GameResult.NotYet)
         {
             turn  = Unit.Opposite(turn);
             state = MainState.Move;
             NewTurn(false);
         }
         else
         {
             state       = MainState.Over;
             this.result = result;
             OnGameOver();
         }
     }
 }