PK ] = META-INF/PK \ = +META-INF/MANIFEST.MFManifest-Version: 1.0 Ant-Version: Apache Ant 1.7.1 Created-By: 10.0-b19 (Sun Microsystems Inc.) Main-Class: com.aswinvanwoudenberg.binary.Binary Class-Path: X-COMMENT: Main-Class will be added automatically by build PK m =com/PK m =com/aswinvanwoudenberg/PK m =com/aswinvanwoudenberg/binary/PK m =1eAAzz*com/aswinvanwoudenberg/binary/Binary.class1i 8 9 : ; <= >? >@A BC DEF GHIJK LMN O P GQ RSboard%Lcom/aswinvanwoudenberg/binary/Board;()VCodeLineNumberTableLocalVariableTablethis&Lcom/aswinvanwoudenberg/binary/Binary;(III)VrIcepuzzleGenerated()Z printBoard findSolution solutionsLjava/util/List;LocalVariableTypeTable7Ljava/util/List;generatePuzzlemain([Ljava/lang/String;)Vbinaryrowscolsemptyargs[Ljava/lang/String; SourceFile Binary.java  -  TU VWX YZ [\#com/aswinvanwoudenberg/binary/Board] ^_` ab8Usage: java -jar Binary.jar #rows #columns #empty_fieldsc de Examples:java -jar Binary.jar 10 10 70java -jar Binary.jar 6 6 25f gh$com/aswinvanwoudenberg/binary/Binary  ' d (java/lang/Objectprint$com/aswinvanwoudenberg/binary/Solversolve8(Lcom/aswinvanwoudenberg/binary/Board;I)Ljava/util/List;java/util/Listsize()Iget(I)Ljava/lang/Object;'com/aswinvanwoudenberg/binary/Generatorgenerate*(III)Lcom/aswinvanwoudenberg/binary/Board;java/lang/SystemoutLjava/io/PrintStream;java/io/PrintStreamprintln(Ljava/lang/String;)Vjava/lang/IntegerparseInt(Ljava/lang/String;)I!A**  F   \ ** *  !" #" $"%&7 *  '6*  (s!*L+*+  "! )*+  ),- W *  % &*  !" #" $" ./`*&     9*2<*2=*2>Y: :01234)60778>9J:O;U<Z=_C4J00/1"7(2">!3"`4567PK m =Eڈ)com/aswinvanwoudenberg/binary/Binary.javapackage com.aswinvanwoudenberg.binary; import java.util.List; /** * The main class for the Binary game solver/generator * @author Aswin F. van Woudenberg */ public class Binary { Board board; public Binary() { generatePuzzle(10, 10, 70); } public Binary(int r, int c, int e) { generatePuzzle(r, c, e); } public boolean puzzleGenerated() { return (board != null); } public void printBoard() { board.print(); } public void findSolution() { List solutions = Solver.solve(board, 1); if (solutions.size() > 0) { board = solutions.get(0); } } public void generatePuzzle(int r, int c, int e) { board = Generator.generate(r, c, e); } /** * The main method of this program. * @param args the command line arguments */ public static void main(String args[]) { int rows; int cols; int empty; if (args.length != 3) { System.out.println("Usage: java -jar Binary.jar #rows #columns #empty_fields"); System.out.println("Examples:"); System.out.println("java -jar Binary.jar 10 10 70"); System.out.println("java -jar Binary.jar 6 6 25"); } else { rows = Integer.parseInt(args[0]); cols = Integer.parseInt(args[1]); empty = Integer.parseInt(args[2]); Binary binary = new Binary(rows,cols,empty); binary.printBoard(); System.out.println(); binary.findSolution(); binary.printBoard(); } } } PK m =M)com/aswinvanwoudenberg/binary/Board.class1Q 6 7 8 9 : ;< =>? =@AB =CD EF 6 G HIsquares[[IrowsIcols()VCodeLineNumberTableLocalVariableTablethis%Lcom/aswinvanwoudenberg/binary/Board;(II)Vrc setupBoardji getSquareAt(II)I setSquareAt(III)Vvalue getRowCount()I getColCount printSquareprintcopy'()Lcom/aswinvanwoudenberg/binary/Board;aCopy SourceFile Board.java  $!   J KLM 0N 0O+-+ PO| /!#com/aswinvanwoudenberg/binary/Board '( )*java/lang/Objectjava/lang/SystemoutLjava/io/PrintStream;java/io/PrintStream(I)V(Ljava/lang/String;)Vprintln! ? **       !Q **    " #$!;***>"6*2O߱&  $.4:4%$&; ;";#'(G *2.!   " #)*V *2O % &*  " # +,-/*)  .-/*-  /!p&*2.*2.  1 24%6 & &"&#0w<*O=*  =* * <*  B9 :;:"=*?4@<AB?HCP9VF`GhFnIvJ4 %,%T&X%w 12VYL+*+*+**=*%>*+*+* MNOP(Q2R<SHRNQTV*4%**&V N3 45PK m =(com/aswinvanwoudenberg/binary/Board.javapackage com.aswinvanwoudenberg.binary; /** * This class represents the board as is used in the puzzle game Binary a sudoku * like puzzle. * @author Aswin F. van Woudenberg */ public class Board { private int squares[][]; private int rows; private int cols; public Board() { setupBoard(10, 10); } public Board(int r, int c) { setupBoard(r, c); } public void setupBoard(int r, int c) { squares = new int[r][c]; rows = r; cols = c; for (int i=0; i()VCodeLineNumberTableLocalVariableTablethis)Lcom/aswinvanwoudenberg/binary/Generator; getRandom()Ljava/util/Random;generate*(III)Lcom/aswinvanwoudenberg/binary/Board;puzzle%Lcom/aswinvanwoudenberg/binary/Board; solutionsLjava/util/List;rIceresultLocalVariableTypeTable7Ljava/util/List; emptySquaresM(Lcom/aswinvanwoudenberg/binary/Board;I)Lcom/aswinvanwoudenberg/binary/Board; returnValaCopyiboard indexList[IhasUniqueSolution((Lcom/aswinvanwoudenberg/binary/Board;)ZgenerateRandomList(I)[Itjlength SourceFileGenerator.java  java/util/Random#com/aswinvanwoudenberg/binary/Board VW XYZ [\ ]^ ./ _\ `\ 89 ab cd ef 67  gh'com/aswinvanwoudenberg/binary/Generatorjava/lang/Object(II)V$com/aswinvanwoudenberg/binary/Solversolve8(Lcom/aswinvanwoudenberg/binary/Board;I)Ljava/util/List;java/util/Listsize()Iget(I)Ljava/lang/Object; getRowCount getColCount getSquareAt(II)Icopy'()Lcom/aswinvanwoudenberg/binary/Board; setSquareAt(III)VnextInt(I)I! /*   4 Y !"<N-7Y:: : N-& !"#$#%0(7):+>&#$%&<'(<)(<*(:+$, %- ./!M* * h No6-dd^*-.* l-.* p>*:-.* l-.* pd : M *M,>5689!:;<A=Y?a@kApBsCv9KNHk 0$A51$f2(3$.(+$t45 67`*L+ RS3$%&, %- 89M L= +O= 1>$6+.6++.O+O݄+6 WY ZY]^$_-`2a9b?^E]KfH2(-'(2 :(&2(4;(M<(I+5= >?PK m =fb1  ,com/aswinvanwoudenberg/binary/Generator.javapackage com.aswinvanwoudenberg.binary; import java.util.List; import java.util.Random; /** * This class generates new binary puzzles. * @author Aswin F. van Woudenberg */ public class Generator { private static Random instance = null; private Generator() { } // Exists only to defeat instantiation. private static Random getRandom() { if(instance == null) { instance = new Random(); } return instance; } /** * Generate a new binary puzzle. * @param r number of rows * @param c number of columns * @param e number of empty squares * @return the generated puzzle */ public static Board generate(int r, int c, int e) { Board result = null; while (result == null) { // Try several times, depending on initial 'solved' situation. Board puzzle = new Board(r,c); List solutions = Solver.solve(puzzle, 1); if (solutions.size() > 0) { puzzle = solutions.get(0); } result = emptySquares(puzzle,e); } return result; } /** * Recursively clear cells. * @param board The board to clear cells from * @param emptySquares The number of cells to clear * @return The resulting board. */ private static Board emptySquares(Board board, int emptySquares) { Board result = null; int[] indexList = generateRandomList(board.getRowCount()*board.getColCount()); if (emptySquares > 0) { for (int i=0; i<(indexList.length-(emptySquares-1)); i++) { if (board.getSquareAt(indexList[i]/board.getColCount(), indexList[i]%board.getColCount()) != -1) { Board aCopy = board.copy(); aCopy.setSquareAt(indexList[i]/board.getColCount(), indexList[i]%board.getColCount(), -1); if (hasUniqueSolution(aCopy)) { Board returnVal = emptySquares(aCopy,emptySquares-1); if (returnVal != null) { result = returnVal; break; } } } } } else { result = board; } return result; } private static boolean hasUniqueSolution(Board board) { List solutions = Solver.solve(board, 2); return (solutions.size() == 1); } private static int[] generateRandomList(int length) { int [] result = new int[length]; for (int i=0; i()VCodeLineNumberTableLocalVariableTablethis%Lcom/aswinvanwoudenberg/binary/Rules;equalDistribution((Lcom/aswinvanwoudenberg/binary/Board;)ZjI countOnes countZerosiboard%Lcom/aswinvanwoudenberg/binary/Board;maxCountOfEachNumberresultZnoMoreThanTwoOfTheSameNumbersalreadyCountedcurrentlyCountingnoEqualRowsOrColumnskisEqual rowHasVars)(Lcom/aswinvanwoudenberg/binary/Board;I)Zr colHasVarsc SourceFile Rules.java 2 34 54 67 "# %##com/aswinvanwoudenberg/binary/Rulesjava/lang/Object#com/aswinvanwoudenberg/binary/Board getColCount()I getRowCount getSquareAt(II)I!  /*    6*l>6*IY=<6*&**6*l>6*IY=<6*&**6 j &1 4!@"CI$S&V\+c,o-s/0123/57,; p ,BC Ov,sBrCfO   <6*I=>6*3*<=*>=ʄ6*I=>6*3*<=*>=ʄ jJKLMNO/Q3R8T=WFXHNNKT]`^b_d`pacdfij`]o f 9DBOg9bDdBWO  <=*]*O*`E`>*966***6<ń=*]*O*`E`>*966***6<ń jz| }~*-9JMSXZ~`|fp f 0# --!">b# -!>hb "# | =>**=   *  $ %# | =>**=   *  &'(PK m =@k(com/aswinvanwoudenberg/binary/Rules.javapackage com.aswinvanwoudenberg.binary; /** * This class implements the rules of the game Binary. * @author Aswin F. van Woudenberg */ public class Rules { private Rules() { } // No instantiation of this class /** * This method checks if there are just as many zeros and ones in every * row and every column. * @param board The binary puzzle board * @return A boolean value indicating if zero's and one's are equally distributed. */ public static boolean equalDistribution(Board board) { int countOnes; int countZeros; int maxCountOfEachNumber; boolean result; result = true; // Count each row maxCountOfEachNumber = board.getColCount() / 2; for (int i=0; i maxCountOfEachNumber || countOnes > maxCountOfEachNumber) { result = false; } } // Count each column maxCountOfEachNumber = board.getRowCount() / 2; for (int i=0; i maxCountOfEachNumber || countOnes > maxCountOfEachNumber) { result = false; } } return result; } /** * This method checks if there are no more than two of the same number * next to or under each other. * @param board The binary puzzle board * @return A boolean indicating whether this board situation complies to the * described rule. */ public static boolean noMoreThanTwoOfTheSameNumbers(Board board) { boolean result; boolean alreadyCounted; int currentlyCounting; result = true; for (int i=0; ii+1) { for (int j=i+1; ji+1) { for (int j=i+1; j ?@ ?A ?B CD =E CF G H CI CJ CKLMinstanceLjava/util/Random;()VCodeLineNumberTableLocalVariableTablethis&Lcom/aswinvanwoudenberg/binary/Solver; getRandom()Ljava/util/Random;solve8(Lcom/aswinvanwoudenberg/binary/Board;I)Ljava/util/List;board%Lcom/aswinvanwoudenberg/binary/Board; minSolutionsIresultLjava/util/List;LocalVariableTypeTable7Ljava/util/List; Signature_(Lcom/aswinvanwoudenberg/binary/Board;I)Ljava/util/List; findSolutions;(Lcom/aswinvanwoudenberg/binary/Board;Ljava/util/List;III)VaCopyv solutionsrcb(Lcom/aswinvanwoudenberg/binary/Board;Ljava/util/List;III)V SourceFile Solver.java  java/util/Randomjava/util/ArrayList -.N OPQ RS TS USV WP XY Z[  \] ^_ `a bP$com/aswinvanwoudenberg/binary/Solverjava/lang/Objectjava/util/Listsize()I#com/aswinvanwoudenberg/binary/RulesequalDistribution((Lcom/aswinvanwoudenberg/binary/Board;)ZnoMoreThanTwoOfTheSameNumbersnoEqualRowsOrColumns#com/aswinvanwoudenberg/binary/Board getRowCountadd(Ljava/lang/Object;)Z getSquareAt(II)InextInt(I)Icopy'()Lcom/aswinvanwoudenberg/binary/Board; setSquareAt(III)V getColCount! /*   4 Y !"jYM*,, "$ #$%& '()  '*+, -.+* * * * +* W*H6=*:*d+`+`ħl6=*:*d+`+`ç&*d*+`*+`b12%5-688B9L:T;Z<c=m>|@:DEFGHJDOPRX\ Z./$O?0&./$@0&#$1(2&3&%&) 1*+4567PK m =   )com/aswinvanwoudenberg/binary/Solver.javapackage com.aswinvanwoudenberg.binary; import java.util.List; import java.util.ArrayList; import java.util.Random; /** * This class implements a simple backtracking algorithm that solves * the Binary puzzle. * @author Aswin F. van Woudenberg */ public class Solver { private static Random instance = null; private Solver() { } // Exists only to defeat instantiation. private static Random getRandom() { if(instance == null) { instance = new Random(); } return instance; } /** * This method returns a list of solutions to a given binary puzzle * @param board * @param minSolutions * @return */ public static List solve(Board board, int minSolutions) { List result = new ArrayList(); findSolutions(board,result,0,0,minSolutions); return result; } /** * This method recursively searches for solutions to the given binary puzzle. * @param board The given binary puzzle * @param solutions A list with the found solutions * @param r The current row * @param c The current column * @param minSolutions The number of solutions to be found, 0 is unlimited. */ private static void findSolutions(Board board, List solutions, int r, int c, int minSolutions) { if (minSolutions == 0 || minSolutions > solutions.size()) { if (Rules.equalDistribution(board) && Rules.noMoreThanTwoOfTheSameNumbers(board) && Rules.noEqualRowsOrColumns(board)) { if (r == board.getRowCount()) { solutions.add(board); } else { if (board.getSquareAt(r, c) == -1) { if (getRandom().nextInt(2) == 0) { for (int v=1; v>=0; v--) { Board aCopy = board.copy(); aCopy.setSquareAt(r, c, v); if (c < board.getColCount()-1) { findSolutions(aCopy,solutions,r,c+1, minSolutions); } else { findSolutions(aCopy,solutions,r+1,0, minSolutions); } } } else { for (int v=0; v<2; v++) { Board aCopy = board.copy(); aCopy.setSquareAt(r, c, v); if (c < board.getColCount()-1) { findSolutions(aCopy,solutions,r,c+1, minSolutions); } else { findSolutions(aCopy,solutions,r+1,0, minSolutions); } } } } else { if (c < board.getColCount()-1) { findSolutions(board,solutions,r,c+1, minSolutions); } else { findSolutions(board,solutions,r+1,0, minSolutions); } } } } } } } PK ] = AMETA-INF/PK \ = ++META-INF/MANIFEST.MFPK m =A?com/PK m =Aacom/aswinvanwoudenberg/PK m =Acom/aswinvanwoudenberg/binary/PK m =1eAAzz*com/aswinvanwoudenberg/binary/Binary.classPK m =Eڈ) com/aswinvanwoudenberg/binary/Binary.javaPK m =M)ccom/aswinvanwoudenberg/binary/Board.classPK m =(com/aswinvanwoudenberg/binary/Board.javaPK m =Gk k -!com/aswinvanwoudenberg/binary/Generator.classPK m =fb1  ,z+com/aswinvanwoudenberg/binary/Generator.javaPK m =uE[ [ )7com/aswinvanwoudenberg/binary/Rules.classPK m =@k(gAcom/aswinvanwoudenberg/binary/Rules.javaPK m =@*Zcom/aswinvanwoudenberg/binary/Solver.classPK m =   )ccom/aswinvanwoudenberg/binary/Solver.javaPKEq