From 211d6a6042d139ab5f3c770aa5ac695a062232c8 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 21 Nov 2015 15:37:55 +0100 Subject: Implement World non-abstract behaviours --- src/ch/epfl/maze/physical/World.java | 75 ++++++++++++++++++++++++++++-------- src/ch/epfl/maze/util/Direction.java | 13 +++++++ 2 files changed, 72 insertions(+), 16 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/maze/physical/World.java b/src/ch/epfl/maze/physical/World.java index d6ac1de..7e89fb5 100644 --- a/src/ch/epfl/maze/physical/World.java +++ b/src/ch/epfl/maze/physical/World.java @@ -3,11 +3,14 @@ package ch.epfl.maze.physical; import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; +import java.util.ArrayList; import java.util.List; /** * World that is represented by a labyrinth of tiles in which an {@code Animal} * can move. + * + * @author Pacien TRAN-GIRARD */ public abstract class World { @@ -19,6 +22,15 @@ public abstract class World { public static final int EXIT = 3; public static final int NOTHING = -1; + /** + * Structure of the labyrinth, an NxM array of tiles + */ + + private final int[][] labyrinth; + + private final Vector2D start; + private final Vector2D exit; + /** * Constructs a new world with a labyrinth. The labyrinth must be rectangle. * @@ -26,7 +38,26 @@ public abstract class World { */ public World(int[][] labyrinth) { - // TODO + this.labyrinth = labyrinth; + + this.start = this.findFirstTileOfType(World.START); + this.exit = this.findFirstTileOfType(World.EXIT); + } + + /** + * Finds the coordinates of the first occurrence of the given tile type. + * + * @param tileType Type of the tile + * @return A Vector2D of the first occurrence of the given tile type + */ + + private Vector2D findFirstTileOfType(int tileType) { + for (int x = 0; x < this.getWidth(); ++x) + for (int y = 0; y < this.getHeight(); ++y) + if (this.getTile(x, y) == tileType) + return new Vector2D(x, y); + + return null; } /** @@ -56,13 +87,14 @@ public abstract class World { * * @param x Horizontal coordinate * @param y Vertical coordinate - * @return The tile number at position (x, y), or the NONE tile if x or y is + * @return The tile number at position (x, y), or the NOTHING tile if x or y is * incorrect. */ public final int getTile(int x, int y) { - // TODO - return 0; + if (x < 0 || x >= this.getWidth()) return World.NOTHING; + if (y < 0 || y >= this.getHeight()) return World.NOTHING; + return this.labyrinth[y][x]; } /** @@ -74,8 +106,19 @@ public abstract class World { */ public final boolean isFree(int x, int y) { - // TODO - return false; + int tile = this.getTile(x, y); + return !(tile == World.WALL || tile == World.NOTHING); + } + + /** + * Determines if coordinates are free to walk on. + * + * @param position The position vector + * @return true if an animal can walk on tile, false otherwise + */ + + public final boolean isFree(Vector2D position) { + return this.isFree(position.getX(), position.getY()); } /** @@ -88,8 +131,12 @@ public abstract class World { */ public final Direction[] getChoices(Vector2D position) { - // TODO - return null; + ArrayList choices = new ArrayList<>(); + for (Direction dir : Direction.POSSIBLE_DIRECTIONS) + if (this.isFree(position.addDirectionTo(dir))) + choices.add(dir); + + return choices.isEmpty() ? new Direction[]{Direction.NONE} : choices.toArray(new Direction[choices.size()]); } /** @@ -99,8 +146,7 @@ public abstract class World { */ public final int getWidth() { - // TODO - return 0; + return this.labyrinth[0].length; } /** @@ -110,8 +156,7 @@ public abstract class World { */ public final int getHeight() { - // TODO - return 0; + return this.labyrinth.length; } /** @@ -122,8 +167,7 @@ public abstract class World { */ public final Vector2D getStart() { - // TODO - return null; + return this.start; } /** @@ -133,7 +177,6 @@ public abstract class World { */ public final Vector2D getExit() { - // TODO - return null; + return this.exit; } } diff --git a/src/ch/epfl/maze/util/Direction.java b/src/ch/epfl/maze/util/Direction.java index e8c0993..5f83c22 100644 --- a/src/ch/epfl/maze/util/Direction.java +++ b/src/ch/epfl/maze/util/Direction.java @@ -4,11 +4,24 @@ package ch.epfl.maze.util; * Directions that an animal can take to move. They represent the four cardinal * points ({@code DOWN, UP, RIGHT, LEFT}) from the frame of reference of the * labyrinth, plus a default one : {@code NONE}. + * + * @author Pacien TRAN-GIRARD */ public enum Direction { DOWN, UP, RIGHT, LEFT, NONE; + /** + * An array of all the possible directions that can be taken. + */ + + public static final Direction[] POSSIBLE_DIRECTIONS = new Direction[]{ + Direction.DOWN, + Direction.UP, + Direction.RIGHT, + Direction.LEFT + }; + /** * Returns the integer value of the direction * -- cgit v1.2.3