views:

246

answers:

2

Hi, i have a tricky problem and seems like i'm stuck. I have an idea how to proceed but no idea how to do it in practice.

What i want to do is convert a string inside .txt file to another format (using regex and variables?). The main problem is when i need to convert those lines marked with //comments.

NOTE: "...villainx calls $x" is calculated differently in original and in the format it should be converted to. And that's the problem i need some serious help.

Example: This needs to be converted...

HERO posts small blind $0.50.
villain4 posts big blind $1.00.    
** Dealing down cards **
Dealt to HERO [  7s 8c 5d 8d ]
villain1 calls $1.00
villain2 raises to $3.00 // total sum a player raises to
villain3 calls $3.00
HERO calls $3.00
villain4 calls $3.00
villain1 calls $3.00     // total sum a player calls whether he has put money in to the pot before (as he has -- $1 call, first to act)
** Dealing Flop ** [ 9c, Ah, Jh ]

...to this:

HERO posts small blind [$0.50 USD].
villain4 posts big blind [$1.00 USD].    
** Dealing down cards **
Dealt to HERO [  7s 8c 5d 8d ]
villain1 calls [$1.00 USD]
villain2 raises [$3.00 USD] // total sum a player raises to
villain3 calls [$3.00 USD]
HERO calls [$2.50 USD]      // a sum player calls = last raise ($3) - money put in (=$0.50 small blind)
villain4 calls [$2.00 USD]  // $3 - $1 (big blind)
villain1 calls [$2.00 USD]  // $3 - $1 (the call first to act)
** Dealing Flop ** [ 9c, Ah, Jh ]

Another example:

HERO posts small blind $0.50.
villain4 posts big blind $1.00.    
** Dealing down cards **
Dealt to HERO [  7s 8c 5d 8d ]
villain1 bets $5.50
villain2 raises to $20.00
villain3 raises to $40.00
villain1 calls $40.00 //THIS NEEDS TO BE "calls $34.50"
villain2 calls $40.00 //THIS NEEDS TO BE "calls $20.00"
** Dealing Flop ** [ 9c, Ah, Jh ]

and here's the full example how the whole hand should look. Txt file could contain a hundreds of hands. I've managed to preg_replace basically all other issues but that above. I'm lost. Please help me! :D

***** Hand History for Game 335502358 ***** (Full Tilt)
$100.00 USD PL Omaha - Thursday, October 15, 01:32:21 ET 2009
Table Foxtrot (Real Money)
Seat 3 is the button
Seat 1: villain1 ( $38.50 USD )
Seat 2: villain2 ( $99.65 USD )
Seat 3: villain3 ( $415.55 USD )
Seat 4: HERO ( $99.00 USD )
Seat 6: villain4 ( $171.20 USD )
HERO posts small blind [$0.50 USD].
villain4 posts big blind [$1.00 USD].
** Dealing down cards **
Dealt to HERO [  7s 8c 5d 8d ]
villain1 calls [$1.00 USD]
villain2 raises [$3.00 USD]
villain3 calls [$3.00 USD]
HERO calls [$2.50 USD]
villain4 calls [$2.00 USD]
villain1 calls [$2.00 USD]
** Dealing Flop ** [ 9c, Ah, Jh ]
HERO checks
villain4 checks
villain1 checks
villain2 bets [$8.00 USD]
villain3 folds
HERO folds
villain4 calls [$8.00 USD]
villain1 folds
** Dealing Turn ** [ Th ]
villain4 checks
villain2 bets [$13.00 USD]
villain4 calls [$13.00 USD]
** Dealing River ** [ 3c ]
villain4 checks
villain2 checks
villain2 shows [Qc, Js 8s Qd ]
villain4 shows [Kh, Tc 7h Kd ]
villain4 wins $54.15 USD from main pot

edit 1: added NOTE to clarify my real question

edit 2: added another example

+1  A: 

Could you use a preg_match to pull out the dollar value and re-arrange the string with a preg_replace?

$regex = '/(\$[0-9.]+)/';

