⌛⌛ 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. The codes needed in this exercise are in the directory Round4/sudoku. Note that you do not have rights to update the repository.

  • SudokuTest.java: a test program to test your implementation

  • input.txt: a test input for the program

  • output.txt: an example output matching input.txt

Using several remotes

On this course, you need to use other remote reposiories in addition to your own to fecth material for course work. Hence, you need to add another remote into your working directory in addition to origin. This is done with: git remote. There is material on the use of several remotes on Programming 2. In short:

  • clone your own remote repository

  • move to your own remote repository

  • add another remote git remote add upstream student_template_project.git-URL, where student_template_project.git-URL is replaced in its entirety with the URL of the repository. It starts either with https:// or with git@

  • pull the contents of the remoten for local use git pull upstream HEAD

NOTE! 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 and git@, if you are.

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 charecter c into the grid cell (i, j). By this we mean the cell in column j of row i.

    • Both the 9 rows and the 9 columns are indexed with 0…8. The function must check that i and j 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 parameter c 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. Returns true (is legal) or false (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!”, where i is the row index.

        • Column: “Column j has multiple c's!”, where j is the column index.

        • Sub-block: “Block at (x, y) has multiple c's!”, where x and y 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.