0% found this document useful (0 votes)
32 views

Coursework 2

Uploaded by

Paul Suciu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Coursework 2

Uploaded by

Paul Suciu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1

Two players are playing the Tic Tac Toe game. Player 1 and 2 take turns to draw
on the 3 x 3 square board.
Player 1 goes first and draws X (capital letter X) and Player 2 draws O (capital
letter O).
The first player to get 3 of their marks in a row (up, down, across, or diagonally) is
the winner.
The board is numbered like this:
123
456
789

2
The players record their games in a sequence of numbers, for instance, 5237649.
This means that the game goes as follows:
Player 1 puts a X at 5.
Player 2 puts a O at 2.
Player 1 puts a X at 3.

Player 1 puts a X at 9.
Write a function prepareBoard(game) .

It has parameter game which is a string of numbers, e.g., "5237649".


The function will check whether the input string game is a valid input by checking

 whether the length is greater than 9;


 whether there are “illegal” number (0) or symbols;
 whether there are two or more duplicate numbers.
If there are invalid input, return the string Invalid input and stop.
If the input is invalid, then return a list of length 9, which is a list of X’s, O’s and
_’s (denoting empty or not drawn on).
As an example, if game is "5237649", then
list index 0 1 2 3 4 5 6 7 8

numbering 1 2 3 4 5 6 7 8 9

numbering 0 O X O X X O 0 X

So prepareBoard("5237649") is to return the following list


['_’, 'O’, 'X’, 'O’, 'X’, 'X’, 'O’, '_’, ‘X’]

3
You are asked to print out the board at the end of the game. For the game above,
the board looks like this:
_OX
OXX
O_X
Note that the underscore means that the position is empty without any player’s
drawings.
As another example, 539128647 should print out the following game:
OXO
OXX
XOX
Write a printBoard(game) function, where game is a string of numbers, as in the
previous question.
In the function, first call the prepareBoard(game) function.
If the result claims that it is an Invalid input , then print this message out.
If the input is valid, then use the list of symbols returned
by prepareBoard(game) to print a board in the above form.
After each symbol, there will be a space.

4
The final step of the Tic Tac Toe game is determine who is the winner or the game
draws.
Write a function whoWins(game) which again takes in a string of numbers as in the
previous question. But this time, we assume that it is a valid input and that the
game will stop immediately when a winner is determined.
The whoWins(game) function will print out the board, as in the previous question
and on a separate line, print out one of the following:

 Player 1 wins
 Player 2 wins
 Draw
For instance, whoWins("539128647") will print out
OXO
OXX
XOX
Draw

This question is related to the previous one.


By considering all the terms in the Fibonacci sequence whose values do not exceed
five million, find the most frequent digit. In other words, which digit appears the
most in all the terms in the Fibonacci sequence whose values do not exceed five
million?
You may want to modify the program in the last question. There is no input in this
question and the output is the (most frequent) digit.

In a school’s Fun Sports Day, there is a game to fill a big bucket. Each child takes
a cup and fills it with water and hops 10 metres to the bucket and empty the water
in the cup to the bucket.
The bucket’s capacity is 1 litre and each cup can hold at most 300 ml. Each team
has 5 players who relay to fill the bucket.
The game stops if

1. the bucket is full, or


2. all 5 players have had a go each
If the bucket is full in the end, print out "Nailed it!"; otherwise, print out "Bad
luck!".
 Use input() function to enter the amount of water (as a whole number in
ml) in each cup. No message is needed in the input() function.
 Note that the amount of each go should be between 0 to 300. If the input
amount is out of this range, then an error message “Invalid amount!” should
be printed out and this person can be allowed another go till a valid amount
is entered.
 You may use break statement if needed

You might also like