1 package com
.github
.puzzles
.core
;
4 import java
.util
.Arrays
;
6 import com
.github
.puzzles
.util
.Matrices
;
8 public class FlippingPuzzle
extends AbstractRectangularPuzzle
<Boolean
>
9 implements Flipable
<Point
> {
11 protected FlippingPuzzle(Boolean
[][] puzzle
) {
12 super(puzzle
, Difficulty
.PERSONALISED
);
15 public FlippingPuzzle(int width
, int height
) {
16 // this(makePuzzleHelper(width, height));
17 super(makePuzzleHelper(width
, height
), Difficulty
.PERSONALISED
);
20 public FlippingPuzzle(Difficulty difficulty
) {
21 this(makePuzzleFromDifficultyHelper(difficulty
));
24 public FlippingPuzzle(FlippingPuzzle puzzle
) {
28 private Boolean
toggle(Boolean bool
) {
32 private static Boolean
[][] makePuzzleHelper(int width
, int height
) {
34 Boolean booleanMatrix
[][] = new Boolean
[width
][height
];
35 for (int i
= 0; i
< width
; i
++) {
36 for (int j
= 0; j
< height
; j
++) {
37 booleanMatrix
[i
][j
] = false;
44 private static Boolean
[][] makePuzzleFromDifficultyHelper(
45 Difficulty difficulty
) throws PersonalisedDifficultyException
{
46 if (difficulty
== Difficulty
.VERY_EASY
) {
47 return makePuzzleHelper(3, 3);
48 } else if (difficulty
== Difficulty
.EASY
) {
49 return makePuzzleHelper(5, 5);
50 } else if (difficulty
== Difficulty
.MEDUIM
) {
51 return makePuzzleHelper(7, 7);
52 } else if (difficulty
== Difficulty
.HARD
) {
53 return makePuzzleHelper(9, 9);
54 } else if (difficulty
== Difficulty
.VERY_HARD
) {
55 return makePuzzleHelper(11, 11);
58 throw new PersonalisedDifficultyException();
62 public Boolean
[][] makeCorrectPuzzle() {
63 Boolean
[][] correctPuzzle
= new Boolean
[getWidth()][getHeight()];
64 Matrices
.fill(correctPuzzle
, true);
69 public boolean check() {
70 return Matrices
.equals(getPuzzle(), getCorrectPuzzle());
74 public void flip(Point index
) {
75 flip((int) index
.getX(), (int) index
.getY());
78 public void flip(int y
, int x
) {
80 puzzle
[y
][x
] = toggle(puzzle
[y
][x
]);
81 } catch (RectangularPuzzleIndexOutOfBoundsException ex
) {
86 puzzle
[y
][x
+ 1] = toggle(puzzle
[y
][x
+ 1]);
87 } catch (RectangularPuzzleIndexOutOfBoundsException ex
) {
91 puzzle
[y
][x
- 1] = toggle(puzzle
[y
][x
- 1]);
92 } catch (RectangularPuzzleIndexOutOfBoundsException ex
) {
96 puzzle
[y
+ 1][x
] = toggle(puzzle
[y
+ 1][x
]);
97 } catch (RectangularPuzzleIndexOutOfBoundsException ex
) {
101 puzzle
[y
- 1][x
] = toggle(puzzle
[y
- 1][x
]);
102 } catch (RectangularPuzzleIndexOutOfBoundsException ex
) {
109 public String
toString() {
110 return "FlipPuzzle [puzzle=" + Arrays
.toString(puzzle
) + "]";