While taking an introductory computer science course that is way below my skill level, I've met several people who understand the syntax of C++, but aren't quite able to understand how a complete
program should fit together. In this tutorial, I hope to accomplish two ends: provide an extremely basic understanding of how the parts of a game can be fit together, and give an example of how to
conceptualize and construct a basic computer program in C++ - one that actually does something that is mildly interesting. This tutorial isn't really intended as an introduction to programming or to
the C++ language - rather it's an example of how you can put a program together once you know the language.
As the title suggests, I chose to implement the simplest game I could possibly think of - guess the number. The game is displayed using text in a console window, so anyone who has done just about
anything with C++ should be familiar with the syntax used to output information.
First, we'll outline the features we want in the game, outside of the absolutely basic functionality of being able to pick a number to see if it's the correct one. These will include:
- Ability to choose a difficulty setting, which will correspond to the size of the range of possible values
- Ability to keep track of the number of guesses the user has made so far
- Provide the player with feedback about incorrect guesses: is the guess lower than the actual value, or higher than the actual value? Guesses that are out of range should be caught so the playerdoesn't waste a guess on a number they could know for certain is incorrect.
That's about all there is to it! Let's outline what will actually need to go into our code:
Well, actually, for a game this simple (and for a text-mode display), we only really need to develop one object. I'm going to call the class "GuessTheNumberGame". It will handle generation of the
number to be guessed, reporting of whether a particular guess is correct, lower, or higher than the actual value, and will keep track of the total number of guesses made in a particular game. We'll
use the output information from the "GuessTheNumberGame" methods to tell our player about the state of the game.
There are a few STL libraries we'll need for this project. First, we'll need the iostream library, which will allow us to perform input and output using the std::cin and std::cout objects. Next,
we'll need the cstdlib library, which will allow us to generate random numbers via the std::rand() function, and we'll need the ctime library to allow us to seed the random number generator (using
the std::srand() function) so the user doesn't get the same set of numbers each time they run the program.
Let's start by defining the interface for our "GuessTheNumberGame" class. Going from the above specifications, we'll need a method to generate a new game, a method to specify what the player's
next guess is, and a method to get the number of guesses the player has made since the game was set up.
The first method, which I'll call "NewGame", will need an argument to tell the "GuessTheNumberGame" class the size of the range of numbers to choose from. The minimum value of the range will
always be 1, and the maximum will be equal to 1 + (size of range).
The second method, which I'll call "Guess", will need an argument to tell the "GuessTheNumberGame" class what the player's guess was. It will return one of several enumerated values so that we can
learn what the result of the guess was. The player could have correctly guessed the number, guessed lower than the number, guessed higher than the number, or guessed outside of the range of allowed
values. We'll need to store this returned value and deal with each possible case.
The final method, which I'll call "GetGuessCount", will just return an integer value equal to the number of guesses the player has made. We won't store guesses that were outside the possible range
of values as guesses. Thus, if the player is to guess a number between 1 and 10, and guesses 17, the guess will NOT be recorded.
The class will also need to store a few values. It will need to keep track of what the number to be guessed is, the range of numbers to guess from, and the number of guesses made so far.
We now have plenty of information to design our class. Take a look at GuessTheNumberGame.h for the class interface and GuessTheNumberGame.cpp for the implementation. The code is very
well-documented, so you should be able to understand exactly what's going on just by reading the comments.
Well, that was pretty simple! Now we just need to construct a driver program that utilizes the above class. The driver will just play one game with the player, then quit. The player can select one
of four difficulty settings: easy, medium, hard, and INSANE!. Easy difficulty will have the range 1-10, medium 1-50, hard 1-100, and INSANE! 1-1000. Once we have the difficulty, we call NewGame on an
instance of GuessTheNumberGame with the appropriate range.
Next, we enter something that you will see on practically every game you ever make: the game loop. This loop, in its simplest form, says "Update the game once per cycle until the player has won
the game." In our case, every time the loop cycles, it prompts the player for a guess, then sends the player's input to the Guess() method of GuessTheNumberGame. The result of the guess is displayed
to the player. When the player has guessed the number correctly, the loop exits and the program ends.
Well, that's about all there is to it. I hope that you'll understand the documentation of the
source. I also hope that someone out there will actually find this useful. Please let me know if there's anything I can do to improve this tutorial, if you loved or hated it, or if you would like
to encourage me to get outside into the sunshine. I can be contacted by email at firstname.lastname@example.org, or via GameDev.net private message to the account silverphyre673. I'd love to write another
tutorial on a more-complex game. If this one is well-received, then I might actually get around to it :)
I am not a professional programmer. I have been programming as a hobby for the last five years or so, and have taken a few courses at a local community college. I've written several games
of my own design along with other non-game-related applications. Despite my lack of a degree, I think my years of personal experience and the simplicity of the subject matter presented are more than
enought to qualify me to write this article.
When I'm not programming, I'm riding my bicycle, playing my piano, sleeping, eating, or inviting pretty girls to dinner with mixed success.