Sliding puzzle works fine, but it need some addings, Other puzzles will
[puzzles.git] / src / com / github / puzzles / core / FlipPuzzle.java
blobb2cdcbf52bdbbd7e173134c5e9064d2e68aa5a2e
1 package com.github.puzzles.core;
3 import java.awt.Point;
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){
21 return !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;
35 @Override
36 public boolean check() {
37 if(puzzle.equals(correctPuzzle))
38 return true;
39 return false;
42 @Override
43 public void flip(Point index) {
44 int x = new Double(index.getX()).intValue();
45 int y = new Double(index.getY()).intValue();
47 try {
48 puzzle[y][x] = toggle(puzzle[y][x]);
49 } catch (ArrayIndexOutOfBoundsException e) {}
51 try {
52 puzzle[y][x + 1] = toggle(puzzle[y][x]);
53 } catch (ArrayIndexOutOfBoundsException e) {}
55 try {
56 puzzle[y][x - 1] = toggle(puzzle[y][x]);
57 } catch (ArrayIndexOutOfBoundsException e) {}
59 try {
60 puzzle[y + 1][x] = toggle(puzzle[y][x]);
61 } catch (ArrayIndexOutOfBoundsException e) {}
63 try {
64 puzzle[y - 1][x] = toggle(puzzle[y][x]);
65 } catch (ArrayIndexOutOfBoundsException e) {}
68 /* (non-Javadoc)
69 * @see java.lang.Object#hashCode()
71 @Override
72 public int hashCode() {
73 final int prime = 31;
74 int result = super.hashCode();
75 result = prime * result + Arrays.hashCode(correctPuzzle);
76 return result;
79 /* (non-Javadoc)
80 * @see java.lang.Object#equals(java.lang.Object)
82 @Override
83 public boolean equals(Object obj) {
84 if (this == obj)
85 return true;
86 if (!super.equals(obj))
87 return false;
88 if (getClass() != obj.getClass())
89 return false;
90 FlipPuzzle other = (FlipPuzzle) obj;
91 if (!Arrays.deepEquals(correctPuzzle, other.correctPuzzle))
92 return false;
93 return true;