Adding sliding puzzle, later i will fix flip function arguments.
[puzzles.git] / src / com / github / puzzles / core / AbstractRectangularPuzzle.java
blob181e1d0fc64537c3984e3a9effcd1fc1e3a60e9c
1 /**
2 * Rectangular puzzles should inherit this class.
3 */
4 package com.github.puzzles.core;
6 import java.util.Arrays;
8 import com.github.puzzles.util.Matrices;
9 import java.util.Random;
11 public abstract class AbstractRectangularPuzzle<T> extends AbstractPuzzle {
13 private int width;
14 private int height;
15 T puzzle[][];
17 protected AbstractRectangularPuzzle(T[][] puzzle, int width, int height, Difficulty difficulty) {
18 super(width * height, 0, difficulty);
19 this.width = width;
20 this.height = height;
21 this.puzzle = puzzle;
24 @SuppressWarnings("unchecked")
25 protected AbstractRectangularPuzzle(AbstractRectangularPuzzle rectangularPuzzle){
26 super(rectangularPuzzle);
27 this.width = rectangularPuzzle.width;
28 this.height = rectangularPuzzle.height;
29 this.puzzle = (T[][])Matrices.copyOf(rectangularPuzzle.puzzle);
31 /**
32 * Getter.
34 * @return the width of the puzzle.
36 public final int getWidth() {
37 return width;
40 /**
41 * Getter.
43 * @return the height of the puzzle.
45 public final int getHeight() {
46 return height;
49 /**
50 * Getter.
52 * @return the puzzle matrix.
54 public final T[][] getPuzzle() {
55 return Matrices.copyOf(puzzle);
56 //return puzzle;
59 /* (non-Javadoc)
60 * @see java.lang.Object#hashCode()
62 @Override
63 public int hashCode() {
64 final int prime = 31;
65 int result = super.hashCode();
66 result = prime * result + height;
67 result = prime * result + Arrays.hashCode(puzzle);
68 result = prime * result + width;
69 return result;
72 /* (non-Javadoc)
73 * @see java.lang.Object#equals(java.lang.Object)
75 @Override
76 public boolean equals(Object obj) {
77 if (this == obj) {
78 return true;
80 if (!super.equals(obj)) {
81 return false;
83 if (getClass() != obj.getClass()) {
84 return false;
86 AbstractRectangularPuzzle other = (AbstractRectangularPuzzle) obj;
87 if (height != other.height) {
88 return false;
90 if (!Arrays.deepEquals(puzzle, other.puzzle)) {
91 return false;
93 if (width != other.width) {
94 return false;
96 return true;
99 /* (non-Javadoc)
100 * @see java.lang.Object#toString()
102 @Override
103 public String toString() {
104 return "AbstractRectangularPuzzle [width=" + width + ", height="
105 + height + ", puzzle=" + Arrays.toString(puzzle) + "]";