Esempio n. 1
0
        public void Test_GameString_FromGameDef_Over()
        {
            GameDefinition gameDef = LoadGameDef();

            GameState gs = new GameState(gameDef, 2);

            VerifyGameState(gs, "", false, -1, 1.5, 1, 1, GameState.InvalidPosition, true, 0, 0, 0, false, 2);
            VerifyGameStateMethods(gs, gameDef, new Ak[] { Ak.d });
            VerifyPlayerState(gs.Players[0], "", -0.5, "", 0.5, 0.5, 0, false, false, false, true);
            VerifyPlayerState(gs.Players[1], "", -1, "", 1, 1, 0, false, false, false, true);

            gs.UpdateByAction(PokerAction.d(0, "Ac Ad"), gameDef);
            gs.UpdateByAction(PokerAction.d(1, "2c 3d"), gameDef);

            VerifyPlayerState(gs.Players[0], "", -0.5, "Ac Ad", 0.5, 0.5, 0, false, false, true, true);
            VerifyPlayerState(gs.Players[1], "", -1, "2c 3d", 1, 1, 0, false, false, true, true);

            gs.UpdateByAction(PokerAction.c(0), gameDef);
            gs.UpdateByAction(PokerAction.c(1), gameDef);
            gs.UpdateByAction(PokerAction.d("Ah As Qc"), gameDef);
            gs.UpdateByAction(PokerAction.c(1), gameDef);
            gs.UpdateByAction(PokerAction.c(0), gameDef);
            gs.UpdateByAction(PokerAction.d("Jh"), gameDef);
            gs.UpdateByAction(PokerAction.c(1), gameDef);
            gs.UpdateByAction(PokerAction.c(0), gameDef);
            gs.UpdateByAction(PokerAction.d("Ts"), gameDef);
            gs.UpdateByAction(PokerAction.c(1), gameDef);
            gs.UpdateByAction(PokerAction.c(0), gameDef);
            UInt32[] ranks = new UInt32[] { 2, 1 };
            gs.UpdateByShowdown(ranks, gameDef);
            VerifyGameState(gs, "", true, 3, 2, 0, 0, 0, false, GameState.InvalidPosition, 0, 0, true, 2);
            VerifyGameStateMethods(gs, gameDef, new Ak[] { });
            VerifyPlayerState(gs.Players[0], "", 1, "Ac Ad Ah As Qc Jh Ts", 1, 0, 1, false, false, false, true);
            VerifyPlayerState(gs.Players[1], "", -1, "2c 3d Ah As Qc Jh Ts", 1, 0, -1, false, false, false, true);
        }
        /// <summary>
        /// Deals cards for the next game. Is used if not replaying from game log.
        /// </summary>
        GameRecord DealCards()
        {
            GameRecord dealRecord = new GameRecord();

            int cardsToDealCount = 0;

            for (int r = 0; r < _gameDef.RoundsCount; ++r)
            {
                cardsToDealCount +=
                    (_gameDef.PrivateCardsCount[r] + _gameDef.PublicCardsCount[r]) * _sessionPlayers.Count
                    + _gameDef.SharedCardsCount[r];
            }

            if (cardsToDealCount > 0)
            {
                _dealer.Shuffle(cardsToDealCount);

                int dealt = 0;
                for (int r = 0; r < _gameDef.RoundsCount; ++r)
                {
                    if (_gameDef.PrivateCardsCount[r] > 0)
                    {
                        for (int p = 0; p < _sessionPlayers.Count; ++p)
                        {
                            dealRecord.Actions.Add(PokerAction.d(p, _gameDef.DeckDescr.GetCardNames(
                                                                     _dealer.Sequence, dealt,
                                                                     _gameDef.PrivateCardsCount[r])));
                            dealt += _gameDef.PrivateCardsCount[r];
                        }
                    }

                    if (_gameDef.PublicCardsCount[r] > 0)
                    {
                        for (int p = 0; p < _sessionPlayers.Count; ++p)
                        {
                            dealRecord.Actions.Add(PokerAction.d(p, _gameDef.DeckDescr.GetCardNames(
                                                                     _dealer.Sequence, dealt,
                                                                     _gameDef.PublicCardsCount[r])));
                            dealt += _gameDef.PublicCardsCount[r];
                        }
                    }

                    if (_gameDef.SharedCardsCount[r] > 0)
                    {
                        dealRecord.Actions.Add(PokerAction.d(_gameDef.DeckDescr.GetCardNames(
                                                                 _dealer.Sequence, dealt, _gameDef.SharedCardsCount[r])));
                        dealt += _gameDef.SharedCardsCount[r];
                    }
                }
            }

            return(dealRecord);
        }
