make a different version of this code with the same functions as i can not use my teacher's code

Programming with Microsoft Visual Basic 2017
8th Edition
ISBN:9781337102124
Author:Diane Zak
Publisher:Diane Zak
Chapter5: The Repetition Structure
Section: Chapter Questions
Problem 17E
icon
Related questions
Question

instructions are in photo and links in photos has code which I have pasted to make it easy to copy.

Below is my professor code for Sudoku #2, please make a different version of this code with the same functions as i can not use my teacher's code

Sudoku #2 code

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 < SIZE; row++) {
String theLine = file.nextLine();
for(int col = 0; col < theLine.length(); col++) {
myBoard[row][col] = theLine.charAt(col);
}
}
} catch(Exception e) {
System.out.println("Something went wrong :(");
e.printStackTrace();
}
}

public boolean isSolved() {
if(!isValid())
return false;

Map<Character,Integer> map = new HashMap<>();
for(char[] row : myBoard) {
for(char cell : row) {
if(map.containsKey(cell))
map.put(cell, map.get(cell) + 1);
else
map.put(cell, 1);
}
}
// info on Collections: https://docs.oracle.com/javase/8/docs/api/?java/util/Collections.html
return map.keySet().size() == 9 && Collections.frequency(map.values(),9) == 9;
}

public boolean isValid() {
// checks for bad data
for(char[] row : myBoard)
for(char cell : row)
if(cell != '.' && (cell < '1' || cell > '9'))
return false;
  
// checks for row/col violations
for(int r = 0; r < myBoard.length; r++) {
Set<Character> trackingRow = new HashSet<>();
Set<Character> trackingCol = new HashSet<>();
for(int c = 0; c < myBoard[r].length; c++) {
// check for row violation
if(trackingRow.contains(myBoard[r][c]))
return false;
else if(myBoard[r][c] != '.')
trackingRow.add(myBoard[r][c]);

// check for col violation
if(trackingCol.contains(myBoard[c][r]))
return false;
else if(myBoard[c][r] != '.')
trackingCol.add(myBoard[c][r]);
}
}
  
// check for mini-squares
for(int square = 1; square <= 9; square++) {   
char[][] mini = miniSquare(square);
Set<Character> trackingMini = new HashSet<>();
for(int r = 0; r < 3; r++)
for(int c = 0; c < 3; c++)
// check for mini violation
if(trackingMini.contains(mini[r][c]))
return false;
else if(mini[r][c] != '.')
trackingMini.add(mini[r][c]);
}
  
// if there weren't any violations above...
return true;
}

private char[][] miniSquare(int spot) {
char[][] mini = new char[3][3];
for(int r = 0; r < 3; r++) {
for(int c = 0; c < 3; c++) {
// whoa - wild! This took me a solid hour to figure out.
// This translates between the "spot" in the 9x9 Sudoku board
// and a new mini square of 3x3
mini[r][c] = myBoard[(spot - 1) / 3 * 3 + r][(spot - 1) % 3 * 3 + c];
}
}
return mini;
}

public String toString() {
String result = "My Board:\n\n";
for(int row = 0; row < SIZE; row++) {
for(int col = 0; col < SIZE; col++) {
result += (myBoard[row][col]);
}
result += ("\n");
}
return result;
}

}

    •  

 

Part 3: Test your Sudoku Solver functionality

To test that everything works, run my SudokuSolverEngine.java using these provided board states: very-fast-solve.sdk and fast-solve.sdk I 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. (the code is below)

SudokuSolverEngine.java code
public class SudokuSolverEngine {

public static void main(String[] args) {
// Here I have called my class `MySudokuBoard` if you named your class
// differently, modify the line below to use your own class name
MySudokuBoard board = new MySudokuBoard("boards/very-fast-solve.sdk");
System.out.println("Initial board");
System.out.println(board);
System.out.println();

System.out.print("Solving board...");
long start = System.currentTimeMillis();
board.solve();
long stop = System.currentTimeMillis();
System.out.printf("SOLVED in %.3f seconds.\n", ((stop-start)/1000.0));
System.out.println();
System.out.println(board);
  
}
}

fast-solve.sdk

827154396
965.27148
3416.9752
.........
.........
61897.435
786235.14
1547968.3
23984....

very-fast-solve.sdk

.34678912
.72195348
198342567
..9.61423
.26853791
.13924.56
.61537284
.8.419635
345.86179

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

What to Submit

  • Your modified board class
  • The SudokuSolverEngine with your modifications

Let me know if you need more information!

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 reviewing 8 Queens
problem from chapter 12 of our text book a the solution is similar 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 my SudokuSolverEngine.java į using these provided board states: very-fast-
solve.sdk I and fast-solve.sdk I 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 so
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
What to Submit
• Your modified board class
• The SudokuSolverEngine with your modifications
Transcribed Image Text: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 reviewing 8 Queens problem from chapter 12 of our text book a the solution is similar 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 my SudokuSolverEngine.java į using these provided board states: very-fast- solve.sdk I and fast-solve.sdk I 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 so 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 What to Submit • Your modified board class • The SudokuSolverEngine with your modifications
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 use this solution to Part 2 of Sudoku , as 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.
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 to take no parameters, as it will operate on the 2D array that is already stored in the Board.
This method will return 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.
o If in your exhaustive searching you find a solution, you'll end up returning true; but
o if you never find a solution, you'll end up returning false.
Part 2: solve() with recursive backtracking
Transcribed Image Text: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 use this solution to Part 2 of Sudoku , as 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. 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 to take no parameters, as it will operate on the 2D array that is already stored in the Board. This method will return 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. o If in your exhaustive searching you find a solution, you'll end up returning true; but o if you never find a solution, you'll end up returning false. Part 2: solve() with recursive backtracking
Expert Solution
Step 1

Please check the step 2 & 3 for solution 

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,