Jump to content

attempt at number guessing game - please offer revisions


benjahnee

Recommended Posts

hi guys

 

i have written the code of a simnple number guessing game which asks the user for a guess between 1 and 10 and then outputs whether or not the guess was correct and what the number was if it was correct.

 

Would somebody please tell me how to adjust this code so that if the guess is wrong, the page will display " the number is lower" or "the number is higher" and prompt the user to guess again.

 

here is what i have so far.

 

thanks

<?php

$random = rand(1, 10);

$guess = $_POST ['guess'];
$submit = $_POST ['submit'];

if (isset($submit)) {

if ($guess < 1 || $guess > 10 ){
echo "Your guess must be a number from 1 to 10!";


}else {
if ($guess!=$random){
echo "Incorrect guess, the answer is ".$random;
} else {
echo " That is correct!";
}

}


}else {

header("location: index.php");
exit ();
}
?>
Edited by ignace
Please, next time use code tags
Link to comment
Share on other sites

<?php
// Check if the form has been submitted
if(isset($_POST['guess'])) {
	$guess = $_POST['guess'];
	$secret = $_POST['secret'];
?>
	<html>
    <head></head>
    <body>
<?php
	if($guess == $secret) {
		// The user guessed right
	?>
    	<h1>Correct!</h1> <a href="">Play again</a>
    <?php
	}
	else {
		// The user did guess wrong, now let's see if it was lower or higher
		if($guess < $secret) {
			// The guess was too low
		?>
        	<h2>Wrong! The secret number is Higher, try again!</h2>
        <?php
		}
		else {
			// The guess was too high
		 ?>
         	<h2>Wrong! The secret number is Lower, try again!</h2>
         <?php
		}
		?>
                <form action="" method="post">
                    <input type="hidden" name="secret" value="<?php echo $secret; ?>" />
                    Guess: <input name="guess" type="text" /> <input type="submit" value="Guess!" />
                </form>
            </body>
            </html>
        <?php
	}
}
else {
	// Generate random number
	$secret = rand(1,10);
?>
	<html>
    <head></head>
    <body>
        <form action="" method="post">
        	<input type="hidden" name="secret" value="<?php echo $secret; ?>" />
        	Guess: <input name="guess" type="text" /> <input type="submit" value="Guess!" />
        </form>
    </body>
    </html>
<?php
}

- W

Link to comment
Share on other sites

Hi benjahnee, good evening to you.

 

I thought I'd re-write your script because I think for this case you should process the PHP code first, then output what you need, instead of mixing processing and raw HTML together.

 

 

<?php

$secret = rand(1,10);

// Check if the form has been submitted
if(isset($_POST['guess'])) {
    $guess = intval($_POST['guess']);
    $secret = intval($_POST['secret']);

    $output = "";
    
    if($guess == $secret)
    {
        $output .= "<h1>Correct!</h1>";
        $output .= "<a href='#'>Play again</a>";
    }
    elseif($guess < $secret)
    {
        $output .= "<h1>Your answer was too low. Please try again!";
    }
    else
    {
        $output .= "<h2>Your answer was too high. Please try again!</h2>";
    }
}


?>
<html>
<head>
    <title>Page Name</title>
</head>

<body>

<form action="" method="post">
    <input type="hidden" name="secret" value="<?php echo $secret; ?>" />
    Guess: <input name="guess" type="text" /> <input type="submit" value="Guess!" />
</form>

<?php if(isset($output)) { print($output); } ?>

</body>
</html>
 

 

It looks a lot cleaner.

 

Hope this helps.

 

Kind regards,

 

L2c.

Edited by Love2c0de
Link to comment
Share on other sites

Thanks alot love2code that is much better for me.

Woud your be able to expand a little further for me though please.

I would like it if when the guess was correct, the guess box is omitted and just the message and option to play again is displayed.

i cant figure out how to do it.

 

thanks

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.