Build a Game Slowly

Since those multiple guess - er - choice exams are pretty tough, and it looks like you need some help, I am going to offer a special extra credit project to help out. Exactly how much this will help depends on what you choose to do, that will be discussed at the end of this note. This project can be worth an exam grade, so it will be worth your time trying it.


Some parts of this project require using concepts we have not covered yet. Do not get too worried about that. You have until the end of the term to complete this project. As you learn something new, think about how that technique might help improve your game.

I am going to give you a fairly simple programming project, but I want you to work it using the “Baby Step” approach discussed in the previous note.

The project involves no graphics, and is a relatively easy console version of a classic game. We will not use anything fancy, like artificial intelligence to get the computer to play one side. Instead, you will play both sides to prove your game works!

While it would be fun to build a shoot-em-up space alien game, that is probably a bit too involved for this point in your programming career. So, let’s try a simple variant of:


For our version of this game, we are just going to set up a simple display so we can record the game on the screen and see who won. This should be fairly simple, so don’t get too worried about the task.

The basic playing field

We want to display a simple board where we will place the ‘’‘X’‘’ and ‘’‘O’‘’ symbols. Since these are characters, we will set up a simple module that draws the board using simple cout statements. You will place a symbol character where it should go, or a blank character if no symbol has been placed in that particular position. (Actually, we will not put blanks in the drawing, see below).

Here is one way to display the basic board:

 | |
 | |
 | |

This drawing is just a bunch of characters: vertical bars for the vertical lines, and dashes for the horizontal lines. There are blanks showing where the players would put a symbol, but we will do something different with those spaces in a moment. Since there are nine spots where a symbol could go, we will set up an array of nine characters, and fill in one of those spots with an ‘’‘X’‘’ of ‘’‘O’‘’ as the game is played.

Playing the game

To make playing the game easy, let’s number the spaces - like so:


All we need to do is ask the player to select the numbered space where they want to place their symbol. Once they provide the number, we figure out how to update the board array to show their character in the right space. If you have your board display module set up, and replace the chosen number with the right symbol, redrawing the display will show the new game situation. (Note that you just print out a new board, you do not have to write over the old board in this game, although some clever students in previous classes figured out an easy way to do that. Hint: There are many character codes that do not display anything, but they make the cursor move around.)

Checking for wins

Now, how do we tell if the game has been won. There are several possibilities, right? Assuming you know how this game is played, we need to check for a number of situations. Like this:

if ((board[0] == 'X') && (board[1] == 'X') && (board[2] == X)) {
    // looks like a win

In this example, the nine spots where players can choose to place their marker are stored in an array of characters named “board”.

Obviously there is a bit more to this, but you should get the picture.

Your task

Write a program that lets two players play the game until one wins. The first player will place X markers on the board, then the next player will place the O markers. Your program should show the board for each turn and ask each player to place their marker (you should tell them which marker to place on each turn). After each move is made, check the board to see if the current player won the game. If so, announce a winner, and end the game. If all the spaces are filled and no one won, we should tell the played that they tied (a draw).

Pay attention to good style in your code. By now you should know what nicely presented code looks like, and I will be looking for this. I will compile your program, play a simple game or two, then check to see that it looks good before assigning a final grade.

Project Report

Here is the hard part. Well, not exactly hard, but different. I want you to keep a log of the steps you take to build this project. You can use any tool you like to build this log. (I just use my trusty programmer’s editor. Not the one in Visual Studio, just a good text editor.) I am not looking for a lot of text, just notes that explain what you are trying to do, and what the result is each time you take a step. If your steps seem to be leading you to a nice program, great! If they lead you to a dead end, say so, then back up and try another approach. I am actually more interested in seeing how you solve this project, not in the actual game you create. This is a chance to do two things: create code, and create documentation on your thinking!

The sad thing about most programming courses is that the instructor never sees how you got to that final code you turn in, and in many cases, how you got there is not the way you should have gotten there. I try to show you the path you should take to get your code written, not just the final product.

Your final report should be a cleaned up version of your logs. Try to tell a story if you wish. (This is not an English Composition class, so do not spend too much time on this part!) Pretend you are creating something for some poor soul in the future who will have to work on your code. Here is another saying:

Pretend that the person who has to maintain your code is a maniac who knows where you live.

OK, a bit extreme, but you should get the point!

Here are some things to consider on each step:

  • what feature am I trying to add to my program? (make it small)
  • What programming tools do I need to add this feature?
  • How will I know if it worked properly? (This is testing)

What to turn in

Your program and final report are due by Sunday at midnight, May 15 (the official end of this term). Upload these two files to Blackboard by that time. You will get partial credit for your work, even if the game does not run correctly. Just document what you were trying to do, and Why you think your program has problems. Turn it in and I will grade the code you wrote plus your report to assign the final grade for this exam.

ANy attempt to complete this project will improve your final grade. Give it a try!

Have fun with this, many students before you have managed to get this done nicely!

WHat is This Worth?

I will replace your worst exam grade with the grade you get for this work. However, here is how I will grade it:

  • If it works properly, and solves the problem assigned, but the report is missing or very weak, you will get a “C”. The idea here is that a programmer is expected to be able to write a working program that solves a problem. That is really he minimum expected result. That is not what you want to do, though.
  • If your report (documentation) is good enough to explain your thinking, and that would help a maintainer of the code, that can earn an “A”. You want to be the programmer who is so good at what they do, if there are personnel cuts to make, they are not about to cut you! Be better than the next person, and you will always move upward in your career. Hopefully, that is what you do want to do!