Jump to content

Guessing game


oneoption

Recommended Posts

i've got the task to create a Guessing game, where you guess the number between 1-100. Also there has to be a number of maxium guesses..10. but just cant figure it out :o.

 

It's in danish. so thats why the ? is everywhere:p

 

<html>
<head>
<title>G?tteleg</title>
</head>
<body>
<?php
session_start();
$_SESSION['antal'] = 10

?>

<form action="guess2.php" method="POST">
	G?t Nummer!: <input type="number" name="g?t"<br/>
	<input type="submit" name="send" value="G?t"> 
</form>





</html>
</body>

 

<?php
session_start();
$rand = rand(1, 100);

$g?t = $_POST['g?t'];
$send = $_POST['send'];
echo $_SESSION['antal'];
$_SESSION['antal']--;



if ($g?t<1 || $g?t>100) {
	echo "Dit g?t skal v?re mellem 1 og 100";
} else 
	if($g?t!=$send) {
		echo "Forkert... Det rigtige nummer var.." . $rand;
	} else {
		echo "Godt G?ttet";


	}


?>

Link to comment
https://forums.phpfreaks.com/topic/267844-guessing-game/
Share on other sites

Why are there two different codes?

Use one.

 

You need to check if the $_SESSION['antal'] is set or not before assigning a new value to it.

if(!isset($_SESSION['antal'])){
$_SESSION['antal'] = 10;
}

 

Only $_SESSION['antal']--; when they have guessed. You also need to make sure if they have guessed. You can't just go ahead and do this:

$g?t = $_POST['g?t'];
$send = $_POST['send'];

Because $_POST['send'] might not be set, and then it will be looking for something that doesn't exist. It's just to use isset() again. Remember that only if they guess you can decrease the amount of guesses left, and you must make sure SESSION then too is already set.

 

$g?t = $_POST['g?t'];

Use numbers and English alphabetical characters only in variable names, and they must start with the latter.

 

 

It also seems like you are setting a new random number each time. Isn't that confusing for the person guessing?

There is nothing stopping a person from clearing the session and get 10 new attempts.

Link to comment
https://forums.phpfreaks.com/topic/267844-guessing-game/#findComment-1374180
Share on other sites

It is working now. The last issue is now.. I need to echo something, if the abs of the guessed number is >50 .

 

Here is some of the code

 

	if ($guess<1 || $guess>100) 
{
	echo "..";
} 

else
{ 
	if($guesst!=$_SESSION['target']) 
	{
		echo "wrong. the correct number was..." . $_SESSION['target'];
	} 

	else 
	if  // This is where i am stuck, i come so far to know i have to use abs something.
	{
	echo "Way to High";
	}

Link to comment
https://forums.phpfreaks.com/topic/267844-guessing-game/#findComment-1374817
Share on other sites

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.