1 package com
.github
.puzzles
.core
;
4 import java
.util
.Random
;
6 import com
.github
.puzzles
.util
.Matrices
;
8 public class SlidingPuzzle
extends AbstractRectangularPuzzle
<Integer
> implements
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
) {
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
++) {
38 puzzle
[width
- 1][height
- 1] = 0;
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
));
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();
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;
90 public boolean check() {
91 return Matrices
.equals(getPuzzle(), getCorrectPuzzle());
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();
105 if (puzzle
[y
- 1][x
] == 0) {
106 Matrices
.swap(puzzle
, x
, y
, x
- 1, y
);
110 } catch (ArrayIndexOutOfBoundsException ex
) {
114 if (puzzle
[y
+ 1][x
] == 0) {
115 Matrices
.swap(puzzle
, x
, y
, x
+ 1, y
);
119 } catch (ArrayIndexOutOfBoundsException ex
) {
123 if (puzzle
[y
][x
- 1] == 0) {
124 Matrices
.swap(puzzle
, x
, y
, x
, y
- 1);
128 } catch (ArrayIndexOutOfBoundsException ex
) {
132 if (puzzle
[y
][x
+ 1] == 0) {
133 Matrices
.swap(puzzle
, x
, y
, x
, y
+ 1);
137 } catch (ArrayIndexOutOfBoundsException ex
) {
140 throw new NoEmptyCaseAdjacentSlidingPuzzleExecption();