Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CSSE1001

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.

• a r c v - The string ’a’ is followed by a row and column number


and a value to place in that square. The system should respond by
putting the given value in the given square. If v is not a valid choice
for the given square the system should respond by displaying the string
"Invalid choice" and leave the game unchanged.
• c r c - The string ’c’ is followed by a row and column number and the
system should respond by listing the choices available for that square.
• af - The string ’af’ is used to auto-fill the game by automatically adding
values at squares where there is exactly one possible choice. The system
should also respond by displaying a list of triples of the form (r,c,v)
giving the row, column and value of each filled square. Note that filling
in some square will typically reduce choices in other squares and so
this command should keep auto-filling until there are no more “unique
values” left.

In all of the above commands, by “choices” we mean the choices returned by


the function choices in sudoku.py.

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.

1.1 Download files


The first task is to download the file sudoku.py (a support module) and to
aid in testing the game testgame.txt and example run in game_dump.txt.
We suggest you create a folder in which to write your solution and put these
files in that folder. The file sudoku.py contains several definitions to support
you in writing your assignment. Do not edit this file.
The file assign1.py is for your assignment. Add your name and student

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.

1.2 Read the support file


The second task is to carefully read and understand sudoku.py. You should
look carefully at game_dump.txt in order to get the same input-output be-
haviour.

1.3 Write the code


Finally, write your solution to the assignment making sure you have included
suitable comments. There are several functions you need to write and these
are described below.

1.3.1 Add Dots

The function add_dots(game) takes a game data structure as produced by


read_game in sudoku.py and replaces all the space characters (’ ’) by dot
characters (’.’). This is to make the game display a little easier to look at.
Here is an example (the output has been modified in order to fit on the page).

>>> game = read_game(’testgame.txt’)


>>> 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’]]

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’]]
>>>

1.3.2 Pretty Print Game

The function pretty_print_game(game) takes a game data structure as


above (using dots rather than spaces) and pretty prints the game.
Continuing from the above example:

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

The function parse_input(text) takes a command from the user (a string)


and attempts to convert the command into a list of strings consisting of
the command followed by its arguments (if any). If the command is a valid
command with the correct number of arguments for that command then it
returns a list. Otherwise it returns None. You can assume that, if the com-
mand is valid and has the correct number of arguments, then the arguments
are valid (i.e. strings representing numbers in the correct range).
Here are some examples. Note that nothing is printed if the result is None.

>>> parse_input(’a 1 2 3’)


[’a’, ’1’, ’2’, ’3’]
>>> parse_input(’a 1 2’)
>>> parse_input(’a 1 2 3 4’)
>>> parse_input(’c 1 1’)
[’c’, ’1’, ’1’]
>>> parse_input(’af’)
[’af’]
>>>

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.

1.3.5 Auto Fill

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

The following may or may not be helpful.


For user interaction: raw_input
For string processing: strip, split

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.

You might also like