// returns true if the game ends
        private bool CheckAndHandleEndgame(MultiplayerGameManager <Player> gameManager, GameState state)
        {
            if (state.winner == -1)
            {
                return(false);
            }

            //gameManager.addToGameSummary("!\n" + state.ToString());

            if (Constants.VERBOSE_LEVEL > 1)
            {
                Console.WriteLine("   Game finished in turn " + (gameTurn - Constants.CARDS_IN_DECK) + ".");
            }
            if (Constants.VERBOSE_LEVEL > 1)
            {
                Console.Write("   Scores: ");
            }
            if (Constants.VERBOSE_LEVEL > 0)
            {
                Console.WriteLine((state.winner == 0 ? "1" : "0") + " " + (state.winner == 1 ? "1" : "0"));
            }

            gameManager.addToGameSummary(gameManager.getPlayer(state.winner).getNicknameToken() + " won!");
            gameManager.getPlayer(state.winner).setScore(1);
            gameManager.endGame();
            return(true);
        }
 private void HandleError(MultiplayerGameManager <Player> gameManager, Player sdkplayer, string errmsg)
 {
     gameManager.addToGameSummary(errmsg);
     sdkplayer.deactivate(errmsg);
     sdkplayer.setScore(-1);
     gameManager.endGame();
 }
        static int[] Battle(int loop, string codeA, string codeB, StreamWriter log, string use_seed, out int used_seed)
        {
            MultiplayerGameManager <Player> _gm = new MultiplayerGameManager <Player>();

            _gm.players.Add(CreatePlayerState(0, codeA));
            _gm.players.Add(CreatePlayerState(1, codeB));

            LOCAM.Referee _ref = new LOCAM.Referee();
            _ref.gameManager = _gm;

            Properties props = new Properties();

            _gm._params = props;
            if (use_seed != "")
            {
                props.Add("seed", use_seed);
            }

            _ref.init();

            int round = 0;

            while (_gm.game_ended == false && round < _gm.maxTurns)
            {
                _ref.gameTurn(round);

                round += 1;
            }
            used_seed = int.Parse(props.getProperty("seed", "0"));
            int sa = _gm.players[0].getScore();
            int sb = _gm.players[1].getScore();


            string lline = $"loop: {loop} seed: {used_seed} round: {round} a: {sa} b: {sb} {Path.GetFileNameWithoutExtension(codeA)} {Path.GetFileNameWithoutExtension(codeB)}";

            log.WriteLine(lline);

            foreach (var p in _gm.players)
            {
                p.pro.Kill();
                p.pro.WaitForExit();
                p.pro.Close();
            }

            int ra = 0, rb = 0;

            ra += sa >= sb ? 1 : 0;
            rb += sb >= sa ? 1 : 0;

            bat_res[0] = ra;
            bat_res[1] = rb;


            return(bat_res);
        }
        private void DraftTurn(MultiplayerGameManager <Player> gameManager)
        {
            if (Constants.VERBOSE_LEVEL > 1 && gameTurn == 0)
            {
                Console.WriteLine("   Draft phase");
            }
            if (Constants.VERBOSE_LEVEL > 2)
            {
                Console.WriteLine("      Draft turn " + gameTurn + "/" + Constants.CARDS_IN_DECK);
            }

            gameManager.setTurnMaxTime(gameTurn == 0 ? Constants.TIMELIMIT_FIRSTDRAFTTURN : Constants.TIMELIMIT_DRAFTTURN);

            for (int player = 0; player < 2; player++)
            {
                Player sdkplayer = gameManager.getPlayer(player);
                foreach (string line in draft.getMockPlayersInput())
                {
                    sdkplayer.sendInputLine(line);
                }

                for (int card = 0; card < 3; card++)
                {
                    sdkplayer.sendInputLine(draft.draft[gameTurn, card].getAsInput());
                }
                sdkplayer.execute();
            }

            for (int player = 0; player < 2; player++)
            {
                Player sdkplayer = gameManager.getPlayer(player);
                try
                {
                    string output = sdkplayer.getOutputs()[0];
                    DraftPhase.ChoiceResultPair choice = draft.PlayerChoice(gameTurn, output, player);
                    draft.text[player] = choice.text;
                    gameManager.addToGameSummary(
                        string.Format("Player %s chose %s", sdkplayer.getNicknameToken(), choice.card.toDescriptiveString())
                        );
                }
                catch (InvalidActionHard e)
                {
                    HandleError(gameManager, sdkplayer, sdkplayer.getNicknameToken() + ": " + e.Message);
                    return;
                }
                catch (TimeoutException e)
                {
                    HandleError(gameManager, sdkplayer, sdkplayer.getNicknameToken() + " timeout!");
                    return;
                }
            }

            gameTurn++;
        }
        public void refereeInit(MultiplayerGameManager <Player> gameManager)
        {
            if (Constants.VERBOSE_LEVEL > 1)
            {
                Console.WriteLine("New game");
            }

            RefereeParams _params = new RefereeParams(gameManager);

            DraftPhase.Difficulty difficulty;
            switch (gameManager.getLeagueLevel())
            {
            case 1:
                difficulty = DraftPhase.Difficulty.VERY_EASY;
                break;

            case 2:
                difficulty = DraftPhase.Difficulty.EASY;
                break;

            case 3:
                difficulty = DraftPhase.Difficulty.LESS_EASY;
                break;

            default:
                difficulty = DraftPhase.Difficulty.NORMAL;
                break;
            }


            Constants.LoadCardlist("main\\resources\\cardlist.txt");
            if (Constants.VERBOSE_LEVEL > 1)
            {
                Console.WriteLine("   CARDSET with " + Constants.CARDSET.Count + " cards loaded.");
            }
            if (Constants.VERBOSE_LEVEL > 1)
            {
                Console.WriteLine("   Difficulty is set to: " + difficulty.ToString() + ".");
            }

            draft = new DraftPhase(difficulty, _params);
            draft.PrepareChoices();

            if (Constants.VERBOSE_LEVEL > 1)
            {
                Console.WriteLine("   Draw Phase Prepared. " + draft.allowedCards.Count + " cards allowed. ");
            }
            if (Constants.VERBOSE_LEVEL > 1)
            {
                Console.WriteLine("   " + draft.draftingCards.Count + " cards selected to the draft.");
            }

            gameManager.setMaxTurns(Constants.MAX_TURNS_HARDLIMIT); // should be never reached, not handled on the referee's side
        }
 public bool refereeGameTurn(MultiplayerGameManager <Player> gameManager, int turn)
 {
     if (showStart && gameTurn == Constants.CARDS_IN_DECK)
     {
         showStart = false;
         //gameManager.addTooltip(gameManager.getPlayer(0), "Battle start!");
     }
     if (gameTurn < Constants.CARDS_IN_DECK)
     {
         DraftTurn(gameManager);
         return(false);
     }
     else
     {
         return(GameTurn(gameManager));
     }
 }
        private bool GameTurn(MultiplayerGameManager <Player> gameManager)
        {
            Player sdkplayer = gameManager.getPlayer(gamePlayer);

            gameManager.setFrameDuration(Constants.FRAME_DURATION_BATTLE);

            if (state == null) // frame-only turn for showing the initial state
            {
                draft.ShuffleDecks();
                if (Constants.VERBOSE_LEVEL > 1)
                {
                    Console.WriteLine("   Decks shuffled.");
                }
                if (Constants.VERBOSE_LEVEL > 1)
                {
                    Console.WriteLine("   Game phase");
                }
                state = new GameState(draft);

                gameManager.setTurnMaxTime(1); // weird try but works ^^
                sdkplayer.execute();

                return(false);
            }

            if (actionsToHandle.Count > 0)     // there is a legal action on top of the list
            {
                gameManager.setTurnMaxTime(1); // weird try but works ^^
                sdkplayer.execute();

                Action a = actionsToHandle[0];
                actionsToHandle.RemoveAt(0);
                gameManager.addToGameSummary("Player " + sdkplayer.getNicknameToken() + " performed action: " + a.toStringNoText());

                state.AdvanceState(a);
                if (a.type == Action.Type.SUMMON)
                {
                    gameManager.setFrameDuration(Constants.FRAME_DURATION_SUMMON);
                }
            }
            else // it's time to actually call a player
            {
                if (Constants.VERBOSE_LEVEL > 2)
                {
                    Console.Write("      Game turn " + (gameTurn - Constants.CARDS_IN_DECK) + ", player " + gamePlayer);
                }

                gameManager.setTurnMaxTime(gameTurn <= Constants.CARDS_IN_DECK + 1 ? Constants.TIMELIMIT_FIRSTGAMETURN : Constants.TIMELIMIT_GAMETURN);

                state.AdvanceState();

                foreach (string line in state.getPlayersInput())
                {
                    sdkplayer.sendInputLine(line);
                }
                foreach (string line in state.getCardsInput())
                {
                    sdkplayer.sendInputLine(line);
                }
                sdkplayer.execute();

                try
                {
                    string output = sdkplayer.getOutputs()[0];
                    actionsToHandle = Action.parseSequence(output);
                    if (Constants.VERBOSE_LEVEL > 2)
                    {
                        Console.WriteLine(" (returned " + actionsToHandle.Count + " actions)");
                    }
                }
                catch (InvalidActionHard e)
                {
                    HandleError(gameManager, sdkplayer, sdkplayer.getNicknameToken() + ": " + e.Message);
                }
                catch (TimeoutException e)
                {
                    HandleError(gameManager, sdkplayer, sdkplayer.getNicknameToken() + " timeout!");
                }
            }

            // now we roll-out actions until next legal is found
            List <Action> legals         = state.computeLegalActions(); //Console.WriteLine(gameTurn + " "+ state.players[state.currentPlayer].currentMana +"/"+state.players[state.currentPlayer].maxMana + "->"+legals);
            int           illegalActions = 0;

            while (actionsToHandle.Count > 0)
            {
                Action a = actionsToHandle[0];
                if (a.type == Action.Type.PASS)
                {
                    actionsToHandle.RemoveAt(0); // pop
                    continue;
                }
                if (legals.Contains(a))
                {
                    break;
                }
                actionsToHandle.RemoveAt(0); // pop
                illegalActions++;
                if (illegalActions <= ILLEGAL_ACTION_SUMMARY_LIMIT)
                {
                    gameManager.addToGameSummary("[Warning] " + sdkplayer.getNicknameToken() + " Action is not legal: " + a.ToString());
                }
            }
            if (illegalActions > ILLEGAL_ACTION_SUMMARY_LIMIT)
            {
                gameManager.addToGameSummary("[Warning] " + sdkplayer.getNicknameToken() + " Performed another " + (illegalActions - ILLEGAL_ACTION_SUMMARY_LIMIT) + " illegalActions");
            }

            if (CheckAndHandleEndgame(gameManager, state))
            {
                return(true);
            }

            if (actionsToHandle.Count == 0) // player change
            {
                gameTurn++;
                gamePlayer = (gamePlayer + 1) % 2;
            }

            return(false);
        }
