I've made the GUI for flipping puzzle, Cancel button still need fixing
[puzzles.git] / src / com / github / puzzles / core / SlidingPuzzle.java
blobf683e758739663fecbfa25b5bed8e739931ec5e3
1 package com.github.puzzles.core;
3 import java.awt.Point;
4 import java.util.Random;
6 import com.github.puzzles.util.Matrices;
8 public class SlidingPuzzle extends AbstractRectangularPuzzle<Integer> implements
9 Slidable<Point> {
11 private static final int MAX_SWAP = 512;
13 protected SlidingPuzzle(Integer[][] puzzle) {
14 super(puzzle, Difficulty.PERSONALISED);
17 public SlidingPuzzle(int width, int height) {
18 // this(makeCorrectPuzzleHelper(width, height));
19 super(makeCorrectPuzzleHelper(width, height), Difficulty.PERSONALISED);
22 public SlidingPuzzle(Difficulty difficulty) {
23 this(makePuzzleFromDifficultyHelper(difficulty));
26 public SlidingPuzzle(SlidingPuzzle puzzle) {
27 super(puzzle);
30 private static Integer[][] makeCorrectPuzzleHelper(int width, int height) {
31 Integer[][] puzzle = new Integer[width][height];
32 for (int i = 0, k = 1; i < width; i++) {
33 for (int j = 0; j < height; j++, k++) {
34 puzzle[i][j] = k;
38 puzzle[width - 1][height - 1] = 0;
40 return puzzle;
43 private static Integer[][] makePuzzleHelper(int width, int height) {
44 Integer[][] puzzle = makeCorrectPuzzleHelper(width, height);
45 Random randoms = new Random();
47 for (int i = 0; i < MAX_SWAP; i++) {
48 Matrices.swap(puzzle, randoms.nextInt(width),
49 randoms.nextInt(width), randoms.nextInt(height),
50 randoms.nextInt(height));
53 return puzzle;
56 private static Integer[][] makePuzzleFromDifficultyHelper(
57 Difficulty difficulty) throws PersonalisedDifficultyException {
58 if (difficulty == Difficulty.VERY_EASY) {
59 return makePuzzleHelper(3, 2);
60 } else if (difficulty == Difficulty.EASY) {
61 return makePuzzleHelper(5, 3);
62 } else if (difficulty == Difficulty.MEDUIM) {
63 return makePuzzleHelper(5, 5);
64 } else if (difficulty == Difficulty.HARD) {
65 return makePuzzleHelper(7, 7);
66 } else if (difficulty == Difficulty.VERY_HARD) {
67 return makePuzzleHelper(11, 11);
70 throw new PersonalisedDifficultyException();
73 @Override
74 public Integer[][] makeCorrectPuzzle() {
75 int width = getWidth();
76 int height = getHeight();
77 Integer[][] correctPuzzle = new Integer[width][height];
79 for (int i = 0; i < correctPuzzle.length; i++) {
80 for (int j = 0; j < correctPuzzle[i].length; j++) {
81 correctPuzzle[i][j] = i * width + j + 1;
84 correctPuzzle[width - 1][height - 1] = 0;
86 return correctPuzzle;
89 @Override
90 public boolean check() {
91 return Matrices.equals(getPuzzle(), getCorrectPuzzle());
94 @Override
95 public void slid(Point index) {
96 slid((int) index.getX(), (int) index.getY());
99 public void slid(int x, int y) {
100 if (puzzle[y][x] == 0) {
101 throw new RectangularPuzzleIndexOutOfBoundsException();
104 try {
105 if (puzzle[y - 1][x] == 0) {
106 Matrices.swap(puzzle, x, y, x - 1, y);
107 incrementCounter();
108 return;
110 } catch (ArrayIndexOutOfBoundsException ex) {
113 try {
114 if (puzzle[y + 1][x] == 0) {
115 Matrices.swap(puzzle, x, y, x + 1, y);
116 incrementCounter();
117 return;
119 } catch (ArrayIndexOutOfBoundsException ex) {
122 try {
123 if (puzzle[y][x - 1] == 0) {
124 Matrices.swap(puzzle, x, y, x, y - 1);
125 incrementCounter();
126 return;
128 } catch (ArrayIndexOutOfBoundsException ex) {
131 try {
132 if (puzzle[y][x + 1] == 0) {
133 Matrices.swap(puzzle, x, y, x, y + 1);
134 incrementCounter();
135 return;
137 } catch (ArrayIndexOutOfBoundsException ex) {
140 throw new NoEmptyCaseAdjacentSlidingPuzzleException();