$matched = preg_match($regex, $stringToMatch, $matches);

if($matched > 0)
{
  $output string = preg_replace($regex, '['.$matches[0].' USD]', $stringToMatch);
}

The only thing this won't do is ignore the lines at the beginning where you declare each 'seat' so you might need to filter those out first [simple strpos($stringToMatch, 'Seat') might be enough there, not wonderfully elegant though].

MalphasWats
You probably meant to escape the `$`: `/(\$[0-9.]+)/`
Bart Kiers
doh! thanks Bart, I always get escape characters the wrong way!
MalphasWats
no no no... :)the dollar sign "USD" and those stuff isn't the problem.. the problem is how a players call is calculated."player1 bets $1player2 raises to $3player1 calls $3"--- needs to be converted ---"player1 bets $1player2 raises to $3player1 calls $2" // the last raise minus the last bet/raise/call a player1 made before that last raise = $2
mika
ah, I see, I totally didn't get that from the question!You're going to need to write a basic parser to do that. I'll have a think, but anything I manage to come up with is likely to be a bit of a hack!
MalphasWats
A: 

OK, I'll have another go. This will be written in a sort of php/psuedocode thing.

while($line = get the next line)
{
    if($line contains 'seat')
    {
        $player = get player from $line
        $pool = get player pool from $line

        $bettingMatrix[$player]['pool'] = $pool;
    }
    else if($line contains 'blind')
    {
        $player = get player from $line
        $betValue = get blind value from $line

        $bettingMatrix[$player]['betTotal'] = $betValue
        $bettingMatrix['pot'] += $betValue //keep a sum of the pot
    }
    else if($line contains 'raises')
    {
        $player = get player from $line
        $betValue = get value from $line
        $betMade = $betValue - $bettingMatrix[$player]['betTotal']; //actual amount raised by
        $bettingMatrix[$player]['betTotal'] = $betValue //$line contains total bet this hand (shortcut)
        $bettingMatrix['raiseValue'] = $betMade
        $bettingMatrix['pot'] += $betMade //keep a sum of the pot
    }
    else if($line contains 'calls')
    {
        $player = get player from $line
        //if player has called, can work out bet from raiseValue
        $betMade = $bettingMatrix['raiseValue']
        $bettingMatrix[$player]['betTotal'] += $betMade
        $bettingMatrix['pot'] += $betMade //keep a sum of the pot
    }
    else if(substr($line, 0, 3) == ' Wins ') //probably do something about players named Wins :)
    {
        //assume all bets resolved
        foreach($bettingMatrix[$player]) 
        {
            update pool.
            zero betTotal
        }
        zero pot, zero raiseValue
    }
}

erm, it's pretty rough and ready, and I probably wouldn't class it as a Parser as such, but it does just about work out all the values you need, I think anyway. The 2 $betMade variables should end up with the values you want.

Edit: I've just noticed that it doesn't quite work if no-one raises and everyone just calls or folds (and probably dies horribly if everyone just folds or whatever). It does need a little bit more work, but it is the general gist - call it a half-answer. Sorry.

MalphasWats
Thanks for giving this a thought. Potsize and players' stacks aren't the things I need to calculate cause they are always correct in the original and in the one i need it to convert to. Only things to calculate are those "calls/bets" vs. "blinds" vs. "raises" vs. additional "calls". Could it be wise to first check the lines between "*** Dealing Down Cards ***" and "*** Dealing Flop ***" and run some kind of blind/bet/raise/call loop thing you already posted and then "Flop-Turn" and lastly "River-xxx Shows".
mika
if you don't need the stack and pot you can safely ignore those lines. The lines with ** in them aren't too useful, they don't change the state, the important lines are the ones where a player bets, etc because those lines change the system state. The bits you are trying to calculate should be the lines where the $betMade variable is being set. Each time the $betMade variable is calculated, you can output a line reformatted the way you want it, otherwise you can just pass the line straight through. It isn't THE answer, but it gives you a rough algorithm to work with :)
MalphasWats