I need help creating this Sudoku board, I created a code but I am not sure if I
ID: 3726250 • Letter: I
Question
I need help creating this Sudoku board, I created a code but I am not sure if I did it correctly, I'm not sure to check if it's correct or if I even created my code the right way.
Instructions 1. Create a Prog4 project in NetBeans 2. Create a 9 x 9 matrix (2 dimensional array) of integers in the Prog4 class representing a Sudoku board and initialize it with 81 integers from 0-9. 0 represents a square that hasn't been assigned a number. Use an initializer for this: int00 a = { (2, 0, 0, 3, 6, 4, 0, 0, 0), 3. Create a method public static void display) that will display the board on the console with aligned columns Any cell in the board that has a value of 0 should be displayed as a blank 4 Create a method public static boolean oneChoiceo that impiements the One-Choice Sudoku solving technique 4.1For each blank cell in the puzzle: 1. Examine all the other cells in the same row, column and 3 x 3 box. 2 If all the numbers from 1.9 except for one are found in these other cells, then assign the missing number to the current blank cell. (This is equivalent to having only one cel blank). 4.2 Return true if the method assigned at least one new number to a cell, false otherwise 5. In the main method s1cal drsplayo to display the initial state of the Sudoku board. 5:2. Then repeatedly Callor choice()asiongasit keeps returning true 53..then call displayo agan to display the (hopefulily) solved puzzie G. Test ihe solver on the oilowing intial Sudoku board 2 0 0 3 6 4 0 00 0705 0b06 540000 0 03 0 0 780s plo 0 590007Ro 001/005 9l00 7 00/0 0 0 0 28 6000 7 0dsc 000620/0 07Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Y2018.March;
import java.util.Arrays;
import java.util.Random;
/**
*
* @author sambh
*/
public class Prog4 {
private static int board[][] = {{2,0,0,3,6,4,0,0,0},
{0,7,0,0,5,0,0,0,6},
{5,4,0,0,0,0,0,0,3},
{0,0,7,8,0,0,3,0,0},
{0,5,9,0,0,0,7,1,0},
{0,0,1,0,0,5,9,0,0},
{7,0,0,0,0,0,0,2,8},
{6,0,0,0,7,0,0,5,0},
{0,0,0,6,2,1,0,0,7}};
public static void display() {
System.out.println("==================");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++)
if (board[i][j] > 0)
System.out.print(board[i][j] + " ");
else
System.out.print(" ");
System.out.println();
}
System.out.println("==================");
}
public static boolean oneChoice() {
int emptyCount, left;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (board[i][j] == 0) {
//col search
left = 45; //this is 1 + 2 + 3 + ... + 8 + 9
emptyCount = 0;
for (int k = 0; k < 9; k++) { //scroll throught the rows of jth cols
if (board[k][j] > 0) {
left -= board[k][j];
}
else {
emptyCount ++;
}
}
if (emptyCount == 1) { //if exactly one element is left blank
board[i][j] = left;
return true;
}
//row search
left = 45;
emptyCount = 0;
for (int k = 0; k < 9; k++) {
if (board[i][k] > 0) {
left -= board[i][k];
}
else {
emptyCount ++;
}
}
if (emptyCount == 1) {
board[i][j] = left;
return true;
}
//3x3 box search
left = 45;
emptyCount = 0;
for (int k = ((i/3)*3); k < ((i/3)*3)+3; k++)
for (int l = ((j/3)*3); l < ((j/3)*3)+3; l++) {
if (board[k][l] > 0) {
left -= board[k][l];
}
else {
emptyCount ++;
}
}
if (emptyCount == 1) {
board[i][j] = left;
return true;
}
}
return false;
}
public static void main(String[] args) {
display();
while (oneChoice());
display();
}
}
OUTPUT:
==================
2 3 6 4
7 5 6
5 4 3
7 8 3
5 9 7 1
1 5 9
7 2 8
6 7 5
6 2 1 7
==================
==================
2 3 6 4
7 5 6
5 4 3
7 8 3
5 9 7 1
1 5 9
7 2 8
6 7 5
6 2 1 7
==================
*NOTE: Please provide a simpler soduku
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.