Esempio n. 3
0
        void ParseActionsForHe(ServerMessage msg, GameRecord gr)
        {
            string[] privateCards = msg.GetPrivateCards();
            // Reverse positions for our format
            for (int p = 1; p >= 0; --p)
            {
                string cards = privateCards[p];
                if (cards == "")
                {
                    cards = "? ?";
                }
                else
                {
                    cards = CardsFromAcpc(cards);
                }
                PokerAction a = PokerAction.d(1 - p, cards);
                gr.Actions.Add(a);
            }

            for (int r = 0; r <= msg.Round; ++r)
            {
                if (r > 0)
                {
                    PokerAction a = PokerAction.d(CardsFromAcpc(msg.Cards[r]));
                    gr.Actions.Add(a);
                }
                for (int move = 0; move < msg.Betting[r].Length; ++move)
                {
                    PokerAction a        = null;
                    int         position = r == 0 ? move % 2 : 1 - (move % 2);
                    switch (msg.Betting[r][move])
                    {
                    case 'r':
                        a = PokerAction.r(position, r < 2 ? 1 : 2);
                        break;

                    case 'c':
                        a = PokerAction.c(position);
                        break;

                    case 'f':
                        a = PokerAction.f(position);
                        break;
                    }
                    gr.Actions.Add(a);
                }
            }
        }
Esempio n. 4
0
        public void Test_GameString_GameDef_NotOver()
        {
            GameDefinition gameDef = LoadGameDef();

            string gameString = "14; P1{100 .5 0} P2{200 1 0}; 0d{Ac Ad} 1d{Kc Kd};";

            GameState gs = new GameState(gameString, gameDef);

            VerifyGameState(gs, "14", false, 0, 1.5, 1, 1, 1, false, 0, 2, 0, false, 2);
            VerifyGameStateMethods(gs, gameDef, new Ak[] { Ak.f, Ak.c, Ak.r });
            VerifyPlayerState(gs.Players[0], "P1", 99.5, "Ac Ad", 0.5, 0.5, 0, false, false, true, true);
            VerifyPlayerState(gs.Players[1], "P2", 199, "Kc Kd", 1, 1, 0, false, false, true, true);

            gs.UpdateByAction(PokerAction.c(0), gameDef);
            VerifyGameState(gs, "14", false, 0, 2, 1, 1, 0, false, 1, 0, 0, false, 2);
            VerifyGameStateMethods(gs, gameDef, new Ak[] { Ak.c, Ak.r });
            VerifyPlayerState(gs.Players[0], "P1", 99, "Ac Ad", 1, 1, 0, false, false, false, true);
            VerifyPlayerState(gs.Players[1], "P2", 199, "Kc Kd", 1, 1, 0, false, false, true, true);

            gs.UpdateByAction(PokerAction.c(1), gameDef);
            VerifyGameState(gs, "14", false, 0, 2, 1, 1, 1, true, GameState.DealerPosition, 0, 0, false, 2);
            VerifyGameStateMethods(gs, gameDef, new Ak[] { Ak.d });
            VerifyPlayerState(gs.Players[0], "P1", 99, "Ac Ad", 1, 1, 0, false, false, false, true);
            VerifyPlayerState(gs.Players[1], "P2", 199, "Kc Kd", 1, 1, 0, false, false, false, true);

            gs.UpdateByAction(PokerAction.d("As 2d 4d"), gameDef);
            VerifyGameState(gs, "14", false, 1, 2, 0, 0, GameState.DealerPosition, false, 1, 1, 0, false, 2);
            VerifyGameStateMethods(gs, gameDef, new Ak[] { Ak.c, Ak.r });
            VerifyPlayerState(gs.Players[0], "P1", 99, "Ac Ad As 2d 4d", 1, 0, 0, false, false, true, true);
            VerifyPlayerState(gs.Players[1], "P2", 199, "Kc Kd As 2d 4d", 1, 0, 0, false, false, true, true);

            gs.UpdateByAction(PokerAction.r(1, gameDef.BetStructure[1]), gameDef);
            VerifyGameState(gs, "14", false, 1, 3, 1, 1, 1, false, 0, 0, 0, false, 2);
            VerifyGameStateMethods(gs, gameDef, new Ak[] { Ak.f, Ak.c, Ak.r });
            VerifyPlayerState(gs.Players[0], "P1", 99, "Ac Ad As 2d 4d", 1, 0, 0, false, false, true, true);
            VerifyPlayerState(gs.Players[1], "P2", 198, "Kc Kd As 2d 4d", 2, 1, 0, false, false, false, true);

            gs.UpdateByAction(PokerAction.f(0), gameDef);
            VerifyGameState(gs, "14", true, 1, 3, 1, 1, 0, false, GameState.InvalidPosition, 0, 1, false, 2);
            VerifyGameStateMethods(gs, gameDef, new Ak[] {});
            VerifyPlayerState(gs.Players[0], "P1", 99, "Ac Ad As 2d 4d", 1, 0, -1, true, false, false, false);
            VerifyPlayerState(gs.Players[1], "P2", 201, "Kc Kd As 2d 4d", 2, 1, 1, false, false, false, true);
        }
