System. out printin(parser.next ) ay one and potato on separate lines.) u is a p
ID: 3910585 • Letter: S
Question
System. out printin(parser.next ) ay one and potato on separate lines.) u is a popular logic puzzle that uses a 9 by 9 array of squares .S re organized into 3 by 3 subarrays. The puzzle solver must fill in ares with the digits 1 to 9 such that no digit is repeated in any column, or any of the nine 3 by 3 subgroups of squares. ally, some squares are filled in already and cannot be changed. For the following might be a starting configuration for a Sudoku 11. Sudoku row, any puzzle 1 2 34 9 7 8 65 4. 8 8 Create a class SudokuPuzzle that has the attributes . board-a 9 by 9 array of integers that represents the current state of the puzzle, where 0 indicates a blank square start-a 9 by 9 array of boolean values that indicates which squares in board are given values that cannot be changed and the following methods: ° SudokuPuzzle-a constructor that creates an empty puzzle o toString-returns a string representation of the puzzle that can be printed addInitial (row, col, value)-sets the given square to the given value as an initial value that cannot be changed by the puzzle solver addGuess (row, col, value)-sets the given square to the given value: the value can be changed later by another call to addGuess checkPuzz1e-returns true if the values in the puzzle do not violate the restrictions getValueIn (row, col)-returns the value in the given squareExplanation / Answer
import java.util.Arrays;
public class SudokuPuzzle {
private int[][] board;
private boolean[][] start;
public SudokuPuzzle() {
// TODO Auto-generated constructor stub
board = new int[9][9];
start = new boolean[9][9];
}
@Override
public String toString() {
// TODO Auto-generated method stub
String string = "";
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
string = string + " " + board[i][j];
}
string = string + " ";
}
return string;
}
public void addInitial(int row, int col, int value) {
board[row][col] = value;
start[row][col] = true;
System.out.println(toString());
}
public void addGuess(int row, int col, int value) {
if (!start[row][col]) {
board[row][col] = value;
if (checkPuzzle()) {
if (isFull()) {
System.out
.println("Congratulations! You have successfully solved the puzzle.");
System.out.println(toString());
}
} else {
System.out
.println("Your guess is wrong! You can't place this value here.");
System.out.println(toString());
}
} else
System.out.println("Player can't change the value at row: " + row
+ " and column: " + col);
}
public int getValueIn(int row, int col) {
return board[row][col];
}
public boolean[] getAllowedValues(int row, int col) {
boolean[] possibleArray = new boolean[9];
for (int b = 1; b < 10; b++) {
possibleArray[b - 1] = true;
}
int prev = board[row][col];
int baseRow;
int baseCol;
if (row < 3)
baseRow = 0;
else if (row < 6)
baseRow = 3;
else
baseRow = 6;
if (col < 3)
baseCol = 0;
else if (col < 6)
baseCol = 3;
else
baseCol = 6;
for (int n = 1; n <= 9; ++n) {
board[row][col] = n;
if (!(checkRow(row) && checkColumn(col) && checkSquare(baseRow,
baseCol, 3)))
possibleArray[n - 1] = false;
}
board[row][col] = prev;
return possibleArray;
}
public boolean isFull() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == 0)
return false;
}
}
return true;
}
public void reset() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (!start[i][j])
board[i][j] = 0;
}
}
}
/**
* Check whether the given 2D array is a valid Sudoku solution.
*
* @return true if the given array is a valid solution, false otherwise
*/
public boolean checkPuzzle() {
final int size = board.length;
// First make sure all the values are in the right range.
if (!checkValues(0, size)) {
return false;
}
// Check that the rows contain no duplicate values
for (int row = 0; row < size; ++row) {
if (!checkRow(row)) {
return false;
}
}
// Check that the columns contain no duplicate values
for (int col = 0; col < size; ++col) {
if (!checkColumn(col)) {
return false;
}
}
// Check that the subsquares contain no duplicate values
for (int baseRow = 0; baseRow < size; baseRow += 3) {
for (int baseCol = 0; baseCol < size; baseCol += 3) {
if (!checkSquare(baseRow, baseCol, 3)) {
return false;
}
}
}
// If we made it to this point, everything is correct!
return true;
}
/**
* Returns true if all values in grid are between min and max, inclusive.
*
* @param min
* the smallest allowable value
* @param max
* the largest allowable value
* @return false if some value in the array is less than min or greater than
* max, true otherwise
*/
private boolean checkValues(int min, int max) {
for (int row = 0; row < board.length; ++row) {
for (int col = 0; col < board[0].length; ++col) {
if (board[row][col] < min || board[row][col] > max) {
return false;
}
}
}
return true;
}
/**
* Returns true if the row of grid denoted by whichRow does not contain any
* duplicate values. Preconditions: whichRow < number of columns in grid all
* values in grid are between 1 and grid.length, inclusive
*
* @param whichRow
* the row to check
* @return true if the row contains no duplicate values, false otherwise
*/
private boolean checkRow(int whichRow) {
final int size = board.length;
boolean[] found = new boolean[size];
for (int col = 0; col < size; ++col) {
// set found[x - 1] to be true if we find x in the row
int index = board[whichRow][col] - 1;
if (index >= 0) {
if (!found[index]) {
found[index] = true;
} else {
// found it twice, so return false
return false;
}
}
}
// didn't find any number twice, so return true
return true;
}
/**
* Returns true if the column of grid denoted by whichCol does not contain
* any duplicate values. Preconditions: whichCol < number of columns in grid
* all values in grid are between 1 and grid.length, inclusive
*
* @param whichCol
* the column to check
* @return true if the column contains no duplicate values, false otherwise
*/
private boolean checkColumn(int whichCol) {
final int size = board.length;
boolean[] found = new boolean[size];
for (int row = 0; row < size; ++row) {
// set found[x - 1] to be true if we find x in the row
int index = board[row][whichCol] - 1;
if (index >= 0) {
if (!found[index]) {
found[index] = true;
} else {
// found it twice, so return false
return false;
}
}
}
// didn't find any number twice, so return true
return true;
}
/**
* Checks whether a square subarray contains duplicate values. Returns true
* if the subSquareSize x subSquareSize subarray of grid, with upper left
* corner at [baseRow, baseCol], does not contain any duplicate values.
* Preconditions: baseRow + subSquareSize <= number of columns in grid
* baseCol + subSquareSize <= number of rows in grid. all values in grid are
* between 1 and grid.length, inclusive
*
* @param grid
* the 2D array to check
* @param baseRow
* topmost row of the square
* @param baseCol
* leftmost column of the square
* @param subSquareSize
* the size of the subsquare to be checked
* @return true if the square contains no duplicates, false otherwise
*/
private boolean checkSquare(int baseRow, int baseCol, int subSquareSize) {
boolean[] found = new boolean[board.length];
for (int row = baseRow; row < (baseRow + subSquareSize); ++row) {
for (int col = baseCol; col < (baseCol + subSquareSize); ++col) {
// set found[x - 1] to be true if we find x in the row
int index = board[row][col] - 1;
if (index >= 0) {
if (!found[index]) {
found[index] = true;
} else {
// found it twice, so return false
return false;
}
}
}
}
return true;
}
}
import java.util.Scanner;
public class Sudoku {
public Sudoku() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
SudokuPuzzle puzzle = new SudokuPuzzle();
System.out.println(puzzle);
int choice = 0;
Scanner sc = new Scanner(System.in);
do {
System.out
.println("Choice 1: Add an initial(fixed) value to the board");
System.out
.println("Choice 2: Add a guess(variable) value to the board and validate");
System.out
.println("Choice 3: Check if the values in the board follow the rules.");
System.out.println("Choice 4: Get a value from the board.");
System.out
.println("Choice 5: Get all allowed values at a position in the board.");
System.out.println("Choice 6: Reset the board.");
System.out.println("Choice 7: to Quit.");
System.out.println("Enter your choice");
choice = Integer.parseInt(sc.nextLine());
if (choice == 1) {
int[] indices = readRowColumn(sc);
int value = readValue(sc);
puzzle.addInitial(indices[0], indices[1], value);
} else if (choice == 2) {
int[] indices = readRowColumn(sc);
int value = readValue(sc);
puzzle.addGuess(indices[0], indices[1], value);
} else if (choice == 3) {
if (puzzle.checkPuzzle())
System.out.println("All the values follow the rules.");
else
System.out
.println("One or more values don't follow the rules.");
} else if (choice == 4) {
int[] indices = readRowColumn(sc);
int value = puzzle.getValueIn(indices[0], indices[1]);
System.out.println("Value is: " + value);
} else if (choice == 5) {
int[] indices = readRowColumn(sc);
boolean[] values = puzzle.getAllowedValues(indices[0], indices[1]);
System.out.print("The allowed values are: ");
for(int i =1; i<=9;i++) {
if(values[i-1])
System.out.print(i+" ");
}
System.out.println();
}
else if (choice == 6) {
puzzle.reset();
}
} while (choice >= 1 && choice <= 6);
}
private static int[] readRowColumn(Scanner sc) {
// TODO Auto-generated method stub
int row, col;
do {
System.out.println("Enter valid row number(0-8)");
row = Integer.parseInt(sc.nextLine());
} while (row < 0 || row > 8);
do {
System.out.println("Enter valid column number(0-8)");
col = Integer.parseInt(sc.nextLine());
} while (col < 0 || col > 8);
int[] array = new int[2];
array[0] = row;
array[1] = col;
return array;
}
private static int readValue(Scanner sc) {
// TODO Auto-generated method stub
int value;
do {
System.out.println("Enter valid value(1-9)");
value = Integer.parseInt(sc.nextLine());
} while (value < 1 || value > 9);
return value;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.