diff options
author | Pacien TRAN-GIRARD | 2015-11-21 10:36:18 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-11-21 10:36:18 +0100 |
commit | 655ac88f4e73b2df532a451aedf5a561ea1b0d2c (patch) | |
tree | ef6f914a465575f313e2b280bf0639d87a4cbd58 /src/ch/epfl/maze/util | |
parent | 56279eb59ccdea48b18daa027a5095d861b4e2f4 (diff) | |
download | maze-solver-655ac88f4e73b2df532a451aedf5a561ea1b0d2c.tar.gz |
Import project structure
Diffstat (limited to 'src/ch/epfl/maze/util')
-rw-r--r-- | src/ch/epfl/maze/util/Action.java | 101 | ||||
-rw-r--r-- | src/ch/epfl/maze/util/Direction.java | 235 | ||||
-rw-r--r-- | src/ch/epfl/maze/util/LabyrinthGenerator.java | 533 | ||||
-rw-r--r-- | src/ch/epfl/maze/util/Statistics.java | 197 | ||||
-rw-r--r-- | src/ch/epfl/maze/util/Vector2D.java | 228 |
5 files changed, 1294 insertions, 0 deletions
diff --git a/src/ch/epfl/maze/util/Action.java b/src/ch/epfl/maze/util/Action.java new file mode 100644 index 0000000..491627f --- /dev/null +++ b/src/ch/epfl/maze/util/Action.java | |||
@@ -0,0 +1,101 @@ | |||
1 | package ch.epfl.maze.util; | ||
2 | |||
3 | /** | ||
4 | * Immutable action that encapsulates a choice made by an animal and information | ||
5 | * about it, such as if it was successful or not, and if the animal will die | ||
6 | * <i>while</i> performing it. | ||
7 | * | ||
8 | */ | ||
9 | |||
10 | public final class Action { | ||
11 | |||
12 | /* variables defining the action */ | ||
13 | private final Direction mDirection; | ||
14 | private final boolean mSuccess; | ||
15 | private final boolean mDies; | ||
16 | |||
17 | /** | ||
18 | * Constructs a successful action of an animal towards a specified | ||
19 | * direction, that does not die between two squares. | ||
20 | * | ||
21 | * @param dir | ||
22 | * Direction towards which the action needs to be performed | ||
23 | */ | ||
24 | |||
25 | public Action(Direction dir) { | ||
26 | if (dir != null) { | ||
27 | mDirection = dir; | ||
28 | } else { | ||
29 | mDirection = Direction.NONE; | ||
30 | } | ||
31 | mSuccess = true; | ||
32 | mDies = false; | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * Constructs an action of an animal towards a specified direction, with a | ||
37 | * specified success, that does not die between two squares. | ||
38 | * | ||
39 | * @param dir | ||
40 | * Direction towards which the action needs to be performed | ||
41 | * @param successful | ||
42 | * Determines whether this action was successful | ||
43 | */ | ||
44 | |||
45 | public Action(Direction dir, boolean successful) { | ||
46 | mDirection = dir; | ||
47 | mSuccess = successful; | ||
48 | mDies = false; | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * Constructs an action of an animal towards a specified direction, with a | ||
53 | * specified success, and that also specifies if the animal dies between two | ||
54 | * squares. | ||
55 | * | ||
56 | * @param dir | ||
57 | * Direction towards which the action needs to be performed | ||
58 | * @param successful | ||
59 | * Determines whether this action was successful | ||
60 | * @param dies | ||
61 | * Determines whether the action will die between two squares | ||
62 | */ | ||
63 | |||
64 | public Action(Direction dir, boolean successful, boolean dies) { | ||
65 | mDirection = dir; | ||
66 | mSuccess = successful; | ||
67 | mDies = dies; | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * Retrieves the direction towards which the action shall move. | ||
72 | * | ||
73 | * @return Direction of the action | ||
74 | */ | ||
75 | |||
76 | public Direction getDirection() { | ||
77 | return mDirection; | ||
78 | } | ||
79 | |||
80 | /** | ||
81 | * Determines if the action was successful or not. | ||
82 | * | ||
83 | * @return <b>true</b> if the action was successful, <b>false</b> otherwise | ||
84 | */ | ||
85 | |||
86 | public boolean isSuccessful() { | ||
87 | return mSuccess; | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Determines if the animal performing the action dies while moving from a | ||
92 | * square to another. | ||
93 | * | ||
94 | * @return <b>true</b> if the action dies between two squares, <b>false</b> | ||
95 | * otherwise | ||
96 | */ | ||
97 | |||
98 | public boolean diesBetweenSquares() { | ||
99 | return mDies; | ||
100 | } | ||
101 | } | ||
diff --git a/src/ch/epfl/maze/util/Direction.java b/src/ch/epfl/maze/util/Direction.java new file mode 100644 index 0000000..a75ba7f --- /dev/null +++ b/src/ch/epfl/maze/util/Direction.java | |||
@@ -0,0 +1,235 @@ | |||
1 | package ch.epfl.maze.util; | ||
2 | |||
3 | /** | ||
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 | ||
6 | * labyrinth, plus a default one : {@code NONE}. | ||
7 | * | ||
8 | */ | ||
9 | |||
10 | public enum Direction { | ||
11 | DOWN, UP, RIGHT, LEFT, NONE; | ||
12 | |||
13 | /** | ||
14 | * Returns the integer value of the direction | ||
15 | * | ||
16 | * @return Integer value of the direction | ||
17 | */ | ||
18 | |||
19 | public int intValue() { | ||
20 | switch (this) { | ||
21 | case DOWN: | ||
22 | return 0; | ||
23 | |||
24 | case UP: | ||
25 | return 1; | ||
26 | |||
27 | case RIGHT: | ||
28 | return 2; | ||
29 | |||
30 | case LEFT: | ||
31 | return 3; | ||
32 | |||
33 | case NONE: | ||
34 | default: | ||
35 | return 4; | ||
36 | } | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * Converts the direction into an orthonormal vector, when possible. | ||
41 | * | ||
42 | * @return Orthonormal {@code Vector2D} that represents the direction. | ||
43 | */ | ||
44 | |||
45 | public Vector2D toVector() { | ||
46 | switch (this) { | ||
47 | case DOWN: | ||
48 | return new Vector2D(0, 1); | ||
49 | |||
50 | case UP: | ||
51 | return new Vector2D(0, -1); | ||
52 | |||
53 | case RIGHT: | ||
54 | return new Vector2D(1, 0); | ||
55 | |||
56 | case LEFT: | ||
57 | return new Vector2D(-1, 0); | ||
58 | |||
59 | case NONE: | ||
60 | default: | ||
61 | return new Vector2D(0, 0); | ||
62 | } | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Reverses the direction. | ||
67 | * | ||
68 | * @return The opposite direction. | ||
69 | */ | ||
70 | |||
71 | public Direction reverse() { | ||
72 | switch (this) { | ||
73 | case DOWN: | ||
74 | return UP; | ||
75 | |||
76 | case UP: | ||
77 | return DOWN; | ||
78 | |||
79 | case RIGHT: | ||
80 | return LEFT; | ||
81 | |||
82 | case LEFT: | ||
83 | return RIGHT; | ||
84 | |||
85 | case NONE: | ||
86 | default: | ||
87 | return NONE; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | /** | ||
92 | * Determines whether the argument is the opposite of another. | ||
93 | * | ||
94 | * @param d | ||
95 | * The direction to compare with | ||
96 | * @return <b>true</b> if the direction is the opposite the argument, | ||
97 | * <b>false</b> otherwise | ||
98 | */ | ||
99 | |||
100 | public boolean isOpposite(Direction d) { | ||
101 | return this == d.reverse(); | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * Converts the argument relative to the frame of reference given by the | ||
106 | * direction that calls the method. | ||
107 | * | ||
108 | * @param dir | ||
109 | * The direction to convert | ||
110 | * @return The direction converted to the frame of reference given by the | ||
111 | * direction called. | ||
112 | */ | ||
113 | |||
114 | public Direction relativeDirection(Direction dir) { | ||
115 | switch (this) { | ||
116 | case DOWN: | ||
117 | return dir.reverse(); | ||
118 | |||
119 | case UP: | ||
120 | return dir; | ||
121 | |||
122 | case RIGHT: | ||