Jump to content

randoming when it shouldn't be


decypher

Recommended Posts

Problem: When i click the button to re-roll the dices, the dices that haven't had their checkboxes ticked should only re-roll but they still all do.

 

  function rolldice()
    {
        global $die, $secondroll, $keepit;
        print "<table border = 1><td><tr>";
        
        for ($i = 0; $i < 5; $i++) {
            if ($keepit[$i] == "") {
                $die[$i] = rand(1, 6);
            } else {
                $die[$i] = $keepit[$i];
            }
            // end if
            
            
            //print out dice images
            print <<<HERE
            <td>
            <img src ="die$die[$i].jpg"
            height = 50
            width = 50<br>
            
HERE;
            
            //print out a checkbox on first roll only
            if ($secondroll == FALSE) {
                print <<<HERE
                <input type = "checkbox"
                name = "$keepit[$i]"
                value = "$die[$i]">
                </td>
HERE;
                
            }
            // end if
        }
        // end for loop

 

Can anyone see the problem?

Link to comment
Share on other sites

Not sure..Learning out of a book but thats all it gives me :S

 

 <?php
    
    $cash = $_POST['cash'];
    $secondroll = $_POST['secondroll'];
    $payoff = $_POST['payoff'];
    
    // Check to see if this is first time here
    if (empty($cash)) $cash = 100;
    
    rolldice();
    
    if ($secondroll == TRUE) {
        print"<h2>Second Roll</h2>\n";
        $secondroll = FALSE;
        evaluate();
    } else {
        print "<h2>First roll</h2>\n";
        $secondroll = TRUE;
    }
    // end if
    
    printstuff();
    
    function rolldice()
    {
        global $die, $secondroll, $keepit;
        print "<table border = 1><td><tr>";
        
        for ($i = 0; $i < 5; $i++) {
            if ($keepit[$i] == "") {
                $die[$i] = rand(1, 6);
            } else {
                $die[$i] = $keepit[$i];
            }
            // end if
            
            
            //print out dice images
            print <<<HERE
            <td>
            <img src ="die$die[$i].jpg"
            height = 50
            width = 50<br>
            
HERE;
            
            //print out a checkbox on first roll only
            if ($secondroll == FALSE) {
                print <<<HERE
                <input type = "checkbox"
                name = "$keepit[$i]"
                value = "$die[$i]">
                </td>
HERE;
                
            }
            // end if
        }
        // end for loop
        
        //print out submit button and end of table
        print <<<HERE
        </tr></tD>
        <tr>
        <td colspan = "5">
        <center>
        <input type = "submit"
               value = "Roll again">
        </center>
        
        </td>
        </tr>
        </table>
        
HERE;
        
    }
    // end rolldice
    
    function evaluate()
    {
        global $die, $cash;
        //set up payoff
        $payoff = o;
        
        //subtract some money for this roll
        $cash -=2;
        
        //count the dice
        $numvals= array(6);
        for ($theval = 1; $theval <= 6; $theval++) {
            for ($dienum = 0; $dienum < 5; $dienum++) {
                if ($die[$dienum] == $theval) {
                    $numvals[$theval]++;
                }
                // end if
            }
            // end dienum for loop
        }
        // end theval for loop
        
        //print out results
        // for ($i = 1; $i <= 6; $i++){
        // print "$i: $numvals[$i]<br>\n";
        // } end for loop
        
        //count how many pairs, threesm fours, fives
        $numpairs = 0;
        $numthrees = 0;
        $numfours = 0;
        $numfives = 0;
        
        for ($i = 1; $i <=6; $i++) {
            switch ($numvals[$i]) {
            case 2:
                $numpairs++;
                break;
            case 3:
                $numthrees++;
                break;
            case 4:
                $numfours++;
                break;
            case 5:
                $numfives++;
                break;
            }
            // end switch
        }
        // end for loop
        
        
        //check for a pair
        if ($numpairs == 1) {
            print "You have two pairs!<br>\n";
            $payoff = 4;
        }
        // end if
        
        //check for two pairs
        if ($numpairs == 2) {
            print "You have two pairs!<br>\n";
            $payoff = 7;
        }
        // end if
        
        //check for a three of a kind and a full house
        if ($numthrees == 1) {
            if ($numpairs ==1) {
                //three of a kind and a pair is a full house
                print "You have a full house!<br>\n";
                $payoff = 15;
            } else {
                print "You have a three of a kind!<br>\n";
                $payoff = 12;
            }
            // end 'pair' if
        }
        // end 'three'if
        
        //check for a four of a kind
        if ($numfours == 1) {
            print "You have a four of a kind!<br>\n";
            $payoff = 25;
        }
        // end if
    }
    // end if
    
    
    //check for a five of a kind
    if ($numfives == 1) {
        print "You have a five of a kind!<br>\n";
        $payoff = 30;
    }
    // end if
    
    //check for flushes
    
    if (($numvals[1] == 1)
    && ($numvals[2] == 1)
    && ($numvals[3] == 1)
    && ($numvals[4] == 1)
    && ($numvals[5] == 1)) {
        print "You have a flush<br>\n";
        $payoff = 20;
    }
    // end if
    
    if (($numvals[2] == 1)
    && ($numvals[3] == 1)
    && ($numvals[4] == 1)
    && ($numvals[5] == 1)
    && ($numvals[6] == 1)) {
        print "You have a flush<br>\n";
        $payoff = 20;
    }
    // end if
    
    print "you bet £5<br>\n";
    print "Payoff is $payoff<br>\n";
    $cash += $payoff;
    
// end evaluate

function printstuff()
{
    
    global $cash, $secondroll;
    
    print "cash: £$cash<br>\n";
    
    //store variables in hidden fields
    print <<<HERE
    <input type = "hidden"
    name = "secondroll"
    value = "$secondroll">
    
    <input type = "hidden"
    name = "cash"
    value = "$cash">
    
    <input type = "hidden"
    name = "payoff"
    value = "$payoff">
    
HERE;
    
}
// end printstuff

 

that's most of the coding if that helps

Link to comment
Share on other sites

  //print out a checkbox on first roll only
            if ($secondroll == FALSE) {
                print <<<HERE
                <input type = "checkbox"
                name = "$keepit[$i]"
                value = "$die[$i]">
                </td>

 

I have a feeling it has to do something with this and me needing to write a code like :

 

$die[$i] = $_POST['$keepit[$i]'];

 

but I've tried that and it doesn't work

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.