java - How do I make this program with multiple classes run? -
i doing project school , have been given code need modify. problem cannot code run in first place. know having problems class paths , packages because have never used them before. these classes:
import java.util.scanner; import java.io.*; public class sudsolve { public static void main(string[] args) { try { scanner scan = new scanner(system.in); system.out.println("enter sudoku file name"); string name = scan.next(); readboard r = new readboard(name); r.readlines(); sudboard b = new sudboard(r.getboardnums()); b.display(); b.solve(); } catch (ioexception e) { system.out.println(e); } } }
import java.util.scanner; import java.io.*; public class echo { string filename; // external file name scanner scan; // scanner object reading external file public echo(string f) throws ioexception{ filename = f; scan = new scanner(new filereader(filename)); } public void readlines() { // reads lines, hands each processline while (scan.hasnext()) { processline(scan.nextline()); } scan.close(); } public void processline(string line) { // real processing work system.out.println(line); } }
package sudoku; public class malformedboardexception extends exception { public malformedboardexception() { super(); } public malformedboardexception(string comment) { super(comment); } }
import java.io.ioexception; import java.util.stringtokenizer; public class readboard extends echo { // assumes board entries of form: 9 rows of 9 nums private int[] boardnums = new int[81]; private int currow = 0; public readboard(string filename) throws ioexception { super(filename); } public void processline(string line) { stringtokenizer s = new stringtokenizer(line); int pos = 0; int n; while (s.hasmoretokens()) { n = integer.parseint(s.nexttoken()); boardnums[9 * currow + pos] = n; pos++; } currow++; } public void checklegality() throws malformedboardexception { (int = 0; < boardnums.length; i++) { if (boardnums[i] < 0 || boardnums[i] > 9) { throw new malformedboardexception("board incorrect " + + " " + boardnums[i]); } } } public int[] getboardnums() { return boardnums; } } /* sudoku board has 9 rows , 9 columns = 81 cells. cells either have row/col designation e.g., row 4, col 7, or (4,7), or single int designation, numbered 0-80. latter numbering works left right, top bottom. sudoku divides board 9 zones, , array zonekeys gives cell numbers correspond nw corner cell of each zone */ public class sudboard { final static int size = 9; final static int maxval = 9; final int[] zonekeys = { 0, 3, 6, 27, 30, 33, 54, 57, 60 }; private int[][] cells = new int[size][size]; int count = 0; public sudboard(int[] nums) { // passed length 81 array - start board int j = 0; // counts though nums (int r = 0; r < size; r++) { (int c = 0; c < size; c++) { cells[r][c] = nums[j]; j++; } } } public sudboard install(int where, int whatval) { // param 0-80 val; r,c = row/col version int r = / size; int c = % size; this.cells[r][c] = whatval; return this; } public boolean impossible() { // current board impossible? boolean ans = false; ans = rowimpossible() || colimpossible() || zoneimpossible(); return ans; } public boolean possible() { return !impossible(); } public boolean rowimpossible() { // 1 of rows impossible? boolean[] boo = new boolean[maxval + 1]; (int k = 0; k < boo.length; k++) boo[k] = false; (int r = 0; r < maxval; r++) { (int c = 0; c < maxval; c++) { int v = cells[r][c]; if ((v > 0) && (boo[v] == true)) return true; else { boo[v] = true; } } (int k = 0; k < boo.length; k++) boo[k] = false; } return false; } public boolean colimpossible() { // 1 of columns impossible? boolean[] boo = new boolean[maxval + 1]; (int k = 0; k < boo.length; k++) boo[k] = false; (int c = 0; c < maxval; c++) { (int r = 0; r < maxval; r++) { int v = cells[r][c]; if ((v > 0) && (boo[v] == true)) return true; else { boo[v] = true; } } (int k = 0; k < boo.length; k++) boo[k] = false; } return false; } public boolean zoneimpossible() { // 1 of zones impossible? boolean[] boo = new boolean[maxval + 1]; // 0 - 9 sudoku boolean ans = false; // true means: zonal conflict, impossible (int j = 0; j < zonekeys.length; j++) { (int k = 0; k < boo.length; k++) boo[k] = false; ans = zonetest(zonekeys[j], boo); if (ans) return true; // there zone clash } return false; } private boolean zonetest(int j, boolean[] boo) { // tests 3x3 zone cell# j nw corner int r = j / size; int c = j % size; // row/col version of j (int = r; < r + 3; i++) (int m = c; m < c + 3; m++) { int v = cells[i][m]; if (v > 0) { if (boo[v] == true) return true; else boo[v] = true; } } return false; } public int firstempty() { // visits cells in english reading order l r, top bot (int j = 0; j < size * size; j++) { int r = j / size; int c = j % size; if (cells[r][c] == 0) return j; } return -1; // -1 means: full } public int wiggle(int r, int c) { // how many ints 1-9 possible @ cell (r,c) if (cells[r][c] != 0) return 0; else { boolean[] covered = new boolean[10]; // awkward avoid 0 covered[0] = true; rowcovered(r, covered); colcovered(c, covered); zonecovered(r, c, covered); int ct = 0; (boolean b : covered) if (!b) ct++; return ct; } } private void zonecovered(int r1, int c1, boolean[] b) { int z = getzonekey(r1, c1); int r = z / size; int c = z % size; (int = r; < r + 3; i++) (int m = c; m < c + 3; m++) { int v = cells[i][m]; if (v > 0) b[v] = true; } } private int getzonekey(int r, int c) { // given cell, zone in (id'd key) return (zonekeys[3 * (r / 3) + c / 3]); } private void rowcovered(int r, boolean[] b) { (int c = 0; c < 9; c++) { int k = cells[r][c]; b[k] = true; } } private void colcovered(int c, boolean[] b) { (int r = 0; r < 9; r++) { int k = cells[r][c]; b[k] = true; } } private boolean filled(int i, int j) { return (cells[i][j] > 0); } public void display() { // prints board (int r = 0; r < size; r++) { system.out.println(); (int c = 0; c < size; c++) { system.out.print(cells[r][c] + " "); } } system.out.println(); } public boolean solve() { count++; int w = firstempty(); if ((w == -1) && possible()) // -1 -> no empties { system.out.println("legal -- recur count: " + count); display(); return true; } else if (impossible()) return false; else { boolean ans = false; (int k = 1; k <= 9; k++) { // try possible values install(w, k); ans = solve(); if (ans) break; // found solution, break out } if (!ans) install(w, 0); // no soln - put 0 return ans; } } }
as can see program supposed solve sudoku puzzle. getting set correctly great!
Comments
Post a Comment