private static bool ReadCard(string gameString, ref int curPos, ref string error, DealerAction parsedAction, DeckDescriptor deckDescr) { int card; int nextSpace = gameString.IndexOf(' ', curPos); int cardLength = nextSpace == -1 ? gameString.Length - curPos : nextSpace - curPos; string textCard = gameString.Substring(curPos, cardLength); if (deckDescr == null) { if (!int.TryParse(textCard, out card)) { return(Error(out error, String.Format("Wrong card {0}", textCard))); } } else { card = deckDescr.GetIndex(textCard); } parsedAction.Card = card; curPos += cardLength; return(true); }
static bool ParseActions(string gameString, List <IStrategicAction> actions, out string error, object parameters) { DeckDescriptor deckDescr = parameters as DeckDescriptor; error = ""; int startPos = 0; char curChar; for (; startPos < gameString.Length;) { // Skip whitespace startPos = SkipWhiteSpace(gameString, startPos); if (startPos == gameString.Length) { break; } curChar = gameString[startPos + 1]; bool isDealer = false; switch (curChar) { case 'd': isDealer = true; break; case 'p': isDealer = false; break; default: return(Error(out error, "Wrong action character: " + curChar.ToString())); } if (!isDealer) { PlayerAction parsedAction = new PlayerAction(); int pos; if (!ParsePlayerPosition(ref error, gameString.Substring(startPos, 1), out pos)) { return(false); } parsedAction.Position = pos; startPos += 2; double amount; if (!ReadAmount(gameString, ref startPos, ref error, out amount)) { return(false); } parsedAction.Amount = amount; actions.Add(parsedAction); } else { DealerAction parsedAction = new DealerAction(); int pos; if (!ParsePlayerPosition(ref error, gameString.Substring(startPos, 1), out pos)) { return(false); } parsedAction.Position = pos; startPos += 2; if (!ReadCard(gameString, ref startPos, ref error, parsedAction, deckDescr)) { return(false); } actions.Add(parsedAction); } } return(true); }