< Previous | Contents | Next >
This game is your most ambitious project yet. You certainly have all the skills you need to create it, but I’m 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, you’ll never get to where you want to go (or it’ll take you a lot longer as you travel the scenic route).
Rea l Worl d
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.
204 Chapter 6 n References: Tic-Tac-Toe
It’s back to your favorite language that’s not really a language—pseudocode. Because I’ll 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 I’ll have to do is write the functions that the plan implies. Here’s 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
All right, I’ve got a good plan, but it is pretty abstract and talks about throwing around different elements that aren’t 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 I’m going to display the game board on the screen, why not just represent a piece as a single character—an 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, 0–8. That means the vector will have nine elements, giving it position numbers 0–8. Because each move indicates a square where a piece should be placed, a move is also just a number, 0–8. That means a move could be represented as an int.
Introducing the Tic-Tac-Toe Game 205
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 char— either 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’.
The pseudocode inspires the different functions I’ll need. I created a list of them, thinking about what each will do, what parameters they’ll have, and what values they’ll return. Table 6.1 shows the results of my efforts.