PHP program that will choose a random number

  • jamesbond
  • Novice
  • Novice
  • jamesbond
  • Posts: 23

Post 3+ Months Ago

I want to make a php program that will choose a random number and then the user will push a button telling the program to guess higher or lower or if it was the correct number. I have some code written already but every time i try to post it here it says no external links allowed and i dont have any links in my program and i have it wrapped in code tags. This is a php program and any help would be appreciated

Code: [ Select ]
<?php session_start() ?>
<center>
<button type="higher">To High</button>
<button type="correct">Correct</button>
<button type="lower">To Low</button>
</center>
<?php
$higher = $_POST["higher"];
$lower = $_POST["lower"];
$correct = $_POST["correct"];
$rand = $_SESSION['aNumber'];//assign rand
$count_guess = $_SESSION['count_guess'];//assign count_guess



if($correct == $rand)
{  
  unset($_SESSION['aNumber']);//if user guesses number then clear
  unset($_SESSION['count_guess']);//if user guesses the clear
}




?>
  1. <?php session_start() ?>
  2. <center>
  3. <button type="higher">To High</button>
  4. <button type="correct">Correct</button>
  5. <button type="lower">To Low</button>
  6. </center>
  7. <?php
  8. $higher = $_POST["higher"];
  9. $lower = $_POST["lower"];
  10. $correct = $_POST["correct"];
  11. $rand = $_SESSION['aNumber'];//assign rand
  12. $count_guess = $_SESSION['count_guess'];//assign count_guess
  13. if($correct == $rand)
  14. {  
  15.   unset($_SESSION['aNumber']);//if user guesses number then clear
  16.   unset($_SESSION['count_guess']);//if user guesses the clear
  17. }
  18. ?>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

This should work:
Code: [ Select ]
<?php
session_start();
if(!isset($_SESSION['new_guess']))
{
    $_SESSION['new_guess'] = rand();
    $_SESSION['highest_value'] = getrandmax();
    $_SESSION['lowest_value'] = 0;
}
else
{
    if(isset($_POST['guess']))
    {
        $guess = $_POST['guess'];
        switch($guess)
        {
            case 'higher':
                $_SESSION['lowest_value'] = $_SESSION['new_guess'];
                $_SESSION['new_guess'] = rand($_SESSION['new_guess'], $_SESSION['highest_value']);
                break;
            case 'lower':
                $_SESSION['highest_value'] = $_SESSION['new_guess'];
                $_SESSION['new_guess'] = rand($_SESSION['lowest_value'], $_SESSION['new_guess']);
                break;
            case 'correct':
                $_SESSION['new_guess'] = rand();
                $_SESSION['highest_value'] = getrandmax();
                $_SESSION['lowest_value'] = 0;
                break;
        }
    }
}

?>
<center>
<p><?php
echo 'The computer guessed:' . $_SESSION['new_guess'] . '<br /> and the next guess will be a value between: ' . $_SESSION['lowest_value'] . ' - ' . $_SESSION['highest_value'];
?> </p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="guess" value="higher" />
<input type="submit" name="guess" value="correct" />
<input type="submit" name="guess" value="lower" />
</form>
</center>
  1. <?php
  2. session_start();
  3. if(!isset($_SESSION['new_guess']))
  4. {
  5.     $_SESSION['new_guess'] = rand();
  6.     $_SESSION['highest_value'] = getrandmax();
  7.     $_SESSION['lowest_value'] = 0;
  8. }
  9. else
  10. {
  11.     if(isset($_POST['guess']))
  12.     {
  13.         $guess = $_POST['guess'];
  14.         switch($guess)
  15.         {
  16.             case 'higher':
  17.                 $_SESSION['lowest_value'] = $_SESSION['new_guess'];
  18.                 $_SESSION['new_guess'] = rand($_SESSION['new_guess'], $_SESSION['highest_value']);
  19.                 break;
  20.             case 'lower':
  21.                 $_SESSION['highest_value'] = $_SESSION['new_guess'];
  22.                 $_SESSION['new_guess'] = rand($_SESSION['lowest_value'], $_SESSION['new_guess']);
  23.                 break;
  24.             case 'correct':
  25.                 $_SESSION['new_guess'] = rand();
  26.                 $_SESSION['highest_value'] = getrandmax();
  27.                 $_SESSION['lowest_value'] = 0;
  28.                 break;
  29.         }
  30.     }
  31. }
  32. ?>
  33. <center>
  34. <p><?php
  35. echo 'The computer guessed:' . $_SESSION['new_guess'] . '<br /> and the next guess will be a value between: ' . $_SESSION['lowest_value'] . ' - ' . $_SESSION['highest_value'];
  36. ?> </p>
  37. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  38. <input type="submit" name="guess" value="higher" />
  39. <input type="submit" name="guess" value="correct" />
  40. <input type="submit" name="guess" value="lower" />
  41. </form>
  42. </center>


I was considering doing it using functions but then I thought that there was no need since this seems to be a none scaling "project". Anyhow I left out comments in the code due to lazyness but here is some basic explanation:

Code: [ Select ]
if(!isset($_SESSION['new_guess']))
{
    $_SESSION['new_guess'] = rand();
    $_SESSION['highest_value'] = getrandmax();
    $_SESSION['lowest_value'] = 0;
}
  1. if(!isset($_SESSION['new_guess']))
  2. {
  3.     $_SESSION['new_guess'] = rand();
  4.     $_SESSION['highest_value'] = getrandmax();
  5.     $_SESSION['lowest_value'] = 0;
  6. }

The code above checks to see if any guessed value has been declared. This is needed the first time the user loads the page or else there will be a warning about undeclared variables etc.

Code: [ Select ]
if(isset($_POST['guess']))

Line 11 checks if the form has been submited, this so you don't get any complaints about undeclared values. Then a switch is used along with three session values to keep track of the guesses and make sure the next guess isn't lower or higher than one of the previous.

As for the html I used <?php echo $_SERVER['PHP_SELF']; ?> to make the form submit the value to itself. Also I added "and the next guess will be a value between:", just to make it more clear how the "program" works.

There is one severe issue with this program that you should be aware of however: the guess could POSSIBLE be the same twice (or more). This could be solved with a while loop for instance.

Also if you want the program to guess more narrow, between 0 and 1000 for instance, you can do so by switching the two getrandmax() for a static value (1000). And if you need bigger values than what this can do then the function mt_rand and mt_getrandmax() could be used.
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

Oups (line 9-11):

Code: [ Select ]
else
{
  if(isset($_POST['guess']))
  {
  }
}
  1. else
  2. {
  3.   if(isset($_POST['guess']))
  4.   {
  5.   }
  6. }


Can (should) be replaced with:
Code: [ Select ]
else if(isset($_POST['guess']))
{
}
  1. else if(isset($_POST['guess']))
  2. {
  3. }
  • jamesbond
  • Novice
  • Novice
  • jamesbond
  • Posts: 23

Post 3+ Months Ago

thank you that was very helpful
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

jamesbond wrote:
thank you that was very helpful


Glad to read that the code helped you in some way. :)

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 89 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.