Overview Over the first few Long Weekly Homework assignments for this quarter, we have been building aSudoku(Links to an external site.)solver program in pieces.This is Part 3 of the Sudoku solver...

1 answer below »

Overview


Over the first few Long Weekly Homework assignments for this quarter, we have been building aSudoku(Links to an external site.)solver program in pieces.This is Part 3 of the Sudoku solver series - the last in the series!


In this assignment, we will add a new method to our board class called solve() which will allow us to use recursive backtracking to solve a Sudoku puzzle.


Topics


recursive backtracking


Solving a Sudoku Puzzle


What does it look like to recursively try and solve a Sudoku puzzle? Basically you try to place the values 1-9 in the empty spots of a Sudoku puzzle (move forward with a brute force attempt) until you either reach an invalid state (which causes you to backtrack) or you solve the puzzle or find out it's unsolvable (which ends the recursion).



Sudoku Backtracking GIF image


Instructions


Part 0: Make a copy of your Sudoku #2 Solution


We will be building off your Sudoku #2 solution. I recommend beginning this assignment by making a new folder with a copy of your Sudoku #2 solution just so that you are working in a new space and can easily start over if needed.




  • If you did not complete Part 2, you can usethis solution to Part 2 of Sudokuas starter code for this assignment.Note that I have intentionally provided a solution to Part 2 that is not stylistically beautiful according to my standards; however it does work. The reason for this is that I don't want to see my solution turned in during future quarters - so I'm giving you a working - but non-optimal - solution as your starting code if you need to use it. This way I can easily identify it should it accidentally make it's way to Chegg (not that any of you would do that).


Part 1: solve() method header and initial checks


Add a method to your board class that will allow the puzzle to be solved.


This method is going totake no parameters, as it will operate on the 2D array that is already stored in the Board (similar to what happened inQueens).


This method willreturn true or false, depending on whether or not the puzzle can be solved.



  • If your board state is invalid - you already have a method that can check this - then you should immediately return false because the board cannot be solved.

  • If your board is already solved - you also already have a method that can check this - then you should return true because the board is already solved.

  • In all other cases you have an incomplete, but valid, board so you will use recursive backtracking to attempt to solve the puzzle.

    • If in your exhaustive searching you find a solution, you'll end up returning true; but

    • if you never find a solution, you'll end up returning false.




Part 2: solve() with recursive backtracking


Tackling the recursive backtracking of this solution is not going to require a lot of code. For reference, my solution is around 20 lines of code in total. Yours does not need to be the same length, but I will be surprised if it is much longer. The hard part with recursive backtracking is *thinking* in the appropriate way. I highly recommend reviewingQueensandWordMakeras parts of those two assignments in combination are essentially the solution to this problem.


The general idea is that you need to try the values 1-9 in each of the empty spaces on your board in an intentional and brute force / exhaustive way. Every time you try a number you check if this creates a valid state on the board and then recurse hoping this leads you to a solution. If it does, you end up returning true up and up out of the recursive calls. However before that is likely to happen, you will hit several dead end solutions where you need to recurse out once and undo whatever change you tried on your board. Again, recursive backtracking is essentially made up of three parts: 1) try something 2) recurse and see if this leads to a solution 3) undo what you tried if the recursive try didn't work out.


Part 3: Test your Sudoku Solver functionality


To test that everything works, run mySudokuSolverEngine.java

Preview the document
using these provided board states:very-fast-solve.sdkandfast-solve.sdkI am giving you these test boards because it turns out that recursive backtracking is both memory in-efficient and slow. This means that if you try to solve() some of the boards from Sudoku #2, the solve() method might run for a very long time.



You need to add two things to the SolverEngine.



  1. Before trying to solve the board, check if the board is in an invalid state. If it is, print a message to the screen that says that the board cannot be solved.

    • You can test with the any of the rules violating boards from Sudoku #2



  2. Before trying to solve the board, check if the board is already solved. If it is, print a message to the screen that says that the board is already solved.

    • You can test with the valid-complete.sdk board from Sudoku #2




(TOTES OPTIONAL; earns no bonus points) Part 4: Make it visual


Want to visually watch your Sudoku solver trying to solve your solution? Try incorporating in this de-contextualized version of the QueensGUI board to watch your Sudoku solver.



  • GrabBoardGUI

    Preview the document


  • Add a field for this variable in your Sudoku board class

  • Instantiate it in the constructor

  • In the solve method, call the update method of the GUI, passing in the current state of your board


Note that printing while solving considerably slows down your algorithm.


