Exemplo n.º 1
0
        internal void TurnStart(ActivePlayer player)
        {
            if (!_isMulliganDone)
            {
                _game.Process(ChooseTask.Mulligan(_game.Player1, _toBeKept));
                _game.Process(ChooseTask.Mulligan(_game.Player2, _game.Player2.Choice.Choices));
            }
            _isMulliganDone = true;
            _isNewTurn      = true;
            _activePlayer   = player;
            _numberOfTurns++;

            if (_numberOfTurns > 1)
            {
                ProcessPlayerAction();
                _game.MainEnd();
                _game.MainNext();
            }

            GameV2 gameV2 = Core.Game;

            if (player == ActivePlayer.Player)
            {
                _messenger.Reset();
                _messenger.SetTitle("Turn " + gameV2.GetTurnNumber());
            }
            Converter.AreGamesInSync(_game, gameV2);
        }
            /**
             * Simulates the tasks against the current game and
             * returns a Dictionary with the following POGame-Object
             * for each task (or null if an exception happened
             * during that game)
             */
            public Dictionary <PlayerTask, POGame> Simulate(List <PlayerTask> tasksToSimulate)
            {
                Dictionary <PlayerTask, POGame> simulated = new Dictionary <PlayerTask, POGame>();

                foreach (PlayerTask task in tasksToSimulate)
                {
                    SabberStoneCore.Model.Game clone = this.origGame.Clone();
                    try {
                        clone.Process(task);
                        simulated.Add(task, new POGame(clone, this.debug));
                    }
                    catch (Exception) {
                        simulated.Add(task, null);
                    }
                }
                return(simulated);
            }