Esempio n. 5
0
        public void Test_GameString_NoGameDef_NotOver()
        {
            // As we do not have GameDef, CurrentActor == invalid and IsDealerAction is false.

            string gameString = "11;P1{100 5 0} P2{200 10 0}; 0d{Ah Ad} 1d{Kh Kd} 0r13 1r10;";

            GameState gs = new GameState(gameString, null);

            VerifyGameState(gs, "11", false, 0, 56, 33, 3, 1, false, GameState.InvalidPosition, 0, 0, false, 2);
            VerifyPlayerState(gs.Players[0], "P1", 77, "Ah Ad", 23, 23, 0, false, false, true, true);
            VerifyPlayerState(gs.Players[1], "P2", 167, "Kh Kd", 33, 33, 0, false, false, false, true);

            gs.UpdateByAction(PokerAction.c(0), null);
            VerifyGameState(gs, "11", false, 0, 66, 33, 3, 0, true, GameState.InvalidPosition, 0, 0, false, 2);
            VerifyPlayerState(gs.Players[0], "P1", 67, "Ah Ad", 33, 33, 0, false, false, false, true);
            VerifyPlayerState(gs.Players[1], "P2", 167, "Kh Kd", 33, 33, 0, false, false, false, true);


            gs.UpdateByAction(PokerAction.d("As 2d 4d"), null);

            VerifyGameState(gs, "11", false, 1, 66, 0, 0,
                            GameState.DealerPosition, false, GameState.InvalidPosition, 1, 0, false, 2);
            VerifyPlayerState(gs.Players[0], "P1", 67, "Ah Ad As 2d 4d", 33, 0, 0, false, false, true, true);
            VerifyPlayerState(gs.Players[1], "P2", 167, "Kh Kd As 2d 4d", 33, 0, 0, false, false, true, true);


            gs.UpdateByAction(PokerAction.r(1, 15), null);
            VerifyGameState(gs, "11", false, 1, 81, 15, 1,
                            1, false, GameState.InvalidPosition, 0, 0, false, 2);
            VerifyPlayerState(gs.Players[0], "P1", 67, "Ah Ad As 2d 4d", 33, 0, 0, false, false, true, true);
            VerifyPlayerState(gs.Players[1], "P2", 152, "Kh Kd As 2d 4d", 48, 15, 0, false, false, false, true);

            gs.UpdateByAction(PokerAction.f(0), null);
            VerifyGameState(gs, "11", true, 1, 81, 15, 1,
                            0, false, GameState.InvalidPosition, 0, 1, false, 2);
            VerifyPlayerState(gs.Players[0], "P1", 67, "Ah Ad As 2d 4d", 33, 0, -33, true, false, false, false);
            VerifyPlayerState(gs.Players[1], "P2", 233, "Kh Kd As 2d 4d", 48, 15, 33, false, false, false, true);
        }
        /// <summary>
        /// Parses Open CFG format to PokerAction list. Converts cards to indexes for both abstracted
        /// and non-abstracted games.
        /// </summary>
        List <PokerAction> ParseActionString(string key, out int round)
        {
            List <PokerAction> actions = new List <PokerAction>();

            actions.Add(new PokerAction());
            string[] parts = key.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length != 2)
            {
                throw new ApplicationException("Wrong format");
            }
            string pocket = parts[0].Substring(0, 1);
            string board  = parts[0].Length == 1 ? null : parts[0].Substring(1, 1);

            // Add two deals, as this is required to update game state.
            // One deal will be ignored later in the conversion to a strategic string.
            actions.Add(PokerAction.d(0, GameDef.DeckDescr.GetIndex(pocket).ToString()));
            actions.Add(PokerAction.d(1, GameDef.DeckDescr.GetIndex(pocket).ToString()));

            round = 0;
            int pos = 0;

            foreach (char actChar in parts[1].Substring(1))
            {
                switch (actChar)
                {
                case 'r':
                    actions.Add(PokerAction.r(pos, GetRaiseAmount(round)));
                    pos = 1 - pos;
                    break;

                case 'c':
                    actions.Add(PokerAction.c(pos));
                    pos = 1 - pos;
                    break;

                case '/':
                    actions.Add(PokerAction.d(GameDef.DeckDescr.GetIndex(board).ToString()));
                    round++;
                    pos = 0;
                    break;

                default:
                    throw new ApplicationException("Wrong format");
                }
            }
            if (ChanceAbstraction != null)
            {
                int[] hand = new int[2];
                round = -1;
                for (int i = 0; i < actions.Count; ++i)
                {
                    PokerAction a = actions[i];
                    if (a.Kind == Ak.d && a.Position <= 0)
                    {
                        ++round;
                        hand[round] = int.Parse(a.Cards);
                    }
                    if (a.IsDealerAction() && a.Cards != "")
                    {
                        int bucket = ChanceAbstraction.GetAbstractCard(hand, round + 1);
                        a.Cards = bucket.ToString();
                    }
                }
            }

            return(actions);
        }
