From 8554edc96eb2da6510fa4159873b940f1202fae3 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Sun, 4 Feb 2018 23:34:13 +0100 Subject: Rearranging the code --- .../fr/umlv/java/wallj/block/BlockFactory.java | 8 +-- .../java/fr/umlv/java/wallj/block/SolidDef.java | 8 +-- src/main/java/fr/umlv/java/wallj/board/Board.java | 64 +++++++++---------- .../java/fr/umlv/java/wallj/board/BoardParser.java | 8 +-- .../fr/umlv/java/wallj/board/BoardValidator.java | 74 +++++++++++----------- src/main/java/fr/umlv/java/wallj/board/Matrix.java | 8 +-- .../java/fr/umlv/java/wallj/board/PathFinder.java | 61 +++++++++--------- .../java/fr/umlv/java/wallj/board/TileVec2.java | 13 ++-- src/main/java/fr/umlv/java/wallj/context/Game.java | 2 +- .../java/fr/umlv/java/wallj/context/Stage.java | 16 ++--- .../fr/umlv/java/wallj/context/Updateables.java | 8 +-- src/main/java/fr/umlv/java/wallj/event/Events.java | 8 +-- src/main/java/fr/umlv/java/wallj/viewer/Main.java | 8 +-- 13 files changed, 142 insertions(+), 144 deletions(-) diff --git a/src/main/java/fr/umlv/java/wallj/block/BlockFactory.java b/src/main/java/fr/umlv/java/wallj/block/BlockFactory.java index 6911c88..411696d 100644 --- a/src/main/java/fr/umlv/java/wallj/block/BlockFactory.java +++ b/src/main/java/fr/umlv/java/wallj/block/BlockFactory.java @@ -9,6 +9,10 @@ import org.jbox2d.common.Vec2; * @author Pacien TRAN-GIRARD */ public final class BlockFactory { + private BlockFactory() { + // static class + } + private static Block forType(BlockType t, Vec2 pos) { switch (t) { case WALL: @@ -35,8 +39,4 @@ public final class BlockFactory { public static Block build(BlockType type, TileVec2 pos) { return forType(type, pos.toVec2()); } - - private BlockFactory() { - // static class - } } diff --git a/src/main/java/fr/umlv/java/wallj/block/SolidDef.java b/src/main/java/fr/umlv/java/wallj/block/SolidDef.java index 6fb7dc3..25ff022 100644 --- a/src/main/java/fr/umlv/java/wallj/block/SolidDef.java +++ b/src/main/java/fr/umlv/java/wallj/block/SolidDef.java @@ -13,6 +13,10 @@ import org.jbox2d.dynamics.FixtureDef; * @author Pacien TRAN-GIRARD */ public final class SolidDef { + private SolidDef() { + // static class + } + /** * @param bodyType type of body * @param pos initial position of the body @@ -53,8 +57,4 @@ public final class SolidDef { shape.m_radius = TileVec2.TILE_DIM / 2; return shape; } - - private SolidDef() { - // static class - } } diff --git a/src/main/java/fr/umlv/java/wallj/board/Board.java b/src/main/java/fr/umlv/java/wallj/board/Board.java index 2e67c53..077dd26 100644 --- a/src/main/java/fr/umlv/java/wallj/board/Board.java +++ b/src/main/java/fr/umlv/java/wallj/board/Board.java @@ -15,38 +15,6 @@ import java.util.stream.Stream; */ public final class Board { - /** - * Board Builder - */ - public static final class Builder { - private final BlockType[][] map; - - /** - * @param width width in tiles - * @param height height in tiles - */ - public Builder(int width, int height) { - map = new BlockType[height][width]; - } - - /** - * @param pos the tile position vector - * @param type the BlockType to set - * @return the Builder - */ - public Builder setBlockTypeAt(TileVec2 pos, BlockType type) { - map[pos.getRow()][pos.getCol()] = type; - return this; - } - - /** - * @return the immutable Board - */ - public Board build() { - return new Board(map); - } - } - private final BlockType[][] map; private Board(BlockType[][] map) { @@ -103,4 +71,36 @@ public final class Board { return Arrays.hashCode(map); } + /** + * Board Builder + */ + public static final class Builder { + private final BlockType[][] map; + + /** + * @param width width in tiles + * @param height height in tiles + */ + public Builder(int width, int height) { + map = new BlockType[height][width]; + } + + /** + * @param pos the tile position vector + * @param type the BlockType to set + * @return the Builder + */ + public Builder setBlockTypeAt(TileVec2 pos, BlockType type) { + map[pos.getRow()][pos.getCol()] = type; + return this; + } + + /** + * @return the immutable Board + */ + public Board build() { + return new Board(map); + } + } + } diff --git a/src/main/java/fr/umlv/java/wallj/board/BoardParser.java b/src/main/java/fr/umlv/java/wallj/board/BoardParser.java index 90fd9c2..71bdc51 100644 --- a/src/main/java/fr/umlv/java/wallj/board/BoardParser.java +++ b/src/main/java/fr/umlv/java/wallj/board/BoardParser.java @@ -15,6 +15,10 @@ import java.util.stream.Collectors; * @author Pacien TRAN-GIRARD */ public final class BoardParser { + private BoardParser() { + // static class + } + private static Board buildBoard(List> map) { if (!Matrix.isShapeValid(map)) throw new IllegalArgumentException("Board must be rectangular."); @@ -60,8 +64,4 @@ public final class BoardParser { .map(BoardParser::parseLine) .collect(Collectors.toList())); } - - private BoardParser() { - // static class - } } diff --git a/src/main/java/fr/umlv/java/wallj/board/BoardValidator.java b/src/main/java/fr/umlv/java/wallj/board/BoardValidator.java index 011ca8b..d7d3583 100644 --- a/src/main/java/fr/umlv/java/wallj/board/BoardValidator.java +++ b/src/main/java/fr/umlv/java/wallj/board/BoardValidator.java @@ -13,6 +13,39 @@ import java.util.stream.IntStream; */ public class BoardValidator { + private final Board board; + private final ValidationException errors = new ValidationException(); + + /** + * @param board the board to validate + */ + public BoardValidator(Board board) { + this.board = board; + } + + /** + * Tests the board against a given validator, using the supplied error message if the validation fails. + * + * @param validator a validity test + * @param msg a failure message + * @return the board validator + */ + public BoardValidator validate(Predicate validator, String msg) { + if (!validator.test(board)) errors.addSuppressed(new ValidationException(msg)); + return this; + } + + /** + * @return the validated board + * @throws ValidationException in case of failure + */ + public Board get() throws ValidationException { + if (errors.getSuppressed().length > 0) + throw errors; + else + return board; + } + /** * A validation exception, witness of validation error(s). */ @@ -33,6 +66,10 @@ public class BoardValidator { public static final int NB_DROPPABLE_BOMBS = 3; + private Constraint() { + // static class + } + private static boolean inBoard(TileVec2 dim, TileVec2 v) { return v.getRow() >= 0 && v.getRow() < dim.getRow() && v.getCol() >= 0 && v.getRow() < dim.getCol(); @@ -104,43 +141,6 @@ public class BoardValidator { b.stream().filter(block -> block.getValue() == BlockType.FREE).count() >= NB_DROPPABLE_BOMBS; } - private Constraint() { - // static class - } - - } - - private final Board board; - private final ValidationException errors = new ValidationException(); - - /** - * @param board the board to validate - */ - public BoardValidator(Board board) { - this.board = board; - } - - /** - * Tests the board against a given validator, using the supplied error message if the validation fails. - * - * @param validator a validity test - * @param msg a failure message - * @return the board validator - */ - public BoardValidator validate(Predicate validator, String msg) { - if (!validator.test(board)) errors.addSuppressed(new ValidationException(msg)); - return this; - } - - /** - * @return the validated board - * @throws ValidationException in case of failure - */ - public Board get() throws ValidationException { - if (errors.getSuppressed().length > 0) - throw errors; - else - return board; } } diff --git a/src/main/java/fr/umlv/java/wallj/board/Matrix.java b/src/main/java/fr/umlv/java/wallj/board/Matrix.java index c6a2528..27c17f2 100644 --- a/src/main/java/fr/umlv/java/wallj/board/Matrix.java +++ b/src/main/java/fr/umlv/java/wallj/board/Matrix.java @@ -8,6 +8,10 @@ import java.util.List; * @author Pacien TRAN-GIRARD */ public final class Matrix { + private Matrix() { + // static class + } + /** * @param m the matrix (2D array) * @return the width of the matrix (0 if null) @@ -47,8 +51,4 @@ public final class Matrix { public static boolean isShapeValid(List> l) { return l != null && l.stream().mapToInt(List::size).allMatch(s -> s == l.get(0).size()); } - - private Matrix() { - // static class - } } diff --git a/src/main/java/fr/umlv/java/wallj/board/PathFinder.java b/src/main/java/fr/umlv/java/wallj/board/PathFinder.java index 098b4a2..6bf12e9 100644 --- a/src/main/java/fr/umlv/java/wallj/board/PathFinder.java +++ b/src/main/java/fr/umlv/java/wallj/board/PathFinder.java @@ -11,26 +11,16 @@ import java.util.stream.Collectors; */ public class PathFinder { private static final int LEAP_COST = 1; + private final Map> graph; - private static class Node { - final T val; - final Map, Integer> neighbors; - - Node(T val) { - this.val = val; - this.neighbors = new HashMap<>(); - } - } - - private static class NodeSearchData { - final Node predecessor; - final double actualCost, estimatedCost; - - NodeSearchData(Node predecessor, double actualCost, double estimatedCost) { - this.predecessor = predecessor; - this.actualCost = actualCost; - this.estimatedCost = estimatedCost; - } + /** + * Builds a new path finder for the supplied board. + * A well-build (validated) board should be fully connected. + * + * @param board the board + */ + public PathFinder(Board board) { + graph = buildGraph(Objects.requireNonNull(board)); } private static double euclideanDistance(TileVec2 a, TileVec2 b) { @@ -101,18 +91,6 @@ public class PathFinder { return map; } - private final Map> graph; - - /** - * Builds a new path finder for the supplied board. - * A well-build (validated) board should be fully connected. - * - * @param board the board - */ - public PathFinder(Board board) { - graph = buildGraph(Objects.requireNonNull(board)); - } - /** * Returns a path from a starting point to a target if it exists, * or throw an IllegalArgumentException if any of the given coordinates are invalid. @@ -128,4 +106,25 @@ public class PathFinder { if (startNode == null) throw new IllegalArgumentException("Invalid starting point."); return findPath(startNode, target, PathFinder::euclideanDistance); } + + private static class Node { + final T val; + final Map, Integer> neighbors; + + Node(T val) { + this.val = val; + this.neighbors = new HashMap<>(); + } + } + + private static class NodeSearchData { + final Node predecessor; + final double actualCost, estimatedCost; + + NodeSearchData(Node predecessor, double actualCost, double estimatedCost) { + this.predecessor = predecessor; + this.actualCost = actualCost; + this.estimatedCost = estimatedCost; + } + } } diff --git a/src/main/java/fr/umlv/java/wallj/board/TileVec2.java b/src/main/java/fr/umlv/java/wallj/board/TileVec2.java index 26deb0c..8d713d5 100644 --- a/src/main/java/fr/umlv/java/wallj/board/TileVec2.java +++ b/src/main/java/fr/umlv/java/wallj/board/TileVec2.java @@ -19,6 +19,12 @@ public final class TileVec2 { of(-1, 0), of(0, 1), of(1, 0)); + private final int col, row; + + private TileVec2(int col, int row) { + this.col = col; + this.row = row; + } /** * @param col the column @@ -37,13 +43,6 @@ public final class TileVec2 { return new TileVec2((int) (v.x / TILE_DIM), (int) (v.y / TILE_DIM)); } - private final int col, row; - - private TileVec2(int col, int row) { - this.col = col; - this.row = row; - } - /** * @return the column */ diff --git a/src/main/java/fr/umlv/java/wallj/context/Game.java b/src/main/java/fr/umlv/java/wallj/context/Game.java index 7d09b26..46fee8a 100644 --- a/src/main/java/fr/umlv/java/wallj/context/Game.java +++ b/src/main/java/fr/umlv/java/wallj/context/Game.java @@ -17,9 +17,9 @@ import java.util.stream.Stream; * @author Adam NAILI */ public final class Game implements Updateable { + private final List boards; private Stage currentStage; private int indexBoard; - private final List boards; private boolean over; /** diff --git a/src/main/java/fr/umlv/java/wallj/context/Stage.java b/src/main/java/fr/umlv/java/wallj/context/Stage.java index 9b66e03..903b5de 100644 --- a/src/main/java/fr/umlv/java/wallj/context/Stage.java +++ b/src/main/java/fr/umlv/java/wallj/context/Stage.java @@ -36,6 +36,14 @@ public class Stage implements Updateable { blocks.forEach(block -> block.link(world)); } + private static TileVec2 findAnyFreeTile(Board board) { + return board.stream() + .filter(entry -> entry.getValue() == BlockType.FREE) + .findAny() + .map(Map.Entry::getKey) + .orElseThrow(IllegalArgumentException::new); + } + /** * @return the JBox2D world */ @@ -126,12 +134,4 @@ public class Stage implements Updateable { .filter(block -> block.getType() == BlockType.BOMB) .count() == BOMB_PLACEMENTS; } - - private static TileVec2 findAnyFreeTile(Board board) { - return board.stream() - .filter(entry -> entry.getValue() == BlockType.FREE) - .findAny() - .map(Map.Entry::getKey) - .orElseThrow(IllegalArgumentException::new); - } } diff --git a/src/main/java/fr/umlv/java/wallj/context/Updateables.java b/src/main/java/fr/umlv/java/wallj/context/Updateables.java index f2aae6a..8f66c93 100644 --- a/src/main/java/fr/umlv/java/wallj/context/Updateables.java +++ b/src/main/java/fr/umlv/java/wallj/context/Updateables.java @@ -12,6 +12,10 @@ import java.util.stream.Stream; * @author Pacien TRAN-GIRARD */ public final class Updateables { + private Updateables() { + // static class + } + /** * @param the updateable type * @param context an update context @@ -33,8 +37,4 @@ public final class Updateables { public static Stream updateAll(Context context, T... updateables) { return updateAll(context, Arrays.asList(updateables)); } - - private Updateables() { - // static class - } } diff --git a/src/main/java/fr/umlv/java/wallj/event/Events.java b/src/main/java/fr/umlv/java/wallj/event/Events.java index b7166b0..7849263 100644 --- a/src/main/java/fr/umlv/java/wallj/event/Events.java +++ b/src/main/java/fr/umlv/java/wallj/event/Events.java @@ -10,6 +10,10 @@ import java.util.stream.Stream; * @author Pacien TRAN-GIRARD */ public final class Events { + private Events() { + // static class + } + /** * @param eventList list of events to filter * @param eventClass event class to keep @@ -31,8 +35,4 @@ public final class Events { public static Optional findFirst(List eventList, Class eventClass) { return filter(eventList, eventClass).findFirst(); } - - private Events() { - // static class - } } diff --git a/src/main/java/fr/umlv/java/wallj/viewer/Main.java b/src/main/java/fr/umlv/java/wallj/viewer/Main.java index 0411e37..d0796af 100644 --- a/src/main/java/fr/umlv/java/wallj/viewer/Main.java +++ b/src/main/java/fr/umlv/java/wallj/viewer/Main.java @@ -30,6 +30,10 @@ public final class Main { private static final String MAP_FILE_PATTERN = "level*.txt"; private static final String JAR_SCHEME = "jar"; + private Main() { + // static class + } + private static Path getMapDirPath() { try { if (System.getProperty(MAP_DIR_KEY) != null) { @@ -89,8 +93,4 @@ public final class Main { Application.run(Viewer.BACKGROUND_COLOR, appContext -> (new Viewer(appContext, levels)).run()); } - - private Main() { - // static class - } } -- cgit v1.2.3