Forum Moderators: coopster

Message Too Old, No Replies

Higher/Lower Game Code

Results not showing

         

AliTaylor4411

7:21 pm on Aug 17, 2010 (gmt 0)

10+ Year Member



Hi Guys,

Just creating a game which allows the user to guess if the next number is going to be higher or lower (Very simple!)

On looking at the page for the first time the user gets this:

The first card is 5(for example)

Higher

Lower

The user then clicks on the higher or lower button

and should get a result!

I am only getting a blank page

This is the code - Any comments would be welcome!

<?php
$base_card = $_POST['card']; // retrieves previous card
$horl = $_POST['horl']; // retrieves high or low decision
$card = rand('1', '10'); // generates new card
// if first visit
if(empty($base_card))
{
echo "The first card is $card
<form method='POST' action='$PHP_SELF'>
<input type='hidden' name='horl' value='high'>
<input type='hidden' name='card' value='$card'>
<input type=submit value='HIGHER'></form>

<form method='POST' action='$PHP_SELF'>
<input type='hidden' name='horl' value='low'>
<input type='hidden' name='card' value='$card'>
<input type=submit value='LOWER'></form>";
}
// If pair is generated lose
if($base_card == $card)
{
echo "NOTHIN FOR A PAIR<br />
Last card was $base_card New Card is $card You went $horl";
}
//if user selected high and new card is higher than old card
if("$horl" == "high" && "$card" > "$base_card")
{
echo "You win<br /><br />";
echo "The last Card was $base_card and you chose to go $horl and the card was $card
<br /><br />
<form method='POST' action='$PHP_SELF'>
<input type='hidden' name='horl' value='high'>
<input type='hidden' name='card' value='$card'>
<input type=submit value='HIGHER'></form>

<form method='POST' action='$PHP_SELF'>
<input type='hidden' name='horl' value='low'>
<input type='hidden' name='card' value='$card'>
<input type=submit value='LOWER'></form>";
}
//if user selected low and old card is higher than new card
elseif("$horl" == "low" && "$card" < "$base_card")
{
echo "You Lose!<br />
Last card $base_card new card $card you went lower";
}
?>

Matthew1980

8:16 pm on Aug 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there AliTaylor4411,

I have just had a play around with this for 5 mins to see if I could play it.

The blank screens only happen when the numbers are even I think, but the reason as you are getting blank screen is because: action='$PHP_SELF', for the sake of this little program, just remove the action attribute (and it's value), form's default action is to post to itself, so really there is no need either to use $_SERVER['PHP_SELF']; and whatever you do, don't use $_SERVER['PHP_SELF'] for public stuff, as there are know security vulnerabilities with it. Just thought I should mention that.

I shall carry on playing this now, quite addictive ;-p though there is still a bug, something to do with the logic of it... Hmmm

Cheers,
MRb

AliTaylor4411

8:50 pm on Aug 17, 2010 (gmt 0)

10+ Year Member



Hello there Matthew 1980,

I have had another play with the code (thanks for the advice on the action="$php_SELF".

Heres what ive got now - see what you think! Still some bugs though and not sure what to do!

<?php
$base_card = $_POST['card']; // retrieves previous card
$user_choice = $_POST['user_choice']; // retrieves high or low decision
$card = rand('1', '100'); // generates new card
// if first visit
if(empty($base_card))
{
echo "<center><strong>The first card is $card<br></br>
<br>Would you like to go</br>
<form method='POST' action='$PHP_SELF'>
<input type='hidden' name='user_choice' value='high'>
<input type='hidden' name='card' value='$card'>
<input type=submit value='HIGHER'></form>

<br>Or</br>
<form method='POST' action='$PHP_SELF'>
<input type='hidden' name='user_choice' value='low'>
<input type='hidden' name='card' value='$card'>
<input type=submit value='LOWER'></center></strong></form>";
}
// If pair is generated lose
if($base_card === $card)
{
echo "NOTHIN FOR A PAIR<br />
Last card was $base_card New Card is $card You went $user_choice";
}
//if user selected high and new card is higher than old card
if("$user_choice" == "high" && "$card" > "$base_card")
{
echo "The last Card was $base_card and you chose to go Higher and the card was $card so YOU WIN!
<br /><br />
<form method='POST'>
<input type='hidden' name='user_choice' value='high'>
<input type='hidden' name='card' value='$card'>
<input type=submit value='HIGHER'></form>

<form method='POST' action='$PHP_SELF'>
<input type='hidden' name='user_choice' value='low'>
<input type='hidden' name='card' value='$card'>
<input type=submit value='LOWER'></form>";
}

//if user selected high and new card is lower than old card
if("$user_choice" == "low" && "$card" < "$base_card")
{
echo "The last Card was $base_card and you chose to go Lower and the card was $card so YOU LOSE!
<br /><br />
<form method='POST' action='$PHP_SELF'>
<input type='hidden' name='user_choice' value='high'>
<input type='hidden' name='card' value='$card'>
<input type=submit value='HIGHER'></form>

<form method='POST' action='$PHP_SELF'>
<input type='hidden' name='user_choice' value='low'>
<input type='hidden' name='card' value='$card'>
<input type=submit value='LOWER'></form>";
}

//if user selected low and new card is higher than old card
if("$user_choice" == "low" && "$card" > "$base_card")
{
echo "The last Card was $base_card and you chose to go Lower and the card was $card so YOU LOSE!
<br /><br />
<form method='POST' action='$PHP_SELF'>
<input type='hidden' name='user_choice' value='high'>
<input type='hidden' name='card' value='$card'>
<input type=submit value='HIGHER'></form>

<form method='POST' action='$PHP_SELF'>
<input type='hidden' name='user_choice' value='low'>
<input type='hidden' name='card' value='$card'>
<input type=submit value='LOWER'></form>";
}

//if user selected low and old card is lower than new card
elseif("$user_choice" == "low" && "$card" < "$base_card")
{
echo "The Last Card was $base_card and you chose to go Lower and the card was $card so YOU WIN!";
}
?>

TheMadScientist

8:30 pm on Aug 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try changing you if()s to elseif()s... Right now they're all being evaluated. It'll be a bit more efficient for sure and may fix the issue.

You might also remove the "" from around the variables in the if statements... They are unnecessary.

I would probably put the 'pair check' as the last one. Check to see if it's higher or lower first... If not and you know there was a 'guess' you know they were a match, so you could even 'default' to it if it's at the end and you check to see if there was a 'guess' made.

EDITED:
This is the issue:
if($base_card === $card)

Change it to:
if($base_card == $card)

=== Matches type of variable.

POSTed variables are strings.
Random numbers are numeric.
They won't ever be === unless you convert the POSTed value to a numeric type or the random number to a string.