www.webdeveloper.com
Results 1 to 7 of 7

Thread: Help with Two Dice Pig (PHP Game)

  1. #1
    Join Date
    Dec 2013
    Posts
    5

    Help with Two Dice Pig (PHP Game)

    I'm new to this forum, so hello and thanks in advance for any advice you can give me.

    I am also new to PHP and am working on my project for my PHP class. I've got some of the code done, but am having trouble figuring out how to set it up to keep score during a game using cookies. The game is called Two-Dice-Pig.

    Here are the rules for the game:

    Each turn, a player repeatedly rolls two dice until either a 1 is rolled or the player decides to "hold":-
    Two standard dice are rolled. If neither shows a 1, their sum is added to the turn total.
    If a single 1 is rolled, the player scores nothing and the turn ends.
    If two 1s are rolled, the player’s entire score is lost, and the turn ends.
    If a player chooses to "hold", their turn total is added to their score, and it becomes the next player's turn.
    The first player to score 100 or more points wins.
    Your game will have three players – you and two computer players. Each time a player “rolls”, the two dice should be displayed – for the human player, an option to either roll again or hold should be displayed. For the computer players, they will randomly choose to roll between 1 and 5 times unless their turn is ended by either winning or rolling a “one”. All players’ scores should be displayed at all times during the game and the turn total should also be displayed as a player is playing – once someone “wins”, an option should be displayed to “play again” or quit the game.

    Here is my code:


    PHP Code:
     <html>
        <head>
            <title>Tom Carp - Two Dice Pig</title>
            <style type="text/css">
                img {
                    width: 100px; height: 100px; margin-right: 5px; }    
            </style>
        </head>
    <body>

        <h1>Two-Dice Pig</h1>

    <?php

    // Get the values from the form
    $roll $_POST['roll'];
    $hold $_POST['hold'];


    // Dice pictures array
    $dice1 = array ( => "<img src='dice1.jpg'>","<img src='dice2.jpg'>","<img src='dice3.jpg'>","<img src='dice4.jpg'>","<img src='dice5.jpg'>","<img src='dice6.jpg'>"  ); 
    $dice2 = array ( => "<img src='dice1.jpg'>","<img src='dice2.jpg'>","<img src='dice3.jpg'>","<img src='dice4.jpg'>","<img src='dice5.jpg'>","<img src='dice6.jpg'>"  ); 


    if(
    $_POST['roll']){ // If roll button was pressed


    // Calculate Dice Rolls
    $humanRoll1 rand(1,6);
    $humanRoll2 rand(1,6);

    $comp1Roll1 rand(1,6);
    $comp1Roll2 rand(1,6);

    $humanSum $humanRoll1 $humanRoll2;
    $comp1Sum $comp1Roll1 $comp1Roll2;



    if(
    $humanSum $comp1Sum){
        print 
    "Congratulations. You have won!";
            }else if(
    $comp1Sum $humanSum){
                print 
    "Sorry! The computer won.";
            }else if(
    $comp1Sum == $humanSum){
                print 
    "It was a tie!";
            }

    print 
    "<h3>You rolled a $humanRoll1 and a $humanRoll2</h3>";
    print 
    "$dice1[$humanRoll1]";
    print 
    "$dice1[$humanRoll2]";

    print 
    "<h3>The computer rolled a $comp1Roll1 and a $comp1Roll2</h3>";
    print 
    "$dice2[$comp1Roll1]";
    print 
    "$dice2[$comp1Roll2]";


    }

    if(
    $hold){
        print 
    "$humanSum"; }

    ?>

    <form method="post" action="<?php print $_SERVER['PHP_SELF']?>">
        <p><table>
            <tr>   
                <th>You</th>
            </tr>
            <tr>
                <td>
                    <input type="hidden" name="counter" value="$counter1">
                    <input type="hidden" name="boxcounter" value="$boxcounter1">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" name="roll" value="Roll">
                    <input type="submit" name="hold" value="Hold">
                </td>
            </tr>
            </table>  

    </form>
    </body>
    </html>
    Thanks again for any help
    Last edited by NogDog; 12-13-2013 at 10:20 PM. Reason: added [php] tags

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,304
    I would use sessions (with session_start() and the $_SESSION array), rather than dealing directly with cookies. Besides being a bit cleaner, it keeps the geeky enough user from manipulating the scores.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2013
    Posts
    5
    Thanks for the help NogDog I really appreciate it! Could you show me how to implement that into my code? I not really sure where to begin and I just started learning about cookies and sessions so I really don't know what I'm doing thanks.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,304
    The basics are pretty simple: just be sure to start each page that needs to access the $_SESSION array by using session_start() before anything at all gets output to the browser. Then just store/access values in the array like you would any other array:
    PHP Code:
    <?php
    session_start
    ();
    ?>
    <!-- your HTML head and form here, perhaps -->
    <?php
    if(some_condition_is_true()) {
        
    $_SESSION['scores']['user'] += 1;
    }
    echo 
    "<p>User's score: ".$_SESSION['scores']['user']."</p>\n";
    ?>
    <!-- rest of page -->
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,346
    Hi JerseyDrummer

    I have taken the liberty to mess around with the code a bit and made some changes, noticed a couple of oddities during play, one where I held and the computer won with a lower score! But this should help you on your way.

    I simplified the image selection by doing away with the arrays you were using, not really needed if you have six images dice1.jpg through to dice6.jpg

    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['dice_one'] = isset($_SESSION['dice_one'])?$_SESSION['dice_one']:0;
    $_SESSION['dice_two'] = isset($_SESSION['dice_two'])?$_SESSION['dice_two']:0;
    ?>
    <html>
        <head>
            <title>Tom Carp - Two Dice Pig</title>
            <style type="text/css">
                img {
                    width: 100px; height: 100px; margin-right: 5px; }    
            </style>
        </head>
    <body>

        <h1>Two-Dice Pig</h1>

    <?php

    // Get the values from the form
    $hold = isset($_POST['hold'])?$_POST['hold']:false;
    $roll = isset($_POST['roll'])?$_POST['roll']:false;

    $output "";
    // if we held then we don't need a human roll, maybe a cheese or ham roll!
    if(isset($_POST['hold'])){
        
    $humanRoll1 $_SESSION['dice_one'];
        
    $humanRoll2 $_SESSION['dice_two'];
    }else{
        
    // Calculate Dice Rolls
        
    $humanRoll1 rand(1,6);
        
    $_SESSION['dice_one'] = $humanRoll1 ;
        
    $humanRoll2 rand(1,6);
        
    $_SESSION['dice_two'] = $humanRoll2;
    }
            
        
    $comp1Roll1 rand(1,6);
        
    $comp1Roll2 rand(1,6);
        
        
    $humanSum $humanRoll1 $humanRoll2;
        
    $comp1Sum $comp1Roll1 $comp1Roll2;
        
        
        
        if(
    $humanSum $comp1Sum){
            
    $output "Congratulations. You have won!";
                }else if(
    $comp1Sum $humanSum){
                    
    $output "Sorry! The computer won.";
                }else if(
    $comp1Sum == $humanSum){
                    
    $output "It was a tie!";
                }

        
    $output .= sprintf("<h3>You %s a $humanRoll1 and a $humanRoll2</h3>",($hold?"held":"rolled"));
        
    $output .= "<img src='dice{$humanRoll1}.jpg'>";
        
    $output .= "<img src='dice{$humanRoll2}.jpg'>";
        
        
    $output .= "<h3>The computer rolled a $comp1Roll1 and a $comp1Roll2</h3>";
        
    $output .= "<img src='dice{$comp1Roll1}.jpg'>";
        
    $output .= "<img src='dice{$comp1Roll2}.jpg'>";


        
        
    // now write output    
        
    echo $output;
    ?>

    <form method="post" action="<?php print $_SERVER['PHP_SELF']?>">
        <p><table>
            <tr>   
                <th>You</th>
            </tr>
            <tr>
                <td>
                    <input type="hidden" name="counter" value="$counter1">
                    <input type="hidden" name="boxcounter" value="$boxcounter1">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" name="roll" value="Roll">
                    <input type="submit" name="hold" value="Hold">
                </td>
            </tr>
            </table>  

    </form>
    </body>
    </html>
    Last edited by \\.\; 12-14-2013 at 03:00 PM.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  6. #6
    Join Date
    Dec 2013
    Posts
    5
    Thanks so much you guys are the best such a big help!!

  7. #7
    Join Date
    Dec 2013
    Posts
    5
    I have one last question. What would I have to do to make a score box for both the comp and the human so that every time the dice is rolled or held, it adds the score until reaching 100? Would I just have to create a variable so that every time hold or roll is pressed, it will add to the current score? I hope that makes sense having a hard time explaining it sorry!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles