// SudokuHelper.java, Ole Marius Hoel Rindal import easyIO.*; class SudokuHelper { public static void main(String[] args) { Methods go = new Methods(); go.makeBoard(); go.printBoard(); go.loop(); } } class Methods { int[][] board = new int[9][9]; int[] nums = new int[9]; In infile = new In("sudoku1.txt"); In in = new In(); //Reads the file into a two dimensional array void makeBoard() { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { board[i][j] = Integer.parseInt(infile.inWord()); } } } //Prints out the board void printBoard() { for (int i = 0; i < board.length; i++) { System.out.print("---" + (i + 1) + "[ "); for (int j = 0; j < board[0].length; j++) { System.out.print(board[i][j] + " "); } System.out.println("]---"); } } void loop() {//Loop getting input from user and going through the board String input = ""; while (!input.equals("0")) { //Fills a nums array with all the possible numbers //As the program finds numbers already used, it puts a -1 instead //of the possible number in this nums array for (int k = 0; k < nums.length; k++) { nums[k] = (k + 1); } System.out.print("---[Type a row and a colomn number (r-c)>"); input = in.inWord(); if (!input.equals("0")) { checkLines(input); //If the user dont quit's - start the checkLines() method } } } void checkLines(String a) { //Convert the input string to numbers int row = Integer.parseInt(Character.toString(a.charAt(0))); int col = Integer.parseInt(Character.toString(a.charAt(2))); if (board[row-1][col-1] == 0) {//See if the place the user requested contains a number //Check the line vertically for (int i = 0; i < board.length; i++) { for (int j = 0; j < nums.length; j++) { if (board[i][col-1] == nums[j]) { nums[j] = -1; } } } //Check the line horizontally for (int i = 0; i < board.length; i++) { for (int j = 0; j < nums.length; j++) { if (board[row-1][i] == nums[j]) { nums[j] = -1; } } } checkQuadrant(row,col); //When done checking lines, start the CheckQuadrant() method } else {//If the place already contains a number, tell the user System.out.println("---[(" + row + "-" + col + ") already contains a number]---"); } } void checkQuadrant(int row, int col) { //Divide the board into quadrants, and check the numbers around the palce the user requested //Find the location of the quadrant (its top left corner) int quadrantRow = 0; int quadrantCol = 0; if (row > 3 && row <= 6) quadrantRow = 3; if (row > 6 && row <= 9) quadrantRow = 6; if (col > 3 && col <= 6) quadrantCol = 3; if (col > 6 && col <= 9) quadrantCol = 6; for (int i = quadrantRow; i < quadrantRow + 3; i++) { for (int l = quadrantCol; l < quadrantCol + 3; l++) { for (int j = 0; j < nums.length; j++) { if (board[i][l] == nums[j]) { nums[j] = -1; } } } } System.out.print("---[The possible numbers for ("+row+"-"+col+") are "); for (int i = 0; i < nums.length; i++) { if (nums[i] != -1) {//Goes through the numbers array checking wich numbers is left and then are possible for the place the user requested System.out.print(nums[i]+","); } } System.out.print("]---\n"); } }