Professional Documents
Culture Documents
Csse
Csse
Semester 1, 2010
Assignment 1
20 marks
Due Thursday 15 April, 2010, 5pm
Sudoku Interface
In this assignment you will write code to provide a simple interactive envi-
ronment to aid in solving games of Sudoku. You are not writing a Sudoku
solver, you are simply providing an interface for a human to solve the puzzle
interactively.
The Sudoku code discussed in the course notes can be thought of as a module
of useful functions that you can use when writing your code.
You will write a simple interface that displays the current state of the game
and waits for the user to enter one of three commands. The supplied file
game_dump.txt gives an example of the expected interaction. The commands
are as follows.
1
You are to implement some simple error handling. This should check if the
the command is one of the three listed above and the command has the
correct number of arguments. You may assume that the user inputs valid
row and column positions - i.e. you do not have to check for this kind of
error. Similarly, you can assume game files supplied to the program are valid
files that are readable.
The supplied file game_dump.txt is an interaction with the system using the
supplied game testgame.txt (a partially completed version of sgame from
the notes). It contains examples of correct and incorrect commands and the
responses to each. Your code should work in this way. The output should
look exactly the same except the elements in the auto-fill lists can be in any
order. Note: in game_dump.txt the string following “Command: ” is the
user input.
The game display looks better with the dot character (’.’) rather than the
space character (’ ’) for the unfilled square. You will write a function to take
the initial game read in from the given file and replace each space character
with a dot character up front rather that repeatedly doing this every time
you display the game. You will note that the support functions in sudoku.py
work just as well using the dot character for unfilled squares.
1 Assignment Tasks
For each function that you write you need to provide a suitable comment
giving a description, the type and any preconditions. You should use the
triple-quote commenting style.
2
number in the space provided. You will note that this file loads sudoku.py.
For this to work the two files need to be in the same folder.
When you have completed your assignment you will submit the file assign1.py
containing your solution to the assignment – see Section 3.
3
>>> add_dots(game)
>>> game
[[’.’, ’.’, ’1’, ’.’, ’.’, ’4’, ’.’, ’9’, ’.’],
[’7’, ’.’, ’9’, ’1’, ’.’, ’3’, ’.’, ’5’, ’.’],
[’.’, ’.’, ’2’, ’.’, ’9’, ’8’, ’1’, ’.’, ’6’],
[’.’, ’9’, ’.’, ’.’, ’4’, ’.’, ’.’, ’.’, ’.’],
[’.’, ’8’, ’4’, ’.’, ’.’, ’9’, ’6’, ’1’, ’.’],
[’.’, ’.’, ’.’, ’.’, ’5’, ’.’, ’.’, ’2’, ’.’],
[’6’, ’1’, ’5’, ’9’, ’3’, ’7’, ’.’, ’.’, ’.’],
[’9’, ’7’, ’3’, ’4’, ’8’, ’2’, ’5’, ’6’, ’1’],
[’4’, ’2’, ’8’, ’6’, ’1’, ’5’, ’3’, ’7’, ’9’]]
>>>
>>> pretty_print_game(game)
. . 1 | . . 4 | . 9 .
7 . 9 | 1 . 3 | . 5 .
. . 2 | . 9 8 | 1 . 6
------+-------+------
. 9 . | . 4 . | . . .
. 8 4 | . . 9 | 6 1 .
. . . | . 5 . | . 2 .
------+-------+------
6 1 5 | 9 3 7 | . . .
9 7 3 | 4 8 2 | 5 6 1
4 2 8 | 6 1 5 | 3 7 9
>>>
Note that you will need to break up each row list into three parts.
4
1.3.3 Parse Input
1.3.4 Finished
The function finished(game) takes a game data structure using dots and
returns True if the game is finished and False otherwise.
The function auto_fill(game) takes a game data structure using dots and
updates game by filling in any square where there is exactly one choice.
It returns a list of triples where each triples contains the row and column
position of a square and the digit (character) that was put in that square.
Note that you need to repeatedly search for squares with unique choices as
filling in a square can reduce choices in other squares. You can stop when a
complete pass over the game does not find any unique choices.
Here is an example continuing on from before.
5
>>> auto_fill(game)
[]
>>> game[5][1] = ’3’
>>> pretty_print_game(game)
. . 1 | . . 4 | . 9 .
7 . 9 | 1 . 3 | . 5 .
. . 2 | . 9 8 | 1 . 6
------+-------+------
. 9 . | . 4 . | . . .
. 8 4 | . . 9 | 6 1 .
. 3 . | . 5 . | . 2 .
------+-------+------
6 1 5 | 9 3 7 | . . .
9 7 3 | 4 8 2 | 5 6 1
4 2 8 | 6 1 5 | 3 7 9
>>> auto_fill(game)
[(5, 0, ’1’), (5, 5, ’6’), (3, 5, ’1’), (5, 2, ’7’), (5, 3, ’8’),
(5, 8, ’4’), (3, 2, ’6’), (5, 6, ’9’)]
>>> pretty_print_game(game)
. . 1 | . . 4 | . 9 .
7 . 9 | 1 . 3 | . 5 .
. . 2 | . 9 8 | 1 . 6
------+-------+------
. 9 6 | . 4 1 | . . .
. 8 4 | . . 9 | 6 1 .
1 3 7 | 8 5 6 | 9 2 4
------+-------+------
6 1 5 | 9 3 7 | . . .
9 7 3 | 4 8 2 | 5 6 1
4 2 8 | 6 1 5 | 3 7 9
>>>
6
1.3.6 Play Game
Finally, you need to write the top-level function play_game(file) that takes
a file (a Sudoku game) and is responsible for interacting with the user. An
example of using this function is given in game_dump.txt.
1.3.7 Hints
2 Marking Criteria
We will mark your assignment according to the following criteria.
Criteria Mark
Comments: 3
- comments that are complete, consistent, clear and succinct 3
- comments with some minor problems 2
- comments with major problems 1
- work with little or no academic merit 0
Code: 17
- code that is mostly complete, correct, clear and succinct 12 - 17
- code with a number of problems 7 - 11
- code that is clearly incomplete, incorrect, too complex or 1-6
hard to understand
- work with little or no academic merit 0
Total marks 20
A partial solution will be marked. If your partial solution causes problems
in the Python interpreter please comment out that code and we will mark
that.
Please read the section in the course profile about plagiarism.
7
3 Assignment Submission
You must submit your completed assignment electronically through the web-
site: http://submit.itee.uq.edu.au
Please read http://submit.itee.uq.edu.au/student-guide.pdf for in-
formation on using electronic submission.
You should electronically submit your copy of the file assign1.py (use this
name - all lower case).
You may submit your assignment multiple times before the deadline - only
the last submission will be marked.
Late submission of the assignment will not be accepted. In the event of
exceptional personal or medical circumstances that prevent you from handing
in the assignment on-time, you should contact the lecturer in charge and
be prepared to supply appropriate documentary evidence. You should be
prepared to submit whatever work you have completed at the deadline, if
required. Requests for extensions should be made as soon as possible, and
preferably before the assignment due date.