Designing a poker parker in Ruby

I am writing a small Ruby program to parse a hand history log from a poker site. The log is split into several lines and looks something like this:

Table 123456 NL Hold'em $1/$2
5 Players
Seat 3 is the button
Seat 1: randomGuy112 $152.56
Seat 2: randomGirl99 $200
Seat 3: PokerPro $357.12
Seat 4: FishCake556 $57.19
Seat 6: MooMoo $188.98
Dealt to MooMoo [Ah, Ks]
randomGuy112 folds
randomGirl99 raises to $7

etc.. etc..

      

I want to summarize this information in an object, which then could, for example, make it differently or store it in the database. When I initially thought about this problem, I thought that I would only have one real straightforward class with multiple regexes and multiple if / else statements. Then I realized that this could turn into a fairly large method and potentially become a debugging / maintainability nightmare. Keep in mind that to collect player actions, you need to loop through each stage of the game (preflop, flop, etc.).

I also want to tackle this with a TDD approach, but the "one long method" method means that tests with subsequent input validation will rely on earlier tests.

I'm new to Ruby and haven't clicked on "Ruby way" to do anything yet. I catch myself writing C # code in another language.

Can you give me some pointers on how to create a parser so that there is no huge mess of if / else statements and is more testable?

+2


source to share


7 replies


State Machine, anyone?

At any point in the game, a poker team has a well-defined set of possible follow-up actions. I think you can encapsulate them in a state machine. There are several, among which (no recommendations, I'm afraid - not enough experience with anyone)



+1


source


Use Treetop

It looks like you are on the borderline between what is appropriate for a suitable string and RE, and what an actual parser is for.

There is nothing wrong with handwritten parsers, and as long as you keep your methods short without much complexity in any of them, having as many operators if

as the parser requires is sufficient .



I'm not sure if 10 lines of incomprehensible regular expressions would be better than 30 lines of pretty code.

Ruby now has an improved PEG parser generator. I think that in this case, I wouldn't worry about whether it was overkill, I would just go ahead and use Treetop.

+2


source


You can check out this open source poker parser

It looks like they created a regex hash and then they are probably iterating over the regex data structures. It is a simpler machine than a parser and probably a lighter approach.

+1


source


I wrote a manual parser for the PokerStars log files https://github.com/malikbakt/pokerstars

+1


source


You can look at: StringScanner .

0


source


I have two different pointers for you that will point you to a solution for how to write code in ruby โ€‹โ€‹mode.

  • Receive the ruby โ€‹โ€‹book. The ruby โ€‹โ€‹book will have many examples of how to write code in ruby โ€‹โ€‹mode. From my personal experience I can recommend you pixake (is it spelled correctly?) Book: http://www.ruby-doc.org/docs/ProgrammingRuby/html/index.html
  • Read existing ruby โ€‹โ€‹code. You seem to know that a ruby โ€‹โ€‹is enough to write your code? Then you can certainly read the existing code. I assume you have already installed ruby โ€‹โ€‹on your system. If so, you will find a lot of source code on your hard drive. If you don't just use the internet.
0


source


I would recommend the book "Refactoring" by Martin Fowler (available in both dead and electronic formats, IIRC). It covers object oriented remedies for the kind of design challenges you're asking for, all in a test driven context. This is one of those books that everyone in the profession should read.

0


source







All Articles