June 2014
June 2014
Instructions for the Exercise: Analyse the problem in the answer booklet provided. Also record in
your answer booklets any other information requested, or that you believe would make it easier to
understand the exercises you have carried out. Instructors will assist you in recording intermediate
samples of work carried out on the computer.
You are expected to print out a single copy of relevant fragments of your program at different times.
PLEASE NOTIFY ZHE INSTRUCTOR OF ANY REQUIRED PRINTOUT THAT WAS NOT DONE!
Carry out the instructions/tasks below in order to write a program that implements a variant of the
game of tic-tac-toe also known as noughts-and-crosses. Make sure you have a code template (in
Pascal or C) on your machine.
Tic-tac-toe is a game for two players, where players alternate in putting a letter X (a cross) and the
letter O (a nought) in a 3x3 grid of 9 cells. Initially, the grid is empty; that is, the cells are blank. The
first player, Player A, puts an X in any empty cell position that he or she desires. The second player,
Player B, puts a nought in any empty cell position. The players play alternately until one of them
wins or there are no blanks in the grid.
The first player to have THREE of his/her letters in a row, column or diagonal wins the game.
The general idea then is to prevent your opponent from winning while, at the same time, creating
winning opportunities for yourself. Thus, for example, the grid configuration shown reflects the
current state of a game where it is Player A's turn to play. (A configuration is a layout of player
tokens on the O grid). Player A may prevent Player B from winning by placing his token (letter X) in
the bottom-left cell In this case, player B also creates a winning opportunity for herself during her
next turn, she may win by placing a letter X in the top-right cell. Of course, Player A (during his/her
turn) may prevent B winning by placing a letter O in the top-right cell.
In the game you are to develop, the computer (a machine) plays against a human (you). The
computer always starts, and its token is letter X.
1) Having read the problem description above, answer the following questions:
a. State in your own words what one needs to do in order to win a game. (2 marks)
b. How would you check whether a player has won a game. (2 marks)
c. Can there be a draw in the game-where no player wins? Justify your answer. (3 marks)
d. What is the maximum number of turns each player can have in a game. Why?(3 marks)
Notation:
(* XXX*)
: XXX is a comment.
03/795/3A
Variables
(*
grid • array variable, to hold cell elements player • The token of person currently playing turns •
array to record trail (turns) of game tụrns_Count • counter for array turns grid_blanks • count of
blank grid cells remaining Used to determine when the game has ended
finished
Algorithm:
(* algorithm fragment to be inserted at the position labelled: CODE FOR SUBTASK (6) STARTS HERE.
*)
fragment
REPEAT
IF current player has won game THEN set variable finished to true
ELSE
We need to represent the grid and player tokens in the machine. The grid cells can be numbered 1
to 9 in a linear array. The tokens X, O and BLANK can be represented by the characters 'X, 'O' and the
space character '". Putting a token in a given cell, then, means inserting the token's character at the
cell's position in the array. Testing a win triple means testing if all cell positions in a win line have the
same token. If that is the case, we say that the win line is true.
2) In your answer booklët, develop algorithms for various aspects of your program.
a. Draw a3 x 3 grid in your answer booklet and number its cells from 1 to 9. Numbering is from left-
to-right, and top- to-bottom. (2 marks) b. Using a different colour pen, draw through the grid cells in
(a), lines corresponding to possible wins. Each such line constitutes a win triple or win line. For
example, draw a line through cels 1,2 and3 because cells 1, 2, and 3 the triple (1,2,3) - is a possible
win. (3 marks)
TurnOver
03/795/3A
Page 4
for more free gce questi ons, notes and free answers, Visit http://www.gcerevisi on.com or
download our app "kawlo" on playstore
4
c. Give a pseudocode for a boolean function called are_same (x, y, z, p), which may be used to test if
a given triple (x, y, z) is a possible win for player p. The arguments x, y and z are cell positions on a
winning line; and p is the token (X' or 'O) for a given player. Assume an array call grid. (2 marks)
a. List all triples found in task 2(b), one to a line. Keep sufficient space (about 5-8cm) to the left of
each triple.
3) In your answer booklet, skip 3-5 lines before answering the following:
b. Convert each triple in 3(a) to a call to function are_same by prefixing the triple with player
argument. e.g., (1,2,3) becomes are_same (1, 2, 3, p). DO NOT CƠPY THE LIST IN 3(a);
c. Hence, modify 3(b) into the pseudocode of a boolean function to test if any one of the win lines is
true. List all triples found in task 2(b), one to a line. Keep sufficient space (about 5-8cm) to the left of
each triple Name the pseudocode has_won (p) werep is the token of a given player. Make use, if
need be, of the space you left earlier; do not re-copy your answer in 3(b)
[HINT: Do a logical OR of the win lines you obtained in 3(b), and make it the body ofyour pseudocode
function.]
(2 marks)
(2 marks) (3 marks)
b. Write a PL boolean function from algorithm are_ same in Task 2(6). c. Write a PL boolean function
from algorithm has_won in Task 3(c).
d. Test and make sure your program at least compiles, and then print the procedures are_same and
has_ won.
(1 mark)
5) In your program from Task 4, implement a PL procedure, shoW grid, that is used to display the tic-
tac-toe grid. (A procedure stump for show_ grid is in the program template given.) Do this by
considering the following:
a. Draw a grid using normal text characters. e.g., use hypens () for row lines and vertical bars () for
vertical lines, and plus sign (+) for joints. Use output statements to output each row of the diagram
obtained. (2 marks) b. Modify 5(a) to display the value of gridin] at cell position n. (You determined
cell positions in Task 2(a). Make the code obtained the body of procedure show_grid.
(2 marks) (1 mark)
c. Make sure your program works, save it and then print procedure show_grid.
a. Implement the algorithm in Figure 2 at the position indicated in your main program. (Its notation
is in Figure 1) Variables already declared in the template, such as grid and player, are properly
initialised. Make sure variables you introduce are corectly Initialised. (8 marks) (1 mark)
b. Make sure your program works, save it and then print the main program section.
(4 marks)
Run your program once, when the human player (you) wins, then save to a file the game trail that is
output at the end of the program run.
a.
(I mark)
b.
Repeat 7(a) for a scenario where the human player (you) does NOT win.
(1 mark) (2 marks)
Print the game trails for 7(a & b) or copy them to your answer booklet.
C.
03/795/3A