Jump to content

Recommended Posts

hello guys

i have some guessing game code that works fine

i just need a few things adding to it that i dont know how to do if some body could please show me how.

its just a simple number game where the user has to keep guessing numbers between 1 and 10 till they get it right.

could somebody please how me how to add to the code so that.

- there is a counter that tells the user how many guesses they have had.

- the program stops at 7 guesses.

 

thanks guys 

 

my code is

 

 

 

if(isset($_POST['guess'])) {
    $guess = intval($_POST['guess']);
    $secret = intval($_POST['secret']);
 
    $output = "";
    
    if($guess == $secret)
    {
        $output .= "<h1>Correct!</h1>";
 
        $output .= "<a href='guessinggame.php'>Play again</a>";
    }
    elseif($guess < $secret)
    {
        $output .= "<h1>Your answer was too low. Please try again!";
    }
    else
    {
        $output .= "<h2>Your answer was too high. Please try again!</h2>";
    }
}
 
 
?>
Link to comment
https://forums.phpfreaks.com/topic/276085-php-guessing-game-help/
Share on other sites

ok i have tried some code but i have an error on the first line.

could someone please tell me how to fix it.

the error is Parse error: syntax error, unexpected '=', expecting ',' or ')' in F:\EasyPHP-12.1\www\scripting\guessinggame.php on line 1

 

and my code is

 

 

 

<?php
if(isset($_POST['guess'])){if(!isset($_SESSION['life']=1);
else if($_SESSION['life']==7){
echo "Your Life end<br>Thanks for playing<br>";
unset($_SESSION['life']);
}
else
$_SESSION['life']++;
$guess=intval($_POST['guess']);
$secret=rand(1,10);
if($guess==$secret){
$output="<h1>Correct!</h1>";
$output .="<a href='guessinggame.php'>Play again</a>";
unset($_SESSION['life']);
}
elseif($guess<$secret){
$output.="<h1>Your answer was too low. Please try again!";
}
else{
$output.="<h2>Your answer was too high. Please try again!</h2>";
}
}
?>
 
[code/]
 
any ideas???

Was bored, so I gave this a little go.

 

Try this:

 

<?PHP

  session_start();
  #session_destroy();
 
  //### Current file name
  $filename = basename($_SERVER['PHP_SELF']);
 
  //### If play is set, reset all values
  if(isset($_GET['play'])) {
    unset($_SESSION['number']);  
    unset($_SESSION['guesses']);
    unset($_SESSION['guessed']);
  }
 
  //### Start the output
  $output = '';
 
  //### Check if the form is posted
  if($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    //### Set the number that was guessed
    $numberGuessed = isset($_POST['numberGuessed']) ? (int)$_POST['numberGuessed'] : FALSE ;
    
    //### If no number is guessed, display error
    if(empty($numberGuessed)) {
      $output .= 'Please enter a number to guess. <br>';
      
    //### Validation passed  
    } else {    
        
      //### Increment guesses
      $_SESSION['guesses']++;
        
      //### Check to make sure they have guesses available
      if($_SESSION['guesses'] <= 7) {
          
        //### check if they guessed the secret number
        if($numberGuessed == $_SESSION['number']) {
          $_SESSION['guessed'] = TRUE;
          
        //### check if the number is lower than the secret number
        } else if($_SESSION['number'] < $numberGuessed) {
          $output .= 'Bad luck! The number is lower. <br>';
        
        //### Else the number is higher
        } else {
          $output .= 'Bad luck! The number is higher. <br>';
        }
        
      }
    }
  }
 
  //### Set the secret number and amount of guesses
  $_SESSION['number']  = isset($_SESSION['number']) ? $_SESSION['number'] : mt_rand(1,10) ;
  $_SESSION['guesses'] = isset($_SESSION['guesses']) ? min($_SESSION['guesses'], 7) : 0 ;
 
  //### Debug purposes
  echo '<pre>';
  print_r($_SESSION);
  echo '</pre>';
 
  //### Check if the secret number has been guessed
  if(isset($_SESSION['guessed'])) {
    $output .= 'Well done! You guessed the number. <br>';
    $output .= '<a href="'.$filename.'?play=true">Play again?</a>';  
    
  //### Check if they have used all of their guesses
  } else if($_SESSION['guesses'] >= 7) {
    $output .= 'You used up all your guesses. <br>';
    $output .= 'The secret number was: '. $_SESSION['number'] .'. <br>';
    $output .= '<a href="'.$filename.'?play=true">Play again?</a>';
    
  //### If both checks passed, show form
  } else {
    $output .= '<form method="POST" action="'. $filename .'">
            Your Guess: <input type="text" name="numberGuessed">
            <input type="submit" value="Submit Guess">
          </form>';
  }
 
  echo $output;
?>

Ohh right, well you need to create a new session variable something like "$_SESSION['numbers_guessed']" and then add each guessed number that is wrong to that variable. Look into array for that.

 

Displaying amount of guesses I explained in my previous post, use the variable "$_SESSION['guesses']"

 

I did the hard work, now have a little try to see what you can do, and pop back with any questions :)

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.