Project Tic Tac Toe -- Hints

Introduction
Representing the board
Trying to make a winning move
Trying to block a winning move
Testing if there is a winner
Making our move
Letting the human make his or her move

Introduction

You must break your program up into functions. There are many ways this can be done. What follows is one approach that you are free to use or ignore as you please.

Representing the board

The project specifically states that you are to use a multidimensional array to represent your game board. It is possible to store actual characters in the array, but we have not covered this in class. Instead, you probably will want to choose to use integers to represent what is in a square. Using a 0 for an empty space, a 1 for an X, and a 2 for an O seems natural.

void ClearBoard(int board[3][3]);
void PrintBoard(int board[3][3]);

Since simply declaring an array in main will leave it filled with unknown values, you need to be able to mark each square as "empty." This is a good place to demonstrate you can use functions. You can write ClearBoard function to do this.

You also need to print the game board. This is another good place for a function with a name like PrintBoard. This function would be the place to change the 0, 1, and 2 values into blanks, X's, and O's.

Trying to make a winning move

We touched on these functions in class. The TryWinLine function takes three locations, which should be in a line, and checks if our piece, us, can make a winning move. If so, make our winning move in this line and return 1 (true). If we can't, return 0 (false).

The TryWinBoard needs to call TryWinLine with all possible lines. If one succeeds, it is done.

int TryWinLine(int board[3][3], int us,
	       int r1, int c1,
	       int r2, int c2,
	       int r3, int c3);

int TryWinBoard(int board[3][3], int us);

Trying to block a winning move

The TryBlockLine function would be similar to the TryWinLine, except that it needs to figure out what piece the other player is using. If it finds two in the line of the other player's piece, and a open space, then put in our mark (us) in that open space and return 1 (true). Otherwise, return 0 (false).

The TryBlockBoard is similar to TryWinBoard, except it is looking to make a blocking move.

int TryBlockLine(int board[3][3], int us,
		 int r1, int c1,
		 int r2, int c2,
		 int r3, int c3);

int TryBlockBoard(int board[3][3], int us);

Testing if there is a winner

The TestWinLine function is very similar to the TryWinLine and TryBlockLine functions, except it is looking to see if the player whose marker is who has three in a row. The TestWinBoard uses this to see if the player using who has won already.

int TestWinLine(int board[3][3], int who,
		int r1, int c1,
		int r2, int c2,
		int r3, int c3);

int TestWinBoard(int board[3][3], int who);

Making our move

The ComputerMove function makes a move for the computer, which is using marker us. It should first try to make a winning move, then a blocking move, then some other move.

void ComputerMove(int board[3][3], int us);

Letting the human make his or her move

This lets the human player make a move. Be sure it is legal.

void HumanMove(int board[3][3], int them);

R. Lindsay Todd
Rensselaer Polytechnic Institute