Esempio n. 7
0
        public void Test_HasPlayerActed()
        {
            GameDefinition gameDef = LoadGameDef();
            GameState      gs      = new GameState(gameDef, 2);

            Assert.IsFalse(gs.HasPlayerActed(0));
            Assert.IsFalse(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.d(0, "Ac Ad"), gameDef);
            Assert.IsFalse(gs.HasPlayerActed(0));
            Assert.IsFalse(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.d(1, "2c 3d"), gameDef);
            Assert.IsFalse(gs.HasPlayerActed(0));
            Assert.IsFalse(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.c(0), gameDef);
            Assert.IsTrue(gs.HasPlayerActed(0));
            Assert.IsFalse(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.c(1), gameDef);
            Assert.IsFalse(gs.HasPlayerActed(0));
            Assert.IsTrue(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.d("Ah As Qc"), gameDef);
            Assert.IsFalse(gs.HasPlayerActed(0));
            Assert.IsFalse(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.c(1), gameDef);
            Assert.IsFalse(gs.HasPlayerActed(0));
            Assert.IsTrue(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.r(0, 1), gameDef);
            Assert.IsTrue(gs.HasPlayerActed(0));
            Assert.IsFalse(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.c(1), gameDef);
            Assert.IsFalse(gs.HasPlayerActed(0));
            Assert.IsTrue(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.d("Jh"), gameDef);
            Assert.IsFalse(gs.HasPlayerActed(0));
            Assert.IsFalse(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.c(1), gameDef);
            Assert.IsFalse(gs.HasPlayerActed(0));
            Assert.IsTrue(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.c(0), gameDef);
            Assert.IsTrue(gs.HasPlayerActed(0));
            Assert.IsFalse(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.d("Ts"), gameDef);
            Assert.IsFalse(gs.HasPlayerActed(0));
            Assert.IsFalse(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.c(1), gameDef);
            Assert.IsFalse(gs.HasPlayerActed(0));
            Assert.IsTrue(gs.HasPlayerActed(1));

            gs.UpdateByAction(PokerAction.c(0), gameDef);
            Assert.IsTrue(gs.HasPlayerActed(0));
            Assert.IsFalse(gs.HasPlayerActed(1));
            Assert.IsTrue(gs.IsGameOver);
        }
