Question
My objective is to create a sudoku puzzle in java, using NetBeans and I need help fixing the code? I have errors when running, and
My objective is to create a sudoku puzzle in java, using NetBeans and I need help fixing the code? I have errors when running, and Im not sure why. Project details at bottom.
package sudoku;
import java.util.Scanner;
public class sudokuMaker {
public static void main(String[] args) { int[][] arr = {{5, 3, 4, 0, 7, 8, 9, 1, 2}, //initialize array {6, 7, 0, 1, 9, 5, 3, 4, 8}, {1, 9, 8, 3, 4, 2, 5, 0, 7}, {8, 0, 9, 7, 6, 1, 4, 2, 3}, {4, 2, 6, 8, 0, 3, 7, 9, 0}, {7, 1, 3, 9, 2, 4, 8, 5, 6}, {9, 0, 1, 5, 3, 7, 2, 8, 4}, {2, 8, 7, 4, 1, 9, 6, 0, 5}, {3, 4, 5, 2, 8, 6, 1, 7, 9} }; Sudoku sudoku = new Sudoku(arr); Scanner scan = new Scanner(System.in);
OUTER: while (!sudoku.isSolved()) { //check if sudoku is solved sudoku.display(); System.out.println("Continue Playing (Y/N)?"); //ask if player wants to play String option = scan.nextLine(); switch (option) { case "N": break OUTER; case "Y": System.out.print("Enter row: "); //input row int row = Integer.parseInt(scan.nextLine()); System.out.print("Enter column: "); //input column int col = Integer.parseInt(scan.nextLine()); System.out.println("Enter value: "); //input value String value = scan.nextLine(); if (!sudoku.isRowColumnValid(row, col)) { //check if row and column is valid System.out.println("Invalid row/column"); break OUTER; } if (!sudoku.isValueValid(value)) { //check if entered value is valid System.out.println("Invalid value"); break OUTER; } if (!sudoku.isRowColumnValid(row, col)) { //check if row and column have empty space System.out.println("Wrong Entry"); break OUTER; } if (!sudoku.isEntryCorrect(row, col, Integer.parseInt(value))) { //check if entered value is correct System.out.println("Incorrect"); } else { System.out.println("Correct"); } break; default: System.out.println("Invalid Input"); break; } } } }
package sudoku;
public class Sudoku {
private int arr[][];
public Sudoku(int a[][]) { arr = new int[9][9]; for (int i = 0; i
public void display() { for (int i = 0; i
public boolean isSolved() { for (int i = 0; i
public boolean isValueValid(String value) { if (value.length() > 1) { return false; } return value.charAt(0) >= '0' && value.charAt(0)
public boolean isRowColumnValid(int row, int col) { if (row = 9 || col = 9) { return false; } return arr[row][col] == 0; }
public boolean isEntryCorrect(int row, int col, int value) { for (int i = 0; i
int r = 0, c = 0; if (row >= 3 && row = 3 && col = 6 && row = 6 && col
}
number that is not between 1 and 9), displays the message "Invalid value" and goes back to the "Continue Playing (YIN)?" question. o Ifuser enters a wrong row/column combination (row/column that is not "O) displays the message "Wrong Entry", displays the current puzzle and then displays the "Continue Playing (Y/N)?" question. If user enters a valid rowicolumn/value, it checks if the entry is correct or not based on Sudoku rules. o -Ifentry is not correct, displays "NOT Correct!, displays the Sudoku puzzle again and then asks "Continue Playing(YIN)? If entry is correct, displays "Correct!", displays the Sudoku puzzle again and then asks "Continue Playing (Y/N)?" - After every 'corect' entry, program also checks if every piece of the puzzle was solved (no "0's left). If no "0 s left, it displays Congratulations, You solved it! message and ends the program. Your project should have the name YourLastName Sudoku Your program should have two classes: Sudoku and Driver Driver class will have the main method which create an object of the Sudoku class and initializes the values of the sudoku array based on the sudoku example above. Then it starts interacting with the user as described above. Sudoku will have all the checks and computations for the program. It will at least have the following methods: - display: Displays current values of the puzzle . is Solved: Checks if puzzle was solved or not . isValueValid: Is entered value is numeric and between 1 to 9 SRowColumnValid: Checks if user entered row, column is between the limits and if entered row/column value is "0 . isEntryCorrect: Checks if user entered row, column, value is correct based on Sudoku rules The puzzle is solved and the program should end when the following status is reached: 5 341 678 1912 6 721195 1348 198134 2156 7 859 761 I 4 23 426 853 1791 7 1 3192418 5 6 961 537 1284 287 4 19 635 3 451 286117 9Step 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