Question
I WILL GIVE THUMBS-UP TO THE ANSWER! (do not forget to comment the output) (this is java) Starter code for this question : /* This
I WILL GIVE THUMBS-UP TO THE ANSWER! (do not forget to comment the output) (this is java)
Starter code for this question :
/* This is started code for Crystal's Sudoku #2. The code is not pretty, but it works. */ import java.util.*; import java.io.*; public class MySudokuBoard { public final int SIZE = 9; protected char[][] myBoard; public MySudokuBoard(String theFile) { myBoard = new char[SIZE][SIZE]; try { Scanner file = new Scanner(new File(theFile)); for(int row = 0; row
SodukuCheckerEngineV2.java :
public class SudokuCheckerEngineV2 {
public static void main(String[] args) { // Note that here I am calling the board object MySudokuBoard // if you named your class something different, you should // find and replace all `MySudokuBoard` with your class name boolean allTests = true; // an empty board is valid, but not solved System.out.print("Checking empty board..."); MySudokuBoard board1 = new MySudokuBoard("boards/empty.sdk"); assert board1.isValid() : "isValid: should be true"; assert !board1.isSolved() : "isSolved: should be false"; if(board1.isValid() && !board1.isSolved()) System.out.println("passed."); else { System.out.println("failed."); allTests = false; } // an incomplete, valid board is valid, but not solved System.out.print("Checking incomplete, valid board..."); MySudokuBoard board2 = new MySudokuBoard("boards/valid-incomplete.sdk"); assert board2.isValid() : "isValid: should be true"; assert !board2.isSolved() : "isSolved: should be false"; if(board2.isValid() && !board2.isSolved()) System.out.println("passed."); else { System.out.println("failed."); allTests = false; } // a complete, valid board is valid and solved System.out.print("Checking complete, valid board..."); MySudokuBoard board3 = new MySudokuBoard("boards/valid-complete.sdk"); assert board3.isValid() : "isValid: should be true"; assert board3.isSolved() : "isSolved: should be true"; if(board3.isValid() && board3.isSolved()) System.out.println("passed."); else { System.out.println("failed."); allTests = false; } // a board with dirty data is not valid and not solved System.out.print("Checking dirty data board..."); MySudokuBoard board4 = new MySudokuBoard("boards/dirty-data.sdk"); assert !board4.isValid() : "isValid: should be false"; assert !board4.isSolved() : "isSolved: should be false"; if(!board4.isValid() && !board4.isSolved()) System.out.println("passed."); else { System.out.println("failed."); allTests = false; } // a board with a row violation is not valid and not solved System.out.print("Checking row violating board..."); MySudokuBoard board5 = new MySudokuBoard("boards/row-violation.sdk"); assert !board5.isValid() : "isValid: should be false"; assert !board5.isSolved() : "isSolved: should be false"; if(!board5.isValid() && !board5.isSolved()) System.out.println("passed."); else { System.out.println("failed."); allTests = false; } // a board with a column violation is not valid and not solved System.out.print("Checking col violating board..."); MySudokuBoard board6 = new MySudokuBoard("boards/col-violation.sdk"); assert !board6.isValid() : "isValid: should be false"; assert !board6.isSolved() : "isSolved: should be false"; if(!board6.isValid() && !board6.isSolved()) System.out.println("passed."); else { System.out.println("failed."); allTests = false; } // a board with both a row and a column violation is not valid and not solved System.out.print("Checking row&col violating board..."); MySudokuBoard board7 = new MySudokuBoard("boards/row-and-col-violation.sdk"); assert !board7.isValid() : "isValid: should be false"; assert !board7.isSolved() : "isSolved: should be false"; if(!board7.isValid() && !board7.isSolved()) System.out.println("passed."); else { System.out.println("failed."); allTests = false; } // a board with a mini-square violation is not valid and not solved System.out.print("Checking mini-square violating board..."); MySudokuBoard board8 = new MySudokuBoard("boards/grid-violation.sdk"); assert !board8.isValid() : "isValid: should be false"; assert !board8.isSolved() : "isSolved: should be false"; if(!board8.isValid() && !board8.isSolved()) System.out.println("passed."); else { System.out.println("failed."); allTests = false; } if(allTests) System.out.println("**** HORRAY: ALL TESTS PASSED ****"); } }
Overview Over the first few Long Weekly Homework assignments for this quarter, we are building a Sudoku G_solver program in pieces. This is Part 2 of the Sudoku solver series. In this assignment, we will add two new methods to our board class that will allow us to 1) verify a board follows the rules of Sudoku and 2) check if the board is solved. Topics sets, maps, efficiency, and boolean zen Reminder of the Rules of Sudoku Sudoku G_puzzles [play online ] begin with a 99 board where each "cell" is a number (1-9) or an empty space. The goal of the puzzle is to make it so that - every row contains the values 1-9 only once per row, - every column contains the values 1-9 only once per column, and - each of the inside 33 squares only contains the values 19 once per small square. The puzzle is solved once all the cells are filled with a number following the rules of the puzzle. In this assignment, we will be verifying that they rules of Sudoku are followed and identifying when the puzzle is solved. Instructions Part 0: Make a copy of your Sudoku \#1 Solution We will be building off your Sudoku \#1 solution. I recommend beginning this assignment by making a new folder with a copy of your Sudoku \# 1 solution just so that you are working in a new space and can easily start over if needed. - If you did not complete Part 1, you can use this solution to Part 1 of Sudoku as starter code for this assignment. Note that I have intentionally provided a solution to Part 1 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. Part 1: isValid You should add a method to your board class that will return true if all of the following constraints are followed according to the board's current data; false otherwise: - there is no data in the board that is not a 1-9 or a space (or your representation of a space - could be a ". or a 0, etc) - no row contains any duplicate values of 1-9 (but may contain multiple spaces and still be valid) - no column contains any duplicate values of 1-9 (but may contain multiple spaces and still be valid) - no mini-square contains any duplicate values of 1-9 (but may contain multiple spaces and still be valid) Use private helper methods I recommend creating private helper methods for each of these four constraints and then checking if all the constraints are true in your isValid method. Make sure to use Boolean zen! (This is similar to how our TicTacToe game had an isSolved method that utilized helper methods isFull and checkWinner.) Checking for valid data Go through the entire 2D structure and check that none of the cells contain data that is not allowed. You can solve this using any way you want - I recommend putting the valid data values in an ArrayList or a Set and then using contains() on that structure to make sure every cell's data is valid. Return true if all data is valid; false if any one piece of data is invalid. Note that in terms of efficiency: - if you choose to use an ArrayList for this solution, your efficiency for this method will be O(nm) at best because for every value in your 2D array you will need to check every value in the ArrayList (in the worst case). - if you choose to use a TreeSet for this solution, your efficiency for this method will be O(nlogm) at best because for every value in your 2D array you will need to check every value in the TreeSet (in the worst case) which is an O(logn) operation. - if you choose to use a HashSet for this solution, your efficiency for this method will be O(n) at best - which is the best of these three options - because for every value in the 2D array you will need to check every value in the HashSet (in the worst case) which is an O(1 ) operation. - if your internal structure is an int[]] rather than a char[]], you can write a solution that does not require auxiliary storage (you can do this with the char[]] as well, but it reads less cleanly, I think) - this solution will be O(n) because you will still need to visit every element in the 2D array to make sure only the values 09 occur. Checking the rows Make sure that no row contains a duplicate value ( but remember it is ok for rows and columns to have have multiple space characters in them (i.e. ". or 0 ). This should be checked using a Set (either Set or Set depending on your board implementation choice). If you put all the values for the row into the set, then you will be able to check if the set contains the value as you go to see if there are duplicates. Part 2: isSolved You should add a method to your board class that will return true if there are nine occurrences of every number 1-9 in the grid AND all the constraints of isValid are followed. There are several ways that you could check to see if the board is solved such as just checking that there are no spaces (or dots or Os left in the grid - depending on your implementation); however, I expect you to use a Map for this check. You should create a single map, iterate over the entire 2D structure counting the occurrences of each value 1-9. If all values nine times AND the board isValid, then the puzzle is solved. Part 3: Test your Sudoku functionality To test that everything works, run my SudokuCheckerEngineV2.java using these provided board data files . If all tests pass, you are good to go. - Assertions allow us to verify that a certain condition is true in the code at a given point. - In JGrasp, you must enable assertions in order to use them. To do this go to the Build menu --> Enable Assertions, then run your file like normal. If an assertion fails, the program will end and the assertion error message will tell you what's wrong. What to Submit - Your modified board class - The SudokuCheckerEngine with output showing the results of your test cases
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started