lang="en-US"> PHP Tutorial: Number Guessing Game (no database required) –, LLC

PHP Tutorial: Number Guessing Game (no database required)

Wow I guess it’s been a while since I’ve posted anything. I’ve been busy moving and working on my games but I did find a few minutes to make this little game. This randomly picks a number and then lets the player try to guess the number. If you like this game you might also like my Ajax Tic-Tac-Toe game or Ajax Hangman game. This assumes that you’ve already done some of my other game tutorials so you are familiar with my game manager and how it works. You can read the code below or you can be lazy and can download the game or view a working example.

            $this->min = $min;
        if (!$max)
            $max = $this->max;
            $this->max = $max;
        //setup the game
        //generate the random number
        $this->randomNumber = rand($min, $max);
        //set how many guesses they get before it's a game over
        if ($max_guesses)
    * Purpose: set or retrieve maximum guesses before game over
    * Preconditions: 
    * Postconditions: 
    function playGame($_POST)
        //player pressed the button to start a new game
        if ($_POST['newgame'] || empty($this->guesses))
        //player is trying to guess a number
        if (!$this->isOver() && $_POST['number'])
            echo $this->guess($_POST['number']);
        //display the game
    * Purpose: set or retrieve maximum guesses they can make
    * Preconditions: amount of guesses (optional)
    * Postconditions: guesses has been updated
    function setGuesses($amount = 0)
        $this->guesses += $amount;
    * Purpose: display the game interface
    * Preconditions: none
    * Postconditions: start a game or keep playing the current game
    function displayGame()
        //while the game isn't over
        if (!$this->isOver())
            echo "
I'm thinking of a number from " . number_format($this->min) . " to " . number_format($this->max) . ".
                        Your Guess:                                                                    
";         }         else         {             //they've won the game             if ($this->won)                 echo successMsg("Congratulations you guessed my number $this->randomNumber! You've won the game.
                                Your final score was: $this->score");             else if ($this->health < 0)             {                 echo errorMsg("Game Over! Good try, my number was $this->randomNumber.
                                Your final score was: $this->score");             }             echo "
";         }     }          /**     * Purpose: guess a number in this word     * Preconditions: a game has started     * Postconditions: the game data is updated     **/     function guess($number)     {                     if ($this->isOver())             return;         if (!$number || !is_numeric($number))             return errorMsg("Oops! Please enter a number.");                       if ($number < $this->min)             return errorMsg("You must enter $this->min or higher.");                       if ($number > $this->max)             return errorMsg("You must enter $this->max ot lower.");                  //if the word contains this number         if ($number == $this->randomNumber) {             $this->won = true;         }         else //word doesn't contain the number         {                      //reduce their health             $this->setHealth(ceil(100/$this->guesses) * -1);                          //calculate 1/6 the random number to get a distance factor             $distance = floor($this->randomNumber / 8);                          if ($this->isOver())                 return;             else {                 if ($number < $this->randomNumber - ($distance * 4) || $number > $this->randomNumber + ($distance * 4))                     return errorMsg("So cold.");                 else if ($number < $this->randomNumber - ($distance * 3) || $number > $this->randomNumber + ($distance * 3))                     return errorMsg("You're getting warmer...");                 else if ($number < $this->randomNumber - ($distance * 2) || $number > $this->randomNumber + ($distance * 2))                     return errorMsg("Warm, Warm, Warm.");                 else if ($number < $this->randomNumber - $distance || $number > $this->randomNumber + $distance)                     return errorMsg("You getting hot!");                 else                     return errorMsg("You're really hot!!");             }         }     } }

You may also like...

Leave a Reply