Exemple #1
0
 public PokerAction(PokerAction other)
 {
     Kind     = other.Kind;
     Position = other.Position;
     Amount   = other.Amount;
     Cards    = other.Cards;
 }
Exemple #2
0
        public void UpdateByAction(PokerAction action, GameDefinition gameDef)
        {
            if (action.Kind == Ak.b)
            {
                // Ignore this, can occur in strategy algorithms in the beginning of the game.
                return;
            }

            Debug.Assert(action.Position <= Players.Length);

            LastActor = action.Position;
            switch (action.Kind)
            {
            case Ak.d:
                if (DealsCount == 0)
                {
                    OnFirstDeal(gameDef);
                }
                DealsCount++;
                if (action.Position >= 0)
                {
                    Players[action.Position].Hand = AddCards(Players[action.Position].Hand, action.Cards);
                }
                else
                {
                    // Shared cards
                    foreach (PlayerState player in Players)
                    {
                        if (!player.IsFolded)
                        {
                            player.Hand = AddCards(player.Hand, action.Cards);
                        }
                    }
                    LastActor = GameState.DealerPosition;
                }
                break;

            case Ak.f:
                ProcessFold(action.Position);
                break;

            case Ak.c:
                ProcessCall(action.Position, action.Amount, gameDef);
                break;

            case Ak.r:
                ProcessRaise(action.Position, action.Amount);
                break;

            default:
                throw new ApplicationException("Unknown action: " + action.ToGameString());
            }
            SetNextActor(gameDef);
        }
Exemple #3
0
 public bool Equals(PokerAction o)
 {
     if (o == null)
     {
         return(false);
     }
     return(Kind == o.Kind &&
            Position == o.Position &&
            Amount == o.Amount &&
            Cards == o.Cards);
 }
 private static bool VerifyAndAddAction(PokerAction parsedAction, List <PokerAction> actions, ref string error)
 {
     try
     {
         parsedAction.Verify();
     }
     catch (Exception e)
     {
         error = e.ToString();
         return(false);
     }
     actions.Add(parsedAction);
     return(true);
 }
Exemple #5
0
        public override bool Equals(object obj)
        {
            PokerAction o = obj as PokerAction;

            return(Equals(o));
        }
        /// <summary>
        /// Parses actions is a string in game string format.
        /// </summary>
        /// <param name="gameString">string of actions</param>
        /// <param name="startPos">starting position in gameString</param>
        /// <param name="playerCount">Number of players to verify actions.</param>
        /// <param name="actions">List to put parsed actions to</param>
        /// <param name="error">Error message</param>
        /// <param name="terminator">Terminator character, if found or '?'</param>
        /// <returns>True if success.</returns>
        internal static bool ParseActions(string gameString, int startPos, int playerCount, List <PokerAction> actions, ref string error, out char terminator)
        {
            char curChar;

            terminator = '?';
            for (; startPos < gameString.Length;)
            {
                // Skip whitespace
                startPos = SkipWhiteSpace(gameString, startPos);

                int nextPos = ReadActionBeginOrTerminator(gameString, startPos);
                curChar = gameString[nextPos - 1];
                if (curChar == ';' || curChar == '.')
                {
                    // Terminator.
                    if (startPos + 1 < nextPos)
                    {
                        return(Error(out error, "Wrong text before terminator: " + gameString.Substring(startPos)));
                    }
                    terminator = curChar;
                    return(true);
                }
                PokerAction parsedAction = new PokerAction();
                Ak          kind;
                if (!PokerAction.AkFromChar(curChar, out kind))
                {
                    return(Error(out error, "Wrong character: " + curChar.ToString()));
                }
                parsedAction.Kind = kind;
                switch (parsedAction.Kind)
                {
                case Ak.r:
                case Ak.c:
                case Ak.f:
                    if (!ParsePlayerPosition(ref error, gameString.Substring(startPos, nextPos - startPos - 1), playerCount, parsedAction))
                    {
                        return(false);
                    }
                    startPos = nextPos;
                    bool   amountFound;
                    double amount;
                    if (!ReadAmount(gameString, ref startPos, ref error, out amountFound, out amount))
                    {
                        return(false);
                    }
                    if (amountFound)
                    {
                        if (parsedAction.Kind == Ak.f)
                        {
                            return(Error(out error, "No amount of fold is allowed."));
                        }
                        parsedAction.Amount = amount;
                    }
                    else if (parsedAction.Kind == Ak.r)
                    {
                        return(Error(out error, "Amount of raise is required"));
                    }
                    if (!VerifyAndAddAction(parsedAction, actions, ref error))
                    {
                        return(false);
                    }
                    continue;

                case Ak.d:
                    if (!ParsePlayerPosition(ref error, gameString.Substring(startPos, nextPos - startPos - 1), playerCount, parsedAction))
                    {
                        return(false);
                    }
                    startPos = nextPos;
                    if (!ReadCards(gameString, ref startPos, ref error, parsedAction))
                    {
                        return(false);
                    }
                    if (!VerifyAndAddAction(parsedAction, actions, ref error))
                    {
                        return(false);
                    }
                    continue;

                case Ak.b:
                    // This can be in calls from other classes that parse action lists.
                    startPos = nextPos;
                    if (!VerifyAndAddAction(parsedAction, actions, ref error))
                    {
                        return(false);
                    }
                    break;

                default:
                    throw new ApplicationException("Don't know how to parse action kind: " + parsedAction.Kind.ToString());
                }
            }
            return(true);
        }
        private static bool ParsePlayerPosition(ref string error, string text, int playerCount, PokerAction action)
        {
            if (text.Length == 0)
            {
                action.Position = -1;
                return(true);
            }

            try
            {
                // This is much faster as parsing with the .Net functions.
                action.Position = 0;
                for (int i = 0; i < text.Length; ++i)
                {
                    int digit = text[i] - '0';
                    if (digit < 0 || digit > 9)
                    {
                        return(Error(out error, String.Format("Wrong character in number: {0}", text)));
                    }
                    action.Position = action.Position * 10 + digit;
                }
            }
            catch (Exception)
            {
                return(Error(out error, String.Format("Wrong player position: {1}", text)));
            }
            if (action.Position >= playerCount)
            {
                return(Error(out error, "Player position out of range number of players: " + text));
            }
            return(true);
        }
        private static bool ReadCards(string gameString, ref int curPos, ref string error, PokerAction parsedAction)
        {
            curPos = SkipWhiteSpace(gameString, curPos);
            int nextPos;

            for (nextPos = curPos; nextPos < gameString.Length; ++nextPos)
            {
                switch (gameString[nextPos])
                {
                case '{':
                    goto leftBraceFound;
                }
            }
            return(Error(out error, "{ before the card list not found"));

leftBraceFound:
            curPos = nextPos + 1;

            for (nextPos = curPos; nextPos < gameString.Length; ++nextPos)
            {
                switch (gameString[nextPos])
                {
                case '}':
                    goto rightBraceFound;
                }
            }
            return(Error(out error, "} after the card list not found"));

rightBraceFound:
            parsedAction.Cards = gameString.Substring(curPos, nextPos - curPos);
            // Allow empty card lists, this is required for strategy algorithms.
            curPos = nextPos + 1;
            return(true);
        }