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
https://forums.phpfreaks.com/topic/248530-trying-to-create-a-dice-guessing-game/
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 />";

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.