### Computer Science I

MC140.01
Spring 2001
Assignment 5
due Wednesday 28 February 2001

The goal of this assignment is to practise using 2-dimensional arrays and modular programming.

Read through the entire assignment before you begin!!!

Tic-Tac-Toe.
The game of tic-tac-toe is played on a 3x3 board. Players take turns marking the board with either X's or 0's.
In this assignment and the next one, you will write a program to play tic-tac-toe.
The first thing to do when writing a computer program to play a game is to decide on a representation for the game such that you can store it in the computer, update it easily and report it to a human player easily (if it is an interactive game).
For tic-tac-toe, we will use a 3x3 matrix (2-dimensional array) to represent the board. Each spot on the board will be given a number, so a human player can easily indicate which spot s/he wants to mark. So the board representation will look like this:

 1 2 3 4 5 6 7 8 9

Write a program that will:
1. define a 3x3 matrix of integers to represent the game board.
2. initalize the game board to contain the numbers 1..9, arranged in the pattern shown above.
3. print the game board.
4. allow a human player to mark the game board with 0's, until all entries have been marked.

Steps to success

1.  (1 point) Start by typing in the following program skeleton:

#include <stdio.h>

int board[3][3];

void initBoard( void );
void printBoard( void );
void markBoard( void );

int main( void ) {
int numturns = 0;
initBoard();
printBoard();
for ( numturns=0; numturns<9; numturns++ ) {
markBoard();
printBoard();
} /* end for numturns */
return( 0 );
} /* end of main() */

2. (4 points) Write the function initBoard( ) that will initalize the game board to contain the numbers 1..9, arranged in the pattern shown above.

3. (5 points) Write the function printBoard( ) that will print the board, like this:

 1 2 3 4 5 6 7 8 9

4. (8 points) Write the function markBoard( ) that will ask the user to enter a number between 1 and 9 and, based on the user's input, will replace that entry in the board with a 0. For example, given the initialized board, above, if the user entered 0, the subsequently marked board would look like this:

 1 0 3 4 5 6 7 8 9
You should be able to handle invalid input from the user (i.e., numbers smaller than 1 or larger than 9).

5. (2 points) Make sure you do the following:

• You must include a header comment at the beginning of your source code file. The header comment must include your name, the assignment number and the date.
• You must include comments after each closing curly bracket ("}") indicating which program block is being ended. For example:  } /* end of for loop */Make sure that the name of the file you submit is entirely in lower-case letters and you use the proper
• You should indent your program according to the conventions we have been using in class or those used in the book. This means, each program block is indented.
• You must name your file entirely in lower-case letters and you use the proper naming format: <yourusername>-ass5.c, for example: sklarel-ass5.c.
6. Submit the assignment in the usual way (i.e., follow the submission instructions from assignment #1). Also, please bring a hardcopy to class!

Sample Run.
Here's a sample run. User input is in big, bold font. Notice that the program stops automatically when all the spots have been marked (this is handled by the for loop in the main program, as show in the skeleton above).

1  2  3
4  5  6
7  8  9
1  0  3
4  5  6
7  8  9
1  0  3
4  0  6
7  8  9
1  0  3
4  0  6
0  8  9
1  0  3
0  0  6
0  8  9
0  0  3
0  0  6
0  8  9
0  0  3
0  0  6
0  8  0
0  0  0
0  0  6
0  8  0