I implemented the focused cells changing color, still a lots of work :)
[puzzles.git] / src / com / github / puzzles / core / FlippingPuzzle.java
blobbf8f84b2bbb415cbc3465178487f8be021d60955
1 package com.github.puzzles.core;
3 import java.awt.Point;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
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) {
27 super(puzzle);
30 private Boolean toggle(Boolean bool) {
31 return !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;
43 return booleanMatrix;
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();
63 @Override
64 public Boolean[][] makeCorrectPuzzle() {
65 Boolean[][] correctPuzzle = new Boolean[getWidth()][getHeight()];
66 Matrices.fill(correctPuzzle, true);
67 return correctPuzzle;
70 @Override
71 public boolean check() {
72 return Matrices.equals(getPuzzle(), getCorrectPuzzle());
75 @Override
76 public void flip(Point cell) {
77 flip((int) cell.getX(), (int) cell.getY());
80 public void flip(int x, int y) {
81 try {
82 puzzle[y][x] = toggle(puzzle[y][x]);
83 } catch (ArrayIndexOutOfBoundsException ex) {
84 throw new RectangularPuzzleIndexOutOfBoundsException();
87 try {
88 puzzle[y][x + 1] = toggle(puzzle[y][x + 1]);
89 } catch (ArrayIndexOutOfBoundsException ex) {
92 try {
93 puzzle[y][x - 1] = toggle(puzzle[y][x - 1]);
94 } catch (ArrayIndexOutOfBoundsException ex) {
97 try {
98 puzzle[y + 1][x] = toggle(puzzle[y + 1][x]);
99 } catch (ArrayIndexOutOfBoundsException ex) {
102 try {
103 puzzle[y - 1][x] = toggle(puzzle[y - 1][x]);
104 } catch (ArrayIndexOutOfBoundsException ex) {
107 incrementCounter();
110 public Boolean[] getWillFlip(int x, int y){
111 List<Boolean> willFlip = new ArrayList<>();
112 try {
113 willFlip.add(new Boolean(puzzle[y][x]));
114 } catch (ArrayIndexOutOfBoundsException ex) {
115 throw new RectangularPuzzleIndexOutOfBoundsException();
118 try {
119 willFlip.add(new Boolean(puzzle[y][x + 1]));
120 } catch (ArrayIndexOutOfBoundsException ex) {
123 try {
124 willFlip.add(new Boolean(puzzle[y][x - 1]));
125 } catch (ArrayIndexOutOfBoundsException ex) {
128 try {
129 willFlip.add(new Boolean(puzzle[y + 1][x]));
130 } catch (ArrayIndexOutOfBoundsException ex) {
133 try {
134 willFlip.add(new Boolean(puzzle[y - 1][x]));
135 } catch (ArrayIndexOutOfBoundsException ex) {
138 return (Boolean[])willFlip.toArray(new Boolean[0]);
141 @Override
142 public String toString() {
143 return "FlipPuzzle [puzzle=" + Arrays.toString(puzzle) + "]";