Exemplo n.º 3
0
        internal void ProcessPlayerAction()
        {
            foreach (var playerAction in _playerActions)
            {
                _isProcessingTask = true;
                _randomController.RandomHappened = false;
                if (playerAction.ActionType == ActionType.PLAYCARD && playerAction.Player == 2)
                {
                    STCEntities.IPlayable toBePlayed = STCEntities.Entity.FromCard(_game.Player2, STC.Cards.FromAssetId(playerAction.Source.Card.DbfIf));
                    _game.Player2.HandZone.Replace(_game.Player2.HandZone[0], toBePlayed);
                    _entityIdMapping.Add(playerAction.Source.Id, toBePlayed.Id);
                }
                List <PlayerTask> allOptions      = playerAction.Player == 1 ? _game.Player1.Options(true) : _game.Player2.Options(true);
                List <PlayerTask> filteredOptions = new List <PlayerTask>();
                allOptions.ForEach(option =>
                {
                    try
                    {
                        switch (playerAction.ActionType)
                        {
                        case ActionType.HEROPOWER:
                            if (option is HeroPowerTask)
                            {
                                filteredOptions.Add(option);
                            }
                            break;

                        case ActionType.PLAYCARD:
                            if (option is PlayCardTask)
                            {
                                if (option.Source.Id == _entityIdMapping[playerAction.Source.Id])
                                {
                                    filteredOptions.Add(option);
                                }
                            }
                            break;

                        case ActionType.MINIONATTACK:
                            if (option is MinionAttackTask)
                            {
                                if (option.Source.Id == _entityIdMapping[playerAction.AttackInfo.Attacker.Id])
                                {
                                    filteredOptions.Add(option);
                                }
                            }
                            if (option is HeroAttackTask)
                            {
                                if (option.Controller.HeroId == _entityIdMapping[playerAction.AttackInfo.Attacker.Id])
                                {
                                    filteredOptions.Add(option);
                                }
                            }
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                });

                if (filteredOptions.Count > 0)
                {
                    if (filteredOptions.Count == 1)
                    {
                        _game.Process(filteredOptions[0]);
                        _game.MainCleanUp();
                    }
                    else
                    {
                        STC.Game nextGame = null;
                        List <List <string> > errorsList = new List <List <string> >();
                        foreach (PlayerTask task in filteredOptions)
                        {
                            STC.Game clonedGame = _game.Clone(false, false, _randomController.Clone());
                            clonedGame.Process(task);
                            clonedGame.MainCleanUp();
                            var errors = Converter.AreGamesInSync(clonedGame, Core.Game);
                            if (errors.Count == 0)
                            {
                                nextGame = clonedGame;
                                break;
                            }
                            else
                            {
                                errorsList.Add(errors);
                            }
                        }
                        if (nextGame == null)
                        {
                            System.Diagnostics.Debugger.Break();
                        }
                        _game             = nextGame;
                        _randomController = (RandomController)nextGame.RandomController;
                    }
                    Converter.SyncEntityIds(ref _entityIdMapping, _game, Core.Game);
                    _game.Step = Step.MAIN_ACTION;
                    _randomController.Reset();
                    if (_randomController.RandomHappened)
                    {
                        launchAgent();
                    }
                }
                else
                {
                    System.Diagnostics.Debugger.Break();
                    var foo = playerAction.Player == 1 ? _game.Player1.Options(true) : _game.Player2.Options(true);
                }
            }
            _isProcessingTask = false;
            _randomController.RandomHappened = false;
            _playerActions = new List <PlayerAction>();
        }
        public bool PlayGame(bool addToGameStats = true, bool debug = false)
        {
            SabberStoneCore.Model.Game game = new SabberStoneCore.Model.Game(gameConfig, setupHeroes);
            //var game = new Game(gameConfig, setupHeroes);
            player1.InitializeGame();
            player2.InitializeGame();

            AbstractAgent currentAgent;
            Stopwatch     currentStopwatch;
            POGame        poGame;
            PlayerTask    playertask = null;

            Stopwatch[] watches   = new[] { new Stopwatch(), new Stopwatch() };
            bool        printGame = false;
            int         numturns  = 0;

            game.StartGame();
            if (gameConfig.SkipMulligan == false)
            {
                var originalStartingPlayer  = game.CurrentPlayer;
                var originalStartinOpponent = game.CurrentOpponent;

                game.CurrentPlayer = originalStartingPlayer;
                currentAgent       = gameConfig.StartPlayer == 1 ? player1 : player2;
                poGame             = new POGame(game, debug);
                playertask         = currentAgent.GetMove(poGame);
                game.Process(playertask);

                game.CurrentPlayer = originalStartinOpponent;
                currentAgent       = gameConfig.StartPlayer == 1 ? player2 : player1;
                poGame             = new POGame(game, debug);
                playertask         = currentAgent.GetMove(poGame);
                game.Process(playertask);

                game.CurrentPlayer = originalStartingPlayer;
                game.MainReady();
            }
#if DEBUG
            try
            {
#endif
            while (game.State != State.COMPLETE && game.State != State.INVALID)
            {
                //if (debug)
                numturns = game.Turn;
                //Console.WriteLine("Turn " + game.Turn);



                //ShowLog(game, LogLevel.INFO);


                if (printGame)
                {
                    //Console.WriteLine(MCGS.SabberHelper.SabberUtils.PrintGame(game));
                    printGame = false;
                }

                if (game.Turn >= maxTurns)
                {
                    break;
                }

                currentAgent = game.CurrentPlayer == game.Player1 ? player1 : player2;
                Controller currentPlayer = game.CurrentPlayer;
                currentStopwatch = game.CurrentPlayer == game.Player1 ? watches[0] : watches[1];
                poGame           = new POGame(game, debug);

                currentStopwatch.Start();
                playertask = currentAgent.GetMove(poGame);
                currentStopwatch.Stop();

                game.CurrentPlayer.Game   = game;
                game.CurrentOpponent.Game = game;

                if (debug)
                {
                    //Console.WriteLine(playertask);
                }

                if (playertask.PlayerTaskType == PlayerTaskType.END_TURN)
                {
                    printGame = true;
                }

                if (playertask.PlayerTaskType == PlayerTaskType.END_TURN && game.CurrentPlayer == game.Player1)
                {
                    if (game.Turn > new Random().Next(-5, 20))
                    {
                        OutputCurrentGameForTrainingData(game);
                    }
                }

                game.Process(playertask);
            }
#if DEBUG
        }

        catch (Exception e)
        //Current Player loses if he throws an exception
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);
            game.State = State.COMPLETE;
            game.CurrentPlayer.PlayState   = PlayState.CONCEDED;
            game.CurrentOpponent.PlayState = PlayState.WON;

            if (addToGameStats && game.State != State.INVALID)
            {
                gameStats.registerException(game, e);
            }
        }
#endif

            if (game.State == State.INVALID || (game.Turn >= maxTurns && repeatDraws))
            {
                return(false);
            }

            if (addToGameStats)
            {
                gameStats.addGame(game, watches);
            }

            List <int> features;

            if (game.Player1.PlayState == PlayState.WON)
            {
                while (tmpData.Count > 0)
                {
                    features = tmpData.Dequeue();
                    features.Add(1);
                    trainingData.Enqueue(features);
                }
            }
            else if (game.Player2.PlayState == PlayState.WON)
            {
                while (tmpData.Count > 0)
                {
                    features = tmpData.Dequeue();
                    features.Add(-1);
                    trainingData.Enqueue(features);
                }
            }
            else
            {
                while (tmpData.Count > 0)
                {
                    features = tmpData.Dequeue();
                    features.Add(-1);
                    trainingData.Enqueue(features);
                }
            }


            player1.FinalizeGame();
            player2.FinalizeGame();

            turnsRecords.Add(numturns);


            //ShowLog(game, LogLevel.INFO);

            return(true);
        }
 public void Process(PlayerTask task)
 {
     game.Process(task);
 }