What to Submit



  • Your modified board class

  • The SudokuSolverEngine with your modifications


Submission Requirements


You should do the following for _all_ assignments submitted for this course.


ProgramComment


Include a comment at the beginning of your program with the followinginformation and a description of the program in your own words:



// Your name here // CS 143 // HW Core Topics: ... // // This program will ...



Program Output


Include the output from a single run of your program as a block comment at the end ofthe program. This comment should come one blank line after the last closing curly brace in your code.


}
/* Paste the output from JGrasp here. Altering output will earn you an automatic zero for the assignment. */
Answered Same DayFeb 21, 2021

Answer To: Overview Over the first few Long Weekly Homework assignments for this quarter, we have been building...

Abhishek answered on Feb 25 2021
142 Votes
51033 - Sudoku Solver/Output/Output.txt
With SudokuCheckerEngine
----jGRASP exec: java SudokuCheckerEngine
---- at: 24 Feb, 2020 6:33:08 PM
----jGRASP: CLASSPATH is ":.:::/home/abhishek/Desktop/jgrasp/extensions/classes".
----jGRASP: PATH is "".
----jGRASP: working directory is [/home/abhishek/Downloads/ 51033 - Sudoku Solver/Solution].
----jGRASP: actual command sent ["/usr/lib/jvm/java-8-oracle/bin/java" "SudokuCheckerEngine"].
Checking empty board...passed.
Checking incomplete, valid board...passed.
Checking complete, valid board...passed.
Checking dirty data board...passed.
Checking row violating board...passed.
Checking col violating board...passed.
Checking row&col violating board...passed.
Checking mini-square violating board...passed.
**** HORRAY: ALL TESTS PASSED ****
----jGRASP: operation complete.
With SudokuSolverEngine (fast-solve-ikcxoqe0.sdk)
----
jGRASP exec: java SudokuSolverEngine
---- at: 24 Feb, 2020 6:34:46 PM
----jGRASP: CLASSPATH is ":.:::/home/abhishek/Desktop/jgrasp/extensions/classes".
----jGRASP: PATH is "".
----jGRASP: working directory is [/home/abhishek/Downloads/ 51033 - Sudoku Solver/Solution].
----jGRASP: actual command sent ["/usr/lib/jvm/java-8-oracle/bin/java" "SudokuSolverEngine"].
Initial board
My Board:
827154396
965.27148
3416.9752
.........
.........
61897.435
786235.14
1547968.3
23984....
Solving board...SOLVED in 8.516 seconds.
My Board:
827154396
965327148
341689752
472513689
593468271
618972435
786235914
154796823
239841567
----jGRASP: operation complete.
With SudokuSolverEngine (very-fast-solve-wtge1xbq.sdk)
----jGRASP exec: java SudokuSolverEngine
---- at: 24 Feb, 2020 6:36:03 PM
----jGRASP: CLASSPATH is ":.:::/home/abhishek/Desktop/jgrasp/extensions/classes".
----jGRASP: PATH is "".
----jGRASP: working directory is [/home/abhishek/Downloads/ 51033 - Sudoku Solver/Solution].
----jGRASP: actual command sent ["/usr/lib/jvm/java-8-oracle/bin/java" "SudokuSolverEngine"].
Initial board
My Board:
.34678912
.72195348
198342567
..9.61423
.26853791
.13924.56
.61537284
.8.419635
345.86179
Solving board...SOLVED in 1.698 seconds.
My Board:
534678912
672195348
198342567
859761423
426853791
713924856
961537284
287419635
345286179
----jGRASP: operation complete.
51033 - Sudoku Solver/Problem/Problem.txt
rencing Style: Others Requirement Tag 1: solve a Sudoku puzzle.using recursive backtracking
Assignment Question
Overview
Over the first few Long Weekly Homework assignments for this quarter, we have been building aSudoku(Links to an external site.)solver program in pieces.This is Part 3 of the Sudoku solver series - the last in the series!
In this assignment, we will add a new method to our board class called solve() which will allow us to use recursive backtracking to solve a Sudoku puzzle.
Topics
recursive backtracking
Solving a Sudoku Puzzle
What does it look like to recursively try and solve a Sudoku puzzle? Basically you try to place the values 1-9 in the empty spots of a Sudoku puzzle (move forward with a brute force attempt) until you either reach an invalid state (which causes you to backtrack) or you solve the puzzle or find out it's unsolvable (which ends the recursion).
Sudoku Backtracking GIF image
Instructions
Part 0: Make a copy of your Sudoku #2 Solution
We will be building off your Sudoku #2 solution. I recommend beginning this assignment by making a new folder with a copy of your Sudoku #2 solution just so that you are working in a new space and can easily start over if needed.
If you did not complete Part 2, you can usethis solution to Part 2 of Sudokuas starter code for this assignment.Note that I have intentionally provided a solution to Part 2 that is not stylistically beautiful according to my standards; however it does work. The reason for this is that I don't want to see my solution turned in during future quarters - so I'm giving you a working - but non-optimal - solution as your starting code if you need to use it. This way I can easily identify it should it accidentally make it's way to Chegg (not that any of you would do that).
Part 1: solve() method header and initial checks
Add a method to your board class that will allow the puzzle to be solved.
This method is going totake no parameters, as it will operate on the 2D array that is already stored in the Board (similar to what happened inQueens).
This method willreturn true or false, depending on whether or not the puzzle can be solved.
If your board state is invalid - you already have a method that can check this - then you should immediately return false because the board cannot be solved.If your board is already solved - you also already have a method that can check this - then you should return true because the board is already solved.In all other cases you have an incomplete, but valid, board so you will use recursive backtracking to attempt to solve the puzzle.
If in your exhaustive searching you find a solution, you'll end up returning true; butif you never find a solution, you'll end up returning false.
Part 2: solve() with recursive backtracking
Tackling the recursive backtracking of this solution is not going to require a lot of code. For reference, my solution is around 20 lines of code in total. Yours does not need to be the same length, but I will be surprised if it is much longer. The hard part with recursive backtracking is *thinking* in the appropriate way. I highly recommend reviewingQueensandWordMakeras parts of those two assignments in combination are essentially the solution to this problem.
The general idea is that you need to try the values 1-9 in each of the empty spaces on your board in an intentional and brute force / exhaustive way. Every time you try a number you check if this creates a valid state on the board and then recurse hoping this leads you to a solution. If it does, you end up returning true up and up out of the recursive calls. However before that is likely to happen, you will hit several dead end solutions where you need to recurse out once and undo whatever change you tried on your board. Again, recursive backtracking is essentially made up of three parts: 1) try something 2) recurse and see if this leads to a solution 3) undo what you tried if the recursive try didn't work out.
Part 3: Test your Sudoku Solver functionality
To test that everything works, run mySudokuSolverEngine.javausing these provided board states:very-fast-solve.sdkandfast-solve.sdkI am giving you these test boards because it turns out that recursive backtracking is both memory in-efficient and slow. This means that if you try to solve() some of the boards from Sudoku #2, the solve() method might run for a very long time.
You need to add two things to the SolverEngine.
Before trying to solve the board, check if the board is in an invalid state. If it is, print a message to the screen that says that the board cannot be solved.
You can test with the any of the rules violating boards from Sudoku #2
Before trying to solve the board, check if the board is already solved. If it is, print a message to the screen that says that the board is already solved.
You can test with the valid-complete.sdk board from Sudoku #2
(TOTES OPTIONAL; earns no bonus points) Part 4: Make it visual
Want to visually watch your Sudoku solver trying to solve your solution? Try incorporating in this de-contextualized version of the QueensGUI board to watch your Sudoku solver.
GrabBoardGUIAdd a field for this variable in your Sudoku board classInstantiate it in the constructorIn the solve method, call the update method of the GUI, passing in the current state of your board
Note that printing while solving considerably slows down your algorithm.
What to Submit
Your modified board classThe SudokuSolverEngine with your modifications
Submission Requirements
You should do the following for _all_ assignments submitted for this course.
ProgramComment
Include a comment at the beginning of your program with the followinginformation and a description of the program in your own words:
// Your name here // CS 143 // HW Core Topics: ... // // This program will ...
Program Output
Include the output from a single run of your program as a block comment at the end ofthe program. This comment should come one blank line after the last closing curly brace in your code.
}
/* Paste the output from JGrasp here. Altering output will earn you an automatic zero for the assignment. */
51033 - Sudoku Solver/Screenshots/O1.png
51033 - Sudoku Solver/Solution/very-fast-solve-wtge1xbq.sdk
.34678912
.72195348
198342567
..9.61423
.26853791
.13924.56
.61537284
.8.419635
345.86179
51033 - Sudoku Solver/Solution/fast-solve-ikcxoqe0.sdk
827154396
965.27148
3416.9752
.........
.........
61897.435
786235.14
1547968.3
23984....
51033 - Sudoku Solver/Solution/MySudokuBoard.java
51033 - Sudoku...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here