< Previous | Contents | Next >

Planning the Game

This game is your most ambitious project yet. You certainly have all the skills you need to create it, but Im going to go through a longer planning section to help you get the big picture and understand how to create a larger program. Remember, the most important part of programming is planning to program. Without a roadmap, youll never get to where you want to go (or itll take you a lot longer as you travel the scenic route).


Rea l Worl d

image

Game designers work countless hours on concept papers, design documents, and prototypes before programmers write any game code. Once the design work is done, the programmers start their work— more planning. It’s only after programmers write their own technical designs that they then begin coding in earnest. The moral of this story? Plan. It’s easier to scrap a blueprint than a 50-story building.

image

204 Chapter 6 n References: Tic-Tac-Toe


Writing the Pseudocode

Its back to your favorite language thats not really a languagepseudocode. Because Ill be using functions for most of the tasks in the program, I can afford to think about the code at a pretty abstract level. Each line of pseudocode should feel like one function call. Later, all Ill have to do is write the functions that the plan implies. Heres the pseudocode:

Create an empty Tic-Tac-Toe board Display the game instructions Determine who goes first

Display the board

While nobody’s won and it’s not a tie If it’s the human’s turn

Get the human’s move

Update the board with the human’s move Otherwise

Calculate the computer’s move

Update the board with the computer’s move Display the board

Switch turns

Congratulate the winner or declare a tie


Representing the Data

All right, Ive got a good plan, but it is pretty abstract and talks about throwing around different elements that arent really defined in my mind yet. I see the idea of making a move as placing a piece on a game board. But how exactly am I going to represent the game board? Or a piece? Or a move?

Since Im going to display the game board on the screen, why not just represent a piece as a single characteran X or an O? An empty piece could just be a space. Therefore, the board itself could be a vector of chars. There are nine squares on a Tic-Tac-Toe board, so the vector should have nine elements. Each square on the board will correspond to an element in the vector. Figure 6.6 illustrates what I mean.

Each square or position on the board is represented by a number, 08. That means the vector will have nine elements, giving it position numbers 08. Because each move indicates a square where a piece should be placed, a move is also just a number, 08. That means a move could be represented as an int.

Introducing the Tic-Tac-Toe Game 205




image


image


image


image


image


image


image


image


image

Figure 6.6

Each square number corresponds to a position in the vector that represents the board.


The side the player and computer play could also be represented by a chareither an ’X’ or an ’O’, just like a game piece. A variable to represent the side of the current turn would also be a char, either an ’X’ or an ’O’.


Creating a List of Functions

The pseudocode inspires the different functions Ill need. I created a list of them, thinking about what each will do, what parameters theyll have, and what values theyll return. Table 6.1 shows the results of my efforts.