Permalink
Browse files

HExagonal grids

  • Loading branch information...
1 parent da90e4d commit 0bd0b401eb45e0b69583e6243b3a63dc1fbd4210 @scravy scravy committed Jan 21, 2014
@@ -4,7 +4,6 @@
import jazz.Jazz;
import jazz.Picture;
import jazz.RandomColor;
-import jazz.pictures.immutable.Circle;
import jazz.pictures.mutable.Pictures;
import jazz.pictures.mutable.Rectangle;
@@ -1,33 +1,12 @@
package jazz.examples.dw;
-import jazz.Event;
import jazz.Jazz;
-import jazz.Picture;
-import jazz.World;
-import jazz.util.SimpleHexagonGrid;
public class DW {
-
+
public static void main(String... args) {
- Jazz.play("DW", 800, 600, new World() {
-
- SimpleHexagonGrid<Object> grid = new SimpleHexagonGrid<Object>(
- null, 10, 45, 35);
-
- @Override
- public void update(double time, double delta) {
-
- }
-
- @Override
- public void on(Event e) {
-
- }
-
- @Override
- public Picture getPicture() {
- return grid.getPicture();
- }
- });
+
+ //Jazz.seed();
+ Jazz.play("DW", 800, 600, new DWWorld());
}
}
@@ -0,0 +1,16 @@
+package jazz.examples.dw;
+
+import jazz.Color;
+
+public class DWTile {
+
+ private final Color color;
+
+ public DWTile(Color c) {
+ color = c;
+ }
+
+ public Color getColor() {
+ return color;
+ }
+}
@@ -0,0 +1,199 @@
+package jazz.examples.dw;
+
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.Iterator;
+import java.util.List;
+
+import jazz.Color;
+import jazz.Event;
+import jazz.Event.Type;
+import jazz.Jazz;
+import jazz.Picture;
+import jazz.World;
+import jazz.pictures.mutable.Pictures;
+import jazz.util.DefaultTileFactory;
+import jazz.util.SimpleHexagonGrid;
+import jazz.util.TileDecorator;
+import jazz.util.TileEventHandler;
+import jazz.util.TilePos;
+
+public class DWWorld extends World {
+
+ private SimpleHexagonGrid<DWTile> grid;
+ private final static List<Color> colors = new ArrayList<Color>();
+ {
+ colors.add(Color.PURPLE);
+ colors.add(Color.OLIVE);
+ colors.add(Color.ORANGE);
+ colors.add(Color.BLUE);
+ colors.add(Color.YELLOW);
+ colors.add(Color.CYAN);
+ colors.add(Color.BROWN);
+ colors.add(Color.DARK_GREEN);
+ colors.add(Color.AZURE);
+ }
+
+ public DWWorld() {
+
+ }
+
+ private void init(int numPlayers, int each) {
+
+ grid = new SimpleHexagonGrid<DWTile>(
+
+ new DefaultTileFactory<DWTile>(),
+
+ new TileDecorator<DWTile>() {
+ @Override
+ public Picture decorate(DWTile tile, Picture picture) {
+ if (tile == null) {
+ return picture;
+ }
+ return picture.filled(true).color(tile.getColor());
+ }
+ },
+
+ new TileEventHandler<DWTile>() {
+ @Override
+ public void on(Event ev, DWTile tile) {
+
+ }
+ },
+
+ 15, 30, 25);
+
+ Deque<Color> colors = new ArrayDeque<>();
+
+ for (int i = 0; i < numPlayers; i++) {
+ colors.add(DWWorld.colors.get(i));
+ }
+
+ List<DWTile> tiles = new ArrayList<DWTile>(32);
+ for (Color c : colors) {
+ for (int j = 0; j < each; j++) {
+ tiles.add(new DWTile(c));
+ }
+ }
+ Jazz.shuffle(tiles);
+
+ int n = grid.getWidth() * grid.getHeight();
+
+ int countries = (int) Math.ceil(Math.sqrt(colors.size() * each));
+ int w = grid.getWidth() / countries;
+ int h = grid.getHeight() / countries;
+
+ Iterator<DWTile> tilesIt = tiles.iterator();
+ for (int i = 0; i < countries; i++) {
+ for (int j = 0; j < countries; j++) {
+ int x = i * w + Jazz.randomInt(w);
+ int y = j * h + Jazz.randomInt(h);
+
+ if (tilesIt.hasNext()) {
+ grid.setTileAt(x, y, tilesIt.next());
+ n--;
+ }
+ }
+ }
+
+ while (n > 0) {
+ for (int i = 0; i < grid.getWidth(); i++) {
+ for (int j = 0; j < grid.getHeight(); j++) {
+
+ TilePos p = new TilePos(i, j);
+ DWTile t = grid.getTileAt(p);
+
+ if (t == null) {
+ continue;
+ }
+
+ List<Integer> xs = new ArrayList<Integer>();
+ for (int k = 0; k < 6; k++) {
+ xs.add(k);
+ }
+ Jazz.shuffle(xs);
+
+ neighbors: for (int x : xs) {
+ switch (x) {
+ case 0: {
+ TilePos p2 = grid.right(p);
+ if (p2 != null && grid.getTileAt(p2) == null) {
+ grid.setTileAt(p2, t);
+ n--;
+ break neighbors;
+ }
+ break;
+ }
+ case 1: {
+ TilePos p2 = grid.topRight(p);
+ if (p2 != null && grid.getTileAt(p2) == null) {
+ grid.setTileAt(p2, t);
+ n--;
+ break neighbors;
+ }
+ break;
+ }
+ case 2: {
+ TilePos p2 = grid.topLeft(p);
+ if (p2 != null && grid.getTileAt(p2) == null) {
+ grid.setTileAt(p2, t);
+ n--;
+ break neighbors;
+ }
+ break;
+ }
+ case 3: {
+ TilePos p2 = grid.left(p);
+ if (p2 != null && grid.getTileAt(p2) == null) {
+ grid.setTileAt(p2, t);
+ n--;
+ break neighbors;
+ }
+ break;
+ }
+ case 4: {
+ TilePos p2 = grid.bottomLeft(p);
+ if (p2 != null && grid.getTileAt(p2) == null) {
+ grid.setTileAt(p2, t);
+ n--;
+ break neighbors;
+ }
+ break;
+ }
+ case 5: {
+ TilePos p2 = grid.bottomRight(p);
+ if (p2 != null && grid.getTileAt(p2) == null) {
+ grid.setTileAt(p2, t);
+ n--;
+ break neighbors;
+ }
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public Picture getPicture() {
+ if (grid != null) {
+ return grid.getPicture();
+ }
+ return new Pictures();
+ }
+
+ @Override
+ public void update(double time, double delta) {
+
+ }
+
+ @Override
+ public void on(Event ev) {
+ if (ev.getType() == Type.CLICK) {
+ init();
+ }
+ }
+}
@@ -0,0 +1,18 @@
+package jazz.util;
+
+import jazz.Event;
+
+/**
+ * A {@link TileEventHandler} that simply does nothing.
+ *
+ * @author Julian Fleischer
+ *
+ * @param <T>
+ */
+public class DefaultTileEventHandler<T> implements TileEventHandler<T> {
+
+ @Override
+ public void on(Event ev, T tile) {
+
+ }
+}
@@ -0,0 +1,37 @@
+package jazz.util;
+
+/**
+ * A {@link TileFactory} that always returns the same tile or null.
+ *
+ * @author Julian Fleischer
+ *
+ * @param <T>
+ * The type of the tile.
+ */
+public class DefaultTileFactory<T> implements TileFactory<T> {
+
+ private final T defaultTile;
+
+ /**
+ * Creates a tile factory that always returns null.
+ */
+ public DefaultTileFactory() {
+ defaultTile = null;
+ }
+
+ /**
+ * Creates a tile factory that always returns the same tile.
+ *
+ * @param tile
+ * The default tile that is returned always.
+ */
+ public DefaultTileFactory(T tile) {
+ this.defaultTile = tile;
+ }
+
+ @Override
+ public T createTile(int x, int y) {
+ return defaultTile;
+ }
+
+}
@@ -5,6 +5,15 @@
import jazz.pictures.UnmodifieablePictures;
import jazz.pictures.mutable.Pictures;
+/**
+ * EXPERIMENTAL: A hexagonal grid that allows different orientations of the
+ * hexagons.
+ *
+ * @author Julian Fleischer
+ *
+ * @param <T>
+ * The type of the tiles.
+ */
public class HexagonGrid2<T> extends AbstractGrid<HexagonGrid2<T>, T> {
public static enum Mode {
@@ -178,36 +187,36 @@ public Picture getPicture() {
switch (gridMode) {
case HOR_FST_OFFSET:
-
+
break;
case HOR_FST_OFFSET_FULL:
-
+
break;
case HOR_SND_OFFSET:
-
+
break;
case HOR_SND_OFFSET_FULL:
-
+
break;
case VERT_FST_OFFSET:
-
+
break;
case VERT_FST_OFFSET_FULL:
-
+
break;
case VERT_SND_OFFSET:
-
+
break;
case VERT_SND_OFFSET_FULL:
-
+
break;
}
-
+
return new UnmodifieablePictures(pictures);
}
@Override
public void on(Event ev) {
-
+
}
}
Oops, something went wrong.

0 comments on commit 0bd0b40

Please sign in to comment.