- COMP.CS.140
- 4. Programming in the Large
- 4.4 ⌛⌛ Sudoku
⌛⌛ Sudoku¶
There is material for this question in the remote material repository:
The material is located in the
round4/sudokudirectory.SudokuTest.java: a test program to test your implementation.input.txt: a test input for the program.output.txt: an example output matchinginput.txt.
Exercise: Sudoku¶
The exercise is returned as a Maven project.
Place the pom.xml file in the round4/sudoku directory of your local repository
and create to this directory the src/main/java subdirectory. Create the class file
Sudoku.java and attach it to the fi.tuni.prog3.sudoku package.
Your files should be in the round4/sudoku/src/main/java/fi/tuni/prog3/sudoku
directory. NetBeans creates the directory structure matching the package for this task
automatically, provided that you enter correct values, when creating your Maven project.
Implement a Sudoku class that maintains a Sudoku grid and can, for example, check if it is legal.
If you have somehow managed to avoid knowing what a Sudoku is, see first its
Wikipedia article.
To be more precise, you should implement the Sudoku class to have the public members given below.
All the other members should be private.
Constructor
Sudoku()that initializes the Sudoku object to hold a 9 × 9 Sudoku grid, all cells of which are empty. Empty cells are expressed using a space character' '.You are free to decide the internal (private) details of how you store the grid.
Member function
set(int i, int j, char c): sets the charactercinto the grid cell (i,j). By this we mean the cell in the columnjof rowi.Both the rows and columns have index values from 0 to 8. The function must check that
iandjare legal row and column indices. If this is not the case, the function prints a message of form “Trying to access illegal cell (i, j)!”.A Sudoku grid is allowed to hold only space characters
' '(empty cells) and the digit characters'1','2','3','4','5','6','7','8'and'9'. If the value of the parametercis some other character, it will not be set and a message of form “Trying to set illegal character c to (i, j)!” should be printed.
Member function
check(): checks if the current values in the Sudoku grid are legal. Returnstrue(is legal) orfalse(is not legal).A Sudoku grid is legal if no row, column or 3 x 3 sub-block contains a digit multiple times (multiple spaces may naturally occur).
Check first the rows, then the columns and finally the sub-blocks (inspect the sub-blocks in a row-wise manner, starting with the upper left corner sub-block).
Record the smallest recurring digit from a row, column or sub-block. This ensures that the messages described below will be unique even if there were several recurring digits. When the smallest recurring digit has been found, print one of the messages desribed below and return immediately from the function with the
falsevalue.Row: “
Row i has multiple c's!”, whereiis the row index.Column: “
Column j has multiple c's!”, wherejis the column index.Sub-block: “
Block at (x, y) has multiple c's!”, wherexandyare the row and column index of the upper left corner cell of the sub-block.
Member function
print(): prints the Sudoku grid. The grid borders are expressed using the characters'#','-','|'and'+'and each cell value has a space on its both sides. See the example output file for further details.All printing should be done using the
System.outstream.
The automatic tests, and the ones given below, assume that you make the following definitions
in your pom.xml project file:
The value of
artifactIdissudoku.The value of
versionis1.0.The values of the
maven.compiler.sourceandmaven.compiler.targetelements are17or lower. The grader uses Java 17, so any newer versions won’t work.A Onejar plugin definition where the value of
mainClassisSudokuTestwhich is the name of the given test class (see below).
Testing¶
You can test your class with the test program given in the file SudokuTest.java, the
test file called input.txt and the example output called output.txt.
Set SudokuTest.java into the root of the src/main/java subdirectory of your Maven project,
and the other files into the root directory of your Maven project, that is, where the pom.xml is. Note
that SudokuTest.java does not include a package definition and therefore is not placed into
a deeper subdirectory.
After this you can compile the program with mvn package and run the test as
java -jar target/sudoku-1.0.one-jar.jar input.txt in the root directory of the project.
The test should produce the output depicted in the file output.txt.
Submitting¶
Submitting is done as usual by entering the URL of your personal remote directory in the field below.
A+ presents the exercise submission form here.