Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please complete to create a Sudoku Puzzle using 2D Arrays public class Sudoku {

ID: 3745732 • Letter: P

Question

Please complete to create a Sudoku Puzzle using 2D Arrays

public class Sudoku {

public String[][] makeSudoku(String s) {

int SIZE = 9;

int k = 0;

String[][] x = new String[SIZE][SIZE];

for (int i = 0; i < SIZE; i++) {

for (int j = 0; j < SIZE; j++) {

x[i][j] = s.substring(k, k + 1);

k++;

}

}

return x;

}

public String getPrintableSudoku(String[][] x) {

int SIZE = 9;

String temp = "";

for (int i = 0; i < SIZE; i++) {

if ((i == 3) || (i == 6)) {

temp = temp + "================= ";

}

for (int j = 0; j < SIZE; j++) {

if ((j == 3) || (j == 6)) {

temp = temp + " || ";

}

temp = temp + x[i][j];

}

temp = temp + " ";

}

return temp;

}

public boolean isValidSudoku(String[][] x) {

return rowsAreLatin(x) && colsAreLatin(x) && goodSubsquares(x);

}

public boolean rowsAreLatin(String[][] x) {

boolean test = true;

for (int i = 0; i < x.length; i++) {

test = test && rowIsLatin(x,i);

}

return test;

}

  

  

// OR...Try the more efficient algorithm below:

//

// public boolean rowsAreLatin(String[][] x)

// {

// boolean test = true;

// int i = 0;

// while (test == true && i < x.length)

// {

// test = test & rowIsLating(x,i);

// }

// }

  

  

  

public boolean rowIsLatin(String[][] x, int i) {

// fill in your code here

return true;

}

public boolean colsAreLatin(String[][] x) {

// fill in your code here

return true;

}

public boolean colIsLatin(String[][] x, int j) {

// fill in your code here

return true;

}

public boolean goodSubsquares(String[][] x) {

// fill in your code here

return true;

}

public boolean goodSubsquare(String[][] x, int i, int j) {

// fill in your code here

return true;

}

}

public class SudokuSimulation {

/**

   * @param args the command line arguments

   */

public static void main(String[] args) {

Sudoku mySudokuPuzzle = new Sudoku();

  

// Row and subsquares are invalid

String config0 = "9234567892345678913456789124567891235678912346"

+ "78912345789123456891234567912345678";

String[][] puzzle0 = mySudokuPuzzle.makeSudoku(config0);

if (mySudokuPuzzle.isValidSudoku(puzzle0)) {

System.out.println("This puzzle is valid.");

} else {

System.out.println("This puzzle is invalid.");

}

System.out.println(mySudokuPuzzle.getPrintableSudoku(puzzle0));

System.out.println("--------------------------------------------------");

  

   // Col and subsquares are invalid

String config00 = "9234567899345678913456789124567891235678912346"

+ "78912345789123456891234567912345678";

String[][] puzzle00 = mySudokuPuzzle.makeSudoku(config00);

if (mySudokuPuzzle.isValidSudoku(puzzle00)) {

System.out.println("This puzzle is valid.");

} else {

System.out.println("This puzzle is invalid.");

}

System.out.println(mySudokuPuzzle.getPrintableSudoku(puzzle00));

System.out.println("--------------------------------------------------");

  

  

  

// Row and column Latin but with invalid subsquares

String config1 = "1234567892345678913456789124567891235678912346"

+ "78912345789123456891234567912345678";

String[][] puzzle1 = mySudokuPuzzle.makeSudoku(config1);

if (mySudokuPuzzle.isValidSudoku(puzzle1)) {

System.out.println("This puzzle is valid.");

} else {

System.out.println("This puzzle is invalid.");

}

System.out.println(mySudokuPuzzle.getPrintableSudoku(puzzle1));

System.out.println("--------------------------------------------------");

  

  

// Row Latin but column not Latin and with invalid subsquares

String config2 = "12345678912345678912345678912345678912345678"

+ "9123456789123456789123456789123456789";

String[][] puzzle2 = mySudokuPuzzle.makeSudoku(config2);

if (mySudokuPuzzle.isValidSudoku(puzzle2)) {

System.out.println("This puzzle is valid.");

} else {

System.out.println("This puzzle is invalid.");

}

System.out.println(mySudokuPuzzle.getPrintableSudoku(puzzle2));

System.out.println("--------------------------------------------------");

  

  

// A valid sudoku

String config3 = "25813764914698532779324685147286319558149273663"

+ "9571482315728964824619573967354218";

String[][] puzzle3 = mySudokuPuzzle.makeSudoku(config3);

if (mySudokuPuzzle.isValidSudoku(puzzle3)) {

System.out.println("This puzzle is valid.");

} else {

System.out.println("This puzzle is invalid.");

}

System.out.println(mySudokuPuzzle.getPrintableSudoku(puzzle3));

System.out.println("--------------------------------------------------");

}

}

Explanation / Answer

Below is your code: -

public class Sudoku {

public String[][] makeSudoku(String s) {

int SIZE = 9;

int k = 0;

String[][] x = new String[SIZE][SIZE];

for (int i = 0; i < SIZE; i++) {

for (int j = 0; j < SIZE; j++) {

x[i][j] = s.substring(k, k + 1);

k++;

}

}

return x;

}

public String getPrintableSudoku(String[][] x) {

int SIZE = 9;

String temp = "";

for (int i = 0; i < SIZE; i++) {

if ((i == 3) || (i == 6)) {

temp = temp + "================= ";

}

for (int j = 0; j < SIZE; j++) {

if ((j == 3) || (j == 6)) {

temp = temp + " || ";

}

temp = temp + x[i][j];

}

temp = temp + " ";

}

return temp;

}

public boolean isValidSudoku(String[][] x) {

return rowsAreLatin(x) && colsAreLatin(x) && goodSubsquares(x);

}

public boolean rowsAreLatin(String[][] x) {

boolean test = true;

for (int i = 0; i < x.length; i++) {

test = test && rowIsLatin(x, i);

}

return test;

}

// OR...Try the more efficient algorithm below:

//

// public boolean rowsAreLatin(String[][] x)

// {

// boolean test = true;

// int i = 0;

// while (test == true && i < x.length)

// {

// test = test & rowIsLating(x,i);

// }

// }

public boolean rowIsLatin(String[][] x, int i) {

boolean[] found = new boolean[9];

for (int j = 0; j < 9; j++) {

found[Integer.parseInt(x[i][j]) - 1] = true;

}

for (int j = 0; j < 9; j++) {

if (!found[j]) {

return false;

}

}

return true;

}

public boolean colsAreLatin(String[][] x) {

boolean result = true; // Assume cols are latin

for (int j = 0; j < 9; j++) {

result = result && colIsLatin(x, j); // Make sure each row is latin

}

return result;

}

public boolean colIsLatin(String[][] x, int j) {

// fill in your code here

boolean[] found = new boolean[9];

for (int i = 0; i < 9; i++) {

found[Integer.parseInt(x[i][j]) - 1] = true;

}

for (int i = 0; i < 9; i++) {

if (!found[i]) {

return false;

}

}

return true;

}

public boolean goodSubsquares(String[][] x) {

for (int i = 0; i<3 ; i++)

{

for( int j = 0; j<3; j++)

{

if (!goodSubsquare(x, i, j)) {

return false;

}

}

  

}

return true;

}

public boolean goodSubsquare(String[][] x, int i, int j) {

boolean[] list = new boolean[9];

for(int p = i * 3, rowEnd = p + 3; p < rowEnd; p++)

{

for(int q = j * 3, colEnd = q + 3; q < colEnd; q++) {

int num = Integer.parseInt(x[p][q]) - 1;

if(list[num])

{

return false;

}

list[num] = true;

}

}

return true;

}

}

Output

This puzzle is invalid.
923 || 456 || 789
234 || 567 || 891
345 || 678 || 912
=================
456 || 789 || 123
567 || 891 || 234
678 || 912 || 345
=================
789 || 123 || 456
891 || 234 || 567
912 || 345 || 678

--------------------------------------------------
This puzzle is invalid.
923 || 456 || 789
934 || 567 || 891
345 || 678 || 912
=================
456 || 789 || 123
567 || 891 || 234
678 || 912 || 345
=================
789 || 123 || 456
891 || 234 || 567
912 || 345 || 678

--------------------------------------------------
This puzzle is invalid.
123 || 456 || 789
234 || 567 || 891
345 || 678 || 912
=================
456 || 789 || 123
567 || 891 || 234
678 || 912 || 345
=================
789 || 123 || 456
891 || 234 || 567
912 || 345 || 678

--------------------------------------------------
This puzzle is invalid.
123 || 456 || 789
123 || 456 || 789
123 || 456 || 789
=================
123 || 456 || 789
123 || 456 || 789
123 || 456 || 789
=================
123 || 456 || 789
123 || 456 || 789
123 || 456 || 789

--------------------------------------------------
This puzzle is valid.
258 || 137 || 649
146 || 985 || 327
793 || 246 || 851
=================
472 || 863 || 195
581 || 492 || 736
639 || 571 || 482
=================
315 || 728 || 964
824 || 619 || 573
967 || 354 || 218

--------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote