diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2020-06-08 16:57:24 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2020-06-08 16:57:24 +0200 |
commit | 9d4961aaec471622458f2cb4371e7b02e983f92f (patch) | |
tree | 0084d2d596acb69a2bb9f70da6cccbd02fcc1a48 /core/src/ch/asynk/gdx/boardgame | |
parent | e553e2a22eac25bc7f3c0f61a8db3377be66e8e8 (diff) | |
download | gdx-boardgame-9d4961aaec471622458f2cb4371e7b02e983f92f.zip gdx-boardgame-9d4961aaec471622458f2cb4371e7b02e983f92f.tar.gz |
implement HexBoard::possibleMoves(…)
Diffstat (limited to 'core/src/ch/asynk/gdx/boardgame')
6 files changed, 95 insertions, 0 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/Piece.java b/core/src/ch/asynk/gdx/boardgame/Piece.java index b08970c..6944afc 100644 --- a/core/src/ch/asynk/gdx/boardgame/Piece.java +++ b/core/src/ch/asynk/gdx/boardgame/Piece.java @@ -123,4 +123,15 @@ public class Piece implements Drawable, Positionable, Rotable, Scalable float h = getHeight(); shapeRenderer.rect(getX(), getY(), (w / 2f), (h / 2f), w, h, sprite.getScaleX(), sprite.getScaleY(), getRotation()); } + + public int getAvailableMP() + { + return 0; + } + + public int moveCost(Tile from, Tile to, Orientation orientation) + { + // Integer.MAX_VALUE means impracticable + return Integer.MAX_VALUE; + } } diff --git a/core/src/ch/asynk/gdx/boardgame/Tile.java b/core/src/ch/asynk/gdx/boardgame/Tile.java index 78a17e6..37a6972 100644 --- a/core/src/ch/asynk/gdx/boardgame/Tile.java +++ b/core/src/ch/asynk/gdx/boardgame/Tile.java @@ -18,6 +18,11 @@ public class Tile implements Drawable public float cy; public boolean blocked; public boolean onMap; + + public Tile parent; + public int acc; + public int searchCount; + private Overlays overlays; public Tile(int x, int y, float cx, float cy) diff --git a/core/src/ch/asynk/gdx/boardgame/boards/Board.java b/core/src/ch/asynk/gdx/boardgame/boards/Board.java index 8abfc34..98161cb 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/Board.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/Board.java @@ -20,6 +20,8 @@ public interface Board extends TileKeyGenerator public Tile[] getAdjacents(); public void buildAdjacents(int x, int y); + public int possibleMoves(Piece piece, Tile from, Collection<Tile> tiles); + public boolean lineOfSight(int x0, int y0, int x1, int y1, Collection<Tile> tiles); default public boolean lineOfSight(Tile from, Tile to, Collection<Tile> tiles) diff --git a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java index 332898a..639741e 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java @@ -2,9 +2,12 @@ package ch.asynk.gdx.boardgame.boards; import com.badlogic.gdx.math.Vector2; +import ch.asynk.gdx.boardgame.Orientation; +import ch.asynk.gdx.boardgame.Piece; import ch.asynk.gdx.boardgame.Tile; import ch.asynk.gdx.boardgame.tilestorages.TileStorage.TileProvider; import ch.asynk.gdx.boardgame.utils.Collection; +import ch.asynk.gdx.boardgame.utils.IterableStack; public class HexBoard implements Board { @@ -21,6 +24,10 @@ public class HexBoard implements Board private final float h; // square height : s + dh private final float slope; // dh / dw + private int aOffset; // to fix Orientation computation from adjacents + private int searchCount; // to differentiate move computations + private IterableStack<Tile> stack; + private final int tl; // tiles in 2 consecutive lines // BoardOrientation.VERTICAL : 2 vertical sides : 2 vertices pointing up and down @@ -65,9 +72,14 @@ public class HexBoard implements Board this.h = side + dh; this.slope = dh / dw; + this.searchCount = 0; + this.stack = new IterableStack<Tile>(10); + if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) { + this.aOffset = 0; this.tl = (2 * cols - 1); } else { + this.aOffset = -60; this.tl = (2 * rows - 1); } @@ -103,6 +115,7 @@ public class HexBoard implements Board @Override public void buildAdjacents(int x, int y) { + // VERTICAL starts with E 0°, HORIZONTAL starts with SE -30° adjacents[0] = getTile(x + 1, y); adjacents[1] = getTile(x + 1, y + 1); adjacents[2] = getTile(x , y + 1); @@ -449,4 +462,52 @@ public class HexBoard implements Board return tiles.get(tiles.size() - 1).blocked; } + + public int possibleMoves(Piece piece, Tile from, Collection<Tile> tiles) + { + tiles.clear(); + searchCount += 1; + + from.acc = piece.getAvailableMP(); + from.parent = null; + from.searchCount = searchCount; + + if (from.acc <= 0 || !from.isOnMap()) + return tiles.size(); + + stack.push(from); + + while(!stack.isEmpty()) { + final Tile src = stack.pop(); + + if (src.acc <= 0) continue; + + buildAdjacents(src.x, src.y); + for (int i = 0, j = 0; i < 6; i++, j++) { + final Tile dst = adjacents[i]; + if (!dst.isOnMap()) continue; + + if (getAngles()[j] == -1) j++; + int cost = piece.moveCost(src, dst, Orientation.fromR(getAngles()[j] + aOffset)); + if (cost == Integer.MAX_VALUE) continue; // impracticable + + int r = src.acc - cost; + if (r < 0 && src != from) continue; // allow at least 1 tile move + + if (dst.searchCount != searchCount) { + dst.searchCount = searchCount; + dst.acc = r; + dst.parent = src; + stack.push(dst); + tiles.add(dst); + } else if (r > dst.acc) { + dst.acc = r; + dst.parent = src; + stack.push(dst); + } + } + } + + return tiles.size(); + } } diff --git a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java index ae3e18b..dc3dbe4 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java @@ -2,6 +2,7 @@ package ch.asynk.gdx.boardgame.boards; import com.badlogic.gdx.math.Vector2; +import ch.asynk.gdx.boardgame.Piece; import ch.asynk.gdx.boardgame.Tile; import ch.asynk.gdx.boardgame.tilestorages.TileStorage.TileProvider; import ch.asynk.gdx.boardgame.utils.Collection; @@ -110,4 +111,11 @@ public class SquareBoard implements Board System.err.println("NOT implemented yet."); return false; } + + @Override public int possibleMoves(Piece piece, Tile from, Collection<Tile> tiles) + { + System.err.println("NOT implemented yet."); + tiles.clear(); + return tiles.size(); + } } diff --git a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java index 42bd378..409966f 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java @@ -2,6 +2,7 @@ package ch.asynk.gdx.boardgame.boards; import com.badlogic.gdx.math.Vector2; +import ch.asynk.gdx.boardgame.Piece; import ch.asynk.gdx.boardgame.Tile; import ch.asynk.gdx.boardgame.tilestorages.TileStorage.TileProvider; import ch.asynk.gdx.boardgame.utils.Collection; @@ -179,4 +180,11 @@ public class TriangleBoard implements Board System.err.println("NOT implemented yet."); return false; } + + @Override public int possibleMoves(Piece piece, Tile from, Collection<Tile> tiles) + { + System.err.println("NOT implemented yet."); + tiles.clear(); + return tiles.size(); + } } |