Ejemplo n.º 1
0
        public override PlayerTask GetMove(POGame game)
        {
            Controller player = game.CurrentPlayer;

            if (h_score == -1)
            {
                determineScore(player);
            }
            // Implement a simple Mulligan Rule
            if (player.MulliganState == Mulligan.INPUT)
            {
                List <int> mulligan = new HarderMidRangeScore().MulliganRule().Invoke(player.Choice.Choices.Select(p => game.getGame().IdEntityDic[p]).ToList());
                return(ChooseTask.Mulligan(player, mulligan));
            }

            int depth;
            int beamWidth;

            // Check how much time we have left on this turn. The hard limit is 75 seconds so we already stop
            // beam searching when 60 seconds have passed, just to be sure.
            if (_watch.ElapsedMilliseconds < 30 * 1000)
            {             // We still have ample time, proceed with beam search
                depth     = 15;
                beamWidth = 12;
            }
            else
            {             // Time is running out, just simulate one timestep now
                depth     = 1;
                beamWidth = 1;
                Console.WriteLine("Over 30s in turn already. Pausing beam search for this turn!");
            }

            _watch.Start();
            PlayerTask move = BeamSearch(game, depth, playerbeamWidth: beamWidth, opponentBeamWidth: 1);

            _watch.Stop();

            if (move.PlayerTaskType == PlayerTaskType.END_TURN)
            {
                _watch.Reset();
            }

            return(move);
        }
        public override PlayerTask GetMove(POGame game)
        {
            Controller player = game.CurrentPlayer;

            if (h_score == -1)
            {
                determineScore(player);
            }
            // Implement a simple Mulligan Rule
            if (player.MulliganState == Mulligan.INPUT)
            {
                List <int> mulligan = new HarderMidRangeScore().MulliganRule().Invoke(player.Choice.Choices.Select(p => game.getGame().IdEntityDic[p]).ToList());
                return(ChooseTask.Mulligan(player, mulligan));
            }


            IEnumerable <KeyValuePair <PlayerTask, POGame> > validOpts = game.Simulate(player.Options()).Where(x => x.Value != null);
            int optcount = validOpts.Count();

            PlayerTask returnValue = validOpts.Any() ?
                                     validOpts.Select(x => score(x, player.PlayerId, (optcount >= 5) ? ((optcount >= 25) ? 1 : 2) : 3)).OrderBy(x => x.Value).Last().Key :
                                     player.Options().First(x => x.PlayerTaskType == PlayerTaskType.END_TURN);

            return(returnValue);

            KeyValuePair <PlayerTask, int> score(KeyValuePair <PlayerTask, POGame> state, int player_id, int max_depth = 3)
            {
                int max_score = Int32.MinValue;

                if (max_depth > 0 && state.Value.CurrentPlayer.PlayerId == player_id)
                {
                    IEnumerable <KeyValuePair <PlayerTask, POGame> > subactions = state.Value.Simulate(state.Value.CurrentPlayer.Options()).Where(x => x.Value != null);

                    foreach (KeyValuePair <PlayerTask, POGame> subaction in subactions)
                    {
                        max_score = Math.Max(max_score, score(subaction, player_id, max_depth - 1).Value);
                    }
                }
                max_score = Math.Max(max_score, Score(state.Value, player_id));
                return(new KeyValuePair <PlayerTask, int>(state.Key, max_score));
            }
        }
Ejemplo n.º 3
0
        public override PlayerTask GetMove(POGame game)
        {
            Controller player = game.CurrentPlayer;

            if (score == -1)
            {
                determineScore(player);
            }
            // Implement a simple Mulligan Rule
            if (player.MulliganState == Mulligan.INPUT)
            {
                List <int> mulligan = new HarderMidRangeScore().MulliganRule().Invoke(player.Choice.Choices.Select(p => game.getGame().IdEntityDic[p]).ToList());
                return(ChooseTask.Mulligan(player, mulligan));
            }

            // Get all simulation results for simulations that didn't fail
            IEnumerable <KeyValuePair <PlayerTask, POGame> > validOpts = game.Simulate(player.Options()).Where(x => x.Value != null);

            // If all simulations failed, play end turn option (always exists), else best according to score function
            return(validOpts.Any() ?
                   validOpts.OrderBy(x => Score(x.Value, player.PlayerId)).Last().Key :
                   player.Options().First(x => x.PlayerTaskType == PlayerTaskType.END_TURN));
        }
        public override PlayerTask GetMove(POGame game)
        {
            Controller player = game.CurrentPlayer;

            if (score == -1)
            {
                determineScore(player);
            }

            if (player.MulliganState == Mulligan.INPUT)
            {
                List <int> mulligan = new HarderMidRangeScore().MulliganRule().Invoke(player.Choice.Choices.Select(p => game.getGame().IdEntityDic[p]).ToList());
                return(ChooseTask.Mulligan(player, mulligan));
            }
            //Console.WriteLine("Beginne Auszurollen");
            PlayerTask move = GetBestMove(game);

            if (move == null)
            {
                move = game.CurrentPlayer.Options()[0];
            }
            //Console.WriteLine("Spiele");
            return(move);
        }
