summaryrefslogtreecommitdiff
path: root/src/ch
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch')
-rw-r--r--src/ch/epfl/maze/physical/World.java75
-rw-r--r--src/ch/epfl/maze/util/Direction.java13
2 files changed, 72 insertions, 16 deletions
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;
3import ch.epfl.maze.util.Direction; 3import ch.epfl.maze.util.Direction;
4import ch.epfl.maze.util.Vector2D; 4import ch.epfl.maze.util.Vector2D;
5 5
6import java.util.ArrayList;
6import java.util.List; 7import java.util.List;
7 8
8/** 9/**
9 * World that is represented by a labyrinth of tiles in which an {@code Animal} 10 * World that is represented by a labyrinth of tiles in which an {@code Animal}
10 * can move. 11 * can move.
12 *
13 * @author Pacien TRAN-GIRARD
11 */ 14 */
12 15
13public abstract class World { 16public abstract class World {
@@ -20,13 +23,41 @@ public abstract class World {
20 public static final int NOTHING = -1; 23 public static final int NOTHING = -1;
21 24
22 /** 25 /**
26 * Structure of the labyrinth, an NxM array of tiles
27 */
28
29 private final int[][] labyrinth;
30
31 private final Vector2D start;
32 private final Vector2D exit;
33
34 /**
23 * Constructs a new world with a labyrinth. The labyrinth must be rectangle. 35 * Constructs a new world with a labyrinth. The labyrinth must be rectangle.
24 * 36 *
25 * @param labyrinth Structure of the labyrinth, an NxM array of tiles 37 * @param labyrinth Structure of the labyrinth, an NxM array of tiles
26 */ 38 */
27 39
28 public World(int[][] labyrinth) { 40 public World(int[][] labyrinth) {
29 // TODO 41 this.labyrinth = labyrinth;
42
43 this.start = this.findFirstTileOfType(World.START);
44 this.exit = this.findFirstTileOfType(World.EXIT);
45 }
46
47 /**
48 * Finds the coordinates of the first occurrence of the given tile type.
49 *
50 * @param tileType Type of the tile
51 * @return A Vector2D of the first occurrence of the given tile type
52 */
53
54 private Vector2D findFirstTileOfType(int tileType) {
55 for (int x = 0; x < this.getWidth(); ++x)
56 for (int y = 0; y < this.getHeight(); ++y)
57 if (this.getTile(x, y) == tileType)
58 return new Vector2D(x, y);
59
60 return null;
30 } 61 }
31 62
32 /** 63 /**
@@ -56,13 +87,14 @@ public abstract class World {
56 * 87 *
57 * @param x Horizontal coordinate 88 * @param x Horizontal coordinate
58 * @param y Vertical coordinate 89 * @param y Vertical coordinate
59 * @return The tile number at position (x, y), or the NONE tile if x or y is 90 * @return The tile number at position (x, y), or the NOTHING tile if x or y is
60 * incorrect. 91 * incorrect.
61 */ 92 */
62 93
63 public final int getTile(int x, int y) { 94 public final int getTile(int x, int y) {
64 // TODO 95 if (x < 0 || x >= this.getWidth()) return World.NOTHING;
65 return 0; 96 if (y < 0 || y >= this.getHeight()) return World.NOTHING;
97 return this.labyrinth[y][x];
66 } 98 }
67 99
68 /** 100 /**
@@ -74,8 +106,19 @@ public abstract class World {
74 */ 106 */
75 107
76 public final boolean isFree(int x, int y) { 108 public final boolean isFree(int x, int y) {
77 // TODO 109 int tile = this.getTile(x, y);
78 return false; 110 return !(tile == World.WALL || tile == World.NOTHING);
111 }
112
113 /**
114 * Determines if coordinates are free to walk on.
115 *
116 * @param position The position vector
117 * @return <b>true</b> if an animal can walk on tile, <b>false</b> otherwise
118 */
119
120 public final boolean isFree(Vector2D position) {
121 return this.isFree(position.getX(), position.getY());
79 } 122 }
80 123
81 /** 124 /**
@@ -88,8 +131,12 @@ public abstract class World {
88 */ 131 */
89 132
90 public final Direction[] getChoices(Vector2D position) { 133 public final Direction[] getChoices(Vector2D position) {
91 // TODO 134 ArrayList<Direction> choices = new ArrayList<>();
92 return null; 135 for (Direction dir : Direction.POSSIBLE_DIRECTIONS)
136 if (this.isFree(position.addDirectionTo(dir)))
137 choices.add(dir);
138
139 return choices.isEmpty() ? new Direction[]{Direction.NONE} : choices.toArray(new Direction[choices.size()]);
93 } 140 }
94 141
95 /** 142 /**
@@ -99,8 +146,7 @@ public abstract class World {
99 */ 146 */
100 147
101 public final int getWidth() { 148 public final int getWidth() {
102 // TODO 149 return this.labyrinth[0].length;
103 return 0;
104 } 150 }
105 151
106 /** 152 /**
@@ -110,8 +156,7 @@ public abstract class World {
110 */ 156 */
111 157
112 public final int getHeight() { 158 public final int getHeight() {
113 // TODO 159 return this.labyrinth.length;
114 return 0;
115 } 160 }
116 161
117 /** 162 /**
@@ -122,8 +167,7 @@ public abstract class World {
122 */ 167 */
123 168
124 public final Vector2D getStart() { 169 public final Vector2D getStart() {
125 // TODO 170 return this.start;
126 return null;
127 } 171 }
128 172
129 /** 173 /**
@@ -133,7 +177,6 @@ public abstract class World {
133 */ 177 */
134 178
135 public final Vector2D getExit() { 179 public final Vector2D getExit() {
136 // TODO 180 return this.exit;
137 return null;
138 } 181 }
139} 182}
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,12 +4,25 @@ package ch.epfl.maze.util;
4 * Directions that an animal can take to move. They represent the four cardinal 4 * Directions that an animal can take to move. They represent the four cardinal
5 * points ({@code DOWN, UP, RIGHT, LEFT}) from the frame of reference of the 5 * points ({@code DOWN, UP, RIGHT, LEFT}) from the frame of reference of the
6 * labyrinth, plus a default one : {@code NONE}. 6 * labyrinth, plus a default one : {@code NONE}.
7 *
8 * @author Pacien TRAN-GIRARD
7 */ 9 */
8 10
9public enum Direction { 11public enum Direction {
10 DOWN, UP, RIGHT, LEFT, NONE; 12 DOWN, UP, RIGHT, LEFT, NONE;
11 13
12 /** 14 /**
15 * An array of all the possible directions that can be taken.
16 */
17
18 public static final Direction[] POSSIBLE_DIRECTIONS = new Direction[]{
19 Direction.DOWN,
20 Direction.UP,
21 Direction.RIGHT,
22 Direction.LEFT
23 };
24
25 /**
13 * Returns the integer value of the direction 26 * Returns the integer value of the direction
14 * 27 *
15 * @return Integer value of the direction 28 * @return Integer value of the direction