You good at math? Help me figure out a formula/solution!

these 2 tables are much simplified version of my actual more complicated tables, if i don’t need to mod the database scheme then i prefer to creat new table to store bet amount, i can change the table that currently stores pocket cards, scores, hand descriptions…etc and add new field of bet amount but should be a last resort.

let’s forget fold player at the moment, there is one interesting rule about it but the official TH rules i read said otherwise. it’s about logic.

ok, i understand your code down until

if($players[$i]->score != $players[$i-1]->score).......

how did you access a score value by $players[$i] ?? based on the 2 tables scheme of course.

at the moment i can think of it by discarding table_total_bet and add total_bet field to table_card_points.

like this…

$q_player_score = mysql_query(“SELECT player, score, total_bet FROM table_card_scores WHERE score>0 AND gameid=‘$game_id’ ORDER BY score DESC”) or die(mysql_error());

while ($row = mysql_fetch_array($q_player_score)) {
    $players[] = $row['score'];
}

lastly, $players[$i]->score, it is an OBJECT ‘$players[$i]’ trying to access variable ‘score’, whereas $players = $row[‘score’]; an array variable acquiring a value of variable ‘$row[‘score’]’. right?

i hope you would show me the block of code that contains ‘score’ and i will study your code. i can really learn and remember more like that.

Hi hash,

let me mod my version of code to match with yours

Your code

private function get_winner_groups($players)
{
    $count = count($players);
    $j = 0;
    $w[$j][] = 0; // sorted, so first player is always highest
    // player 0 is taken care of, so start at 1
    for ($i = 1; $i < $count; $i++) {
        // started at 1, so can check this score against one before it
        if ($players[$i]->score != $players[$i - 1]->score) {
            $j++; // found a new score, create new array
        }
        $w[$j][] = $i; // store the index
    }
    return $w;
}

my ori. version

for ($i = 0; $i < $num_player_left; $i++) {

    if ($arr_scores[$i] == $max_card_score) {
    $arr_player_grouped[$ii][$i] = $arr_player[$i];
    $arr_scores_grouped[$ii][$i] = $arr_scores[$i];
    $arr_bet_grouped[$ii][$i] = $arr_bet[$i];

    echo "$ii $i Player {$arr_player_grouped[$ii][$i]} | {$arr_scores_grouped[$ii][$i]} | {$arr_bet_grouped[$ii][$i]}<br>";
    } else {
    if ($arr_scores[$i] != $arr_scores[$i - 1])
    $ii++;

    $arr_player_grouped[$ii][$i] = $arr_player[$i];
    $arr_scores_grouped[$ii][$i] = $arr_scores[$i];
    $arr_bet_grouped[$ii][$i] = $arr_bet[$i];

    echo "$ii $i Player {$arr_player_grouped[$ii][$i]} | {$arr_scores_grouped[$ii][$i]} | {$arr_bet_grouped[$ii][$i]}<br>";
    }
}

I mod to


for ($i = 0; $i < $num_player_left; $i++) {
   
    if ($arr_scores[$i] == $max_card_score) {

        $j = 0;//highest score players always hv key 0

    } else {

        // found a new score, create new array
        if ($arr_scores[$i] != $arr_scores[$i - 1])
            $j++;

    }

    $w[$j][] = $i; // store the index
}

is my new version doing just the same thing as yours?

mine is missing the ‘bet amount’ however.

waiting eagerly from your reply!

This is just one part of your whole app, you need to fit it in somehow. But it’s based on having an array of players, where each player has cash, a bet, and a score. You probably have more than that, and if in array rather than object, just change the syntax. If you need data from more than one table, then use JOINs, but what you want is something like:

array (
    [0]
        ['name'] => 'A'
        ['cash'] => 500
        ['total_bet'] => 100
        ['score'] => 3
    [1]
        ['name'] => 'C'
        ['cash'] => 550
        ['total_bet'] => 120
        ['score'] => 3
    [2]
        ....
)

So with this players array, you can access like


if($players[$i]['score'] != $players[$i-1]['score']

The way I deal with scores/bets won’t help you much as it’s just a mock method that has nothing to do with the db - I didn’t create one.

Yes, that will work because you the first player will be equal to max_card_score, hence there is no risk of executing an else that tries to compare 0 and -1. You will need to set max_card_score each time you find a new one though.

There is many cases that can cause a php script (or any software for that matter) to stop in the middle of an execution.

It can be anything from as you mention a server reboot, the webservice crashing, buffer overflow, to normal coding issues. I.e. if something as simple as a case appeared which the coding the not accord for (for example if a user manage to go into a negative capital due to a race condition).

As you said your new to PHP and possibly SQL as well, I would recommend that you take a look on transaction based vs non transaction based database operations when you get time. For MySQL that is InnoDB vs MyISAM, in most cases you have nothing to gain by chosing MyISAM, the only advantage would be a more conservative approach to disk space management and full text index possibility. If you by a chance need the full text index, it would be wiser to shell those columns out in a own table that is MyISAM and use InnoDB on the rest.

In general using transaction will cause you to spend some more time when coding, but when you get used to it you will never get back.

Using Joins on queries is a good thing.

When I said race condition, it was not exactly that I meant.
If you have time take a look on this page: http://en.wikipedia.org/wiki/Race_condition for a good explanation.