www.webdeveloper.com
Results 1 to 7 of 7

Thread: Help with Two Dice Pig (PHP Game)

Threaded View

  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 11:20 PM. Reason: added [php] tags

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