you're reading...

A Simple C++ Game

Basics of Game Programming in C++
Slides are here. Video is here.

using namespace std;

int difficulty = 10;
char input = 'n';
int lowerBound = 0;
int upperBound = difficulty + 1;
int myGuess = (upperBound-lowerBound)/2; 

bool MainMenu();
void ShowStory();
void ResetGame();
void GetInput();
bool UpdateWorld();
void DrawGraphics();
bool GameOverScreen();
bool PlayGame();

int main() {
while(MainMenu()) {
return 0;

bool PlayGame() {

  while(input != 'y') { // This is the main game loop


  return GameOverScreen();

bool MainMenu()	{
  cout << "Welcome to the Game!" << endl;
  while(true) {
    cout << "1. Play" << endl;
    cout << "2. Exit" << endl;
    cout << ">" << endl;
    cin >> input;

    if(input == '1') return true; // This play the game
    else if(input == '2') return false; // This will quit

// This function could show a little cutscene intro story.
// I've used it to display some instructions.
void ShowStory() {
  input = 'n'; // Make sure input is not 'y'

  cout << " Once upon a time..." << endl;
  cout << "You thought of a number between 1 and " << difficulty << endl;
  while(input != 'y') {
    cout << "Have you got one (y/n)? ";
    cin >> input;
    if(input != 'y') cout << "Ok, I'll wait a bit..." << endl;

  cout << endl;

void ResetGame() {
  lowerBound = 0;
  upperBound = difficulty + 1;
  input = 'n';

bool UpdateWorld() {
  myGuess = (lowerBound + upperBound) / 2; // Generate the next guess
  return true;

void DrawGraphics()	{
  cout << "I think it's " << myGuess << "! Am I right?" << endl;
  cout << "l. Lower " << endl;
  cout << "h. Higher" << endl;
  cout << "y. Yes!" << endl;

void GetInput()	{
  cin >> input; // Read something from the keyboard

  // Act on input:
  switch(input) {
  case 'l': upperBound = myGuess; break;
  case 'h': lowerBound = myGuess; break;
  case 'y': cout << "YaY!!!" << endl; break;

bool GameOverScreen()
  cout << endl;
  cout << "That was pretty good, play again (y/n)? " << endl;
  cin >> input;

  if(input == 'y') return true;
  else return false;

Complete this TODO list of 10 tasks:
1. Run the program a number of times and understand which function you are in at each point in the game.
2. On a piece of paper write down all the major functions. And draw arrows each time a function is called from the source or “caller function” to the destination or “called function”.
3. Add some text to the ShowStory() function. Add text to the MainMenu(). In addition, before each of the return statements add appropriate text to the cout output terminal.
4. In the GetInput() function add several lines expressing joy for winning the guessing game.
5. Do a global replace on myGuess and replace with AI-guess.
6. Add a default value in the switch statement for the GetInput() function.
7. In the main loop in PlayGame() function, change the while condition to use a boolean userDone variable.
Declare this as a global variable, and make appropriate changes to the code.
8. When the game is reset in ResetGame() make it harder by increasing the difficulty.
9. Add the option of allowing the user to choose the difficulty level in the function GameOverScreen().
10. Add logic to Update World so that it remembers the last guess it made – and then if the AI makes the same guess twice in a row then it knows that the user cheated, and prints out a nasty message and returns false to end the game.

Lab Homework Assignment: Complete all 10 TODO tasks and then refactor the program using parameter passing so that there are no global variables. Code refactoring is the process of restructuring existing computer code without changing its external behavior. Eliminating global variables improves software quality and maintainability.



No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: