The goal of this assignment is to practise using 2-dimensional arrays and modular programming.
This assignment is worth 2% of your semester grade.
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>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.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() */
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 |
5. (2 points) Make sure you do the following:
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
please enter spot: 2
1 0 3
4 5 6
7 8 9
please enter spot: 5
1 0 3
4 0 6
7 8 9
please enter spot: 10
invalid entry. please try again
please enter spot: 7
1 0 3
4 0 6
0 8 9
please enter spot: 4
1 0 3
0 0 6
0 8 9
please enter spot: 1
0 0 3
0 0 6
0 8 9
please enter spot: 9
0 0 3
0 0 6
0 8 0
please enter spot: 3
0 0 0
0 0 6
0 8 0
please enter spot: 6
0 0 0
0 0 0
0 8 0
please enter spot: 8
0 0 0
0 0 0
0 0 0"c:\mc140\ass5\lcc\ass5.exe"
Return code 0
Execution time 15.654 seconds
Press any key to continue...