Creating a Number Guessing Game
by Larry Kubin
In this tutorial, we will create a simple number guessing game. The game will consist of a single controller containing the game logic and a view containing the user interface. This tutorial assumes you have already successfully installed the Stratos Framework.
Rules of the Game
The requirements for our number guessing game are as follows:
- The program should generate a random number between 1 and 10. This is the number that the player will attempt to guess.
- A form should be displayed where the player will enter his/her guess
- When the form is submitted, the player will be informed whether the guess was too high, too low, or exactly right.
- If the number guessed matches the random number that was generated, the player will see a success message, and a new random number will be generated so that the game can be played again.
The Controller
Let's get started by creating the class that will contain the game logic. First, create an empty class called GuessNumberController:
<?php class GuessNumberController { } ?>
Sure it's not much to look at right now, but we'll change all of that soon. I promise. For now, note that we used the word Controller in the class name. This is a requirement when creating controllers in a Stratos application. Once you are done, save your class to the controllers/ directory with the filename GuessNumberController.php.
Now, let's modify that class to give it some functionality. First, let's add a variable to the class for the random number.
<?php class GuessNumberController { var $randomNumber; } ?>
So far so good. Now let's create some methods for our class. Our first method will be called _generateNumber(). It will take care of generating a new random number, storing it in the session, and returning the number back to the caller. Our second method will be the start() method. The start() method is a special method that contains code that should be executed before any method in the class. This method is not required, but we will use it to ensure that a random number has been generated. Our start() method will check to see if there is a number already in the session. If so, then it will set $this->randomNumber equal to the random number in the session. If not, it will call the _generateNumber() method in order to get a new random number. Let's code it:
<?php class GuessNumberController { var $randomNumber; function _generateNumber() { $_SESSION['randomNumber'] = rand(1, 10); return $_SESSION['randomNumber']; } function start() { $this->randomNumber = isset($_SESSION['randomNumber']) ? $_SESSION['randomNumber'] : $this->_generateNumber(); } } ?>
Note that we prefixed our _generateNumber() method with an underscore. This is a convention that is used to denote that this method is private and can not be executed from the web. The start() method is a special method that will automatically run before any other method in the class is executed. It will ensure that we have a random number before we do anything else.
Now that we are prepared to generate random numbers, let's add the guessNumber() method, which will process the player's guess by comparing the guess to the random number we generated.
<?php class GuessNumberController { var $randomNumber; function _generateNumber() { $_SESSION['randomNumber'] = rand(1, 10); return $_SESSION['randomNumber']; } function start() { $this->randomNumber = isset($_SESSION['randomNumber']) ? $_SESSION['randomNumber'] : $this->_generateNumber(); } function guessNumber( $number_guessed = null, $submit = false ) { $number_guessed = (int) $number_guessed; $message = ''; if( $submit ) { if( $number_guessed == $this->randomNumber ) { $message = "You guessed the number. Let's play again!"; unset($_SESSION['randomNumber']); } elseif( $number_guessed < $this->randomNumber ) { $message = "Your guess ($number_guessed) was too low! Try again."; } elseif( $number_guessed > $this->randomNumber ) { $message = "Your guess ($number_guessed) was too high! Try again."; } } return array( 'message' => $message ); } } ?>
You'll notice that we have added two parameters to the method's signature. The first parameter is called $number_guessed, and we gave it a default value of null. This value will be automatically filled in when the player submits the form. We called the second parameter $submit, and we will use it to tell whether or not the form has been submitted. In our example, we cast the $number_guessed to an integer, so any non-numeric value will become 0. Then, we checked to see if the form was submitted. If the form was submitted, we compared the number guessed to the random number, and set the message variable to an appropriate message. If the player guessed the number correctly, we unset the session variable so that a new random number would be generated by the start() method the next time the controller is executed.
The View
Wait a second. Our class is finished, but we haven't given the player a way to guess a number. Let's create the view. Create a new script called GuessNumberController.guessNumber.php and save it to the views/ directory of your application. Create a form for accepting a number, like so:
<?= $message ?>
<form id="guess_form" name="guess_form" action="" method="get">
<input id="number_guessed" name="number_guessed" type="text" />
<input id="submit" name="submit" value="Guess" type="submit" />
</form>
That's all there is to it! As you can see, the message we returned from the controller is placed above the form. Each time the user submits a number, a message will be returned to the view for display. This message will indicate whether the user's guess was too high, too low, or exactly right.
Also, note how the form inputs are named the same as the parameters to our controller's method. Stratos will automatically detect this and pass the input values to our method.
Testing It Out
To test out the number guessing game, simply point your browser to the controller by using the URL:
http://www.yourdomain/your_path/index.php/GuessNumber/guessNumber
You should now be able to play the number guessing game using the form you created.
Learn More
The applications you create using Stratos will be much more complicated than the one discussed in this tutorial. To find out more, read the message board tutorial, which is a more detailed example application that demonstrates database access, roles and authorizations, and other features of the Stratos PHP Framework.