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
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
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
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.