From 5fa497a19a5356189a2b070f08bf948cd51920bd Mon Sep 17 00:00:00 2001 From: LaVloZ Date: Fri, 20 Dec 2013 02:27:15 +0100 Subject: [PATCH] Adding sliding puzzle, later i will fix flip function arguments. --- .gitignore | 2 + Puzzles.uml | 2 + Puzzles_.umlcd | 4 + Puzzles_com.github.puzzles.core.umlcd | 4 + Puzzles_com.github.puzzles.test.umlcd | 4 + Puzzles_com.github.puzzles.util.umlcd | 4 + build.xml | 73 + nbproject/build-impl.xml | 1390 ++++++++++++++++++++ nbproject/genfiles.properties | 8 + nbproject/project.properties | 77 ++ nbproject/project.xml | 13 + src/com/github/puzzles/core/AbstractPuzzle.java | 246 ++-- .../puzzles/core/AbstractRectangularPuzzle.java | 191 +-- src/com/github/puzzles/core/FlipPuzzle.java | 117 -- src/com/github/puzzles/core/FlipingPuzzle.java | 122 ++ .../core/PersonalisedDifficultyException.java | 5 + ...RectangularPuzzleIndexOutOfBoundsException.java | 15 + src/com/github/puzzles/core/SlidingPuzzle.java | 116 +- src/com/github/puzzles/test/FlipPuzzleTest.java | 19 +- src/com/github/puzzles/test/Main.java | 16 +- src/com/github/puzzles/test/MatricesTest.java | 2 +- src/com/github/puzzles/util/Matrices.java | 109 +- 22 files changed, 2168 insertions(+), 371 deletions(-) create mode 100644 Puzzles.uml create mode 100644 Puzzles_.umlcd create mode 100644 Puzzles_com.github.puzzles.core.umlcd create mode 100644 Puzzles_com.github.puzzles.test.umlcd create mode 100644 Puzzles_com.github.puzzles.util.umlcd create mode 100644 build.xml create mode 100644 nbproject/build-impl.xml create mode 100644 nbproject/genfiles.properties create mode 100644 nbproject/project.properties create mode 100644 nbproject/project.xml rewrite src/com/github/puzzles/core/AbstractPuzzle.java (92%) rewrite src/com/github/puzzles/core/AbstractRectangularPuzzle.java (85%) delete mode 100644 src/com/github/puzzles/core/FlipPuzzle.java create mode 100644 src/com/github/puzzles/core/FlipingPuzzle.java create mode 100644 src/com/github/puzzles/core/PersonalisedDifficultyException.java create mode 100644 src/com/github/puzzles/core/RectangularPuzzleIndexOutOfBoundsException.java rewrite src/com/github/puzzles/util/Matrices.java (91%) diff --git a/.gitignore b/.gitignore index 4d6556c..15684fc 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ *.war *.ear /bin +/nbproject/private/ +/build/ \ No newline at end of file diff --git a/Puzzles.uml b/Puzzles.uml new file mode 100644 index 0000000..8e5b03a --- /dev/null +++ b/Puzzles.uml @@ -0,0 +1,2 @@ + + diff --git a/Puzzles_.umlcd b/Puzzles_.umlcd new file mode 100644 index 0000000..39988c7 --- /dev/null +++ b/Puzzles_.umlcd @@ -0,0 +1,4 @@ + + + + diff --git a/Puzzles_com.github.puzzles.core.umlcd b/Puzzles_com.github.puzzles.core.umlcd new file mode 100644 index 0000000..68f64d7 --- /dev/null +++ b/Puzzles_com.github.puzzles.core.umlcd @@ -0,0 +1,4 @@ + + + + diff --git a/Puzzles_com.github.puzzles.test.umlcd b/Puzzles_com.github.puzzles.test.umlcd new file mode 100644 index 0000000..f4dd913 --- /dev/null +++ b/Puzzles_com.github.puzzles.test.umlcd @@ -0,0 +1,4 @@ + + + + diff --git a/Puzzles_com.github.puzzles.util.umlcd b/Puzzles_com.github.puzzles.util.umlcd new file mode 100644 index 0000000..e2b0b8d --- /dev/null +++ b/Puzzles_com.github.puzzles.util.umlcd @@ -0,0 +1,4 @@ + + + + diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..d0ef6e9 --- /dev/null +++ b/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project Puzzles. + + + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..a42d5f6 --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1390 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..1c1c44c --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=6e70980a +build.xml.script.CRC32=bfe30667 +build.xml.stylesheet.CRC32=8064a381@1.68.1.46 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=6e70980a +nbproject/build-impl.xml.script.CRC32=888a5719 +nbproject/build-impl.xml.stylesheet.CRC32=5a01deb7@1.68.1.46 diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..e81f0d9 --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,77 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +auxiliary.org-netbeans-modules-projectimport-eclipse-core.key=src=src;output=bin; +auxiliary.org-netbeans-modules-projectimport-eclipse-core.project=. +auxiliary.org-netbeans-modules-projectimport-eclipse-core.timestamp=1384618043000 +auxiliary.org-netbeans-modules-projectimport-eclipse-core.workspace=.. +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/Puzzles.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +file.reference.Puzzles-src=src +includes=** +jar.compress=false +javac.classpath=\ + ${libs.junit_4.classpath} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=com.github.puzzles.test.Main +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=true +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=${file.reference.Puzzles-src} diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..c04edec --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,13 @@ + + + org.netbeans.modules.java.j2seproject + + + Puzzles + + + + + + + diff --git a/src/com/github/puzzles/core/AbstractPuzzle.java b/src/com/github/puzzles/core/AbstractPuzzle.java dissimilarity index 92% index 5fc4279..a639627 100644 --- a/src/com/github/puzzles/core/AbstractPuzzle.java +++ b/src/com/github/puzzles/core/AbstractPuzzle.java @@ -1,108 +1,138 @@ -/** - * All puzzles must inherit this class. - */ - -package com.github.puzzles.core; - -public abstract class AbstractPuzzle { - private int size; - private int counter; - private Difficulty difficulty; - - protected AbstractPuzzle(int size, int count, Difficulty difficulty){ - this.size = size; - this.counter = count; - this.difficulty = difficulty; - } - - /** - * Check to see if the puzzle is correct or not. - * @return true if the puzzle is correct, otherwise false. - */ - public abstract boolean check(); - - /** - * Increment the counter of played times. - * @return the counter. - */ - protected int incrementCounter(){ - setCounter(++counter); - return counter; - } - - /** - * Decrement the counter of played times. - * @return the counter. - */ - protected int decrementCounter(){ - setCounter(--counter); - return counter; - } - - /** - * Set the counter value. - * @param counter - */ - protected void setCounter(int counter){ - this.counter = counter; - } - - /** - * Getter. - * @return the size of the puzzle. - */ - public final int getSize() { - return size; - } - - /** - * Getter. - * @return the number of the played times. - */ - public final int getCounter() { - return counter; - } - - /** - * Getter. - * @return the difficulty of the puzzle. - */ - public final Difficulty getDifficulty() { - return difficulty; - } - - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + counter; - result = prime * result - + ((difficulty == null) ? 0 : difficulty.hashCode()); - result = prime * result + size; - return result; - } - - /* (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - AbstractPuzzle other = (AbstractPuzzle) obj; - if (counter != other.counter) - return false; - if (difficulty != other.difficulty) - return false; - if (size != other.size) - return false; - return true; - } -} +/** + * All puzzles must inherit this class. + */ +package com.github.puzzles.core; + +public abstract class AbstractPuzzle { + + private int size; + private int counter; + private Difficulty difficulty; + + protected AbstractPuzzle(int size, int count, Difficulty difficulty) { + this.size = size; + this.counter = count; + this.difficulty = difficulty; + } + + protected AbstractPuzzle(AbstractPuzzle puzzle){ + this.size = puzzle.size; + this.counter = puzzle.counter; + this.difficulty = puzzle.difficulty; + } + + /** + * Check to see if the puzzle is correct or not. + * + * @return true if the puzzle is correct, otherwise false. + */ + public abstract boolean check(); + + /** + * Increment the counter of played times. + * + * @return the counter. + */ + protected int incrementCounter() { + return --counter; + } + + /** + * Decrement the counter of played times. + * + * @return the counter. + */ + protected int decrementCounter() { + return --counter; + } + + /** + * Set the counter value. + * + * @param counter + */ + protected void setCounter(int counter) { + this.counter = counter; + } + + protected void reintialiseCounter() { + this.counter = 0; + } + + /** + * Getter. + * + * @return the size of the puzzle. + */ + public final int getSize() { + return size; + } + + /** + * Getter. + * + * @return the number of the played times. + */ + public final int getCounter() { + return counter; + } + + /** + * Getter. + * + * @return the difficulty of the puzzle. + */ + public final Difficulty getDifficulty() { + return difficulty; + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + counter; + result = prime * result + + ((difficulty == null) ? 0 : difficulty.hashCode()); + result = prime * result + size; + return result; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "AbstractPuzzle [size=" + size + ", counter=" + counter + + ", difficulty=" + difficulty + "]"; + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + AbstractPuzzle other = (AbstractPuzzle) obj; + if (counter != other.counter) { + return false; + } + if (difficulty != other.difficulty) { + return false; + } + if (size != other.size) { + return false; + } + return true; + } +} diff --git a/src/com/github/puzzles/core/AbstractRectangularPuzzle.java b/src/com/github/puzzles/core/AbstractRectangularPuzzle.java dissimilarity index 85% index ea455c0..181e1d0 100644 --- a/src/com/github/puzzles/core/AbstractRectangularPuzzle.java +++ b/src/com/github/puzzles/core/AbstractRectangularPuzzle.java @@ -1,84 +1,107 @@ -/** - * Rectangular puzzles should inherit this class. - */ - -package com.github.puzzles.core; - -import java.util.Arrays; - -import com.github.puzzles.util.Matrices; - -public abstract class AbstractRectangularPuzzle extends AbstractPuzzle { - - private int width; - private int height; - T puzzle[][]; - - protected AbstractRectangularPuzzle(T[][] puzzle, int width, int height, Difficulty difficulty) { - super(width * height, 0, difficulty); - this.width = width; - this.height = height; - this.puzzle = puzzle; - } - - /** - * Getter. - * @return the width of the puzzle. - */ - public final int getWidth() { - return width; - } - - /** - * Getter. - * @return the height of the puzzle. - */ - public final int getHeight() { - return height; - } - - /** - * Getter. - * @return the puzzle matrix. - */ - public final T[][] getPuzzle() { - return Matrices.copyOf(puzzle); - //return puzzle; - } - - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - final int prime = 31; - int result = super.hashCode(); - result = prime * result + height; - result = prime * result + Arrays.hashCode(puzzle); - result = prime * result + width; - return result; - } - - /* (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; - AbstractRectangularPuzzle other = (AbstractRectangularPuzzle) obj; - if (height != other.height) - return false; - if (!Arrays.deepEquals(puzzle, other.puzzle)) - return false; - if (width != other.width) - return false; - return true; - } - - -} +/** + * Rectangular puzzles should inherit this class. + */ +package com.github.puzzles.core; + +import java.util.Arrays; + +import com.github.puzzles.util.Matrices; +import java.util.Random; + +public abstract class AbstractRectangularPuzzle extends AbstractPuzzle { + + private int width; + private int height; + T puzzle[][]; + + protected AbstractRectangularPuzzle(T[][] puzzle, int width, int height, Difficulty difficulty) { + super(width * height, 0, difficulty); + this.width = width; + this.height = height; + this.puzzle = puzzle; + } + + @SuppressWarnings("unchecked") + protected AbstractRectangularPuzzle(AbstractRectangularPuzzle rectangularPuzzle){ + super(rectangularPuzzle); + this.width = rectangularPuzzle.width; + this.height = rectangularPuzzle.height; + this.puzzle = (T[][])Matrices.copyOf(rectangularPuzzle.puzzle); + } + /** + * Getter. + * + * @return the width of the puzzle. + */ + public final int getWidth() { + return width; + } + + /** + * Getter. + * + * @return the height of the puzzle. + */ + public final int getHeight() { + return height; + } + + /** + * Getter. + * + * @return the puzzle matrix. + */ + public final T[][] getPuzzle() { + return Matrices.copyOf(puzzle); + //return puzzle; + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + height; + result = prime * result + Arrays.hashCode(puzzle); + result = prime * result + width; + return result; + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + AbstractRectangularPuzzle other = (AbstractRectangularPuzzle) obj; + if (height != other.height) { + return false; + } + if (!Arrays.deepEquals(puzzle, other.puzzle)) { + return false; + } + if (width != other.width) { + return false; + } + return true; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "AbstractRectangularPuzzle [width=" + width + ", height=" + + height + ", puzzle=" + Arrays.toString(puzzle) + "]"; + } +} diff --git a/src/com/github/puzzles/core/FlipPuzzle.java b/src/com/github/puzzles/core/FlipPuzzle.java deleted file mode 100644 index 2668317..0000000 --- a/src/com/github/puzzles/core/FlipPuzzle.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.github.puzzles.core; - -import java.awt.Point; -import java.util.Arrays; -import java.util.Random; - -import com.github.puzzles.util.Matrices; - -public class FlipPuzzle extends AbstractRectangularPuzzle implements Flipable { - - Boolean correctPuzzle[][]; - - public FlipPuzzle(Boolean[][] puzzle){ - this(puzzle, Difficulty.PERSONALISED); - } - - public FlipPuzzle(Boolean [][] puzzle, Difficulty difficulty){ - super(puzzle, GetWidth(puzzle), GetHeight(puzzle), difficulty); - - this.correctPuzzle = new Boolean[getWidth()][getHeight()]; - Matrices.fill(correctPuzzle, new Boolean(true)); - } - - public FlipPuzzle(int width, int height, Difficulty difficulty) { - this(makeRandomPuzzleHelper(width, height), difficulty); - } - - private static int GetWidth(Boolean [][] puzzle){ - if(puzzle != null) - return puzzle.length; - return 0; - } - - private static int GetHeight(Boolean [][] puzzle){ - if((puzzle != null) && (puzzle[0] != null)) - return puzzle[0].length; - return 0; - } - - private Boolean toggle(Boolean bool){ - return !bool; - } - - private static Boolean[][] makeRandomPuzzleHelper(int width, int height){ - Boolean booleansMatrice[][] = new Boolean[width][height]; - Random randoms = new Random(); - - for (int i = 0; i < width; i++) - for (int j = 0; j < height; j++) - booleansMatrice[i][j] = randoms.nextBoolean(); - - return booleansMatrice; - } - - @Override - public boolean check() { - if(puzzle.equals(correctPuzzle)) - return true; - return false; - } - - @Override - public void flip(Point index) { - //int x = new Double(index.getX()).intValue(); - //int y = new Double(index.getY()).intValue(); - int x = (int)index.getX(); - int y = (int)index.getY(); - - try { - puzzle[y][x] = toggle(puzzle[y][x]); - } catch (ArrayIndexOutOfBoundsException e) {} - - try { - puzzle[y][x + 1] = toggle(puzzle[y][x]); - } catch (ArrayIndexOutOfBoundsException e) {} - - try { - puzzle[y][x - 1] = toggle(puzzle[y][x]); - } catch (ArrayIndexOutOfBoundsException e) {} - - try { - puzzle[y + 1][x] = toggle(puzzle[y][x]); - } catch (ArrayIndexOutOfBoundsException e) {} - - try { - puzzle[y - 1][x] = toggle(puzzle[y][x]); - } catch (ArrayIndexOutOfBoundsException e) {} - } - - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - final int prime = 31; - int result = super.hashCode(); - result = prime * result + Arrays.hashCode(correctPuzzle); - return result; - } - - /* (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; - FlipPuzzle other = (FlipPuzzle) obj; - if (!Arrays.deepEquals(correctPuzzle, other.correctPuzzle)) - return false; - return true; - } -} diff --git a/src/com/github/puzzles/core/FlipingPuzzle.java b/src/com/github/puzzles/core/FlipingPuzzle.java new file mode 100644 index 0000000..41f58c1 --- /dev/null +++ b/src/com/github/puzzles/core/FlipingPuzzle.java @@ -0,0 +1,122 @@ +package com.github.puzzles.core; + +import java.awt.Point; +import java.util.Arrays; +import java.util.Random; +import com.github.puzzles.util.Matrices; + +public class FlipingPuzzle extends AbstractRectangularPuzzle implements Flipable { + + protected FlipingPuzzle(Boolean[][] puzzle) { + super(puzzle, Matrices.getWidth(puzzle), Matrices.getHeight(puzzle), Difficulty.PERSONALISED); + } + + public FlipingPuzzle(int width, int height) { + //this(makePuzzleHelper(width, height)); + super(makePuzzleHelper(width, height), width, height, Difficulty.PERSONALISED); + } + + public FlipingPuzzle(Difficulty difficulty) { + this(makePuzzleFromDifficultyHelper(difficulty)); + } + + public FlipingPuzzle(FlipingPuzzle puzzle){ + super(puzzle); + } + + private Boolean toggle(Boolean bool) { + return !bool; + } + + private static Boolean[][] makePuzzleHelper(int width, int height) { + Boolean booleanMatrix[][] = new Boolean[width][height]; + for (int i = 0; i < booleanMatrix.length; i++) { + for (int j = 0; j < booleanMatrix[i].length; j++) { + booleanMatrix[i][j] = false; + } + } + + return booleanMatrix; + } + + private static Boolean[][] makePuzzleFromDifficultyHelper(Difficulty difficulty) throws PersonalisedDifficultyException { + if (difficulty == Difficulty.VERY_EASY) { + return makePuzzleHelper(3, 3); + } else if (difficulty == Difficulty.EASY) { + return makePuzzleHelper(5, 5); + } else if (difficulty == Difficulty.MEDUIM) { + return makePuzzleHelper(7, 7); + } else if (difficulty == Difficulty.HARD) { + return makePuzzleHelper(9, 9); + } else if (difficulty == Difficulty.VERY_HARD) { + return makePuzzleHelper(11, 11); + } + + throw new PersonalisedDifficultyException(); + } + + private static Boolean[][] makeRandomPuzzleHelper(int width, int height) { + Boolean booleansMatrix[][] = new Boolean[width][height]; + Random randoms = new Random(); + + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + booleansMatrix[i][j] = randoms.nextBoolean(); + } + } + + return booleansMatrix; + } + + private static Boolean[][] makeRandomPuzzleFromDifficultyHelper(Difficulty difficulty) { + return new Boolean[0][]; + } + + @Override + public boolean check() { + for (Boolean[] line : puzzle) { + for (Boolean bool : line) { + if (bool.equals(false)) { + return false; + } + } + } + return true; + } + + @Override + public void flip(Point index) { + flip((int) index.getX(), (int) index.getY()); + } + + public void flip(int x, int y) { + try { + puzzle[y][x] = toggle(puzzle[y][x]); + } catch (RectangularPuzzleIndexOutOfBoundsException ex) { + throw ex; + } + + try { + puzzle[y][x + 1] = toggle(puzzle[y][x + 1]); + } catch (RectangularPuzzleIndexOutOfBoundsException ex) {} + + try { + puzzle[y][x - 1] = toggle(puzzle[y][x - 1]); + } catch (RectangularPuzzleIndexOutOfBoundsException ex) {} + + try { + puzzle[y + 1][x] = toggle(puzzle[y + 1][x]); + } catch (RectangularPuzzleIndexOutOfBoundsException ex) {} + + try { + puzzle[y - 1][x] = toggle(puzzle[y - 1][x]); + } catch (RectangularPuzzleIndexOutOfBoundsException ex) {} + + incrementCounter(); + } + + @Override + public String toString() { + return "FlipPuzzle [puzzle=" + Arrays.toString(puzzle) + "]"; + } +} diff --git a/src/com/github/puzzles/core/PersonalisedDifficultyException.java b/src/com/github/puzzles/core/PersonalisedDifficultyException.java new file mode 100644 index 0000000..3347972 --- /dev/null +++ b/src/com/github/puzzles/core/PersonalisedDifficultyException.java @@ -0,0 +1,5 @@ +package com.github.puzzles.core; + +public class PersonalisedDifficultyException extends RuntimeException { + +} diff --git a/src/com/github/puzzles/core/RectangularPuzzleIndexOutOfBoundsException.java b/src/com/github/puzzles/core/RectangularPuzzleIndexOutOfBoundsException.java new file mode 100644 index 0000000..966e6ee --- /dev/null +++ b/src/com/github/puzzles/core/RectangularPuzzleIndexOutOfBoundsException.java @@ -0,0 +1,15 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package com.github.puzzles.core; + +/** + * + * @author root + */ +public class RectangularPuzzleIndexOutOfBoundsException extends RuntimeException{ + +} diff --git a/src/com/github/puzzles/core/SlidingPuzzle.java b/src/com/github/puzzles/core/SlidingPuzzle.java index 55b6ef9..f5c73a5 100644 --- a/src/com/github/puzzles/core/SlidingPuzzle.java +++ b/src/com/github/puzzles/core/SlidingPuzzle.java @@ -1,12 +1,114 @@ package com.github.puzzles.core; -public class SlidingPuzzle extends AbstractRectangularPuzzle implements - Slidable { +import java.awt.Point; +import java.util.Random; +import com.github.puzzles.util.Matrices; - @Override - public boolean check() { - // TODO Auto-generated method stub - return false; - } +public class SlidingPuzzle extends AbstractRectangularPuzzle implements Slidable { + protected SlidingPuzzle(Integer[][] puzzle) { + super(puzzle, Matrices.getWidth(puzzle), Matrices.getHeight(puzzle), Difficulty.PERSONALISED); + } + + public SlidingPuzzle(int width, int height) { + //this(makePuzzleHelper(width, height)); + super(makePuzzleHelper(width, height), width, height, Difficulty.PERSONALISED); + } + + public SlidingPuzzle(Difficulty difficulty) { + this(makePuzzleRandomFromDifficultyHelper(difficulty)); + } + + public SlidingPuzzle(SlidingPuzzle puzzle){ + super(puzzle); + } + + private static Integer[][] makePuzzleHelper(int width, int height){ + Integer[][] integersMatrix = new Integer[width][height]; + for(int i = 0, k = 0; i < width; i++) + for(int j = 0; j < height; j++, k++) + integersMatrix[i][j] = k; + + return integersMatrix; + } + + private static Integer[][] makeRandomPuzzleHelper(int width, int height) { + Integer[][] integersMatrix = makePuzzleHelper(width, height); + Random randoms = new Random(); + + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + Matrices.swap(integersMatrix, randoms.nextInt() % width, randoms.nextInt() % width, randoms.nextInt() % height, randoms.nextInt() % height); + } + } + + return integersMatrix; + } + + private static Integer[][] makePuzzleRandomFromDifficultyHelper(Difficulty difficulty) throws PersonalisedDifficultyException { + if (difficulty == Difficulty.VERY_EASY) { + return makeRandomPuzzleHelper(3, 2); + } else if (difficulty == Difficulty.EASY) { + return makeRandomPuzzleHelper(5, 3); + } else if (difficulty == Difficulty.MEDUIM) { + return makeRandomPuzzleHelper(5, 5); + } else if (difficulty == Difficulty.HARD) { + return makeRandomPuzzleHelper(7, 7); + } else if (difficulty == Difficulty.VERY_HARD) { + return makeRandomPuzzleHelper(11, 11); + } + + throw new PersonalisedDifficultyException(); + } + + @Override + public boolean check() { + int i = 1; + for(Integer[] line : puzzle) + for(Integer element : line) + if(element != i) + return false; + + return true; + } + + @Override + public void slid(Point index) { + slid((int) index.getX(), (int) index.getY()); + } + + public void slid(int x, int y) { + if(puzzle[x][y] == 0) + throw new RectangularPuzzleIndexOutOfBoundsException(); + + try { + if(puzzle[x - 1][y] == 0){ + Matrices.swap(puzzle, x, y, x - 1, y); + return; + } + }catch(ArrayIndexOutOfBoundsException ex){} + + try { + if(puzzle[x + 1][y] == 0){ + Matrices.swap(puzzle, x, y, x + 1, y); + return; + } + }catch(ArrayIndexOutOfBoundsException ex){} + + try { + if(puzzle[x][y - 1] == 0){ + Matrices.swap(puzzle, x, y, x, y - 1); + return; + } + }catch(ArrayIndexOutOfBoundsException ex){} + + try { + if(puzzle[x][y + 1] == 0){ + Matrices.swap(puzzle, x, y, x, y + 1); + return; + } + }catch(ArrayIndexOutOfBoundsException ex){} + + incrementCounter(); + } } diff --git a/src/com/github/puzzles/test/FlipPuzzleTest.java b/src/com/github/puzzles/test/FlipPuzzleTest.java index 841294b..99b06cd 100644 --- a/src/com/github/puzzles/test/FlipPuzzleTest.java +++ b/src/com/github/puzzles/test/FlipPuzzleTest.java @@ -1,25 +1,36 @@ package com.github.puzzles.test; -import static org.junit.Assert.*; +import static org.junit.Assert.fail; import org.junit.Test; import com.github.puzzles.core.Difficulty; -import com.github.puzzles.core.FlipPuzzle; +import com.github.puzzles.core.FlipingPuzzle; +import com.github.puzzles.core.PersonalisedDifficultyException; public class FlipPuzzleTest { @Test public void testCheck() { - new FlipPuzzle(5, 5, Difficulty.EASY).check(); + fail("Not yet implemented"); } @Test - public void testFlipPuzzle() { + public void testFlipPuzzleBooleanArrayArray() { fail("Not yet implemented"); } @Test + public void testFlipPuzzleIntInt() { + fail("Not yet implemented"); + } + + @Test(expected = PersonalisedDifficultyException.class) + public void testFlipPuzzleDifficulty() { + new FlipingPuzzle(Difficulty.PERSONALISED); + } + + @Test public void testFlip() { fail("Not yet implemented"); } diff --git a/src/com/github/puzzles/test/Main.java b/src/com/github/puzzles/test/Main.java index 66a1ab9..9ee7d2e 100644 --- a/src/com/github/puzzles/test/Main.java +++ b/src/com/github/puzzles/test/Main.java @@ -3,21 +3,23 @@ package com.github.puzzles.test; import java.awt.Point; import com.github.puzzles.core.Difficulty; -import com.github.puzzles.core.FlipPuzzle; +import com.github.puzzles.core.*; public class Main { public static void main(String[] args) { - FlipPuzzle sp = new FlipPuzzle(5, 5, Difficulty.EASY); - printPuzzle(sp); - sp.flip(new Point(0, 0)); - System.out.println(); + SlidingPuzzle sp = new SlidingPuzzle(5, 5); + //printPuzzle(sp); + sp.slid(new Point(0, 1)); + sp.slid(new Point(1, 1)); + sp.slid(new Point(1, 1)); + //System.out.println(); printPuzzle(sp); } - public static void printPuzzle(FlipPuzzle puzzle){ + public static void printPuzzle(AbstractRectangularPuzzle puzzle){ - Boolean[][] puz = puzzle.getPuzzle(); + Object[][] puz = puzzle.getPuzzle(); for(int i = 0; i < puz.length; i++){ for (int j = 0; j < puz[i].length; j++) { System.out.print(puz[i][j] + " "); diff --git a/src/com/github/puzzles/test/MatricesTest.java b/src/com/github/puzzles/test/MatricesTest.java index bcb8d3d..74faa14 100644 --- a/src/com/github/puzzles/test/MatricesTest.java +++ b/src/com/github/puzzles/test/MatricesTest.java @@ -5,7 +5,7 @@ import static org.junit.Assert.*; import org.junit.Test; import com.github.puzzles.core.Difficulty; -import com.github.puzzles.core.FlipPuzzle; +import com.github.puzzles.core.FlipingPuzzle; import com.github.puzzles.util.Matrices; public class MatricesTest { diff --git a/src/com/github/puzzles/util/Matrices.java b/src/com/github/puzzles/util/Matrices.java dissimilarity index 91% index a58d22d..4ca9569 100644 --- a/src/com/github/puzzles/util/Matrices.java +++ b/src/com/github/puzzles/util/Matrices.java @@ -1,43 +1,66 @@ -package com.github.puzzles.util; - -import java.util.Arrays; - -public class Matrices { - - //Don't let anyone instance this class. - private Matrices(){ - } - - /** - * Copy 2 dimensions array. - * - * @param original the matrix which you want make a copy of it. - * @return the new copy. - */ - public static T[][] copyOf(T[][] original, int colsLength, int rowsLength){ - T[][] returnedPuzzle = Arrays.copyOf(original, colsLength); - for(int i = 0; i < colsLength; i++) - returnedPuzzle[i] = Arrays.copyOf(original[i], rowsLength); - - return returnedPuzzle; - } - - public static T[][] copyOf(T[][] original){ - if (original.length < 0) { - return null; - } - return copyOf(original, original.length, original[0].length); - } - - - /** - * Fill a value in a matrix. - * - * @param matrix the matrix which you want to fill in it. - * @param val the value which you want fill it in the matrix. - */ - public static void fill(T[][] matrix, T val){ - for(int i = 0; i < matrix.length; i++) - Arrays.fill(matrix[i], val); - } -} +package com.github.puzzles.util; + +import java.util.Arrays; + +public class Matrices { + + //Don't let anyone instance this class. + private Matrices() { + } + + /** + * Copy 2 dimensions array. + * + * @param original the matrix which you want make a copy of it. + * @return the new copy. + */ + public static T[][] copyOf(T[][] original, int colsLength, int rowsLength) { + T[][] returnedPuzzle = Arrays.copyOf(original, colsLength); + for (int i = 0; i < colsLength; i++) { + returnedPuzzle[i] = Arrays.copyOf(original[i], rowsLength); + } + + return returnedPuzzle; + } + + public static T[][] copyOf(T[][] original) { + if (original.length < 0) { + return null; + } + return copyOf(original, original.length, original[0].length); + } + + /** + * Fill a value in a matrix. + * + * @param matrix the matrix which you want to fill in it. + * @param val the value which you want fill it in the matrix. + */ + public static void fill(T[][] matrix, T val) { + for (int i = 0; i < matrix.length; i++) { + Arrays.fill(matrix[i], val); + } + } + + public static int getWidth(T[][] matrix) { + if (matrix != null) { + return matrix.length; + } + return 0; + } + + public static int getHeight(T[][] matrix) { + if ((getWidth(matrix) != 0) && (matrix[0] != null)) { + return matrix[0].length; + } + return 0; + } + + //Swap two elements with XOR swap algorithm. + public static void swap(Integer[][] matrix, int fromX, int fromY, int toX, int toY){ + matrix[fromX][fromY] = matrix[fromX][fromY] ^ matrix[toX][toY]; + matrix[toX][toY] = matrix[fromX][fromY] ^ matrix[toX][toY]; + matrix[fromX][fromY] = matrix[fromX][fromY] ^ matrix[toX][toY]; + } + +} -- 2.11.4.GIT