1 package com
.github
.puzzles
.core
;
4 import java
.util
.ArrayList
;
5 import java
.util
.Arrays
;
8 import com
.github
.puzzles
.util
.Matrices
;
10 public class FlippingPuzzle
extends AbstractRectangularPuzzle
<Boolean
>
11 implements Flipable
<Point
> {
13 protected FlippingPuzzle(Boolean
[][] puzzle
) {
14 super(puzzle
, Difficulty
.PERSONALISED
);
17 public FlippingPuzzle(int width
, int height
) {
18 // this(makePuzzleHelper(width, height));
19 super(makePuzzleHelper(width
, height
), Difficulty
.PERSONALISED
);
22 public FlippingPuzzle(Difficulty difficulty
) {
23 this(makePuzzleFromDifficultyHelper(difficulty
));
26 public FlippingPuzzle(FlippingPuzzle puzzle
) {
30 private Boolean
toggle(Boolean bool
) {
34 private static Boolean
[][] makePuzzleHelper(int width
, int height
) {
36 Boolean booleanMatrix
[][] = new Boolean
[width
][height
];
37 for (int i
= 0; i
< width
; i
++) {
38 for (int j
= 0; j
< height
; j
++) {
39 booleanMatrix
[i
][j
] = false;
46 private static Boolean
[][] makePuzzleFromDifficultyHelper(
47 Difficulty difficulty
) throws PersonalisedDifficultyException
{
48 if (difficulty
== Difficulty
.VERY_EASY
) {
49 return makePuzzleHelper(3, 3);
50 } else if (difficulty
== Difficulty
.EASY
) {
51 return makePuzzleHelper(5, 5);
52 } else if (difficulty
== Difficulty
.MEDUIM
) {
53 return makePuzzleHelper(7, 7);
54 } else if (difficulty
== Difficulty
.HARD
) {
55 return makePuzzleHelper(9, 9);
56 } else if (difficulty
== Difficulty
.VERY_HARD
) {
57 return makePuzzleHelper(11, 11);
60 throw new PersonalisedDifficultyException();
64 public Boolean
[][] makeCorrectPuzzle() {
65 Boolean
[][] correctPuzzle
= new Boolean
[getWidth()][getHeight()];
66 Matrices
.fill(correctPuzzle
, true);
71 public boolean check() {
72 return Matrices
.equals(getPuzzle(), getCorrectPuzzle());
76 public void flip(Point cell
) {
77 flip((int) cell
.getX(), (int) cell
.getY());
80 public void flip(int x
, int y
) {
82 puzzle
[y
][x
] = toggle(puzzle
[y
][x
]);
83 } catch (ArrayIndexOutOfBoundsException ex
) {
84 throw new RectangularPuzzleIndexOutOfBoundsException();
88 puzzle
[y
][x
+ 1] = toggle(puzzle
[y
][x
+ 1]);
89 } catch (ArrayIndexOutOfBoundsException ex
) {
93 puzzle
[y
][x
- 1] = toggle(puzzle
[y
][x
- 1]);
94 } catch (ArrayIndexOutOfBoundsException ex
) {
98 puzzle
[y
+ 1][x
] = toggle(puzzle
[y
+ 1][x
]);
99 } catch (ArrayIndexOutOfBoundsException ex
) {
103 puzzle
[y
- 1][x
] = toggle(puzzle
[y
- 1][x
]);
104 } catch (ArrayIndexOutOfBoundsException ex
) {
110 public Boolean
[] getWillFlip(int x
, int y
){
111 List
<Boolean
> willFlip
= new ArrayList
<>();
113 willFlip
.add(new Boolean(puzzle
[y
][x
]));
114 } catch (ArrayIndexOutOfBoundsException ex
) {
115 throw new RectangularPuzzleIndexOutOfBoundsException();
119 willFlip
.add(new Boolean(puzzle
[y
][x
+ 1]));
120 } catch (ArrayIndexOutOfBoundsException ex
) {
124 willFlip
.add(new Boolean(puzzle
[y
][x
- 1]));
125 } catch (ArrayIndexOutOfBoundsException ex
) {
129 willFlip
.add(new Boolean(puzzle
[y
+ 1][x
]));
130 } catch (ArrayIndexOutOfBoundsException ex
) {
134 willFlip
.add(new Boolean(puzzle
[y
- 1][x
]));
135 } catch (ArrayIndexOutOfBoundsException ex
) {
138 return (Boolean
[])willFlip
.toArray(new Boolean
[0]);
142 public String
toString() {
143 return "FlipPuzzle [puzzle=" + Arrays
.toString(puzzle
) + "]";