From 8d8b9a0fdda9102d9de1389760fa468039b6b31f Mon Sep 17 00:00:00 2001 From: La VloZ Date: Fri, 24 Jan 2014 23:54:46 +0100 Subject: [PATCH] Making some changes, I added makeCorrectPuzzle method, which i have to overide it in each method :), it make puzzle comparing simple :) --- .classpath | 1 + 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 - src/com/github/puzzles/core/AbstractPuzzle.java | 114 +++++---- .../puzzles/core/AbstractRectangularPuzzle.java | 231 +++++++++-------- src/com/github/puzzles/core/Difficulty.java | 7 +- src/com/github/puzzles/core/Flipable.java | 2 +- src/com/github/puzzles/core/FlippingPuzzle.java | 217 ++++++++-------- src/com/github/puzzles/core/Movable.java | 2 +- .../NoEmptyCaseAdjacentSlidingPuzzleExecption.java | 4 +- ...RectangularPuzzleIndexOutOfBoundsException.java | 7 +- src/com/github/puzzles/core/Slidable.java | 2 +- src/com/github/puzzles/core/SlidingPuzzle.java | 273 +++++++++++---------- src/com/github/puzzles/test/FlipPuzzleTest.java | 38 --- src/com/github/puzzles/test/Main.java | 41 ---- src/com/github/puzzles/util/Matrices.java | 171 +++++++------ test/com/github/puzzles/test/FlipPuzzleTest.java | 44 ++++ test/com/github/puzzles/test/Main.java | 31 +++ test/com/github/puzzles/test/MatricesTest.java | 73 ++++++ .../github/puzzles/test}/SlidingTest.java | 34 +-- test/testing/MatricesTest.java | 36 --- 24 files changed, 715 insertions(+), 631 deletions(-) delete mode 100644 Puzzles.uml delete mode 100644 Puzzles_.umlcd delete mode 100644 Puzzles_com.github.puzzles.core.umlcd delete mode 100644 Puzzles_com.github.puzzles.test.umlcd delete mode 100644 Puzzles_com.github.puzzles.util.umlcd rewrite src/com/github/puzzles/core/AbstractRectangularPuzzle.java (65%) rewrite src/com/github/puzzles/core/FlippingPuzzle.java (77%) rewrite src/com/github/puzzles/core/SlidingPuzzle.java (80%) delete mode 100644 src/com/github/puzzles/test/FlipPuzzleTest.java delete mode 100644 src/com/github/puzzles/test/Main.java rewrite src/com/github/puzzles/util/Matrices.java (68%) create mode 100644 test/com/github/puzzles/test/FlipPuzzleTest.java create mode 100644 test/com/github/puzzles/test/Main.java create mode 100644 test/com/github/puzzles/test/MatricesTest.java rename test/{testing => com/github/puzzles/test}/SlidingTest.java (77%) delete mode 100644 test/testing/MatricesTest.java diff --git a/.classpath b/.classpath index 3e0fb27..e72ef7c 100644 --- a/.classpath +++ b/.classpath @@ -1,6 +1,7 @@ + diff --git a/Puzzles.uml b/Puzzles.uml deleted file mode 100644 index 8e5b03a..0000000 --- a/Puzzles.uml +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/Puzzles_.umlcd b/Puzzles_.umlcd deleted file mode 100644 index 39988c7..0000000 --- a/Puzzles_.umlcd +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/Puzzles_com.github.puzzles.core.umlcd b/Puzzles_com.github.puzzles.core.umlcd deleted file mode 100644 index 68f64d7..0000000 --- a/Puzzles_com.github.puzzles.core.umlcd +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/Puzzles_com.github.puzzles.test.umlcd b/Puzzles_com.github.puzzles.test.umlcd deleted file mode 100644 index f4dd913..0000000 --- a/Puzzles_com.github.puzzles.test.umlcd +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/Puzzles_com.github.puzzles.util.umlcd b/Puzzles_com.github.puzzles.util.umlcd deleted file mode 100644 index e2b0b8d..0000000 --- a/Puzzles_com.github.puzzles.util.umlcd +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/src/com/github/puzzles/core/AbstractPuzzle.java b/src/com/github/puzzles/core/AbstractPuzzle.java index a639627..2ad7359 100644 --- a/src/com/github/puzzles/core/AbstractPuzzle.java +++ b/src/com/github/puzzles/core/AbstractPuzzle.java @@ -10,129 +10,135 @@ public abstract class AbstractPuzzle { private Difficulty difficulty; protected AbstractPuzzle(int size, int count, Difficulty difficulty) { - this.size = size; - this.counter = count; - this.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; + + 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; + return --counter; } /** * Decrement the counter of played times. - * + * * @return the counter. */ protected int decrementCounter() { - return --counter; + return --counter; } /** * Set the counter value. - * + * * @param counter */ protected void setCounter(int counter) { - this.counter = counter; + this.counter = counter; } protected void reintialiseCounter() { - this.counter = 0; + this.counter = 0; } /** * Getter. - * + * * @return the size of the puzzle. */ public final int getSize() { - return size; + return size; } /** * Getter. - * + * * @return the number of the played times. */ public final int getCounter() { - return counter; + return counter; } /** * Getter. - * + * * @return the difficulty of the puzzle. */ public final Difficulty getDifficulty() { - return difficulty; + return difficulty; } - /* (non-Javadoc) + /* + * (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; + 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) + /* + * (non-Javadoc) + * * @see java.lang.Object#toString() */ @Override public String toString() { - return "AbstractPuzzle [size=" + size + ", counter=" + counter - + ", difficulty=" + difficulty + "]"; + return "AbstractPuzzle [size=" + size + ", counter=" + counter + + ", difficulty=" + difficulty + "]"; } - /* (non-Javadoc) + /* + * (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; + 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 65% index 54de352..2ae8ba2 100644 --- a/src/com/github/puzzles/core/AbstractRectangularPuzzle.java +++ b/src/com/github/puzzles/core/AbstractRectangularPuzzle.java @@ -1,107 +1,124 @@ -/** - * 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) + "]"; - } -} +/** + * 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 { + + final protected int width; + final protected int height; + final protected T puzzle[][]; + final protected T correctPuzzle[][]; + + protected AbstractRectangularPuzzle(T[][] puzzle, Difficulty difficulty) { + super(Matrices.getWidth(puzzle) * Matrices.getHeight(puzzle), 0, + difficulty); + this.width = Matrices.getWidth(puzzle); + this.height = Matrices.getHeight(puzzle); + this.puzzle = puzzle; + this.correctPuzzle = makeCorrectPuzzle(); + } + + protected AbstractRectangularPuzzle( + AbstractRectangularPuzzle rectangularPuzzle) { + super(rectangularPuzzle); + this.width = rectangularPuzzle.width; + this.height = rectangularPuzzle.height; + this.puzzle = (T[][]) Matrices.copyOf(rectangularPuzzle.puzzle); + this.correctPuzzle = makeCorrectPuzzle(); + } + + public T[][] getCorrectPuzzle() { + return Matrices.copyOf(correctPuzzle); + } + + /** + * Getter. + * + * @return the width of the puzzle. + */ + public final int getWidth() { + return width; + } + + abstract public T[][] makeCorrectPuzzle(); + + /** + * 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/Difficulty.java b/src/com/github/puzzles/core/Difficulty.java index 3ed5366..849870d 100644 --- a/src/com/github/puzzles/core/Difficulty.java +++ b/src/com/github/puzzles/core/Difficulty.java @@ -1,10 +1,5 @@ package com.github.puzzles.core; public enum Difficulty { - VERY_EASY, - EASY, - MEDUIM, - HARD, - VERY_HARD, - PERSONALISED + VERY_EASY, EASY, MEDUIM, HARD, VERY_HARD, PERSONALISED } diff --git a/src/com/github/puzzles/core/Flipable.java b/src/com/github/puzzles/core/Flipable.java index d99f0e7..6c1b10e 100644 --- a/src/com/github/puzzles/core/Flipable.java +++ b/src/com/github/puzzles/core/Flipable.java @@ -1,5 +1,5 @@ package com.github.puzzles.core; public interface Flipable { - public void flip(T flip); + public void flip(T flip); } diff --git a/src/com/github/puzzles/core/FlippingPuzzle.java b/src/com/github/puzzles/core/FlippingPuzzle.java dissimilarity index 77% index 104fab9..a2d97a6 100644 --- a/src/com/github/puzzles/core/FlippingPuzzle.java +++ b/src/com/github/puzzles/core/FlippingPuzzle.java @@ -1,105 +1,112 @@ -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 FlippingPuzzle extends AbstractRectangularPuzzle implements Flipable { - - protected FlippingPuzzle(Boolean[][] puzzle) { - super(puzzle, Matrices.getWidth(puzzle), Matrices.getHeight(puzzle), Difficulty.PERSONALISED); - } - - public FlippingPuzzle(int width, int height) { - //this(makePuzzleHelper(width, height)); - super(makePuzzleHelper(width, height), width, height, Difficulty.PERSONALISED); - } - - public FlippingPuzzle(Difficulty difficulty) { - this(makePuzzleFromDifficultyHelper(difficulty)); - } - - public FlippingPuzzle(FlippingPuzzle 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 < width; i++) { - for (int j = 0; j < height; 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(); - } - - @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 y, int x) { - 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) + "]"; - } -} +package com.github.puzzles.core; + +import java.awt.Point; +import java.util.Arrays; + +import com.github.puzzles.util.Matrices; + +public class FlippingPuzzle extends AbstractRectangularPuzzle + implements Flipable { + + protected FlippingPuzzle(Boolean[][] puzzle) { + super(puzzle, Difficulty.PERSONALISED); + } + + public FlippingPuzzle(int width, int height) { + // this(makePuzzleHelper(width, height)); + super(makePuzzleHelper(width, height), Difficulty.PERSONALISED); + } + + public FlippingPuzzle(Difficulty difficulty) { + this(makePuzzleFromDifficultyHelper(difficulty)); + } + + public FlippingPuzzle(FlippingPuzzle 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 < width; i++) { + for (int j = 0; j < height; 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(); + } + + @Override + public Boolean[][] makeCorrectPuzzle() { + Boolean[][] correctPuzzle = new Boolean[getWidth()][getHeight()]; + Matrices.fill(correctPuzzle, true); + return correctPuzzle; + } + + @Override + public boolean check() { + return Matrices.equals(getPuzzle(), getCorrectPuzzle()); + } + + @Override + public void flip(Point index) { + flip((int) index.getX(), (int) index.getY()); + } + + public void flip(int y, int x) { + 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/Movable.java b/src/com/github/puzzles/core/Movable.java index 903b70f..75d05b0 100644 --- a/src/com/github/puzzles/core/Movable.java +++ b/src/com/github/puzzles/core/Movable.java @@ -1,5 +1,5 @@ package com.github.puzzles.core; public interface Movable { - public void move(T fromIndex, T toIndex); + public void move(T fromIndex, T toIndex); } diff --git a/src/com/github/puzzles/core/NoEmptyCaseAdjacentSlidingPuzzleExecption.java b/src/com/github/puzzles/core/NoEmptyCaseAdjacentSlidingPuzzleExecption.java index 0b5a966..c1ab876 100644 --- a/src/com/github/puzzles/core/NoEmptyCaseAdjacentSlidingPuzzleExecption.java +++ b/src/com/github/puzzles/core/NoEmptyCaseAdjacentSlidingPuzzleExecption.java @@ -7,9 +7,9 @@ package com.github.puzzles.core; /** - * + * * @author root */ public class NoEmptyCaseAdjacentSlidingPuzzleExecption extends RuntimeException { - + } diff --git a/src/com/github/puzzles/core/RectangularPuzzleIndexOutOfBoundsException.java b/src/com/github/puzzles/core/RectangularPuzzleIndexOutOfBoundsException.java index 966e6ee..98dcb66 100644 --- a/src/com/github/puzzles/core/RectangularPuzzleIndexOutOfBoundsException.java +++ b/src/com/github/puzzles/core/RectangularPuzzleIndexOutOfBoundsException.java @@ -7,9 +7,10 @@ package com.github.puzzles.core; /** - * + * * @author root */ -public class RectangularPuzzleIndexOutOfBoundsException extends RuntimeException{ - +public class RectangularPuzzleIndexOutOfBoundsException extends + RuntimeException { + } diff --git a/src/com/github/puzzles/core/Slidable.java b/src/com/github/puzzles/core/Slidable.java index 63edae5..cbe0e53 100644 --- a/src/com/github/puzzles/core/Slidable.java +++ b/src/com/github/puzzles/core/Slidable.java @@ -1,5 +1,5 @@ package com.github.puzzles.core; public interface Slidable { - public void slid(T index); + public void slid(T index); } diff --git a/src/com/github/puzzles/core/SlidingPuzzle.java b/src/com/github/puzzles/core/SlidingPuzzle.java dissimilarity index 80% index ab8e727..5dfac3a 100644 --- a/src/com/github/puzzles/core/SlidingPuzzle.java +++ b/src/com/github/puzzles/core/SlidingPuzzle.java @@ -1,131 +1,142 @@ -package com.github.puzzles.core; - -import java.awt.Point; -import java.util.Random; -import com.github.puzzles.util.Matrices; - -public class SlidingPuzzle extends AbstractRectangularPuzzle implements Slidable { - - private static final int MAX_SWAP = 512; - - protected SlidingPuzzle(Integer[][] puzzle) { - super(puzzle, Matrices.getWidth(puzzle), Matrices.getHeight(puzzle), Difficulty.PERSONALISED); - } - - public SlidingPuzzle(int width, int height) { - //this(makeCorrectPuzzleHelper(width, height)); - super(makeCorrectPuzzleHelper(width, height), width, height, Difficulty.PERSONALISED); - } - - public SlidingPuzzle(Difficulty difficulty) { - this(makePuzzleFromDifficultyHelper(difficulty)); - } - - public SlidingPuzzle(SlidingPuzzle puzzle) { - super(puzzle); - } - - private static Integer[][] makeCorrectPuzzleHelper(int width, int height) { - Integer[][] integersMatrix = new Integer[width][height]; - for (int i = 0, k = 1; i < width; i++) { - for (int j = 0; j < height; j++, k++) { - integersMatrix[i][j] = k; - } - } - - integersMatrix[width - 1][height - 1] = 0; - - return integersMatrix; - } - - private static Integer[][] makePuzzleHelper(int width, int height) { - Integer[][] integersMatrix = makeCorrectPuzzleHelper(width, height); - Random randoms = new Random(); - - for (int i = 0; i < MAX_SWAP; i++) { - Matrices.swap(integersMatrix, randoms.nextInt(width), randoms.nextInt(width), randoms.nextInt(height), randoms.nextInt(height)); - } - - return integersMatrix; - } - - private static Integer[][] makePuzzleFromDifficultyHelper(Difficulty difficulty) throws PersonalisedDifficultyException { - if (difficulty == Difficulty.VERY_EASY) { - return makePuzzleHelper(3, 2); - } else if (difficulty == Difficulty.EASY) { - return makePuzzleHelper(5, 3); - } else if (difficulty == Difficulty.MEDUIM) { - return makePuzzleHelper(5, 5); - } else if (difficulty == Difficulty.HARD) { - return makePuzzleHelper(7, 7); - } else if (difficulty == Difficulty.VERY_HARD) { - return makePuzzleHelper(11, 11); - } - - throw new PersonalisedDifficultyException(); - } - - @Override - public boolean check() { - int i = 1, total = getWidth() * getHeight(); - for (Integer[] line : puzzle) { - for (Integer element : line) { - if (element != i % total) { - return false; - } - i++; - } - } - - return true; - } - - @Override - public void slid(Point index) { - slid((int) index.getX(), (int) index.getY()); - } - - public void slid(int x, int y) { - if (puzzle[y][x] == 0) { - throw new RectangularPuzzleIndexOutOfBoundsException(); - } - - try { - if (puzzle[y - 1][x] == 0) { - Matrices.swap(puzzle, x, y, x - 1, y); - incrementCounter(); - return; - } - } catch (ArrayIndexOutOfBoundsException ex) { - } - - try { - if (puzzle[y + 1][x] == 0) { - Matrices.swap(puzzle, x, y, x + 1, y); - incrementCounter(); - return; - } - } catch (ArrayIndexOutOfBoundsException ex) { - } - - try { - if (puzzle[y][x - 1] == 0) { - Matrices.swap(puzzle, x, y, x, y - 1); - incrementCounter(); - return; - } - } catch (ArrayIndexOutOfBoundsException ex) { - } - - try { - if (puzzle[y][x + 1] == 0) { - Matrices.swap(puzzle, x, y, x, y + 1); - incrementCounter(); - return; - } - } catch (ArrayIndexOutOfBoundsException ex) { - } - - throw new NoEmptyCaseAdjacentSlidingPuzzleExecption(); - } -} +package com.github.puzzles.core; + +import java.awt.Point; +import java.util.Random; + +import com.github.puzzles.util.Matrices; + +public class SlidingPuzzle extends AbstractRectangularPuzzle implements + Slidable { + + private static final int MAX_SWAP = 512; + + protected SlidingPuzzle(Integer[][] puzzle) { + super(puzzle, Difficulty.PERSONALISED); + } + + public SlidingPuzzle(int width, int height) { + // this(makeCorrectPuzzleHelper(width, height)); + super(makeCorrectPuzzleHelper(width, height), Difficulty.PERSONALISED); + } + + public SlidingPuzzle(Difficulty difficulty) { + this(makePuzzleFromDifficultyHelper(difficulty)); + } + + public SlidingPuzzle(SlidingPuzzle puzzle) { + super(puzzle); + } + + private static Integer[][] makeCorrectPuzzleHelper(int width, int height) { + Integer[][] integersMatrix = new Integer[width][height]; + for (int i = 0, k = 1; i < width; i++) { + for (int j = 0; j < height; j++, k++) { + integersMatrix[i][j] = k; + } + } + + integersMatrix[width - 1][height - 1] = 0; + + return integersMatrix; + } + + private static Integer[][] makePuzzleHelper(int width, int height) { + Integer[][] integersMatrix = makeCorrectPuzzleHelper(width, height); + Random randoms = new Random(); + + for (int i = 0; i < MAX_SWAP; i++) { + Matrices.swap(integersMatrix, randoms.nextInt(width), + randoms.nextInt(width), randoms.nextInt(height), + randoms.nextInt(height)); + } + + return integersMatrix; + } + + private static Integer[][] makePuzzleFromDifficultyHelper( + Difficulty difficulty) throws PersonalisedDifficultyException { + if (difficulty == Difficulty.VERY_EASY) { + return makePuzzleHelper(3, 2); + } else if (difficulty == Difficulty.EASY) { + return makePuzzleHelper(5, 3); + } else if (difficulty == Difficulty.MEDUIM) { + return makePuzzleHelper(5, 5); + } else if (difficulty == Difficulty.HARD) { + return makePuzzleHelper(7, 7); + } else if (difficulty == Difficulty.VERY_HARD) { + return makePuzzleHelper(11, 11); + } + + throw new PersonalisedDifficultyException(); + } + + @Override + public Integer[][] makeCorrectPuzzle() { + int width = getWidth(); + int height = getHeight(); + Integer[][] correctPuzzle = new Integer[width][height]; + + for (int i = 0; i < correctPuzzle.length; i++) { + for (int j = 0; j < correctPuzzle[i].length; j++) { + correctPuzzle[i][j] = i * width + j + 1; + } + } + correctPuzzle[width - 1][height - 1] = 0; + + return correctPuzzle; + } + + @Override + public boolean check() { + return Matrices.equals(getPuzzle(), getCorrectPuzzle()); + } + + @Override + public void slid(Point index) { + slid((int) index.getX(), (int) index.getY()); + } + + public void slid(int x, int y) { + if (puzzle[y][x] == 0) { + throw new RectangularPuzzleIndexOutOfBoundsException(); + } + + try { + if (puzzle[y - 1][x] == 0) { + Matrices.swap(puzzle, x, y, x - 1, y); + incrementCounter(); + return; + } + } catch (ArrayIndexOutOfBoundsException ex) { + } + + try { + if (puzzle[y + 1][x] == 0) { + Matrices.swap(puzzle, x, y, x + 1, y); + incrementCounter(); + return; + } + } catch (ArrayIndexOutOfBoundsException ex) { + } + + try { + if (puzzle[y][x - 1] == 0) { + Matrices.swap(puzzle, x, y, x, y - 1); + incrementCounter(); + return; + } + } catch (ArrayIndexOutOfBoundsException ex) { + } + + try { + if (puzzle[y][x + 1] == 0) { + Matrices.swap(puzzle, x, y, x, y + 1); + incrementCounter(); + return; + } + } catch (ArrayIndexOutOfBoundsException ex) { + } + + throw new NoEmptyCaseAdjacentSlidingPuzzleExecption(); + } +} diff --git a/src/com/github/puzzles/test/FlipPuzzleTest.java b/src/com/github/puzzles/test/FlipPuzzleTest.java deleted file mode 100644 index 402b589..0000000 --- a/src/com/github/puzzles/test/FlipPuzzleTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.github.puzzles.test; - -import static org.junit.Assert.fail; - -import org.junit.Test; - -import com.github.puzzles.core.Difficulty; -import com.github.puzzles.core.FlippingPuzzle; -import com.github.puzzles.core.PersonalisedDifficultyException; - -public class FlipPuzzleTest { - - @Test - public void testCheck() { - fail("Not yet implemented"); - } - - @Test - public void testFlipPuzzleBooleanArrayArray() { - fail("Not yet implemented"); - } - - @Test - public void testFlipPuzzleIntInt() { - fail("Not yet implemented"); - } - - @Test(expected = PersonalisedDifficultyException.class) - public void testFlipPuzzleDifficulty() { - new FlippingPuzzle(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 deleted file mode 100644 index de38b46..0000000 --- a/src/com/github/puzzles/test/Main.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.github.puzzles.test; - -import java.awt.Point; - -import com.github.puzzles.core.Difficulty; -import com.github.puzzles.core.*; - -public class Main { - - public static void main(String[] args) { - SlidingPuzzle sp = new SlidingPuzzle(5, 5); - printPuzzle(sp); - sp.slid(new Point(4, 3)); - sp.slid(new Point(4, 2)); - sp.slid(new Point(3, 2)); - sp.slid(new Point(1, 2)); - //System.out.println(); - printPuzzle(sp); - boolean bool = sp.check(); - System.out.println(bool); - } - - public static void printPuzzle(AbstractRectangularPuzzle puzzle) { - - 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] + " "); - } - System.out.println(); - } - - /* - for(Boolean[] line : puzzle.getPuzzle()){ - for(Boolean block : line) - System.out.print(block); - System.out.println(); - } - //*/ - } -} diff --git a/src/com/github/puzzles/util/Matrices.java b/src/com/github/puzzles/util/Matrices.java dissimilarity index 68% index 30e3e9c..878e3a3 100644 --- a/src/com/github/puzzles/util/Matrices.java +++ b/src/com/github/puzzles/util/Matrices.java @@ -1,71 +1,100 @@ -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(T[][] 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]; - //*/ - T temp = matrix[fromX][fromY]; - matrix[fromX][fromY] = matrix[toX][toY]; - matrix[toX][toY] = temp; - } - -} +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); + } + + public static boolean equals(T[][] matrix, T[][] withMatrix) + throws NullPointerException { + if (matrix == withMatrix) + return true; + + if (matrix == null && withMatrix == null) + return true; + + if (matrix == null || withMatrix == null) + return false; + + int width = getWidth(matrix); + int height = getHeight(matrix); + if (width != getWidth(withMatrix) && height != getHeight(withMatrix)) + return false; + + for (int i = 0; i < width; i++) { + if (!Arrays.equals(matrix[i], withMatrix[i])) + return false; + } + + return true; + } + + /** + * 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(T[][] 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]; // + */ + T temp = matrix[fromX][fromY]; + matrix[fromX][fromY] = matrix[toX][toY]; + matrix[toX][toY] = temp; + } + +} diff --git a/test/com/github/puzzles/test/FlipPuzzleTest.java b/test/com/github/puzzles/test/FlipPuzzleTest.java new file mode 100644 index 0000000..1d49faf --- /dev/null +++ b/test/com/github/puzzles/test/FlipPuzzleTest.java @@ -0,0 +1,44 @@ +package com.github.puzzles.test; + +import static org.junit.Assert.fail; + +import org.junit.Test; + +import com.github.puzzles.core.Difficulty; +import com.github.puzzles.core.FlippingPuzzle; +import com.github.puzzles.core.PersonalisedDifficultyException; + +public class FlipPuzzleTest { + + @Test + public void testCheck() { + FlippingPuzzle fp = new FlippingPuzzle(Difficulty.MEDUIM); + fp.flip(3, 4); + } + + @Test + public void testFlipPuzzleBooleanArrayArray() { + fail("Not yet implemented"); + } + + @Test + public void testFlipPuzzleIntInt() { + fail("Not yet implemented"); + } + + @Test(expected = PersonalisedDifficultyException.class) + public void testFlipPuzzleDifficulty() { + new FlippingPuzzle(Difficulty.PERSONALISED); + } + + @Test + public void testFlip() { + fail("Not yet implemented"); + } + + @Test + public void getPuzzle() { + + } + +} diff --git a/test/com/github/puzzles/test/Main.java b/test/com/github/puzzles/test/Main.java new file mode 100644 index 0000000..93784da --- /dev/null +++ b/test/com/github/puzzles/test/Main.java @@ -0,0 +1,31 @@ +package com.github.puzzles.test; + +import java.awt.Point; + +import com.github.puzzles.core.*; + +public class Main { + + public static void main(String[] args) { + FlippingPuzzle fp = new FlippingPuzzle(Difficulty.MEDUIM); + printPuzzle(fp); + System.out.println(fp.check()); + } + + public static void printPuzzle(AbstractRectangularPuzzle puzzle) { + + Boolean[][] 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] == true) ? 1 : 0) + " "); + } + System.out.println(); + } + System.out.println(); + + /* + * for(Boolean[] line : puzzle.getPuzzle()){ for(Boolean block : line) + * System.out.print(block); System.out.println(); } // + */ + } +} diff --git a/test/com/github/puzzles/test/MatricesTest.java b/test/com/github/puzzles/test/MatricesTest.java new file mode 100644 index 0000000..b800b48 --- /dev/null +++ b/test/com/github/puzzles/test/MatricesTest.java @@ -0,0 +1,73 @@ +package com.github.puzzles.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.github.puzzles.core.Difficulty; +import com.github.puzzles.core.FlippingPuzzle; +import com.github.puzzles.core.SlidingPuzzle; +import com.github.puzzles.util.Matrices; +import java.util.Arrays; +import static org.hamcrest.CoreMatchers.is; + +public class MatricesTest { + + public static void main(String[] argv) { + + } + + /* + * @Test public void swapTest() { SlidingPuzzle sp = new SlidingPuzzle(5, + * 5); Integer[][] puzzle = sp.getPuzzle(); Integer[] items = new + * Integer[25]; Arrays.fill(items, 0, 25, 0); for (int i = 0; i < 5; i++) { + * for (int j = 0; j < 5; j++) { items[puzzle[i][j]]++; } } + * + * for (Integer item : items) { assertThat(item, is(1)); } } // + */ + + @Test + public void equalsTest() { + Boolean[][] m1 = new Boolean[][] { { true, true, false }, + { true, false, true } }; + Boolean[][] m2 = new Boolean[][] { { true, true, false }, + { true, false, true } }; + + assertThat(Matrices.equals(m1, m2), is(true)); + } + + @Test + public void equalsTest2() { + Boolean[][] m1 = new Boolean[][] { { true, true, false }, + { true, false, false } }; + Boolean[][] m2 = new Boolean[][] { { true, true, false }, + { true, false, true } }; + + assertThat(Matrices.equals(m1, m2), is(false)); + } + + @Test + public void equalsTest3() { + Boolean[][] m1 = new Boolean[][] { { true, true, false }, + { true, false, false } }; + + assertThat(Matrices.equals(m1, m1), is(true)); + } + + @Test + public void equalsTest4() { + Boolean[][] m1 = null; + Boolean[][] m2 = null; + + assertThat(Matrices.equals(m1, m2), is(true)); + } + + @Test + public void equalsTest5() { + Boolean[][] m1 = null; + Boolean[][] m2 = new Boolean[][] { { true, true, false }, + { true, false, true } }; + + assertThat(Matrices.equals(m1, m2), is(false)); + } +} diff --git a/test/testing/SlidingTest.java b/test/com/github/puzzles/test/SlidingTest.java similarity index 77% rename from test/testing/SlidingTest.java rename to test/com/github/puzzles/test/SlidingTest.java index 77c7c2a..25c8982 100644 --- a/test/testing/SlidingTest.java +++ b/test/com/github/puzzles/test/SlidingTest.java @@ -4,54 +4,56 @@ * and open the template in the editor. */ -package testing; +package com.github.puzzles.test; import com.github.puzzles.core.SlidingPuzzle; -import com.github.puzzles.test.Main; + import static org.hamcrest.CoreMatchers.is; + import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; + import static org.junit.Assert.*; /** - * + * * @author root */ public class SlidingTest { - + SlidingPuzzle sp = new SlidingPuzzle(5, 5); - + public SlidingTest() { } - + @BeforeClass public static void setUpClass() { } - + @AfterClass public static void tearDownClass() { } - + @Before public void setUp() { } - + @After public void tearDown() { } @Test - public void checkTest(){ - - Main.printPuzzle(sp); - //assertThat(sp.check(), is(true)); + public void checkTest() { + + // Main.printPuzzle(sp); + // assertThat(sp.check(), is(true)); } - + @Test - public void slidTest(){ - + public void slidTest() { + } } diff --git a/test/testing/MatricesTest.java b/test/testing/MatricesTest.java deleted file mode 100644 index eac9c55..0000000 --- a/test/testing/MatricesTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package testing; - -import static org.junit.Assert.*; - -import org.junit.Test; - -import com.github.puzzles.core.Difficulty; -import com.github.puzzles.core.FlippingPuzzle; -import com.github.puzzles.core.SlidingPuzzle; -import com.github.puzzles.util.Matrices; -import java.util.Arrays; -import static org.hamcrest.CoreMatchers.is; - -public class MatricesTest { - - public static void main(String [] argv){ - - } - - @Test - public void swapTest() { - SlidingPuzzle sp = new SlidingPuzzle(5, 5); - Integer[][] puzzle = sp.getPuzzle(); - Integer[] items = new Integer[25]; - Arrays.fill(items, 0, 25, 0); - for (int i = 0; i < 5; i++) { - for (int j = 0; j < 5; j++) { - items[puzzle[i][j]]++; - } - } - - for (Integer item : items) { - assertThat(item, is(1)); - } - } -} -- 2.11.4.GIT