TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Tried to make a guessing game, what do u think? (http://www.talkphp.com/absolute-beginners/2056-tried-make-guessing-game-what-do-u-think.html)

karq 01-22-2008 08:11 PM

Tried to make a guessing game, what do u think?
 
So I just started a week ago to learn php. Today I tried to make a guessing game.My first tryout :D
What do u think?

PHP Code:

html>
<body>
<form action="opin.php" method="GET">
Vali arv:<input type="text" name="nr"/>
<input type="submit" value="Arva"/>
</form>
<?php
//I used Estonian in this script, U propally wont understand some thing's :D
$number=$_GET["nr"];
$arva=(rand(1,20));
if (isset(
$arva)) {            //Kas genereeriti arv? Kui jh siis jätkame.
if ($number<$arva) {
echo 
"Vähe pakkusid";
    }
else if (
$number>$arva) {
    echo 
"Liiga palju pakkusid";

}
else if (
$Number==$arva) {
     echo 
"Panid täppi!";

}
}
else {
    echo 
"Midagi läks pekki";
}
?>
</body>
</html>


RobertK 01-22-2008 08:25 PM

One problem, karq, and that is your number (not the guess) doesn't persist. This means that if the number is 1 the first time and I say 3, the returned message is that the number is smaller. So, next time I pick 2 ... only to have it tell me the number is bigger.

Try using a cookie to make the random number persist.

PHP Code:

<?php

// This MUST be at the top before ANY output happens!
if(!isset($_COOKIE['arva'])) {
  
// The zero makes it expire when the browser session terminates.
  
setcookie('arva'rand(1,20), 0);
}
$arva intval($_COOKIE['arva']);
?>

Now, put that at the top and remove the parts within your section that assign values to arva. Now it'll persist between guesses and you can get it right. :-)

TlcAndres 01-22-2008 09:28 PM

Thanks to my ability to speak PHP I can more or less understand the estonian, and you could also use sessions

karq 01-23-2008 05:03 PM

So I made 2 games 1 with cookies and 1 with sessions.
Here are the codes:
Cookies:
PHP Code:

<?php
if (!isset($_COOKIE["arva"])) {
    
setcookie('arva',rand(1,20),0);
}
$arvas=intval($_COOKIE["arva"]);
?>

<html>
<body>
<center><form action="arvamismang_cookidega.php" method="GET">
Vali arv:<input type="text" name="nr"/>
<input type="submit" value="Arva"/>
</form></center>
<?php
//I used Estonian in this script, U propally wont understand something's :D

$number=$_GET["nr"];
if (isset(
$arvas)) {
    if (
$number<$arvas) {
            echo 
"<center>Paku rohkem</center>";
        }
    if (
$number>$arvas) {
            echo 
"<center>Paku vähem</center>";
        }
    if (
$number==$arvas) {
            echo 
"<center>Panid täppi!!!</center>";

    }
}
else {
    echo 
"<center>Kuskil on viga vist :S</center>";
}

?>
</body>
</html>

Sessions:
PHP Code:

<?php
session_start
();
if (!isset(
$_SESSION["arva"])) {

    
$_SESSION["arva"]=rand(1,20);
}
$arvas=intval($_SESSION["arva"]);
?>

<html>
<body>
<center><form action="arvamismang_cookidega.php" method="GET">
Vali arv:<input type="text" name="nr"/>
<input type="submit" value="Arva"/>
</form></center>
<?php
//I used Estonian in this script, U propally wont understand something's :D
$number=$_GET["nr"];
if (isset(
$arvas)) {
    if (
$number<$arvas) {
            echo 
"<center>Paku rohkem</center>";
        }
    if (
$number>$arvas) {
            echo 
"<center>Paku vähem</center>";
        }
    if (
$number==$arvas) {
            echo 
"<center>Panid täppi!!!</center>";
                         unset(
$_SESSION["arva"]);
    }
}
else {
    echo 
"<center>Kuskil on viga vist :S</center>";
}

?>
</body>
</html>

Big thanks to RobertK for help.
Maybe U can explane to me why thosent it change the number with this code:
PHP Code:

<?php
if (!isset($_COOKIE["arva"])) {
    
setcookie('arva',rand(1,20),0);
}
$arvas=intval($_COOKIE["arva"]);
?>

But those change every time in this code:

PHP Code:

$arva=(rand(1,20));
if (isset(
$arva)) {            //Kas genereeriti arv? Kui jh siis jätkame.
if ($number<$arva) {
echo 
"Vähe pakkusid";
    } 

Now I have no idea what do too next.

RobertK 01-23-2008 06:38 PM

Sure.

What the script does, is it checks if a cookie named "arva" is set. If it is your first visit/guess with a new browser session (IE, the browser was closed before running the script) then it knows the cookie isn't there, and it sets "arva" to be a random number. It then pulls from the cookie the random number for the script's use.

The other one, your original one, always sets "arva" to be a random value. This value is never saved anywhere in your script. Thus, if you run it again you have a different number--in fact, your last answer could have been right this time.

That is the basic explanation of the two executions.


All times are GMT. The time now is 07:37 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0