Esempio n. 1
0
        private void initGame()
        {
            // Init decks
            List <STC.Card> cardsInDeck = new List <STC.Card>();

            HearthMirror.Objects.Deck MirrorDeck = Core.Game.CurrentSelectedDeck;
            if (MirrorDeck != null)
            {
                MirrorDeck.Cards.ForEach(card =>
                {
                    for (var i = 0; i < card.Count; i++)
                    {
                        cardsInDeck.Add(STC.Cards.FromId(card.Id));
                    }
                });
            }

            List <STC.Card> UnknownDeck = new List <STC.Card>();

            for (int i = 0; i < 30; i++)
            {
                UnknownDeck.Add(new STC.Card()
                {
                    Id   = "Unknown",
                    Name = "Unknown",
                    Tags = new Dictionary <GameTag, int> {
                        [GameTag.CARDTYPE] = (int)CardType.MINION, [GameTag.COST] = 51
                    },
                });
            }

            // Init agent
            _agent = new Expectiminimax(cardsInDeck, Converter.GetCardClass(Core.Game.Player.Class), Strategy.Control);

            // Init game
            GameV2 gameV2 = Core.Game;

            _game = new STC.Game(
                new GameConfig()
            {
                StartPlayer      = gameV2.Player.HandCount == 3 ? 1 : 2,
                Player1Name      = gameV2.Player.Name,
                Player1HeroClass = Converter.GetCardClass(gameV2.Player.Class),
                Player1Deck      = cardsInDeck,
                Player2Name      = gameV2.Opponent.Name,
                Player2HeroClass = Converter.GetCardClass(gameV2.Opponent.Class),
                Player2Deck      = UnknownDeck,
                FillDecks        = false,
                Shuffle          = false,
                SkipMulligan     = false,
                AutoNextStep     = false,
                Logging          = false,
                History          = false,
                RandomController = _randomController
            });
            _game.StartGame();
            _game.BeginDraw();
            _game.BeginMulligan();
            Converter.SyncEntityIds(ref _entityIdMapping, _game, gameV2);
        }
        public static SabberGame GetTestGame()
        {
            var game = new SabberGame(new GameConfig
            {
                FillDecks = false,
                History   = false,
                Logging   = false,
                Shuffle   = false
            });

            game.StartGame();
            return(game);
        }
Esempio n. 3
0
        public static Game GenerateGame(string deckString1, string deckString2, bool history = false,
                                        long seed = 0)
        {
            Deck deck1, deck2;

            try
            {
                deck1 = Deserialise(deckString1);
            }
            catch (Exception e)
            {
                Console.WriteLine("Deckstring #1 is not a valid deckstring");
                throw e;
            }

            try
            {
                deck2 = Deserialise(deckString2);
            }
            catch (Exception e)
            {
                Console.WriteLine("Deckstring #2 is not a valid deckstring");
                throw e;
            }

            if (seed == 0)
            {
                seed = DateTime.UtcNow.Ticks;
            }

            var game = new Game(new SabberStoneCore.Config.GameConfig
            {
                StartPlayer      = -1,
                Player1HeroClass = deck1.Class,
                Player1Deck      = deck1.GetCardList(),
                Player2HeroClass = deck2.Class,
                Player2Deck      = deck2.GetCardList(),

                Logging      = false,
                History      = history,
                FillDecks    = false,
                Shuffle      = true,
                SkipMulligan = true,
                RandomSeed   = seed
            });

            game.StartGame();

            return(game);
        }
Esempio n. 4
0
        public static API.Game GenerateGameAPI(string deckString1, string deckString2)
        {
            Deck deck1, deck2;

            try
            {
                deck1 = Deserialise(deckString1);
            }
            catch (Exception e)
            {
                Console.WriteLine("Deckstring #1 is not a valid deckstring");
                throw e;
            }

            try
            {
                deck2 = Deserialise(deckString2);
            }
            catch (Exception e)
            {
                Console.WriteLine("Deckstring #2 is not a valid deckstring");
                throw e;
            }



            var game = new Game(new SabberStoneCore.Config.GameConfig
            {
                StartPlayer      = -1,
                Player1HeroClass = deck1.Class,
                Player1Deck      = deck1.GetCardList(),
                Player2HeroClass = deck2.Class,
                Player2Deck      = deck2.GetCardList(),

                Logging      = false,
                History      = false,
                FillDecks    = false,
                Shuffle      = true,
                SkipMulligan = true,
            });

            game.StartGame();

            Console.WriteLine(Printers.PrintGame(game));

            return(new API.Game(game));
        }
Esempio n. 5
0
        public static void Test()
        {
            var game = new SabberStoneCore.Model.Game(new GameConfig
            {
                StartPlayer = 1,
                Shuffle     = false,
                History     = false,
                Logging     = false,
                Player1Deck = new List <Card>
                {
                    Cards.FromName("Stonetusk Boar"),
                    Cards.FromName("Wisp"),
                    Cards.FromName("Bloodfen Raptor"),
                    Cards.FromName("Dalaran Mage")
                }
            });

            game.StartGame();

            var    unmanagedHand = new HandZone_unmanaged(game.CurrentPlayer.HandZone);
            IntPtr ptr           = unmanagedHand.Playables;

            for (int i = 0; i < unmanagedHand.Count; i++)
            {
                Playable playable = Marshal.PtrToStructure <Playable>(ptr);
                ptr += Marshal.SizeOf <Playable>();

                Console.WriteLine("- Card 1:");
                Console.WriteLine($"\t{Cards.FromAssetId(playable.CardId).Name}");
                Console.WriteLine($"\tCost: {playable.Cost}");
                Console.WriteLine($"\tATK: {playable.ATK}");
                Console.WriteLine($"\tHP: {playable.BaseHealth}");
            }

            unmanagedHand.Free();
        }
        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);
        }