import java.util.Scanner; import java.io.*; public class KPuzzle { //a 2-d array of the cages, cages that are more than 1 square will have alieases on all of the squares. KCage[] p;//The puzzle int size; boolean[] p2; //The possiblilities for every square - 3D public KPuzzle() { } public KPuzzle(String filename) { parsePuzzle(filename); makePossible(); checkCages(); } private void checkCages() { for(int i = 0; i < p.length; i++) { KCage c = p[i]; if(c.getNumCells() == 1) { Cell cell = c.getCell(0); this.putNumber(cell.getX(),cell.getY(),c.getTargNumber()); System.out.println("After putting " + c.getTargNumber() + " into (" + cell.getX() + "," + cell.getY() + ") Possibilities are:"); this.printPossibilities(); return; } } } public Cell getCell(int x, int y) { KCage k = p[y*size + x]; for(int i = 0; i < k.getNumCells();i++) { Cell c = k.getCell(i); if(c.getX() == x && c.getY() == y) { return c; } } return null; } public boolean isComplete() { for(int i = 0; i < size; i++) { for(int j = 0; j < size; j++) { int possibilitiesForCell = 0; for(int k = 0; k < size; k++) { if(p2[(i*size*size)+(j*size)+k]) { possibilitiesForCell++; } } if(possibilitiesForCell != 1) { return false; } } } return true; } public boolean[] getPossibilities(int xn, int yn) { boolean[] output = new boolean[size]; for(int i = 0; i < size; i++) { output[i] = p2[(yn*size*size)+(xn*size)+i]; } return output; } public void removePossibility(int xn, int yn, int num) { p2[(yn*size*size)+(xn*size)+num-1] = false; } public void addPossibility(int xn, int yn, int num) { p2[(yn*size*size)+(xn*size)+num-1] = true; } public void putNumber(int xn, int yn, int num) { System.out.println("putting " + num + " in (" + xn + "," + yn + ")"); for(int y = 0; y < size; y++) { for(int z = 0; z < size; z++) { if(z == num-1) { //System.out.println("Setting (" + xn + "," + y + "," + z + ") to false"); p2[(y*size*size)+(xn*size)+z] = false; } } } for(int x = 0; x < size; x++) { for(int z = 0; z < size; z++) { if(z == num-1) p2[(yn*size*size)+(x*size)+z] = false; } } for(int z = 0; z < size; z++) { p2[(yn*size*size)+(xn*size)+z] = false; } System.out.println("Setting (" + xn + "," + yn + "," + (num-1) + ") to true"); p2[(yn*size*size)+(xn*size)+num-1] = true; } public void takeNumber(int xn, int yn) { int thisnum = -1; for(int z = 0; z < size; z++) { if(p2[(yn*size*size)+(xn*size)+z]) thisnum = z; } if(thisnum == -1) { System.out.println("I couldn't find a number there!"); return; } for(int y = 0; y < size; y++) { for(int z = 0; z < size; z++) { if(z == thisnum) { System.out.println("Setting (" + xn + "," + y + "," + z + ") to true"); p2[(y*size*size)+(xn*size)+z] = true; } } } for(int x = 0; x < size; x++) { for(int z = 0; z < size; z++) { if(z == thisnum) p2[(yn*size*size)+(x*size)+z] = true; } } for(int z = 0; z < size; z++) { p2[(yn*size*size)+(xn*size)+z] = true; } } public void printPossibilities() { for(int y = 0; y < size; y++) { for(int x = 0; x < size; x++) { System.out.print("Possibilities for (" + x + "," + y + ") are: "); for(int z = 0; z < size; z++) { if(p2[(y*size*size)+(x*size)+z]) { System.out.print(z+1); } } System.out.println(""); } } } private void makePossible() { p2 = new boolean[size*size*size]; //System.out.println("Makeing possibilities:"); for(int i = 0; i < size; i++) { for(int j = 0; j < size; j++) { for(int k = 0; k < size; k++) { p2[(i*size*size)+(j*size)+k] = true; //System.out.print(p2[(i*size*size)+(j*size)+k] + " "); } //System.out.print("\t"); } //System.out.println(""); } } private void parsePuzzle(String filename) { Scanner s = null; try { s = new Scanner(new FileInputStream(filename)); } catch (Exception e) { System.out.println("Something went terribly wrong!\n" + e); } size = Integer.parseInt(s.nextLine().replaceAll("\n","")); p = new KCage[size*size]; //System.out.println("Puzzle size: " + size + " Cages: " + p.length); for(int i = 0; s.hasNextLine();i++) { String t1 = s.nextLine(); //System.out.println("Praseing: " + t1); String[] t2 = t1.split(","); //System.out.println("Parts length: " + t2.length); if(t2.length > 1) { Cell[] allcells = new Cell[Integer.parseInt(t2[2])]; for(int j = 0; j < Integer.parseInt(t2[2]);j++) { String[] coords = s.nextLine().split(","); allcells[j] = new Cell(Integer.parseInt(coords[0]),Integer.parseInt(coords[1])); } for(Cell c : allcells) { p[c.getY()*size + c.getX()] = new KCage(Integer.parseInt(t2[0]),getOp(t2[1]),allcells); } //p[i] = new KCage(Integer.parseInt(t2[0]),getOp(t2[1]),allcells); //System.out.println("Created " + p[i]); } } } public KCage getCage(int i) { return p[i]; } public int getNumCages() { return p.length; } public String toString() { String output = "Puzzle{\n\t"; if(p.length == 0) return output+"}"; output += p[0]; for(int i = 1; i < p.length; i++) { output += ",\n\t"+p[i]; } return output + "\n}"; } private static Operator getOp(String s) { switch(s) { case "+": return Operator.PLUS; case "-": return Operator.MINUS; case "*": return Operator.MULTIPLY; case "/": return Operator.DIVIDE; case " ": return Operator.NONE; default: return Operator.NULL; } } }