Beispiel #1
0
        static void Main(string[] args)
        {
            cards = Enumerable.Range(0, 20).ToDictionary(x => x, x => 0);
            var game = new Game();

            // game loop
            while (true)
            {
                game.UpdatePlayers();
                game.UpdateCards();

                CgTimer.Reset(90);

                if (game.Draw)
                {
                    Pick(game);
                    continue;
                }

                var s = game.Serialize();
                //D(s);

                DoActions(game);
            }
        }
        public static IEnumerable <GameAction> GetBestActions(Game game)
        {
            var allActions = new List <GameActionsBatch>();

            ResolveActions(0, game, null, allActions);

            CgPlayer.D($"actions: {allActions.Count}");

            allActions = allActions.OrderByDescending(x => x.Score).ToList();

            CgTimer.Log("my finished");

            IEnumerable <GameAction> actions = GameAction.PassActions;

            //if (allActions.Count > 0)
            //{
            //    var top = allActions.First();
            //    actions = top.Actions;
            //}

            foreach (var a in allActions)
            {
                if (CgTimer.IsTimeout())
                {
                    break;
                }
                var oppActions = new List <GameActionsBatch>();
                ResolveActions(1, a.Game, null, oppActions);
                var best = oppActions.OrderByDescending(x => x.Score).FirstOrDefault();
                if (best != null)
                {
                    a.Score1   = best.Score;
                    a.Actions1 = best.Actions;
                }
                else
                {
                    a.Score1   = double.MaxValue;
                    a.Actions1 = GameAction.PassActions;
                }
            }

            if (allActions.Count > 0)
            {
                var top = allActions.First();
                actions = top.Actions;
                CgPlayer.D($"top: {top.Score}/{top.Score1} {string.Join(";", actions)}");

                var top1 = allActions.Where(x => x.Score1 > double.MinValue).OrderByDescending(x => x.Score1).FirstOrDefault();
                if (top1 != null)
                {
                    CgPlayer.D($"top1: {top1?.Score}/{top1?.Score1} {string.Join(";", top1?.Actions)} / {string.Join(";", top1?.Actions1)}");
                    actions = top1.Actions;
                }
            }

            CgTimer.Log("op finished");

            return(actions);
        }
        static private void ResolveActions(int playerIndex, Game game, LinkedList <GameAction> moves, List <GameActionsBatch> allActions)
        {
            if (CgTimer.IsTimeout())
            {
                return;
            }

            CgTimer.Tick();

            game = new Game(game);

            if (moves != null)
            {
                Simulator.ApplyMove(playerIndex, moves.Last(), game);
            }
            else
            {
                moves = new LinkedList <GameAction>();
            }

            var availableMoves = MovesEnumerator.GetAvailableActions(game, playerIndex);

            if (!availableMoves.Any())
            {
                if (moves.Any())
                {
                    allActions.Add(new GameActionsBatch {
                        Game = game, Actions = moves.ToArray(), Score = CalcScore(game)
                    });
                }
            }
            else
            {
                foreach (var m in availableMoves)
                {
                    if (CgTimer.IsTimeout())
                    {
                        return;
                    }

                    moves.AddLast(m);
                    ResolveActions(playerIndex, game, moves, allActions);
                    moves.RemoveLast();
                }
            }
        }
        private static double CalcScoreWithOpponentAction(Game game)
        {
            var actions   = MovesEnumerator.GetAvailableActions(game, 1);
            var bestScore = double.MinValue;

            foreach (var a in actions)
            {
                if (CgTimer.IsTimeout())
                {
                    break;
                }

                var g = new Game(game);
                Simulator.ApplyMove(1, a, g);
                var score = CalcScore(g);
                if (score > bestScore)
                {
                    bestScore = score;
                }
            }

            return(bestScore);
        }
        public static IEnumerable <GameAction> GetBestActions(Game game)
        {
            IEnumerable <GameAction> bestActions = GameAction.PassActions;
            var bestScore    = double.MinValue;
            var batchActions = new LinkedList <GameAction>();

            while (!CgTimer.IsTimeout())
            {
                var g = new Game(game);
                batchActions.Clear();

                while (!CgTimer.IsTimeout())
                {
                    CgTimer.Tick();

                    var actions = MovesEnumerator.GetAvailableActions(g, 0).ToArray();
                    if (actions.Length == 0)
                    {
                        break;
                    }

                    var action = actions[rnd.Next(actions.Length)];
                    Simulator.ApplyMove(0, action, g);
                    batchActions.AddLast(action);
                }

                //var score = CalcScoreWithOpponentAction(g);
                var score = CalcScore(g);

                if (score > bestScore)
                {
                    bestScore   = score;
                    bestActions = batchActions.ToArray();
                }
            }
            return(bestActions);
        }