Esempio n. 8
0
        private static void ProcessGameContext(GameContext context)
        {
            int            nodeId = context.Global.NodeId++;
            GameDefinition gd     = context.Global.GameDef;
            ChanceTree     tree   = context.Global.Tree;

            while (!context.GameState.IsGameOver && !context.GameState.IsDealerActing)
            {
                context.GameState.UpdateByAction(PokerAction.c(context.GameState.CurrentActor), gd);
            }

            if (tree != null)
            {
                tree.SetDepth(nodeId, (byte)context.Depth);
                int position = context.GameState.LastActor;
                if (context.Deal != null && context.Deal.SharedCardsPlayer != -1)
                {
                    position = context.Deal.SharedCardsPlayer;
                }
                if (nodeId == 0)
                {
                    position = gd.MinPlayers;
                }
                tree.Nodes[nodeId].Position = position;
                if (context.Deal == null)
                {
                    tree.Nodes[nodeId].Card = -1;
                }
                else
                {
                    // This double-conversion assures that for decks with duplicates
                    // the same index
                    int stableCardIdx = gd.DeckDescr.GetIndex(gd.DeckDescr.CardNames[context.Deal.CardIdx]);
                    tree.Nodes[nodeId].Card = stableCardIdx;
                }
            }

            if (context.GameState.IsGameOver)
            {
                context.CombCountOfLeaves = context.DealCount;
                if (tree != null)
                {
                    tree.Nodes[nodeId].Probab = (double)context.CombCountOfLeaves / context.Global.TotalCombCountOfLeaves;
                    int     playersCount = gd.MinPlayers;
                    uint[]  ranks        = new uint[playersCount];
                    int[][] hands        = new int[playersCount][];
                    for (int p = 0; p < playersCount; ++p)
                    {
                        hands[p] = gd.DeckDescr.GetIndexes(context.GameState.Players[p].Hand);
                    }
                    gd.GameRules.Showdown(gd, hands, ranks);
                    context.GameState.UpdateByShowdown(ranks, gd);
                    double[] potShares = new double[gd.MinPlayers];
                    for (int p = 0; p < playersCount; ++p)
                    {
                        potShares[p] = (context.GameState.Players[p].Result + context.GameState.Players[p].InPot)
                                       / context.GameState.Pot;
                    }
                    tree.Nodes[nodeId].SetPotShare(context.GameState.GetActivePlayers(), potShares);
                }
                return;
            }

            // Deal next cards.
            List <DealT> deals = DealCards(context);

            foreach (DealT d in deals)
            {
                GameContext childContext = new GameContext(context);
                childContext.Depth++;

                childContext.DealtCards.UnionWith(gd.DeckDescr.CardSets[d.CardIdx]);
                if (d.SharedCardsPlayer == -1)
                {
                    // Non-shared deal - update game state and deal count
                    PokerAction a = PokerAction.d(context.GameState.CurrentActor, d.CardName);
                    childContext.GameState.UpdateByAction(a, gd);
                    childContext.DealCount *= d.Count;
                }
                else if (d.SharedCardsPlayer == gd.MinPlayers - 1)
                {
                    // Shared deal for last player - update game state and deal count once for all.
                    PokerAction a = PokerAction.d(-1, context.Deal.CardName);
                    childContext.GameState.UpdateByAction(a, gd);
                    childContext.DealCount *= d.Count;
                }
                childContext.Deal = d;
                ProcessGameContext(childContext);
                context.CombCountOfLeaves += childContext.CombCountOfLeaves;
                context.ChildCount        += childContext.ChildCount + 1;
            }
            if (tree != null)
            {
                tree.Nodes[nodeId].Probab = (double)context.CombCountOfLeaves / context.Global.TotalCombCountOfLeaves;
            }
        }