Jump to content

Trying to create a dice guessing game


jetlife76

Recommended Posts

Im new to php and i cant get my program to display correctly. I want it to take a user's guess of a single Die roll and display whether the user guessed correctly and display a picture of the corresponding Die. i think there is something wrong with my "If" statement or logic.

Please Help.

 

Here's the Code:

INPUT FORM:

 

<html>

<body>

 

<form name = "Dice Game" action="diceroll.php" method="post">

Your Guess: <br>

<input type= "text" name="" size= "1">

 

<input type ="submit" name = "submit">

 

</form>

</body>

</html>

 

 

OUTPUT FORM:

 

 

<html>

<body>

<?php

$roll = $_POST['submit'];

$dice =  rand(1, 6);

 

    if ($roll == 1 && $dice == 1){

        print "Great Job, You're Good!";

        }else if ($roll == 1 && $dice /= 1) {

              print "Wrong";

                  }

else if ($roll == 2 && $dice == 2) {

print "Great Job, You're Good!";}

else if ($roll == 2 && $dice /= 2) {

print "Wrong"; }

else if ($roll == 3 && $dice == 3) {

print "Great Job, You're Good!"; }

else if ($roll == 3 && $dice /= 3) {

print "Wrong"; }

else if ($roll == 4 && $dice == 4) {

print "Great Job, You're Good!"; }

else if ($roll == 4 && $dice /= 4) {

print "Wrong"; }

else if ($roll == 5 && $dice == 5) {

print "Great Job, You're Good!"; }

else if ($roll == 5 && $dice /= 5) {

print "Wrong"; }

else if ($roll == 6 && $dice == 6) {

print "Great Job, You're Good!"; }

else if ($roll == 6 && $dice /= 6) {

print "Wrong"; }

else {

print " "; }

       

        if($dice == 1) print "<img src='dice1.png'>"."<br>";

        if($dice == 2) print "<img src='dice2.png'>"."<br>";

        if($dice == 3) print "<img src='dice3.png'>"."<br>";

        if($dice == 4) print "<img src='dice4.png'>"."<br>";

        if($dice == 5) print "<img src='dice5.png'>"."<br>";

        if($dice == 6) print "<img src='dice6.png'>"."<br>";

 

else {

            print "Thanks for playing". "<br>";

    }

                 

?>

 

<form name= "back" action= "dice.php" method= "post">

<input type="submit" name="back" value ="Play Again">

</form>

</body>

</html>

 

Link to comment
Share on other sites

!= is does not equal, not /=

 

That said, there is a very much an easier way of doing this ...

 

/* Check if the dice and roll match */
if ( $roll == $dice )
{
     /* They do, congrats */
     print "Great Job, You're Good!";
}
else
{
     /* They don't, too bad */
     print "Wrong";
}

/* Print the dice number */
print "<img src='dice" . $dice . ".png'>"."<br />";

Link to comment
Share on other sites

You are also using the incorrect form element to capture the "guess"

You will need to assign a name to your "guess" input box and retrieve it.

Currently, you are getting the value from the submit button as your roll variable.

Link to comment
Share on other sites

<?php

$roll = $_POST['guess'];

$dice =  rand(1, 6);

    /* Check if the dice and roll match */

if ( $roll == $dice ){

    /* They do, congrats */   

print "Great Job, You're Good!";}

else{    /* They don't, too bad */   

print "Wrong";}

/* Print the dice number */

print "<img src='dice" . $dice . ".png'>"."<br />";       

?>

 

I gave it the name guess it works now thanks alot

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.