0% found this document useful (0 votes)
41 views8 pages

June 2014

Uploaded by

fast furios
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views8 pages

June 2014

Uploaded by

fast furios
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Exercise (Programming: Applied to Concurrent Systems)

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.

Task 1 –Problem Understanding

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:

: Used to define a procedure or concept


:Assignment operator

==,<>: equal, not equal ELSE

TE THEN IF. THEN WHILE DO REPEAT

(* XXX*)

Choice control Constructs

END; END; IF .. THEN END; FOR UNTIL condition

DO : Loop Control constructs.

: XXX is a comment.

Figure 1 - Notation used in Algorithms

03/795/3A

795 -computer science: Practicals - A

Variables

Some variables used in questions candidates *)

COde fragments given to

(*
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

set variable finished to false

(* local variable is set to true if game is finished *)

REPEAT

current player plays his/her turn display the tic-tac-toe grid

IF current player has won game THEN set variable finished to true

ELSE

(* switch to other player *) IF player == X THEN

set variable player to '0 ELSE


set variable player to 'X

UNTIL (£inished = true) or (grid_blanks <= zero) END (* fragment *);

Figure 2 - Algorithm for code fragment to be inserted in main program

Task 2- Program Design

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:

(3 marks) are same and adding the MODIFY JN-PLACE! (3 marks)

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)

Task 3- Develop Program Code

4) Implement algorithms developed in Task 3:


a. Use a text editor or your favourite program development environment to open a copy of the
template file given to you by instructors. The template contains Programming Language (PL)
procedures and functions you will need. It also contains stumps for the functions and procedures
you will implement below. (You may use or delete the stumps.) (1 mark)

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

Task 4- Develop Main Program


6) Do the following in the main program of your program. A procedure, play, is already implemented
in the template given you. It requests a number (in range 1 to 9) from a human player, but generates
a number for the machine. It assumes player X is the machine.

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.

Task 5-Sample Output Results

7) Now, try out your working program.

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

..o0 THE END O0.

03/795/3A

795-computer science: Practicals - A

You might also like