CS1101C Lab 4 (Even Week)

Let's Play Tic-Tac-Toe!

The deadline for this lab question is Wednesday 06 April 2005, 23:59:59 hours.

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

Background

The game of tic-tac-toe is a familiar game, often played while we were kids. It is played on a 3 x 3 board containing nine squares. Initially, all the squares are empty. There are two players. The X player always places ‘X’ (crosses) and the O player always places ‘O’ (knots; this is a capital O, not the digit zero). The X player always starts first, and the players then take turns to place pieces on empty squares. Each square can only accommodate one piece.

Outcomes

A player wins when three of his pieces line up in a straight line: either horizontally, vertically, or diagonally. A draw occurs when all nine squares have been filled but no one has won. The game ends immediately whenever any player wins.

The following show some examples where the X player has won; there are three X’s lined up in a straight line. An empty square is marked by a single full-stop:

The following show some examples where the O player has won; there are three O’s lined up in a straight line:

The following show some examples where a draw has occurred:

A Single Simulation

We assume that the players are total idiots; they place pieces completely at random. Each simulation requires the computer to keep placing pieces on a random empty square, starting with the X player first (place an ‘X’), then alternating between the two players. The simulation ends immediately only when either the X player or the O player wins, or when all nine squares are filled (in which case there are two possible outcomes: either the X player wins or it is a draw).

Text File

The text file ttt1.txt contains an unknown number of lines. Each line contains a random number seed (unsigned integer) followed by the number of simulations to carry out (integer). A sample ttt1.txt file is given below (we will test your program with other text files):
3056 100
617  500
141  1000
3022 5000
4981 10000
7958 50000
1030 100000

You may copy the file ttt1.txt into your current directory by typing the following cp (copy) command at the Unix prompt. Highlight the entire line, right-click on the mouse and choose “Copy”, then switching to your Secure Shell Window, right-click on the mouse and choose “Paste”. Press Enter to execute the command.

cp ~cs1101cl/www/lab4/evenweek/ttt1.txt .

In the text file shown above, the first line indicates that the random number seed is 3056 and after seeding with this random number seed, we should carry out 100 simulations. The second line in the text file indicates that the random number seed is 617 and after seeding with this random number seed, we should carry out 500 simulations. You may assume that the random number seed will not exceed 32767 and the number of simulations will not exceed 1,000,000 (one million).

Seeding Simulations

For each seed and number of simulations, count the total number of times the X player wins, the total number of times the O player wins, and the total number of draws. Show also the percentage of each outcome.

Sample Run

The following shows a sample run, where $ indicates the Unix prompt. It shows that for the first seed 3056 with 100 simulations, the X player won 58 times which occurred 58.000% of the time, the O player won 34 times which occurred 34.000% of the time, and there were 8 draws which occurred 8.000% of the time. Note that 58 + 34 + 8 = 100. Print the percentages to three decimal places.

You are reminded to follow the sample outcome exactly, else marks will be deducted. Note that your results are likely to differ slightly since random number generation is system dependent.

Hint: To print a % sign, use %% in your format string.

$ gcc -Wall ttt.c -o ttt
$ ./ttt
Seed: 3056, number of simulations: 100
X player wins: 58, 58.000%
O player wins: 34, 34.000%
Draws: 8, 8.000%

Seed: 617, number of simulations: 500
X player wins: 299, 59.800%
O player wins: 141, 28.200%
Draws: 60, 12.000%

Seed: 141, number of simulations: 1000
X player wins: 591, 59.100%
O player wins: 293, 29.300%
Draws: 116, 11.600%

Seed: 3022, number of simulations: 5000
X player wins: 2907, 58.140%
O player wins: 1424, 28.480%
Draws: 669, 13.380%

Seed: 4981, number of simulations: 10000
X player wins: 5724, 57.240%
O player wins: 2921, 29.210%
Draws: 1355, 13.550%

Seed: 7958, number of simulations: 50000
X player wins: 29255, 58.510%
O player wins: 14292, 28.584%
Draws: 6453, 12.906%

Seed: 1030, number of simulations: 100000
X player wins: 58556, 58.556%
O player wins: 28672, 28.672%
Draws: 12772, 12.772%

$

All The Best!

Luck does not feature in programming.


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

A total of 8 different hosts have accessed this document in the last 407 days; your host, 216.73.216.159, has accessed it 1 times.

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