Ejemplo n.º 5
0
        public override PlayerTask GetMove(POGame game)
        {
            Controller player = game.CurrentPlayer;

            if (h_score == -1)
            {
                determineScore(player);
            }
            // Implement a simple Mulligan Rule
            if (player.MulliganState == Mulligan.INPUT)
            {
                List <int> mulligan = new HarderMidRangeScore().MulliganRule().Invoke(player.Choice.Choices.Select(p => game.getGame().IdEntityDic[p]).ToList());
                return(ChooseTask.Mulligan(player, mulligan));
            }

            finishedBuildingTree = false;

            if (!finishedBuildingTree)
            {
                actionsToTake = new List <PlayerTask>();
            }

            if (actionsToTake.Count != 0)
            {
                PlayerTask action = DequeueTask();
                finishedBuildingTree = actionsToTake.Count != 0;

                /*if (!finishedBuildingTree) {
                 *  Console.WriteLine("My health: " + poGame.CurrentPlayer.Hero.Health);
                 *              Console.WriteLine("Opponent health: " + poGame.CurrentOpponent.Hero.Health);
                 *  Console.WriteLine("\n\n\n");
                 * }
                 * Console.WriteLine(String.Format("Action: {0}", action));*/
                return(action);
            }

            List <MinMaxAgentState> leaves       = new List <MinMaxAgentState>();
            List <MinMaxAgentState> parentStates = new List <MinMaxAgentState>()
            {
                new MinMaxAgentState(game)
            };

            // while we have moves to play
            // int generation = 0;
            while (true)
            {
                // split currentLevel into leaves "partitioned[false]"
                // and non-leaves "partitioned[true]"
                ILookup <bool, MinMaxAgentState> partitioned = parentStates.ToLookup(
                    currentLevelNonLeaf => currentLevelNonLeaf.OnlyActionIsEndTurn ||
                    currentLevelNonLeaf.ActionsToReachState.Count >= 6
                    );

                IEnumerable <MinMaxAgentState> parentLeaves    = partitioned[true];
                List <MinMaxAgentState>        nonParentLeaves = partitioned[false].ToList();

                leaves.AddRange(parentLeaves.ToList());

                if (nonParentLeaves.Count == 0)
                {
                    break;
                }

                //Console.WriteLine(String.Format("leaves.Count = {0}", leaves.Count));
                //Console.WriteLine(String.Format("haveMoreActions.Count = {0}\n", haveMoreActions.Count));

                //if (haveMoreActions.Count == 0) {
                //    break;
                //}

                List <MinMaxAgentState> childrenStates = new List <MinMaxAgentState>();

                //Console.WriteLine(String.Format("========== Actions My Bot Can Take =========="));
                //Console.WriteLine(String.Format("Iteration #{0}\n\n", ++generation));

                foreach (MinMaxAgentState hasMoreAction in nonParentLeaves)
                {
                    Dictionary <PlayerTask, POGame> mapping = hasMoreAction.State.Simulate(
                        hasMoreAction.AvailableActions.ToList()
                        );//.Where( x => x.Value != null );
                    foreach (KeyValuePair <PlayerTask, POGame> actionState in mapping)
                    {
                        if (actionState.Value != null)
                        {
                            /*if (actionState.Key.FullPrint().Contains("Player1")) {
                             *  continue;
                             * }
                             * Console.WriteLine(actionState.Key.FullPrint());*/
                            childrenStates.Add(new MinMaxAgentState(hasMoreAction, actionState));
                        }
                    }
                }

                // Get the top N childrenStates
                var childrenStatesWithScore = childrenStates.Zip(childrenStates.Select(
                                                                     childState => Score(childState.State, childState.State.CurrentPlayer.PlayerId)
                                                                     ), (state, score) => new Tuple <MinMaxAgentState, double>(state, score)).ToList();
                childrenStatesWithScore.Sort((lhs, rhs) => rhs.Item2.CompareTo(lhs.Item2));

                parentStates = childrenStatesWithScore.Take(MAX_CHILD_COUNT).Select(
                    stateScore => stateScore.Item1
                    ).ToList();
            }

            // No moves possible i.e. no leaves
            var finalCandidates = leaves.ToList();

            if (finalCandidates.Count == 0)
            {
                actionsToTake.Add(EndTurnTask.Any(parentStates[0].State.CurrentPlayer.Controller));
                return(DequeueTask());
            }

            // Get the very best
            MinMaxAgentState bestState = MaxByKey(finalCandidates, (lhs, rhs) => {
                return(Score(lhs.State, lhs.State.CurrentPlayer.Id) < Score(rhs.State, rhs.State.CurrentPlayer.Id) ? rhs : lhs);
            });

            actionsToTake = bestState.ActionsToReachState;
            if (actionsToTake.Count == 0 || actionsToTake[actionsToTake.Count - 1].PlayerTaskType != PlayerTaskType.END_TURN)
            {
                actionsToTake.Add(EndTurnTask.Any(bestState.State.CurrentPlayer.Controller));
            }

            finishedBuildingTree = true;
            // Console.WriteLine(String.Format("Turn Counter @ {0}", ++turnCounter));
            return(DequeueTask());
        }