Question
This code is supposed to tell you if win sudoku but never does. import java.lang.reflect.Array; import java.util.Scanner; //Sudok //2/18/2019 public class Sudoku { public static
This code is supposed to tell you if win sudoku but never does.
import java.lang.reflect.Array;
import java.util.Scanner;
//Sudok
//2/18/2019
public class Sudoku {
public static void main(String[] args) {
// TOD O Auto-generated method stub
int[][] board = new int[9][9];
int[][] orig = new int[9][9];
Scanner kb = new Scanner(System.in);
initializeBoard(board);
initializeBoard(orig);
printBoard(board);
// 1) get input from user, mark on gameboard
while (true) {
int row, col, val;
for (row=0;row<10;row++) {
for (col=0;col<10;col++) {
System.out.print("Enter row#:");
row = kb.nextInt();
System.out.print("Enter col#:");
col = kb.nextInt();
System.out.print("Enter value:");
val = kb.nextInt();
if (orig[row][col] == 0) { // was the spot blankjQuery22402091165809012574_1550814015438?
board[row][col] = val;}
else {
System.out.println("Not a blank space, choose another spot");
}
printBoard(board);}
break;}
// 2) is the puzzle finished yet?
int win = 0;
int sum= 0;
for (col = 0; col < 9; col++) {
for (row = 0; row<9; row++) {
sum = sum + board[row][col];
}
if (sum == 45) {
continue;
}
else {
win++;
}
}
//Check the boxes for winning
if (WinningBoard(board, 0, 0)) {
continue;
}
else {
win++;
}
if (WinningBoard(board, 0, 3)) {
continue;
}
else {
win++;
}
if (WinningBoard(board, 0, 6)) {
continue;
}
else {
win++;
}
if (WinningBoard(board, 3, 0)) {
continue;
}
else {
win++;
}
if (WinningBoard(board, 3, 3)) {
continue;
}
else {
win++;
}
if (WinningBoard(board, 3, 6)) {
continue;
}
else {
win++;
}
if (WinningBoard(board, 6, 0)) {
continue;
}
else {
win++;
}
if (WinningBoard(board, 6, 3)) {
continue;
}
else {
win++;
}
if (WinningBoard(board, 6, 6)) {
continue;
}
else {
win++;
}
if (win == 0) {
System.out.println("WINNER. Game over");
System.exit(0);
}
win=0;
}
}
public static void printBoard (int[][] gb) {
int i, j;
for (i=0; i System.out.println("-------------------"); for (j=0; j if (gb[i][j] == 0) {// it's a blank System.out.print("| "); } else { System.out.print("|" + gb[i][j]); } } System.out.println("|"); } System.out.println("-------------------"); } public static void initializeBoard (int[][] gb) { gb[0][0] = 2; gb[0][1] = 4; gb[0][2] = 3; gb[0][3] = 7; gb[0][4] = 0; gb[0][5] = 1; gb[0][6] = 8; gb[0][7] = 6; gb[0][8] = 9; gb[1][0] = 5; gb[1][1] = 0; gb[1][2] = 7; gb[1][3] = 9; gb[1][4] = 0; gb[1][5] = 0; gb[1][6] = 0; gb[1][7] = 3; gb[1][8] = 0; gb[2][0] = 0; gb[2][1] = 1; gb[2][2] = 0; gb[2][3] = 0; gb[2][4] = 4; gb[2][5] = 0; gb[2][6] = 0; gb[2][7] = 0; gb[2][8] = 5; gb[3][0] = 0; gb[3][1] = 2; gb[3][2] = 0; gb[3][3] = 0; gb[3][4] = 7; gb[3][5] = 0; gb[3][6] = 5; gb[3][7] = 0; gb[3][8] = 0; gb[4][0] = 3; gb[4][1] = 0; gb[4][2] = 5; gb[4][3] = 0; gb[4][4] = 0; gb[4][5] = 0; gb[4][6] = 2; gb[4][7] = 0; gb[4][8] = 7; gb[5][0] = 0; gb[5][1] = 0; gb[5][2] = 1; gb[5][3] = 0; gb[5][4] = 3; gb[5][5] = 0; gb[5][6] = 0; gb[5][7] = 4; gb[5][8] = 0; gb[6][0] = 1; gb[6][1] = 0; gb[6][2] = 0; gb[6][3] = 0; gb[6][4] = 9; gb[6][5] = 0; gb[6][6] = 0; gb[6][7] = 7; gb[6][8] = 0; gb[7][0] = 0; gb[7][1] = 6; gb[7][2] = 0; gb[7][3] = 0; gb[7][4] = 0; gb[7][5] = 7; gb[7][6] = 4; gb[7][7] = 0; gb[7][8] = 8; gb[8][0] = 7; gb[8][1] = 3; gb[8][2] = 4; gb[8][3] = 5; gb[8][4] = 0; gb[8][5] = 8; gb[8][6] = 6; gb[8][7] = 9; gb[8][8] = 1; } public static boolean WinningBoard (int[][] gb,int row, int col) { int i,j; int sum=0; for (i=row; i<=row+2; i++) { for (j=col; j<=col+2; j++) { sum += gb[i][j]; } } if (sum==45) { return true; } else { return false; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started