Hello guys and girls,
I'm doing this assignment and it involves making a mini sudoku game 4x4 ( four 2x2 grids) and the user enters 5 numbers into the board and starts solving it. Also we are not allowed to use arrays for this task.
I've been trying to write the code up and this is what I have come up with.
import java.util.Scanner; public class Main { public static final Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { int n = 1; Sudoku board = new Sudoku(); System.out.println("Configure puzzle"); System.out.print("Region: "); int region = keyboard.nextInt(); System.out.print("Cell: "); int cell = keyboard.nextInt(); System.out.print("Number: "); int number = keyboard.nextInt(); board.enterNumber(region, cell, number, true); board.print(); while (n < 5){ System.out.print("Region: "); region = keyboard.nextInt(); System.out.print("Cell: "); cell = keyboard.nextInt(); System.out.print("Number: "); number = keyboard.nextInt(); board.enterNumber(region, cell, number,true); board.print(); n += 1; } System.out.println("Puzzle entered. Time to solve!"); int p = 1; while (p < 13){ System.out.print("Region: "); region = keyboard.nextInt(); System.out.print("Cell: "); cell = keyboard.nextInt(); System.out.print("Number: "); number = keyboard.nextInt(); board.enterNumber(region, cell, number,false); board.print(); p += 1; } } }
and
public class Sudoku { private int a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4; public Sudoku() { } public void enterNumber(int region, int cell, int number, boolean lock) { { switch (region){ case 1: if (cell == 1){ a1 = number; }else if (cell == 2){ a2 = number; }else if (cell == 3){ a3 = number; }else if (cell == 4){ a4 = number; } break; case 2: if (cell == 1){ b1 = number; }else if (cell == 2){ b2 = number; }else if (cell == 3){ b3 = number; }else if (cell == 4){ b4 = number; } break; case 3: if (cell == 1){ c1 = number; }else if (cell == 2){ c2 = number; }else if (cell == 3){ c3 = number; }else if (cell == 4){ c4 = number; } break; case 4: if (cell == 1){ d1 = number; }else if (cell == 2){ d2 = number; }else if (cell == 3){ d3 = number; }else if (cell == 4){ d4 = number; } break; } } } public void print() { System.out.println(); System.out.println(+ a1 + " " + a2 + " " + b1 + " " + b2); System.out.println(+ a3 + " " + a4 + " " + b3 + " " + b4); System.out.println(+ c1 + " " + c2 + " " + d1 + " " + d2); System.out.println(+ c3 + " " + c4 + " " + d3 + " " + d4); System.out.println(); } }
Basically it's working to an extent but I am unsure about a few things.
1. The task requires me to print out blank spaces " " instead of 0's for spots that don't have a value yet. ( The teacher gave us a hint of making all of them -1 but I'm not sure how that helps me)
2. I used boolean lock to try lock in the first 5 entries the user inputs when they configure the board. However, when I input a new number to one of the initial spots, the original entry gets overwritten.
Sorry for the wall of text. Any help/advice would be appreicated.
Thanks