Permalink
Browse files

Few steps further, added search possible fits for single position free

situation
  • Loading branch information...
wouterla committed Aug 29, 2011
1 parent 571ca9f commit 3b4651dedd545f1d4e475312d04f327a2de7840e
@@ -0,0 +1,26 @@
+package org.geenz.puzzler;
+
+public class Coordinate {
+
+ int x = 0;
+ int y = 0;
+
+ public Coordinate(int x, int y) {
+ this.x = x;
+ this.y = y;
+ }
+
+ public int getX() {
+ return x;
+ }
+
+ public int getY() {
+ return y;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ Coordinate other = (Coordinate) obj;
+ return (other.getX() == getX() && other.getY() == getY());
+ }
+}
@@ -1,23 +1,29 @@
package org.geenz.puzzler;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
public class Grid {
- private static final char EMPTY = '0';
+ protected static final char EMPTY = '0';
+ protected static final int NO_FIT = -1;
- private static final int GRID_WIDTH = 11;
-
- private static final int GRID_HEIGHT = 5;
+ protected static final int GRID_WIDTH = 11;
+ protected static final int GRID_HEIGHT = 5;
private char[][] grid = new char[GRID_WIDTH][GRID_HEIGHT];
public Grid() {
- // initialize grid
+ fillWith(EMPTY);
+ }
+
+ protected void fillWith(char value) {
for (int i = 0; i < grid.length; i++) {
- Arrays.fill(grid[i], EMPTY);
- }
+ Arrays.fill(grid[i], value);
+ }
}
public int getHeight() {
@@ -44,10 +50,17 @@ public char valueAt(int i, int j) {
return grid[i][j];
}
+ protected void setValueAt(int i, int j, char value) {
+ grid[i][j] = value;
+ }
+
public void place(int i, int j, Piece piece, int orientation) {
for (int k = 0; k < piece.getWidth(orientation); k++) {
for (int l = 0; l < piece.getHeight(orientation); l++) {
- fillWith(i + k, j + l, piece.getElementAt(l, k, orientation));
+ char value = piece.getElementAt(l, k, orientation);
+ if (value != EMPTY) {
+ fillWith(i + k, j + l, value);
+ }
}
}
}
@@ -59,5 +72,115 @@ public void print() {
}
System.out.println();
}
+ System.out.println();
+ }
+
+ public boolean findPlaceFor(Piece piece) {
+ List<Coordinate> emptyPlaces = getAllEmptyPlaces();
+ for (Coordinate coordinate : emptyPlaces) {
+ int orientation = fits(piece, coordinate);
+ if (NO_FIT != orientation) {
+ place(coordinate.getX(), coordinate.getY(), piece, orientation);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public List<PlacedPiece> findPlacesFor(Piece piece) {
+ List<PlacedPiece> foundPlaces = new ArrayList<PlacedPiece>();
+ List<Coordinate> emptyPlaces = getAllEmptyPlaces();
+ for (Coordinate coordinate : emptyPlaces) {
+ int orientation = fits(piece, coordinate);
+ if (NO_FIT != orientation) {
+ PlacedPiece found = new PlacedPiece(piece, coordinate, orientation);
+ foundPlaces.add(found);
+ }
+ }
+ return foundPlaces;
+ }
+
+ private int fits(Piece piece, Coordinate coordinate) {
+ int baseX = coordinate.getX();
+ int baseY = coordinate.getY();
+ for (Integer orientation : piece.getOrientations()) {
+ char[][] elements = piece.getElements(orientation);
+ System.out.println("Trying to fit: ");
+ Piece.print(elements);
+ if (fits(elements, baseX, baseY)) {
+ return orientation;
+ }
+ }
+ return NO_FIT;
+ }
+
+ private boolean fits(char[][] elements, int baseX, int baseY) {
+ if (!fitsWithinGrid(elements, baseX, baseY)) {
+ return false;
+ }
+ for (int height = 0; height < elements[0].length; height++) {
+ for (int width = 0; width < elements.length; width++) {
+ if (Piece.isUsed(elements, width, height)
+ && isFilled(baseX + height, baseY + width)) {
+ return false;
+ }
+ }
+ }
+ return true;
}
+
+ private boolean fitsWithinGrid(char[][] elements, int baseX, int baseY) {
+ if ((baseX + elements[0].length < GRID_WIDTH)
+ && (baseY + elements.length < GRID_HEIGHT)) {
+ return true;
+ }
+ return false;
+ }
+
+ public Coordinate findEmptyPlace() {
+ for (int i = 0; i < getWidth(); i++) {
+ for (int j = 0; j < getHeight(); j++) {
+ if (valueAt(i, j) == EMPTY) {
+ return new Coordinate(i, j);
+ }
+ }
+ }
+ return null;
+ }
+
+ public boolean hasEmptyPlace() {
+ return findEmptyPlace() != null;
+ }
+
+ public List<Coordinate> getAllEmptyPlaces() {
+ List<Coordinate> foundPlaces = new ArrayList<Coordinate>();
+ for (int i = 0; i < getWidth(); i++) {
+ for (int j = 0; j < getHeight(); j++) {
+ if (valueAt(i, j) == EMPTY) {
+ foundPlaces.add(new Coordinate(i, j));
+ }
+ }
+ }
+ return foundPlaces;
+ }
+
+ public Piece findFittingPiece(Map<Character, Piece> pieces) {
+ for (Piece piece : pieces.values()) {
+ if (findPlaceFor(piece)) {
+ return piece;
+ }
+ }
+ return null;
+ }
+
+ public List<PlacedPiece> findFittingPieces(Map<Character, Piece> pieces) {
+ List<PlacedPiece> foundPieces = new ArrayList<PlacedPiece>();
+ for (Piece piece : pieces.values()) {
+ foundPieces.addAll(findPlacesFor(piece));
+ }
+ return foundPieces;
+ }
+
+
+
}
@@ -1,14 +1,20 @@
package org.geenz.puzzler;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
public class Piece {
public static final int DEFAULT_ORIENTATION = 0;
public static final int TURNED_90_DEGREES = 90;
public static final int TURNED_180_DEGREES = 180;
public static final int TURNED_270_DEGREES = 270;
+ public static final int DEFAULT_ORIENTATION_FLIPPED = -360;
+ public static final int TURNED_90_DEGREES_FLIPPED = -90;
+ public static final int TURNED_180_DEGREES_FLIPPED = -180;
+ public static final int TURNED_270_DEGREES_FLIPPED = -270;
private char[][] piece = null;
private Map<Integer, char[][]> orientations = new HashMap<Integer, char[][]>();
@@ -19,6 +25,10 @@ public Piece(char[][] elements) {
orientations.put(TURNED_90_DEGREES, turn(piece));
orientations.put(TURNED_180_DEGREES, turn(orientations.get(TURNED_90_DEGREES)));
orientations.put(TURNED_270_DEGREES, turn(orientations.get(TURNED_180_DEGREES)));
+ orientations.put(DEFAULT_ORIENTATION_FLIPPED, flip(orientations.get(DEFAULT_ORIENTATION)));
+ orientations.put(TURNED_90_DEGREES_FLIPPED, flip(orientations.get(TURNED_90_DEGREES)));
+ orientations.put(TURNED_180_DEGREES_FLIPPED, flip(orientations.get(TURNED_180_DEGREES)));
+ orientations.put(TURNED_270_DEGREES_FLIPPED, flip(orientations.get(TURNED_270_DEGREES)));
}
public char[][] getElements() {
@@ -53,6 +63,10 @@ public int getHeight(int orientation) {
return orientations.get(orientation).length;
}
+ protected static boolean isUsed(char[][] elements, int width, int height) {
+ return elements[width][height] != Grid.EMPTY;
+ }
+
protected static char[] getColumnOfArray(char[][] elements, int columnNumber) {
char[] column = new char[elements.length];
for (int i = 0; i < elements.length; i++) {
@@ -70,4 +84,42 @@ public int getHeight(int orientation) {
return turned;
}
+ protected Set<Integer> getOrientations() {
+ return orientations.keySet();
+ }
+
+ public static char[][] flip(char[][] elements) {
+ char[][] flipped = new char[elements.length][elements[0].length];
+ for (int i = 0; i < elements.length; i++) {
+ flipped[i] = reverseRow(elements[i]);
+ }
+ return flipped;
+ }
+
+ protected static void print(char[][] elements) {
+ for (int j = elements.length - 1; j >= 0 ; j--) {
+ for (int i = 0; i < elements[0].length; i++) {
+ System.out.print(elements[j][i] + " ");
+ }
+ System.out.println();
+ }
+ System.out.println();
+ }
+
+ public static char[] reverseRow(char[] elements) {
+ char[] result = new char[elements.length];
+ int index = 0;
+ for (int i = elements.length - 1; i >= 0 ; i--) {
+ result[index++] = elements[i];
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (!(obj instanceof Piece)) {
+ return false;
+ }
+ return Arrays.equals(piece, ((Piece) obj).getElements());
+ }
}
@@ -0,0 +1,73 @@
+package org.geenz.puzzler;
+
+import java.util.Map;
+import java.util.TreeMap;
+
+public class Pieces {
+ public final static Piece pieceA = new Piece(new char[][] {
+ { 'A', 'A' },
+ { '0', 'A' },
+ { '0', 'A' }});
+ public final static Piece pieceB = new Piece(new char[][] {
+ { 'B', 'B' },
+ { 'B', 'B' },
+ { '0', 'B' }});
+ public final static Piece pieceC = new Piece(new char[][] {
+ { 'C', 'C' },
+ { '0', 'C' },
+ { '0', 'C' },
+ { '0', 'C' }});
+ public final static Piece pieceD = new Piece(new char[][] {
+ { '0', 'D' },
+ { 'D', 'D' },
+ { '0', 'D' },
+ { '0', 'D' }});
+ public final static Piece pieceE = new Piece(new char[][] {
+ { 'E', '0' },
+ { 'E', 'E' },
+ { '0', 'E' },
+ { '0', 'E' }});
+ public final static Piece pieceF = new Piece(new char[][] {
+ { 'F', 'F' },
+ { '0', 'F' }});
+ public final static Piece pieceG = new Piece(new char[][] {
+ { 'G', 'G', 'G' },
+ { '0', '0', 'G' },
+ { '0', '0', 'G'}});
+ public final static Piece pieceH = new Piece(new char[][] {
+ { 'H', 'H', '0' },
+ { '0', 'H', 'H' },
+ { '0', '0', 'H'}});
+ public final static Piece pieceI = new Piece(new char[][] {
+ { 'I', '0', 'I' },
+ { 'I', 'I', 'I'}});
+ public final static Piece pieceJ = new Piece(new char[][] {
+ { 'J' },
+ { 'J' },
+ { 'J' },
+ { 'J' }});
+ public final static Piece pieceK = new Piece(new char[][] {
+ { 'K', 'K' },
+ { 'K', 'K' }});
+ public final static Piece pieceL = new Piece(new char[][] {
+ { '0', 'L', '0' },
+ { 'L', 'L', 'L' },
+ { '0', 'L', '0'}});
+
+ public final static Map<Character, Piece> pieces = new TreeMap<Character, Piece>();
+
+ static {
+ pieces.put('A', pieceA);
+ pieces.put('B', pieceB);
+ pieces.put('C', pieceC);
+ pieces.put('D', pieceD);
+ pieces.put('E', pieceE);
+ pieces.put('F', pieceF);
+ pieces.put('G', pieceG);
+ pieces.put('H', pieceH);
+ pieces.put('I', pieceI);
+ pieces.put('J', pieceJ);
+ pieces.put('K', pieceK);
+ pieces.put('L', pieceL);
+ }
+}
@@ -0,0 +1,26 @@
+package org.geenz.puzzler;
+
+public class PlacedPiece {
+
+ private Piece piece = null;
+ private Coordinate coordinate = null;
+ private int orientation = Piece.DEFAULT_ORIENTATION;
+
+ public PlacedPiece(Piece piece, Coordinate coordinate, int orientation) {
+ this.piece = piece;
+ this.coordinate = coordinate;
+ this.orientation = orientation;
+ }
+
+ public Coordinate getCoordinate() {
+ return coordinate;
+ }
+
+ public Piece getPiece() {
+ return piece;
+ }
+
+ public int getOrientation() {
+ return orientation;
+ }
+}
Oops, something went wrong.

0 comments on commit 3b4651d

Please sign in to comment.