Exemple #1
0
 internal void PlayerDraw(Entity entity)
 {
     Console.WriteLine("PlayerDraw " + entity.Card.Name);
     _randomController.cardsToDraw.Add(entity);
     if (!_isMulliganPhase)
     {
         if (_isNewTurn)
         {
             _isNewTurn = false;
             _game.MainReady();
             _game.MainStartTriggers();
             _game.MainRessources();
             _game.MainDraw();
             _game.MainStart();
             _game.Step = Step.MAIN_ACTION;
         }
         if (!_isProcessingTask)
         {
             launchAgent();
         }
     }
 }
Exemple #2
0
        /// <summary>Invokes the method which corresponds to the next simulation step.</summary>
        /// <param name="game">The game subject.</param>
        /// <param name="step">The (next) step value.</param>
        /// <exception cref="ArgumentOutOfRangeException">step - when the provided step is unknown</exception>
        public void NextStepEvent(Game game, Step step)
        {
            game.Log(LogLevel.DEBUG, BlockType.TRIGGER, "Event", !game.Logging? "":$"NextStepEvent - {step}");
            switch (step)
            {
            case Step.BEGIN_FIRST:
                game.Step = step;
                game.BeginFirst();
                break;

            case Step.BEGIN_SHUFFLE:
                game.Step = step;
                game.BeginShuffle();
                break;

            case Step.BEGIN_DRAW:
                game.Step = step;
                game.BeginDraw();
                break;

            case Step.BEGIN_MULLIGAN:
                game.Step = step;
                game.BeginMulligan();
                break;

            case Step.MAIN_BEGIN:
                game.Step = step;
                game.MainBegin();
                break;

            case Step.MAIN_DRAW:
                game.Step = step;
                game.MainDraw();
                break;

            case Step.MAIN_READY:
                game.Step = step;
                game.MainReady();
                break;

            case Step.MAIN_RESOURCE:
                game.Step = step;
                game.MainRessources();
                break;

            case Step.MAIN_START:
                game.Step = step;
                game.MainStart();
                break;

            case Step.MAIN_START_TRIGGERS:
                game.Step = step;
                game.MainStartTriggers();
                break;

            case Step.MAIN_ACTION:
                game.Step = step;
                break;

            case Step.MAIN_COMBAT:
                break;

            case Step.MAIN_CLEANUP:
                game.Step = step;
                game.MainCleanUp();
                break;

            case Step.MAIN_END:
                game.Step = step;
                game.MainEnd();
                break;

            case Step.MAIN_NEXT:
                game.Step = step;
                game.MainNext();
                break;

            case Step.FINAL_WRAPUP:
                game.FinalWrapUp();
                break;

            case Step.FINAL_GAMEOVER:
                game.FinalGameOver();
                break;

            case Step.INVALID:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(step), step, null);
            }
        }
        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);
        }