CS1101C Lab 4 (Even Week)

Tic-Tac-Toe #2

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

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

This week's lab is based on last week's lab question. If you have not read it, please do so now.

Last week, we allowed two players to play tic-tac-toe. This week, we want to see the outcome of players place their pieces randomly on unoccupied squares. Assume that Player X always moves first. What are the probabilities of player X winning, player O winning, and a draw result?

You may wish to review the material on Command Line Arguments in Lecture 10, as well as the program conversion1.c.

Sample Runs for ttt2beta.c

ttt2beta.c is the preliminary version of the program. You may wish to complete this version first before going on to writing ttt2.c. You do not need to submit ttt2beta.c.

Assuming that the executable is ttt2beta, 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 ttt2beta.c -o ttt2beta

$ ./ttt2beta

Usage: ./ttt2beta seed

$ ./ttt2beta 6105

Seed: 6105

Move by player X at (0, 0).
X . .
. . .
. . .

Move by player O at (0, 1).
X O .
. . .
. . .

Move by player X at (1, 1).
X O .
. X .
. . .

Move by player O at (2, 2).
X O .
. X .
. . O

Move by player X at (2, 0).
X O .
. X .
X . O

Move by player O at (0, 2).
X O O
. X .
X . O

Move by player X at (1, 2).
X O O
. X X
X . O

Move by player O at (1, 0).
X O O
O X X
X . O

Move by player X at (2, 1).
X O O
O X X
X X O

Draw result. How boring.

$ ./ttt2beta 6106

Seed: 6106

Move by player X at (2, 2).
. . .
. . .
. . X

Move by player O at (1, 1).
. . .
. O .
. . X

Move by player X at (0, 1).
. X .
. O .
. . X

Move by player O at (0, 0).
O X .
. O .
. . X

Move by player X at (2, 0).
O X .
. O .
X . X

Move by player O at (0, 2).
O X O
. O .
X . X

Move by player X at (1, 0).
O X O
X O .
X . X

Move by player O at (1, 2).
O X O
X O O
X . X

Move by player X at (2, 1).
O X O
X O O
X X X

Player X wins!

$ ./ttt2beta 6107

Seed: 6107

Move by player X at (0, 0).
X . .
. . .
. . .

Move by player O at (2, 0).
X . .
. . .
O . .

Move by player X at (1, 0).
X . .
X . .
O . .

Move by player O at (0, 2).
X . O
X . .
O . .

Move by player X at (0, 1).
X X O
X . .
O . .

Move by player O at (1, 1).
X X O
X O .
O . .

Player O wins!

$

Sample Runs for ttt2.c

Assuming that the executable is ttt2, 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 ttt2.c -o ttt2

$ ./ttt2

Usage: ./ttt2 seed number_of_simulations

$ ./ttt2 6105

Usage: ./ttt2 seed number_of_simulations

$ ./ttt2 6105 1000

Seed: 6105
Number of simulations: 1000, wins by player X: 586
    wins by player O: 292, draws: 122
    Percentage of wins by player X: 58.600%
    Percentage of wins by player O: 29.200%
    Percentage of draws: 12.200%

$ ./ttt2 6106 10000

Seed: 6106
Number of simulations: 10000, wins by player X: 5881
    wins by player O: 2875, draws: 1244
    Percentage of wins by player X: 58.810%
    Percentage of wins by player O: 28.750%
    Percentage of draws: 12.440%

$ ./ttt2 6107 100000

Seed: 6107
Number of simulations: 100000, wins by player X: 58466
    wins by player O: 28817, draws: 12717
    Percentage of wins by player X: 58.466%
    Percentage of wins by player O: 28.817%
    Percentage of draws: 12.717%

$

Note and Ponder

  1. Assume that all user input are integers.

  2. Note that you will probably get different results even if you use the same seed. This is OK, as long as your simulation results do not differ much from the sample runs.

  3. 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.

  4. 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?

  5. Most importantly, have lots of fun programming!


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

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

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