Beispiel #8
0
        public RefereeParams(MultiplayerGameManager <Player> gameManager)
        {
            // pure initialization if seed set by the manager
            long mainSeed = gameManager.getSeed();

            _params = gameManager.getGameParameters();

            Random RNG = new Random((int)mainSeed);

            if (long.TryParse(_params.getProperty("seed", ((int)DateTime.Now.Ticks).ToString()), out long val)) // overriding when seed given as parameter
            {
                mainSeed = val;
                RNG      = new Random((int)mainSeed);
            }
            long draftChoicesSeed   = RNG.Next();
            long shufflePlayer0Seed = RNG.Next();
            long shufflePlayer1Seed = RNG.Next();

            // overriding remaining seeds

            if (long.TryParse(_params.getProperty("draftChoicesSeed", ""), out val))
            {
                draftChoicesSeed = val;
            }
            if (long.TryParse(_params.getProperty("shufflePlayer0Seed", ""), out val))
            {
                shufflePlayer0Seed = val;
            }
            if (long.TryParse(_params.getProperty("shufflePlayer1Seed", ""), out val))
            {
                shufflePlayer1Seed = val;
            }

            if (_params.getProperty("predefinedDraftIds", "") != "")
            {
                predefinedDraftIds = new int[Constants.CARDS_IN_DECK, 3];
                string[] picks = _params.getProperty("predefinedDraftIds", "").Split(",");

                //assert(picks.Length >= Constants.CARDS_IN_DECK);

                for (int pick = 0; pick < Constants.CARDS_IN_DECK; pick++)
                {
                    string[] choice = picks[pick].Trim().Split("\\s+");
                    for (int i = 0; i < 3; i++)
                    {
                        predefinedDraftIds[pick, i] = int.Parse(choice[i].Trim());
                    }
                }
            }

            // update params values
            // we can't update predefinedDraftIds if there were not set by the user...
            _params.Add("seed", mainSeed.ToString());
            _params.Add("draftChoicesSeed", draftChoicesSeed.ToString());
            _params.Add("shufflePlayer0Seed", shufflePlayer0Seed.ToString());
            _params.Add("shufflePlayer1Seed", shufflePlayer1Seed.ToString());

            // set RNG's
            draftChoicesRNG   = new Random((int)draftChoicesSeed);
            shufflePlayer0RNG = new Random((int)shufflePlayer0Seed);
            shufflePlayer1RNG = new Random((int)shufflePlayer1Seed);

            //Console.WriteLine(ToString());
        }