Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please complete to create a Sudoku Puzzle using 2D Arrays public class Sudoku { public String[][] makeSudoku(String s) { int SIZE = 9; int k

 

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("--------------------------------------------------");

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions

Question

2. Be a good example:

Answered: 1 week ago

Question

=+to live and work wherever he or she wants?

Answered: 1 week ago

Question

=+How will this affect the recruiting process?

Answered: 1 week ago