CS1101C Lab 4 (Odd Week)

Tic-Tac-Toe #1

The deadline for this lab question is Friday 04 April 2008, 23:59:59 hours.

The name of your C program file must be called ttt1.c, files with any other name will not be marked.

Sample Runs

Assuming that the executable is ttt1, sample runs of the program are shown below. The sample runs are self-explanatory. Follow the sample output precisely. User input is denoted in bold.

$ gcc -Wall ttt1.c -o ttt1

$ ./ttt1

Welcome to the game of Tic-Tac-Toe!

Each square in the 3x3 board is numbered as shown:

 0 | 1 | 2
---+---+---
 3 | 4 | 5
---+---+---
 6 | 7 | 8

Enter move for player X (0-8, anything else quits): 9

Thank you for playing tic-tac-toe.

$ ./ttt1

Welcome to the game of Tic-Tac-Toe!

Each square in the 3x3 board is numbered as shown:

 0 | 1 | 2
---+---+---
 3 | 4 | 5
---+---+---
 6 | 7 | 8

Enter move for player X (0-8, anything else quits): 0

Valid move by player X at (0, 0).
X . .
. . .
. . .

Enter move for player O (0-8, anything else quits): 0

Invalid move by player O, (0, 0) is already occupied.

Enter move for player O (0-8, anything else quits): 1

Valid move by player O at (0, 1).
X O .
. . .
. . .

Enter move for player X (0-8, anything else quits): 1

Invalid move by player X, (0, 1) is already occupied.

Enter move for player X (0-8, anything else quits): 2

Valid move by player X at (0, 2).
X O X
. . .
. . .

Enter move for player O (0-8, anything else quits): 3

Valid move by player O at (1, 0).
X O X
O . .
. . .

Enter move for player X (0-8, anything else quits): 4

Valid move by player X at (1, 1).
X O X
O X .
. . .

Enter move for player O (0-8, anything else quits): 5

Valid move by player O at (1, 2).
X O X
O X O
. . .

Enter move for player X (0-8, anything else quits): 6

Valid move by player X at (2, 0).
X O X
O X O
X . .

Player X wins!

$ ./ttt1

Welcome to the game of Tic-Tac-Toe!

Each square in the 3x3 board is numbered as shown:

 0 | 1 | 2
---+---+---
 3 | 4 | 5
---+---+---
 6 | 7 | 8

Enter move for player X (0-8, anything else quits): 0

Valid move by player X at (0, 0).
X . .
. . .
. . .

Enter move for player O (0-8, anything else quits): 1

Valid move by player O at (0, 1).
X O .
. . .
. . .

Enter move for player X (0-8, anything else quits): 2

Valid move by player X at (0, 2).
X O X
. . .
. . .

Enter move for player O (0-8, anything else quits): 3

Valid move by player O at (1, 0).
X O X
O . .
. . .

Enter move for player X (0-8, anything else quits): 5

Valid move by player X at (1, 2).
X O X
O . X
. . .

Enter move for player O (0-8, anything else quits): 4

Valid move by player O at (1, 1).
X O X
O O X
. . .

Enter move for player X (0-8, anything else quits): 7

Valid move by player X at (2, 1).
X O X
O O X
. X .

Enter move for player O (0-8, anything else quits): 6

Valid move by player O at (2, 0).
X O X
O O X
O X .

Enter move for player X (0-8, anything else quits): 8

Valid move by player X at (2, 2).
X O X
O O X
O X X

Player X wins!

$ ./ttt1

Welcome to the game of Tic-Tac-Toe!

Each square in the 3x3 board is numbered as shown:

 0 | 1 | 2
---+---+---
 3 | 4 | 5
---+---+---
 6 | 7 | 8

Enter move for player X (0-8, anything else quits): 4

Valid move by player X at (1, 1).
. . .
. X .
. . .

Enter move for player O (0-8, anything else quits): 6

Valid move by player O at (2, 0).
. . .
. X .
O . .

Enter move for player X (0-8, anything else quits): 2

Valid move by player X at (0, 2).
. . X
. X .
O . .

Enter move for player O (0-8, anything else quits): 8

Valid move by player O at (2, 2).
. . X
. X .
O . O

Enter move for player X (0-8, anything else quits): 7

Valid move by player X at (2, 1).
. . X
. X .
O X O

Enter move for player O (0-8, anything else quits): 1

Valid move by player O at (0, 1).
. O X
. X .
O X O

Enter move for player X (0-8, anything else quits): 3

Valid move by player X at (1, 0).
. O X
X X .
O X O

Enter move for player O (0-8, anything else quits): 5

Valid move by player O at (1, 2).
. O X
X X O
O X O

Enter move for player X (0-8, anything else quits): 0

Valid move by player X at (0, 0).
X O X
X X O
O X O

Draw result. How boring.

$

Note and Ponder

  1. Assume that all user input are integers.

  2. If your program does not work as you expect (logical errors), use extra printf statements to print out all the values of your variables to aid in your debugging.

  3. How do you know if your program is working perfectly? Did you test your programs with your own test data? What constitutes as good test data?

  4. Most importantly, have lots of fun programming!


This document, index.html, has been accessed 26 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 16 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 8 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.