2009-11-03 151 views
0

比較變量我有一個棘手的問題,似乎就像我卡住了。我有一個想法如何繼續,但不知道如何在實踐中做到這一點。PHP正則表達式和字符串

我想要做什麼是轉換.txt文件爲另一種格式中的字符串(使用正則表達式和變量?)。主要問題是當我需要轉換那些標有//註釋的行時。

注意:「... villainx調用$ x」的計算方式在原始方式和格式上應該轉換爲不同的格式。這就是我需要一些嚴重幫助的問題。

舉例: 這需要轉換...

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 ] 

...這樣的:

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 ] 

又如:

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 ] 

和這裏的完整的例子整隻手應該看起來如何。 txt文件可能包含數百手。我設法基本上除了上面提到的所有其他問題之外,我迷路了。請幫幫我! :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 

編輯1:補充說明,以澄清

編輯2我真正的問題:加入另一個例子

+0

我認爲你需要更具體一點,你希望達到的目標。新值如何計算?舉例來說,你怎麼知道villain3的通話保持爲3.00美元,但隨後的通話是否改變? – Ben 2009-11-03 13:21:04

回答

1

你可以使用的preg_match拉出美元價值和重新安排串用preg_replace?

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

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

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

這不會做的唯一的事情就是忽略開頭,你聲明每個「座位」,所以你可能需要篩選出那些第[簡單strpos($ stringToMatch,「座位」)可能行就夠了,雖然不是很高雅]。

+0

你可能是想逃避'$':'/(\ $ [0-9。] +)/' – 2009-11-03 12:42:11

+0

doh!感謝巴特,我總是以錯誤的方式獲得轉義字符! – MalphasWats 2009-11-03 12:49:31

+0

沒有沒有沒有... :) 美元符號「美元」,而那些東西是沒問題的..問題是一個玩家大呼如何計算。 「PLAYER1下注$ 1 player2加註到$ 3 PLAYER1調用$ 3」 ---需要轉換--- 「PLAYER1下注$ 1 player2加註到$ 3 PLAYER1調用$ 2」 //最後加薪減去最後的賭注/ raise/call一個player1之前最後一次加註= $ 2 – mika 2009-11-03 13:06:21

0

好吧,我有另一個去。這將寫在一種PHP/psuedocode的東西。

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 
    } 
} 

呃,這是相當粗糙和準備好了,我可能不會階級作爲一個解析器正因爲如此,但它確實只是工作,你需要的所有值,我想反正。 2 $ betMade變量應該以您想要的值結尾。

編輯:我剛剛注意到,如果沒有人提出,每個人都只是調用或摺疊(也可能死可怕,如果大家只是摺疊或其他),它完全不是那麼回事。它確實需要更多的工作,但它是一般的要點 - 將其稱爲半答案。抱歉。

+0

感謝您給這個想法。 Potsize和玩家的籌碼並不是我需要計算的東西,因爲他們在原始籌碼中始終是正確的,在我需要轉換的籌碼中也是如此。只有要計算的是那些「呼叫/投注」與「盲注」與「加註」對比的附加「呼叫」。首先檢查「***交易卡***」和「***交易觸發器***」之間的界限並運行您已經發布的某種盲/下注/加註/調用循環,這會是明智之舉嗎?然後「翻轉」,最後是「River-xxx Shows」。 – mika 2009-11-04 13:48:44

+0

如果你不需要堆棧和鍋,你可以放心地忽略這些線。帶有**的行不太有用,它們不會改變狀態,重要的行是玩家下注的行等,因爲這些行會改變系統狀態。您試圖計算的位應該是$ betMade變量被設置的行。每次計算$ betMade變量時,都可以按照需要重新格式化輸出行,否則可以直接通過該行。這不是答案,但它給你一個粗略的算法來處理:) – MalphasWats 2009-11-04 14:48:25