1 package com
.github
.puzzles
.core
;
4 import java
.util
.Arrays
;
5 import java
.util
.Random
;
7 import com
.github
.puzzles
.util
.Matrices
;
9 public class FlipPuzzle
extends AbstractRectangularPuzzle
<Boolean
> implements Flipable
<Point
> {
11 Boolean correctPuzzle
[][];
13 public FlipPuzzle(int width
, int height
, Difficulty difficulty
) {
14 super(makeRandomPuzzleHelper(width
, height
), width
, height
, difficulty
);
16 this.correctPuzzle
= new Boolean
[getWidth()][getHeight()];
17 Matrices
.fill(correctPuzzle
, new Boolean(true));
20 private Boolean
toggle(Boolean bool
){
24 private static Boolean
[][] makeRandomPuzzleHelper(int width
, int height
){
25 Boolean booleansMatrice
[][] = new Boolean
[width
][height
];
26 Random randoms
= new Random();
28 for (int i
= 0; i
< width
; i
++)
29 for (int j
= 0; j
< height
; j
++)
30 booleansMatrice
[i
][j
] = randoms
.nextBoolean();
32 return booleansMatrice
;
36 public boolean check() {
37 if(puzzle
.equals(correctPuzzle
))
43 public void flip(Point index
) {
44 int x
= new Double(index
.getX()).intValue();
45 int y
= new Double(index
.getY()).intValue();
48 puzzle
[y
][x
] = toggle(puzzle
[y
][x
]);
49 } catch (ArrayIndexOutOfBoundsException e
) {}
52 puzzle
[y
][x
+ 1] = toggle(puzzle
[y
][x
]);
53 } catch (ArrayIndexOutOfBoundsException e
) {}
56 puzzle
[y
][x
- 1] = toggle(puzzle
[y
][x
]);
57 } catch (ArrayIndexOutOfBoundsException e
) {}
60 puzzle
[y
+ 1][x
] = toggle(puzzle
[y
][x
]);
61 } catch (ArrayIndexOutOfBoundsException e
) {}
64 puzzle
[y
- 1][x
] = toggle(puzzle
[y
][x
]);
65 } catch (ArrayIndexOutOfBoundsException e
) {}
69 * @see java.lang.Object#hashCode()
72 public int hashCode() {
74 int result
= super.hashCode();
75 result
= prime
* result
+ Arrays
.hashCode(correctPuzzle
);
80 * @see java.lang.Object#equals(java.lang.Object)
83 public boolean equals(Object obj
) {
86 if (!super.equals(obj
))
88 if (getClass() != obj
.getClass())
90 FlipPuzzle other
= (FlipPuzzle
) obj
;
91 if (!Arrays
.deepEquals(correctPuzzle
, other
.correctPuzzle
))