Before going into PHP & MySQL, let me story a bit. This is a problem of Texas Holdem game project I am working on, and of course you don’t need a math major.
We have 2 players now at the moment, Player A and player B.
A has total bet of $100
B has total bet of $80
According to Texas Holdem betting rules, whenever there is a different in total bet amount, a side pot is created.
So, our example above will creat a side-pot of $20, with main-pot of $80 + $80 = $160
Main-Pot = $160, contributed by both A & B
Side-Pot = $20, contributed by A only
If Player B win the hand, he will take back $160(the main-pot), Player A will take back $20 since he is the only one has the right of the side-pot.
This is easy.
Now let’s look at more players, say 5 players, each with total bet amount of the following:–
Player A = $100
Player B = $90
Player C = $80
Player D = $70
Player E = $60
Let’s calculate main-pot, main-pot is always the smallest bet amount times number of players, that is $60 * 5 = $300
Side-pots? OMG! This is headache, really!
player A bet amount $100, B $90, a different of $10. Thus we have Side-Pot_1 pot size of $10, only A has right to it.
Comes Player C of $80. Look at the graph above, diff between C($80) and B($90) is $10, different between B($90) and A($100) is $10, thus we have Side-Pot_2 pot size of $10 + $10 = $20, A and B have right to it.
Just imagine a horizontal dotted line at top of C bar and cut through B and A bars. If C wins, he can only win max. of $80 from B as well as $80 from A, and all of $70 from D and $60 from E.
Leaving a surplus of $30, where $10 are claimed by A only, and $20 which A & B have the right to it.
Then come Player D, Player E. Side-pots are created like above mentioned
Main-Pot = $300, every players have the right.
Side-Pot_1 = $10, Player A has the right.
Side-Pot_2 = $20, contributors A & B.
Side-Pot_3 = $30, contributors A & B & C.
Side-Pot_4 = $40, contributors A & B & C & D.
The winner is found with highest card-points. Now let’s say Player C has highest card points follow by B, then D, then E, then A
Let’s calculate total Player C can take back.
Main-Pot of $300
Side-Pot_3 of $30
Side-Pot_4 of $40
$300 + $30 + $40 = $370
Leaving Side-Pot_1 and Side-Pot_2 which Player C has NO right to claim. So we must determine who take Side-Pot_1 and Side-Pot_2.
Player B has second highest points, thus B takes all $20 in Side-Pot_2, and Player A take back $10 of Side-Pot_1
Again, now imagine Player D has highest points(the winner),
How much Player D can take back?
Main-Pot = $300 PLUS Side-Pot_4 = $40, total = $340.
The rest of side-pots are to be claimed by who has the right and with highest card points.
Side-Pot_1 taken by Player A only.
Side-Pot_2? Player B has higher card points and claimed all $20 by him.
Side-Pot_3? Player C has higher points and thus he takes back $30.
These are Texas Holdem betting rule for Pot.
Now we can look into PHP/MySQL.
i created 2 tables, one table stores bet amount, another card points. Example below:–
mysql_query("DROP TABLE table_total_bet", $conn);
$q = "CREATE TABLE table_total_bet(
player VARCHAR(20) DEFAULT NULL,
total_bet MEDIUMINT(20) UNSIGNED NOT NULL DEFAULT 0,
gameid VARCHAR(255) NOT NULL DEFAULT '',
INDEX(gameid))TYPE=MyISAM";
mysql_query($q, $conn) or die();
mysql_query("DROP TABLE table_card_points", $conn);
$q = "CREATE TABLE table_card_points (
player VARCHAR(20) DEFAULT NULL,
point MEDIUMINT(20) UNSIGNED DEFAULT NULL,
gameid VARCHAR(255) NOT NULL DEFAULT '',
INDEX(gameid))TYPE=MyISAM";
mysql_query($q, $conn) or die();
$game_id = '1234';
mysql_query("INSERT INTO table_total_bet VALUES('A',100,'$game_id'),('B',90,'$game_id'),('C',80,'$game_id'),('D',70,'$game_id'),('E',60,'$game_id')", $conn) or die(mysql_error());
mysql_query("INSERT INTO table_card_points VALUES('A',100,'$game_id'),('B',400,'$game_id'),('C',500,'$game_id'),('D',800,'$game_id'),('E',200,'$game_id')", $conn) or die(mysql_error());
I need to find out main-pot size, all size-pots created according to the above mentioned Texas Holdem Pot rules, with contributors of the side-pots.
I manage to extract each Player names with respective card points and total bet amounts
$game_id = '1234';
$sql_card_point = mysql_query("SELECT player,point FROM table_card_points WHERE point>0 AND gameid='$game_id' ORDER BY player ASC") or die(mysql_error());
$sql_total_bet = mysql_query("SELECT player,total_bet FROM table_total_bet WHERE gameid='$game_id' ORDER BY player ASC") or die(mysql_error());
$c = 0;
while ($row = mysql_fetch_array($sql_card_point)){
$arr_player_p[$c] = $row['player'];
$arr_points[$c] = $row['point'];
$c++;
}
$i = 0;
while ($row = mysql_fetch_array($sql_total_bet)){
$arr_player_b[$i] = $arr_player_p[$i];
$arr_total_bet[$i] = $row['total_bet'];
$i++;
}
echo "Player $arr_player_p[0] | Point $arr_points[0] || Player $arr_player_b[0] | Total Bet $$arr_total_bet[0]";
But I stuck here…
How to work out an algo for finding out Main-Pot size, Side-Pots with all respective contributors, the highest points player, lowest points player…etc.
I hope I am not asking too much. Many thanks in advance!!