Jump to content

Recommended Posts

hi everyone, i'm new here.  I'm taking a PHP class and it is kicking my butt.  So, maybe a little help, since I can't get any from my teacher.  :'(

 

I have to use loops to create a game called "I'm thinking of a number".  The program is supposed to take a number from a user as input and a hidden value.  It then tells whether the user is too high, low or correct and tell how many chances it took.

 

So, how can I fix my code?

 

<html>
<head>	
<title>
	Number Guessing v1.1.1.90
</title>
<style type = "text/css">
	body {
		background: green;
		color: white;
		}
</style>
</head>

<body>
<center>
	<h1>Number Guessing</h1><br><br>
<h2>Please guess a number between 1 and 10</h2><br><br>

<?php
if (empty ($guessNum)){
$guessNum = 1;
}
run();

for($guessNum = 1;$guess != $hiddenGuess; $guessNum++){
if ($guess < $hiddenGuess){
	echo "You guessed too low";
}else if ($guess > $hiddenGuess){
	echo "You guessed too high";
}
}

if ($guess == $guessNum){
echo "You are correct!<br>You get a cookie!<br>";
echo "It took you $guessNum tries.";
}else{
echo "Umm, Houston, there is a problem<br>";
}

function run(){

global $guess, $hiddenGuess;
print <<<HERE
         			<input type = "text"
        				   name = "guess"
        				  value = $guess>
        				  
        			<input type = "hidden"
        				   name = "hiddenGuess"
        				  value = $hiddenGuess>
        				  
        			<input type = "submit"
        				  value = Enter>
HERE;
}
  

?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/180150-solved-im-thinkin-of-a-number-looping/
Share on other sites

The code is terrible. Global variables are bad. Your HTML markup is invalid. You dont have a form. You do not even need a loop for this task nor do you require functions. Here is you script simplified:

<html>
<head>	
<title>Number Guessing v1.1.1.90</title>
<style type = "text/css">
body { background: green; color: white;	}
</style>
</head>
<body>
<h1>Number Guessing</h1><br><br>
<h2>Please guess a number between 1 and 10</h2><br><br>
<?php
if($_POST['submit'] == 'Enter') {
$_POST['attempts']++;
if(is_numeric($_POST['guess'])) {
	if($_POST['guess'] == $_POST['hiddenGuess']) {
		print "<p>You guessed correctly</p>";
	}
	if($_POST['guess'] > $_POST['hiddenGuess']) {
		print "<p>You guessed too high</p>";
	}
	if($_POST['guess'] < $_POST['hiddenGuess']) {
		print "<p>You guessed too low</p>";
	}		
}
else {
	print "<p>Please enter a number</p>";
}
}
?>
<p>Attempts: <?php print isset($_POST['attempts']) ? $_POST['attempts'] : 0; ?></p>
<form method="post" action="">
<input type="text" name="guess" />
<input type="hidden" name="attempts" value="<?php print $_POST['attempts']; ?>" />
<input type="hidden" name="hiddenGuess" value="<?php print is_numeric($_POST['hiddenGuess']) ? $_POST['hiddenGuess'] : rand(1,10); ?>" />
<input type="submit" name="submit" value="Enter" />
</form>
</body>
</html>

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.