From d0897446407a1c792dbe83fafe6b8f723ec13779 Mon Sep 17 00:00:00 2001 From: La VloZ Date: Thu, 30 Jan 2014 01:38:25 +0100 Subject: [PATCH] I'm making the GUI for flipping puzzle :) --- src/com/github/puzzles/core/AbstractPuzzle.java | 288 ++++++++++----------- .../puzzles/core/AbstractRectangularPuzzle.java | 248 +++++++++--------- src/com/github/puzzles/core/Difficulty.java | 2 +- src/com/github/puzzles/core/Flipable.java | 2 +- src/com/github/puzzles/core/FlippingPuzzle.java | 224 ++++++++-------- src/com/github/puzzles/core/Movable.java | 2 +- .../NoEmptyCaseAdjacentSlidingPuzzleExecption.java | 4 +- .../core/PersonalisedDifficultyException.java | 4 +- ...RectangularPuzzleIndexOutOfBoundsException.java | 6 +- src/com/github/puzzles/core/Slidable.java | 2 +- src/com/github/puzzles/core/SlidingPuzzle.java | 284 ++++++++++---------- src/com/github/puzzles/gui/AboutMeDialog.java | 100 +++---- .../github/puzzles/gui/AbstractPuzzlePanel.java | 31 +++ .../github/puzzles/gui/FlippingPuzzleDialog.java | 141 ++++++++++ .../github/puzzles/gui/FlippingPuzzlePanel.java | 21 +- src/com/github/puzzles/gui/MainWindow.java | 274 +++++++++++++------- .../github/puzzles/gui/SlidingPuzzleDialog.java | 138 ++++++++++ src/com/github/puzzles/gui/SlidingPuzzlePanel.java | 14 +- src/com/github/puzzles/util/Matrices.java | 200 +++++++------- test/com/github/puzzles/test/FlipPuzzleTest.java | 88 +++---- test/com/github/puzzles/test/Main.java | 62 ++--- test/com/github/puzzles/test/MatricesTest.java | 138 +++++----- test/com/github/puzzles/test/SlidingTest.java | 46 ++-- 23 files changed, 1355 insertions(+), 964 deletions(-) rewrite src/com/github/puzzles/core/AbstractPuzzle.java (94%) rewrite src/com/github/puzzles/core/AbstractRectangularPuzzle.java (90%) rewrite src/com/github/puzzles/core/FlippingPuzzle.java (91%) rewrite src/com/github/puzzles/core/SlidingPuzzle.java (92%) rewrite src/com/github/puzzles/gui/AboutMeDialog.java (74%) create mode 100644 src/com/github/puzzles/gui/AbstractPuzzlePanel.java create mode 100644 src/com/github/puzzles/gui/FlippingPuzzleDialog.java rewrite src/com/github/puzzles/gui/MainWindow.java (84%) create mode 100644 src/com/github/puzzles/gui/SlidingPuzzleDialog.java rewrite src/com/github/puzzles/util/Matrices.java (94%) rewrite test/com/github/puzzles/test/FlipPuzzleTest.java (66%) rewrite test/com/github/puzzles/test/Main.java (74%) rewrite test/com/github/puzzles/test/MatricesTest.java (86%) diff --git a/src/com/github/puzzles/core/AbstractPuzzle.java b/src/com/github/puzzles/core/AbstractPuzzle.java dissimilarity index 94% index 2ad7359..2d0627f 100644 --- a/src/com/github/puzzles/core/AbstractPuzzle.java +++ b/src/com/github/puzzles/core/AbstractPuzzle.java @@ -1,144 +1,144 @@ -/** - * 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; - } -} +/** + * 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 90% index c1321eb..4503f5c 100644 --- a/src/com/github/puzzles/core/AbstractRectangularPuzzle.java +++ b/src/com/github/puzzles/core/AbstractRectangularPuzzle.java @@ -1,124 +1,124 @@ -/** - * 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 { - - 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 - @SuppressWarnings("rawtypes") - 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; + +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 + @SuppressWarnings("rawtypes") + 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 849870d..f551f8a 100644 --- a/src/com/github/puzzles/core/Difficulty.java +++ b/src/com/github/puzzles/core/Difficulty.java @@ -1,5 +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 6c1b10e..d99f0e7 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 91% index a2d97a6..22c56a6 100644 --- a/src/com/github/puzzles/core/FlippingPuzzle.java +++ b/src/com/github/puzzles/core/FlippingPuzzle.java @@ -1,112 +1,112 @@ -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) + "]"; - } -} +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 75d05b0..903b70f 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 9f72de4..f5fd652 100644 --- a/src/com/github/puzzles/core/NoEmptyCaseAdjacentSlidingPuzzleExecption.java +++ b/src/com/github/puzzles/core/NoEmptyCaseAdjacentSlidingPuzzleExecption.java @@ -12,9 +12,9 @@ package com.github.puzzles.core; */ public class NoEmptyCaseAdjacentSlidingPuzzleExecption extends RuntimeException { - /** + /** * */ - private static final long serialVersionUID = -9215866099344609257L; + private static final long serialVersionUID = -9215866099344609257L; } diff --git a/src/com/github/puzzles/core/PersonalisedDifficultyException.java b/src/com/github/puzzles/core/PersonalisedDifficultyException.java index 08767c2..3fd7562 100644 --- a/src/com/github/puzzles/core/PersonalisedDifficultyException.java +++ b/src/com/github/puzzles/core/PersonalisedDifficultyException.java @@ -2,9 +2,9 @@ package com.github.puzzles.core; public class PersonalisedDifficultyException extends RuntimeException { - /** + /** * */ - private static final long serialVersionUID = 984870086601632505L; + private static final long serialVersionUID = 984870086601632505L; } diff --git a/src/com/github/puzzles/core/RectangularPuzzleIndexOutOfBoundsException.java b/src/com/github/puzzles/core/RectangularPuzzleIndexOutOfBoundsException.java index 7bb567a..4abb4b4 100644 --- a/src/com/github/puzzles/core/RectangularPuzzleIndexOutOfBoundsException.java +++ b/src/com/github/puzzles/core/RectangularPuzzleIndexOutOfBoundsException.java @@ -11,11 +11,11 @@ package com.github.puzzles.core; * @author root */ public class RectangularPuzzleIndexOutOfBoundsException extends - RuntimeException { + RuntimeException { - /** + /** * */ - private static final long serialVersionUID = -3574940876834216117L; + private static final long serialVersionUID = -3574940876834216117L; } diff --git a/src/com/github/puzzles/core/Slidable.java b/src/com/github/puzzles/core/Slidable.java index cbe0e53..63edae5 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 92% index b1f8ed1..5d428da 100644 --- a/src/com/github/puzzles/core/SlidingPuzzle.java +++ b/src/com/github/puzzles/core/SlidingPuzzle.java @@ -1,142 +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, 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[][] puzzle = new Integer[width][height]; - for (int i = 0, k = 1; i < width; i++) { - for (int j = 0; j < height; j++, k++) { - puzzle[i][j] = k; - } - } - - puzzle[width - 1][height - 1] = 0; - - return puzzle; - } - - private static Integer[][] makePuzzleHelper(int width, int height) { - Integer[][] puzzle = makeCorrectPuzzleHelper(width, height); - Random randoms = new Random(); - - for (int i = 0; i < MAX_SWAP; i++) { - Matrices.swap(puzzle, randoms.nextInt(width), - randoms.nextInt(width), randoms.nextInt(height), - randoms.nextInt(height)); - } - - return puzzle; - } - - 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(); - } -} +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[][] puzzle = new Integer[width][height]; + for (int i = 0, k = 1; i < width; i++) { + for (int j = 0; j < height; j++, k++) { + puzzle[i][j] = k; + } + } + + puzzle[width - 1][height - 1] = 0; + + return puzzle; + } + + private static Integer[][] makePuzzleHelper(int width, int height) { + Integer[][] puzzle = makeCorrectPuzzleHelper(width, height); + Random randoms = new Random(); + + for (int i = 0; i < MAX_SWAP; i++) { + Matrices.swap(puzzle, randoms.nextInt(width), + randoms.nextInt(width), randoms.nextInt(height), + randoms.nextInt(height)); + } + + return puzzle; + } + + 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/gui/AboutMeDialog.java b/src/com/github/puzzles/gui/AboutMeDialog.java dissimilarity index 74% index 6ec82f5..13a8e93 100644 --- a/src/com/github/puzzles/gui/AboutMeDialog.java +++ b/src/com/github/puzzles/gui/AboutMeDialog.java @@ -1,50 +1,50 @@ -package com.github.puzzles.gui; - -import java.awt.BorderLayout; -import java.awt.FlowLayout; - -import javax.swing.JDialog; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.SwingConstants; -import javax.swing.border.EmptyBorder; - -public class AboutMeDialog extends JDialog { - - /** - * - */ - private static final long serialVersionUID = -3200331392530781095L; - private final JPanel contentPanel = new JPanel(); - - /** - * Launch the application. - */ - public static void main(String[] args) { - try { - AboutMeDialog dialog = new AboutMeDialog(); - dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - dialog.setVisible(true); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Create the dialog. - */ - public AboutMeDialog() { - setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - setBounds(100, 100, 450, 300); - getContentPane().setLayout(new BorderLayout()); - contentPanel.setLayout(new FlowLayout()); - contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); - getContentPane().add(contentPanel, BorderLayout.CENTER); - { - JLabel label = new JLabel("Coded by La VloZ ^__^"); - label.setHorizontalAlignment(SwingConstants.CENTER); - contentPanel.add(label); - } - } - -} +package com.github.puzzles.gui; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; + +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.SwingConstants; +import javax.swing.border.EmptyBorder; + +public class AboutMeDialog extends JDialog { + + /** + * + */ + private static final long serialVersionUID = -3200331392530781095L; + private final JPanel contentPanel = new JPanel(); + + /** + * Launch the application. + */ + public static void main(String[] args) { + try { + AboutMeDialog dialog = new AboutMeDialog(); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Create the dialog. + */ + public AboutMeDialog() { + setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + setBounds(100, 100, 450, 300); + getContentPane().setLayout(new BorderLayout()); + contentPanel.setLayout(new FlowLayout()); + contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + getContentPane().add(contentPanel, BorderLayout.CENTER); + { + JLabel label = new JLabel("Coded by La VloZ ^__^"); + label.setHorizontalAlignment(SwingConstants.CENTER); + contentPanel.add(label); + } + } + +} diff --git a/src/com/github/puzzles/gui/AbstractPuzzlePanel.java b/src/com/github/puzzles/gui/AbstractPuzzlePanel.java new file mode 100644 index 0000000..50ec8c4 --- /dev/null +++ b/src/com/github/puzzles/gui/AbstractPuzzlePanel.java @@ -0,0 +1,31 @@ +package com.github.puzzles.gui; + +import javax.swing.JPanel; + +abstract public class AbstractPuzzlePanel extends JPanel { + + /** + * + */ + private static final long serialVersionUID = 5714650403244806247L; + + final private int xIndex; + final private int yIndex; + + /** + * Create the panel. + */ + public AbstractPuzzlePanel(int xIndex, int yIndex) { + this.xIndex = xIndex; + this.yIndex = yIndex; + } + + public int getxIndex() { + return xIndex; + } + + public int getyIndex() { + return yIndex; + } + +} diff --git a/src/com/github/puzzles/gui/FlippingPuzzleDialog.java b/src/com/github/puzzles/gui/FlippingPuzzleDialog.java new file mode 100644 index 0000000..9fc81db --- /dev/null +++ b/src/com/github/puzzles/gui/FlippingPuzzleDialog.java @@ -0,0 +1,141 @@ +package com.github.puzzles.gui; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; + +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import javax.swing.JTextField; +import javax.swing.JLabel; + +import com.github.puzzles.core.FlippingPuzzle; + +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class FlippingPuzzleDialog extends JDialog { + + /** + * + */ + private static final long serialVersionUID = 4729972122941718936L; + private final JPanel contentPanel = new JPanel(); + private JTextField rowsText; + private JTextField colsText; + private JLabel rowsNumberMessage; + private JLabel rowsErrorMessage; + private JLabel colsNumberMessage; + private JLabel colsErrorMessage; + + /** + * Launch the application. + */ + /* + * public static void main(String[] args) { try { FlippingPuzzleDialog + * dialog = new FlippingPuzzleDialog(); + * dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + * dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } + * // + */ + + /** + * Create the dialog. + */ + public FlippingPuzzleDialog(final MainWindow mainWindow) { + setBounds(100, 100, 450, 300); + getContentPane().setLayout(new BorderLayout()); + contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + getContentPane().add(contentPanel, BorderLayout.CENTER); + contentPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); + { + JPanel panel = new JPanel(); + contentPanel.add(panel); + { + rowsNumberMessage = new JLabel("Rows number"); + panel.add(rowsNumberMessage); + } + { + rowsText = new JTextField(); + panel.add(rowsText); + rowsText.setColumns(10); + } + { + rowsErrorMessage = new JLabel("* should be a number."); + rowsErrorMessage.setVisible(false); + panel.add(rowsErrorMessage); + } + } + + JPanel panel = new JPanel(); + contentPanel.add(panel); + { + colsNumberMessage = new JLabel("Cols number"); + panel.add(colsNumberMessage); + } + { + colsText = new JTextField(); + panel.add(colsText); + colsText.setColumns(10); + } + { + colsErrorMessage = new JLabel("* should be a number"); + colsErrorMessage.setVisible(false); + panel.add(colsErrorMessage); + } + { + JPanel buttonPane = new JPanel(); + buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); + getContentPane().add(buttonPane, BorderLayout.SOUTH); + { + JButton okButton = new JButton("OK"); + okButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseReleased(MouseEvent e) { + int rows = 0; + int cols = 0; + boolean error = false; + rowsErrorMessage.setVisible(false); + colsErrorMessage.setVisible(false); + try { + rows = Integer.parseInt(rowsText.getText()); + error = true; + } catch (NumberFormatException e1) { + rowsErrorMessage.setVisible(true); + } + try { + cols = Integer.parseInt(colsText.getText()); + error = true; + } catch (NumberFormatException e1) { + colsErrorMessage.setVisible(true); + } + + if (error) { + mainWindow.setFlippingPuzzle(new FlippingPuzzle( + rows, cols)); + FlippingPuzzleDialog.this.dispose(); + } + + FlippingPuzzleDialog.this.dispose(); + + } + }); + okButton.setActionCommand("OK"); + buttonPane.add(okButton); + getRootPane().setDefaultButton(okButton); + } + { + JButton cancelButton = new JButton("Cancel"); + cancelButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseReleased(MouseEvent e) { + FlippingPuzzleDialog.this.dispose(); + } + }); + cancelButton.setActionCommand("Cancel"); + buttonPane.add(cancelButton); + } + } + } +} diff --git a/src/com/github/puzzles/gui/FlippingPuzzlePanel.java b/src/com/github/puzzles/gui/FlippingPuzzlePanel.java index 76eaa9a..7f792e5 100644 --- a/src/com/github/puzzles/gui/FlippingPuzzlePanel.java +++ b/src/com/github/puzzles/gui/FlippingPuzzlePanel.java @@ -1,19 +1,18 @@ package com.github.puzzles.gui; -import javax.swing.JPanel; -public class FlippingPuzzlePanel extends JPanel { +public class FlippingPuzzlePanel extends AbstractPuzzlePanel { - /** + /** * */ - private static final long serialVersionUID = 4872124821136999470L; - - /** - * Create the panel. - */ - public FlippingPuzzlePanel() { - - } + private static final long serialVersionUID = 4872124821136999470L; + /** + * Create the panel. + */ + public FlippingPuzzlePanel(int xIndex, int yIndex) { + super(xIndex, yIndex); + + } } diff --git a/src/com/github/puzzles/gui/MainWindow.java b/src/com/github/puzzles/gui/MainWindow.java dissimilarity index 84% index ecaf3df..1a0ce85 100644 --- a/src/com/github/puzzles/gui/MainWindow.java +++ b/src/com/github/puzzles/gui/MainWindow.java @@ -1,96 +1,178 @@ -package com.github.puzzles.gui; - -import java.awt.BorderLayout; -import java.awt.EventQueue; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; - -import javax.swing.JDialog; -import javax.swing.JFrame; -import javax.swing.JMenu; -import javax.swing.JMenuBar; -import javax.swing.JMenuItem; -import javax.swing.JPanel; - -public class MainWindow { - - private JFrame frame; - - /** - * Launch the application. - */ - public static void main(String[] args) { - EventQueue.invokeLater(new Runnable() { - public void run() { - try { - MainWindow window = new MainWindow(); - window.frame.setVisible(true); - } catch (Exception e) { - e.printStackTrace(); - } - } - }); - } - - /** - * Create the application. - */ - public MainWindow() { - initialize(); - } - - /** - * Initialize the contents of the frame. - */ - private void initialize() { - frame = new JFrame(); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - - frame.setBounds(0, 0, 800, 600); - - JMenuBar topMenuBar = new JMenuBar(); - frame.setJMenuBar(topMenuBar); - - JMenu menuFile = new JMenu("File"); - topMenuBar.add(menuFile); - - JMenu newPuzzleMenu = new JMenu("New puzzle"); - menuFile.add(newPuzzleMenu); - - JMenuItem flippingPuzzle = new JMenuItem("Flipping Puzzle"); - flippingPuzzle.addMouseListener(new MouseAdapter() { - @Override - public void mouseReleased(MouseEvent e) { - System.out.println("Hellow world!"); - } - }); - newPuzzleMenu.add(flippingPuzzle); - - JMenuItem slidingPuzzle = new JMenuItem("Sliding Puzzle"); - newPuzzleMenu.add(slidingPuzzle); - - JMenu helpMenu = new JMenu("Help"); - topMenuBar.add(helpMenu); - - JMenuItem aboutMenu = new JMenuItem("About me"); - aboutMenu.addMouseListener(new MouseAdapter() { - @Override - public void mouseReleased(MouseEvent e) { - //JFrame aboutFrame = new AboutFrame(); - //aboutFrame.setVisible(true); - JDialog aboutMeDialog = new AboutMeDialog(); - aboutMeDialog.setAlwaysOnTop(true); - aboutMeDialog.setModal(true); - aboutMeDialog.setVisible(true); - } - }); - helpMenu.add(aboutMenu); - - JPanel mainPanel = new JPanel(); - frame.getContentPane().add(mainPanel, BorderLayout.CENTER); - - JPanel puzzlePanel = new JPanel(); - mainPanel.add(puzzlePanel); - } - -} +package com.github.puzzles.gui; + +import java.awt.EventQueue; +import java.awt.FlowLayout; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +import javax.swing.BoxLayout; +import javax.swing.JDialog; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JPanel; + +import com.github.puzzles.core.FlippingPuzzle; +import com.github.puzzles.core.SlidingPuzzle; +import java.awt.Color; + +public class MainWindow { + + private JFrame frame; + private FlippingPuzzle flippingPuzzle; + private SlidingPuzzle sliddingPuzzle; + private JPanel mainPanel; + private JPanel puzzlePanel; + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + MainWindow window = new MainWindow(); + window.frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + /** + * Create the application. + */ + public MainWindow() { + initialize(); + } + + /** + * Initialize the contents of the frame. + */ + private void initialize() { + + frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + frame.setBounds(0, 0, 800, 600); + + JMenuBar topMenuBar = new JMenuBar(); + frame.setJMenuBar(topMenuBar); + + JMenu menuFile = new JMenu("File"); + topMenuBar.add(menuFile); + + JMenu newPuzzleMenu = new JMenu("New puzzle"); + menuFile.add(newPuzzleMenu); + + JMenuItem flippingPuzzle = new JMenuItem("Flipping Puzzle"); + flippingPuzzle.addMouseListener(new MouseAdapter() { + @Override + public void mouseReleased(MouseEvent e) { + JDialog flippingPuzzleDialog = new FlippingPuzzleDialog( + MainWindow.this); + flippingPuzzleDialog + .setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + flippingPuzzleDialog.setAlwaysOnTop(true); + flippingPuzzleDialog.setModal(true); + flippingPuzzleDialog.setVisible(true); + + int rowsNumber = MainWindow.this.flippingPuzzle.getWidth(); + int colsNumber = MainWindow.this.flippingPuzzle.getHeight(); + + //for (int i = 0; i < rowsNumber; i++) { + //* + + //*/ + //} + JPanel panel = new JPanel(); + panel.setBackground(Color.RED); + /* + for (int i = 0; i < rowsNumber; i++) { + JPanel rowsPanel = new JPanel(); + for (int j = 0; j < colsNumber; j++) { + FlippingPuzzlePanel cell = new FlippingPuzzlePanel(j, j); + cell.add(new JLabel("Label")); + cell.setBackground(Color.RED); + rowsPanel.add(cell); + } + puzzlePanel.add(rowsPanel); + } + //*/ + } + }); + newPuzzleMenu.add(flippingPuzzle); + + JMenuItem slidingPuzzle = new JMenuItem("Sliding Puzzle"); + slidingPuzzle.addMouseListener(new MouseAdapter() { + @Override + public void mouseReleased(MouseEvent e) { + // * + JDialog slidingPuzzleDialog = new SlidingPuzzleDialog( + MainWindow.this); + slidingPuzzleDialog + .setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + slidingPuzzleDialog.setAlwaysOnTop(true); + slidingPuzzleDialog.setModal(true); + slidingPuzzleDialog.setVisible(true); + + // */ + } + }); + newPuzzleMenu.add(slidingPuzzle); + + JMenu helpMenu = new JMenu("Help"); + topMenuBar.add(helpMenu); + + JMenuItem aboutMenu = new JMenuItem("About me"); + aboutMenu.addMouseListener(new MouseAdapter() { + @Override + public void mouseReleased(MouseEvent e) { + // JFrame aboutFrame = new AboutFrame(); + // aboutFrame.setVisible(true); + JDialog aboutMeDialog = new AboutMeDialog(); + aboutMeDialog.setAlwaysOnTop(true); + aboutMeDialog.setModal(true); + aboutMeDialog.setVisible(true); + } + }); + helpMenu.add(aboutMenu); + frame.getContentPane().setLayout( + new FlowLayout(FlowLayout.CENTER, 5, 5)); + + mainPanel = new JPanel(); + frame.getContentPane().add(mainPanel); + mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS)); + + puzzlePanel = new JPanel(); + mainPanel.add(puzzlePanel); + puzzlePanel.setLayout(new BoxLayout(puzzlePanel, BoxLayout.PAGE_AXIS)); + } + + public FlippingPuzzle getFlippingPuzzle() { + return flippingPuzzle; + } + + public void setFlippingPuzzle(FlippingPuzzle flippingPuzzle) { + this.flippingPuzzle = flippingPuzzle; + } + + public SlidingPuzzle getSliddingPuzzle() { + return sliddingPuzzle; + } + + public void setSliddingPuzzle(SlidingPuzzle sliddingPuzzle) { + this.sliddingPuzzle = sliddingPuzzle; + } + + public JPanel getMainPanel() { + return mainPanel; + } + + public JPanel getPanel() { + return puzzlePanel; + } +} diff --git a/src/com/github/puzzles/gui/SlidingPuzzleDialog.java b/src/com/github/puzzles/gui/SlidingPuzzleDialog.java new file mode 100644 index 0000000..94c16df --- /dev/null +++ b/src/com/github/puzzles/gui/SlidingPuzzleDialog.java @@ -0,0 +1,138 @@ +package com.github.puzzles.gui; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.border.EmptyBorder; + +import com.github.puzzles.core.SlidingPuzzle; + +public class SlidingPuzzleDialog extends JDialog { + + /** + * + */ + private static final long serialVersionUID = 4729972122941718936L; + private final JPanel contentPanel = new JPanel(); + private JTextField rowsText; + private JTextField colsText; + private JLabel rowsNumberMessage; + private JLabel rowsErrorMessage; + private JLabel colsNumberMessage; + private JLabel colsErrorMessage; + + /** + * Launch the application. + */ + /* + * public static void main(String[] args) { try { FlippingPuzzleDialog + * dialog = new FlippingPuzzleDialog(); + * dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + * dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } + * // + */ + + /** + * Create the dialog. + */ + public SlidingPuzzleDialog(final MainWindow mainWindow) { + setBounds(100, 100, 450, 300); + getContentPane().setLayout(new BorderLayout()); + contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + getContentPane().add(contentPanel, BorderLayout.CENTER); + contentPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); + { + JPanel panel = new JPanel(); + contentPanel.add(panel); + { + rowsNumberMessage = new JLabel("Rows number"); + panel.add(rowsNumberMessage); + } + { + rowsText = new JTextField(); + panel.add(rowsText); + rowsText.setColumns(10); + } + { + rowsErrorMessage = new JLabel("* should be a number."); + rowsErrorMessage.setVisible(false); + panel.add(rowsErrorMessage); + } + } + + JPanel panel = new JPanel(); + contentPanel.add(panel); + { + colsNumberMessage = new JLabel("Cols number"); + panel.add(colsNumberMessage); + } + { + colsText = new JTextField(); + panel.add(colsText); + colsText.setColumns(10); + } + { + colsErrorMessage = new JLabel("* should be a number"); + colsErrorMessage.setVisible(false); + panel.add(colsErrorMessage); + } + { + JPanel buttonPane = new JPanel(); + buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); + getContentPane().add(buttonPane, BorderLayout.SOUTH); + { + JButton okButton = new JButton("OK"); + okButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseReleased(MouseEvent e) { + int rows = 0; + int cols = 0; + boolean error = false; + rowsErrorMessage.setVisible(false); + colsErrorMessage.setVisible(false); + try { + rows = Integer.parseInt(rowsText.getText()); + error = true; + } catch (NumberFormatException e1) { + rowsErrorMessage.setVisible(true); + } + try { + cols = Integer.parseInt(colsText.getText()); + error = true; + } catch (NumberFormatException e1) { + colsErrorMessage.setVisible(true); + } + + if (error) { + mainWindow.setSliddingPuzzle(new SlidingPuzzle( + rows, cols)); + SlidingPuzzleDialog.this.dispose(); + } + SlidingPuzzleDialog.this.dispose(); + } + }); + okButton.setActionCommand("OK"); + buttonPane.add(okButton); + getRootPane().setDefaultButton(okButton); + } + { + JButton cancelButton = new JButton("Cancel"); + cancelButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseReleased(MouseEvent e) { + SlidingPuzzleDialog.this.dispose(); + } + }); + cancelButton.setActionCommand("Cancel"); + buttonPane.add(cancelButton); + } + } + } +} diff --git a/src/com/github/puzzles/gui/SlidingPuzzlePanel.java b/src/com/github/puzzles/gui/SlidingPuzzlePanel.java index 1dcab6e..58ca379 100644 --- a/src/com/github/puzzles/gui/SlidingPuzzlePanel.java +++ b/src/com/github/puzzles/gui/SlidingPuzzlePanel.java @@ -4,16 +4,16 @@ import javax.swing.JPanel; public class SlidingPuzzlePanel extends JPanel { - /** + /** * */ - private static final long serialVersionUID = -2769199725994710949L; + private static final long serialVersionUID = -2769199725994710949L; - /** - * Create the panel. - */ - public SlidingPuzzlePanel() { + /** + * Create the panel. + */ + public SlidingPuzzlePanel() { - } + } } diff --git a/src/com/github/puzzles/util/Matrices.java b/src/com/github/puzzles/util/Matrices.java dissimilarity index 94% index 878e3a3..f76d035 100644 --- a/src/com/github/puzzles/util/Matrices.java +++ b/src/com/github/puzzles/util/Matrices.java @@ -1,100 +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); - } - - 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; - } - -} +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 dissimilarity index 66% index 1d49faf..c415701 100644 --- a/test/com/github/puzzles/test/FlipPuzzleTest.java +++ b/test/com/github/puzzles/test/FlipPuzzleTest.java @@ -1,44 +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() { - - } - -} +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 dissimilarity index 74% index 8367c5c..6795de2 100644 --- a/test/com/github/puzzles/test/Main.java +++ b/test/com/github/puzzles/test/Main.java @@ -1,31 +1,31 @@ -package com.github.puzzles.test; - -import com.github.puzzles.core.AbstractRectangularPuzzle; -import com.github.puzzles.core.Difficulty; -import com.github.puzzles.core.FlippingPuzzle; - -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(); } // - */ - } -} +package com.github.puzzles.test; + +import com.github.puzzles.core.AbstractRectangularPuzzle; +import com.github.puzzles.core.Difficulty; +import com.github.puzzles.core.FlippingPuzzle; + +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 dissimilarity index 86% index 671d3d9..f77a02f 100644 --- a/test/com/github/puzzles/test/MatricesTest.java +++ b/test/com/github/puzzles/test/MatricesTest.java @@ -1,69 +1,69 @@ -package com.github.puzzles.test; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -import org.junit.Test; - -import com.github.puzzles.util.Matrices; - -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)); - } -} +package com.github.puzzles.test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +import com.github.puzzles.util.Matrices; + +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/com/github/puzzles/test/SlidingTest.java b/test/com/github/puzzles/test/SlidingTest.java index e0ebfd5..2c049d7 100644 --- a/test/com/github/puzzles/test/SlidingTest.java +++ b/test/com/github/puzzles/test/SlidingTest.java @@ -20,36 +20,36 @@ import com.github.puzzles.core.SlidingPuzzle; */ public class SlidingTest { - SlidingPuzzle sp = new SlidingPuzzle(5, 5); + SlidingPuzzle sp = new SlidingPuzzle(5, 5); - public SlidingTest() { - } + public SlidingTest() { + } - @BeforeClass - public static void setUpClass() { - } + @BeforeClass + public static void setUpClass() { + } - @AfterClass - public static void tearDownClass() { - } + @AfterClass + public static void tearDownClass() { + } - @Before - public void setUp() { - } + @Before + public void setUp() { + } - @After - public void tearDown() { - } + @After + public void tearDown() { + } - @Test - public void checkTest() { + @Test + public void checkTest() { - // Main.printPuzzle(sp); - // assertThat(sp.check(), is(true)); - } + // Main.printPuzzle(sp); + // assertThat(sp.check(), is(true)); + } - @Test - public void slidTest() { + @Test + public void slidTest() { - } + } } -- 2.11.4.GIT