Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: javajazz/jazz
base: 66ff00157c
...
head fork: javajazz/jazz
compare: e1fe28b4ac
  • 4 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jan 20, 2014
@scravy scravy DW! 4577926
@scravy scravy DW2! da90e4d
Commits on Jan 21, 2014
@scravy scravy HExagonal grids 0bd0b40
Commits on Jan 23, 2014
@scravy scravy Hexagon Grid stuff e1fe28b
View
1  src/main/java/jazz/examples/basic/Funky.java
@@ -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;
View
12 src/main/java/jazz/examples/dw/DW.java
@@ -0,0 +1,12 @@
+package jazz.examples.dw;
+
+import jazz.Jazz;
+
+public class DW {
+
+ public static void main(String... args) {
+
+ //Jazz.seed();
+ Jazz.play("DW", 800, 600, new DWWorld());
+ }
+}
View
16 src/main/java/jazz/examples/dw/DWTile.java
@@ -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;
+ }
+}
View
199 src/main/java/jazz/examples/dw/DWWorld.java
@@ -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(8, 4);
+ }
+ }
+}
View
18 src/main/java/jazz/util/DefaultTileEventHandler.java
@@ -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) {
+
+ }
+}
View
37 src/main/java/jazz/util/DefaultTileFactory.java
@@ -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;
+ }
+
+}
View
36 src/main/java/jazz/util/HexagonGrid.java → src/main/java/jazz/util/HexagonGrid2.java
@@ -5,7 +5,16 @@
import jazz.pictures.UnmodifieablePictures;
import jazz.pictures.mutable.Pictures;
-public class HexagonGrid<T> extends AbstractGrid<HexagonGrid<T>, T> {
+/**
+ * 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 {
/**
@@ -145,14 +154,17 @@
HOR_SND_OFFSET_FULL
}
+ @SuppressWarnings("unused")
private final int gridWidth;
+
+ @SuppressWarnings("unused")
private final int gridHeight;
private final Mode gridMode;
private final T[][] tiles;
@SuppressWarnings("unchecked")
- public HexagonGrid(int gridWidth, int gridHeight, double side,
+ public HexagonGrid2(int gridWidth, int gridHeight, double side,
Mode gridMode,
TileFactory<T> tileFactory,
TileEventHandler<T> tileHandler,
@@ -178,36 +190,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) {
-
+
}
}
View
152 src/main/java/jazz/util/SimpleHexagonGrid.java
@@ -0,0 +1,152 @@
+package jazz.util;
+
+import jazz.Event;
+import jazz.Picture;
+import jazz.Vector;
+import jazz.pictures.mutable.Pictures;
+import jazz.pictures.mutable.Polygon;
+
+public class SimpleHexagonGrid<T> {
+
+ private final TileEventHandler<T> tileHandler;
+ private final TileDecorator <T> tileDecorator;
+
+ private final int height;
+ private final int width;
+
+ private final double a, w, h, w1, w2, h1, h2;
+
+ private final T[][] tiles;
+
+ @SuppressWarnings("unchecked")
+ public SimpleHexagonGrid(
+ TileFactory<T> tileFactory,
+ TileDecorator<T> tileDecorator,
+ TileEventHandler<T> tileHandler,
+ double a, int width, int height) {
+
+ this.tileDecorator = tileDecorator;
+ this.tileHandler = tileHandler;
+
+ this.width = width;
+ this.height = height;
+
+ this.a = a;
+
+ tiles = (T[][]) new Object[width][height];
+
+ w1 = Math.sqrt(3) * a;
+ w2 = w1 / 2;
+
+ h1 = a / 2;
+ h2 = h1 * 3;
+
+ w = width * w1 + w2;
+ h = height * h2 + h1;
+ }
+
+ public Picture getPicture() {
+
+ Pictures pictures = new Pictures();
+
+ for (int i = 0; i < width; i++) {
+ for (int j = 0; j < height; j++) {
+
+ double x = i * w1 - w / 2;
+ double y = j * h2 - h / 2;
+
+ if (j % 2 != 0) {
+ x += w2;
+ }
+
+ Polygon p = new Polygon(
+ new Vector(x + w2, y),
+ new Vector(x + w1, y + h1),
+ new Vector(x + w1, y + h2),
+ new Vector(x + w2, y + 2 * a),
+ new Vector(x, y + h2),
+ new Vector(x, y + h1)
+ );
+
+ pictures.add(tileDecorator.decorate(tiles[i][j], p));
+ }
+ }
+ return pictures;
+ }
+
+ public void on(Event ev) {
+
+ }
+
+ public void setTileAt(int x, int y, T tile) {
+ tiles[x][y] = tile;
+ }
+
+ public void setTileAt(TilePos p, T tile) {
+ tiles[p.x][p.y] = tile;
+ }
+
+ public T getTileAt(int x, int y) {
+ return tiles[x][y];
+ }
+
+ public T getTileAt(TilePos p) {
+ return tiles[p.x][p.y];
+ }
+
+ public TilePos topLeft(TilePos p) {
+ if (p.y + 1 >= height) {
+ return null;
+ } else if (p.y % 2 == 0) {
+ return p.x == 0 ? null : new TilePos(p.x - 1, p.y + 1);
+ } else {
+ return new TilePos(p.x, p.y + 1);
+ }
+ }
+
+ public TilePos topRight(TilePos p) {
+ if (p.y + 1 >= height) {
+ return null;
+ } else if (p.y % 2 == 0) {
+ return new TilePos(p.x, p.y + 1);
+ } else {
+ return p.x + 1 >= width ? null : new TilePos(p.x + 1, p.y + 1);
+ }
+ }
+
+ public TilePos bottomLeft(TilePos p) {
+ if (p.y == 0) {
+ return null;
+ } else if (p.y % 2 == 0) {
+ return p.x == 0 ? null : new TilePos(p.x - 1, p.y - 1);
+ } else {
+ return new TilePos(p.x, p.y - 1);
+ }
+ }
+
+ public TilePos bottomRight(TilePos p) {
+ if (p.y == 0) {
+ return null;
+ } else if (p.y % 2 == 0) {
+ return new TilePos(p.x, p.y - 1);
+ } else {
+ return p.x + 1 >= width ? null : new TilePos(p.x + 1, p.y - 1);
+ }
+ }
+
+ public TilePos left(TilePos p) {
+ return p.x == 0 ? null : new TilePos(p.x - 1, p.y);
+ }
+
+ public TilePos right(TilePos p) {
+ return p.x + 1 >= width ? null : new TilePos(p.x + 1, p.y);
+ }
+
+ public int getHeight() {
+ return height;
+ }
+
+ public int getWidth() {
+ return width;
+ }
+}
View
8 src/main/java/jazz/util/TileDecorator.java
@@ -0,0 +1,8 @@
+package jazz.util;
+
+import jazz.Picture;
+
+public interface TileDecorator<T> {
+
+ Picture decorate(T tile, Picture picture);
+}
View
24 src/main/java/jazz/util/TilePos.java
@@ -0,0 +1,24 @@
+package jazz.util;
+
+public class TilePos {
+
+ public final int x;
+ public final int y;
+
+ public TilePos(int x, int y) {
+ this.x = x;
+ this.y = y;
+ }
+
+ public int getX() {
+ return x;
+ }
+
+ public int getY() {
+ return y;
+ }
+
+ public String toString() {
+ return String.format("(%d,%d)", x, y);
+ }
+}

No commit comments for this range

Something went wrong with that request. Please try again.