⌛⌛ Sudoku¶
A class testing your code and an example test input file is available for the exercise.
The files needed in this task can be pulled from version control.
They are located in the repository student_template_project.
You must set student_template_project
as a remote in your local repository.
Remotes are set:
clone your own repository if not already cloned
move into your own local repository
add the course repository as a remote there e.g.
git remote add course-upstream git@course-gitlab.tuni.fi:compcs140-fall2022/student_template_project.git
Note! Name the remote to your own liking. The URL of the remote is decided based on whether you have set the SSH key to GitLab or not: :https://
, if you are not using an SSH key andgit@
, if you are.merge and fetch the version history of the repository with
git pull course-upstream main --allow-unrelated-histories
The--allow-unrelated-histories
due to a configuration issue where the course repository is not sharing a common history with yours from creation. Once the remote is set and merged, this will not require further actions.note that you do not have and do not need rights to update the repository.
There is also material on the use of several remotes on Programming 2.
The codes needed in this exercise are in the directory Round4/sudoku
.
SudokuTest.java
: a test program to test your implementationinput.txt
: a test input for the programoutput.txt
: an example output matchinginput.txt
Exercise: Sudoku¶
Place your code into a file named Sudoku.java.
Implement a class Sudoku
that maintains a Sudoku grid and can e.g. check if it is legal.
If you have somehow managed to avoid knowing what Sudoku is, see first e.g. the
Wikipedia article.
To be more precise, you should implement the Sudoku
class to have the following public members:
A constructor
Sudoku()
that initializes the Sudoku object to hold a 9×9 Sudoku grid whose all cells are empty. Empty cells are expressed using space characters' '
.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 charecterc
into the grid cell (i
,j
). By this we mean the cell in columnj
of rowi
.Both the 9 rows and the 9 columns are indexed with 0…8. The function must check that
i
andj
are legal row and column indices, and if this is not the case, print a message of form “Trying to access illegal cell (i, j)!
”.A Sudoku grid is allowed to hold only space characters
' '
(ie. empty cells) and the digit characters'1'
,'2'
,'3'
,'4'
,'5'
,'6'
,'7'
,'8'
and'9'
. If the parameterc
is 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 3x3 sub-block contains the same digit character twice (spaces may naturally occur multiple times).
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).
If the check finds two same digit characters from the same row, column or sub-block, record the smallest such duplicate digit character. This ensures that the messages described below will be unique even if there were several different duplicate digits. When a duplicate digit has been found, print one of the messages desribed below and stop the check, returning
false
.Row: “
Row i has multiple c's!
”, wherei
is the row index.Column: “
Column j has multiple c's!
”, wherej
is the column index.Sub-block: “
Block at (x, y) has multiple c's!
”, wherex
andy
are 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 further details from the provided example output.
Testing¶
You may test your class by using the test program given in the file SudokuTest.java
, the test
data given in the file input.txt
, and the example output given in the file output.txt
.
Place these files and your own class implementation into the same directory, compilte the
program e.g. as javac *.java
, and run the test as java SudokuTest input.txt
. The program
should produce exactly the same output as shown in the file output.txt
.
A+ presents the exercise submission form here.