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 ();
}
?>
<?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

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.

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

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.