Jump to content

[SOLVED] Dice game


decypher

Recommended Posts

<html>
<head>
<title>Guess the number<title>
</head>
<body bgcolor = "maroon">
<center>
<font face = "Arial">
<h1>Guess the number!</h1>

<?php

printgreeting();
printdice();
printform();

function printgreeting(){
global $guess, $numpetals;
if (empty($guess)){
   print"<h3>Welcome to Guess the number!</h3>";
} else if ($guess == $numpetals){
   print "<h3>You got it!</h3>";
} else {

   print <<<HERE

       <h3>from last try: </h3>
       you guessed: $guess<br><br>
       and the correct answer was: $numpetals

HERE;
   
  } // end if

} // end printgreeting


function printdice(){
global  $numpetals;


  $numpetals =0;

  $die1 = rand(1,6);
  $die2 = rand(1,6);
  $die3 = rand(1,6);
  $die4 = rand(1,6);
  $die5 = rand(1,6);

   showDie($die1);
   showDie($die2);
   showDie($die3);
   showDie($die4);
   showDie($die5);

print "<br>";

Calcnumpetals($die1);
Calcnumpetals($die2);
Calcnumpetals($die3);
Calcnumpetals($die4);
Calcnumpetals($die5);

} // end printdice

function showDie($value){
   print <<<HERE
<img src = "die$value.jpg"
    height = 100
    width = 100>
HERE;
} // end showDie


function calcnumpetals($value){

  global  $numpetals;

switch($value) {
   case 3:
   $numpetals += 2;
   break;
   case 5:
   $numpetals += 4;
   break;
} // end switch

} // end calcnumpetals


function printform(){
global   $numpetals;

  print <<<HERE

<H3>What is the total amount?</h3>




<form method = "post">

<input type = "text"
       name = "guess"
       value = "0">
<input type = "hidden"
       name = "numpetals"
       value = "$numpetals">


<br>
<input type = "submit"
       value = "guess">

HERE;

} // end printform


?>

</font>
</center>
</body>
</html>

 

 

Basically it will load up everything...However when I go to guess the total number (amount of dots) it just refreshes the page but doesn't tell me if I was right or wrong...Can anyone see the problem...So you know  only been learning approx 2days...so don't start mentionin arrays and more complicated stuff :D

 

Thnks in advanced

Link to comment
Share on other sites

for your form, try this:

 

<form method="post" action="{$_SERVER['PHP_SELF']}">
<input type = "text" name = "guess" value = "0">
<input type="hidden" name = "numpetals" value="{$numpetals}">
<br>
<input type = "submit" value = "guess" name="submit">
</form>

 

then at the top of your page, put this:

if(isset($_POST['submit'])){ //check if the submit button is hit
$guess = $_POST['guess'];
$numpetals = $_POST['numpetals'];
}

printgreeting();
printdice();
printform();

 

but looking at it, i think most of your variables arnt being passed along properly...

Link to comment
Share on other sites

"else if" should not have a space in it.

 

I can't help you much more as you really haven't really defined the problem very well.

 

But to help yourself, try debugging a little. Instead of printing the messages, change in temporarily to print out the values of the variables you are checking. I usually put them in colons so I know where they should show up, like this:

 

echo ":$guess:$numpetals:";

 

I think you'll find it always outputs ':::' as you haven't set the values of these variables anywhere prior to first checking them.

 

Look in $_POST['guess'] as well.

 

 

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.