summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/maze')
-rw-r--r--src/ch/epfl/maze/graphics/Animation.java245
-rw-r--r--src/ch/epfl/maze/graphics/Display.java543
-rw-r--r--src/ch/epfl/maze/graphics/GraphicComponent.java237
-rw-r--r--src/ch/epfl/maze/main/Console.java162
-rw-r--r--src/ch/epfl/maze/main/Program.java106
-rw-r--r--src/ch/epfl/maze/physical/Animal.java77
-rw-r--r--src/ch/epfl/maze/physical/Daedalus.java136
-rw-r--r--src/ch/epfl/maze/physical/Maze.java79
-rw-r--r--src/ch/epfl/maze/physical/Predator.java59
-rw-r--r--src/ch/epfl/maze/physical/Prey.java55
-rw-r--r--src/ch/epfl/maze/physical/World.java146
-rw-r--r--src/ch/epfl/maze/physical/pacman/Blinky.java39
-rw-r--r--src/ch/epfl/maze/physical/pacman/Clyde.java40
-rw-r--r--src/ch/epfl/maze/physical/pacman/Inky.java40
-rw-r--r--src/ch/epfl/maze/physical/pacman/PacMan.java32
-rw-r--r--src/ch/epfl/maze/physical/pacman/Pinky.java39
-rw-r--r--src/ch/epfl/maze/physical/zoo/Bear.java46
-rw-r--r--src/ch/epfl/maze/physical/zoo/Hamster.java43
-rw-r--r--src/ch/epfl/maze/physical/zoo/Monkey.java41
-rw-r--r--src/ch/epfl/maze/physical/zoo/Mouse.java41
-rw-r--r--src/ch/epfl/maze/physical/zoo/Panda.java43
-rw-r--r--src/ch/epfl/maze/physical/zoo/SpaceInvader.java52
-rw-r--r--src/ch/epfl/maze/simulation/DaedalusSimulation.java364
-rw-r--r--src/ch/epfl/maze/simulation/MazeSimulation.java211
-rw-r--r--src/ch/epfl/maze/simulation/Simulation.java84
-rw-r--r--src/ch/epfl/maze/tests/AnimalTest.java100
-rw-r--r--src/ch/epfl/maze/tests/Competition.java163
-rw-r--r--src/ch/epfl/maze/tests/DaedalusTest.java123
-rw-r--r--src/ch/epfl/maze/tests/GhostsTest.java149
-rw-r--r--src/ch/epfl/maze/tests/MazeTest.java91
-rw-r--r--src/ch/epfl/maze/tests/WorldTest.java330
-rw-r--r--src/ch/epfl/maze/tests/ZooTest.java209
-rw-r--r--src/ch/epfl/maze/util/Action.java101
-rw-r--r--src/ch/epfl/maze/util/Direction.java235
-rw-r--r--src/ch/epfl/maze/util/LabyrinthGenerator.java533
-rw-r--r--src/ch/epfl/maze/util/Statistics.java197
-rw-r--r--src/ch/epfl/maze/util/Vector2D.java228
37 files changed, 5419 insertions, 0 deletions
diff --git a/src/ch/epfl/maze/graphics/Animation.java b/src/ch/epfl/maze/graphics/Animation.java
new file mode 100644
index 0000000..0502a92
--- /dev/null
+++ b/src/ch/epfl/maze/graphics/Animation.java
@@ -0,0 +1,245 @@
1package ch.epfl.maze.graphics;
2
3import java.awt.Graphics2D;
4import java.awt.image.BufferedImage;
5import java.awt.image.ImageObserver;
6import java.io.File;
7import java.io.IOException;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11import java.util.TreeMap;
12
13import javax.imageio.ImageIO;
14
15import ch.epfl.maze.physical.Animal;
16import ch.epfl.maze.util.Action;
17import ch.epfl.maze.util.Direction;
18import ch.epfl.maze.util.Vector2D;
19
20/**
21 * Handles the animation of a {@code Simulation} by extrapolating the positions
22 * of animals.
23 *
24 */
25
26public final class Animation {
27
28 /** Default number of waiting frames to display when animation is aborting. */
29 public static final int DEFAULT_WAITING_FRAMES = 2;
30
31 /** Maps animals identity to graphical components that will be animated. */
32 private Map<Integer, GraphicComponent> mGraphMap;
33
34 /** Buffer of images of animals. Key format: "superclass.class" */
35 private Map<String, BufferedImage> mImages;
36
37 /** Drawing ratio variable. */
38 private float mRatio;
39
40 /** Control variable. */
41 private boolean mDone;
42
43 /** Current number of waiting frames, to prevent screen from flashing. */
44 private int mWaitingFrames;
45
46 /**
47 * Constructs an animation handler that will animate animals on a graphic
48 * environment by extrapolating their position.
49 *
50 * @param animals
51 * The {@code List} of animals that will be shown on the first
52 * frame
53 */
54
55 public Animation(List<Animal> animals) {
56 mGraphMap = new TreeMap<Integer, GraphicComponent>();
57 mImages = new HashMap<String, BufferedImage>();
58
59 // sanity check
60 if (animals != null) {
61 // puts default action to draw animals and loads corresponding image
62 Action none = new Action(Direction.NONE);
63 for (int i = 0; i < animals.size(); i++) {
64 Animal animal = animals.get(i);
65 BufferedImage img = loadImage(animal);
66 Vector2D position = animal.getPosition().mul(Display.SQUARE_SIZE);
67
68 mGraphMap.put(i, new GraphicComponent(img, position, none));
69 }
70 }
71
72 // default values
73 mDone = true;
74 mWaitingFrames = 0;
75 }
76
77 /**
78 * Asks the animation to update an animal on the screen with a corresponding
79 * action. The animal is identified by a number, so it can be overwritten in
80 * case of a future update.
81 *
82 * @param animal
83 * Animal to update with action
84 * @param id
85 * Unique identifier for animal
86 * @param action
87 * Action that animal needs to perform
88 */
89
90 public void update(Animal animal, int id, Action action) {
91 // sanity checks
92 if (action == null) {
93 action = new Action(Direction.NONE, false);
94 }
95 if (animal != null) {
96 // retrieves BufferedImage
97 String folder = animal.getClass().getSuperclass().getSimpleName();
98 String file = animal.getClass().getSimpleName();
99 BufferedImage img = mImages.get(folder + "." + file);
100 if (img == null) {
101 img = loadImage(animal);
102 }
103
104 // transforms position
105 Vector2D position = animal.getPosition().mul(Display.SQUARE_SIZE);
106
107 mGraphMap.put(id, new GraphicComponent(img, position, action));
108 }
109 }
110
111 /**
112 * Asks the animation to make the animal corresponding to the identifier die
113 * between two squares. This will be done by animating only half of its
114 * action.
115 *
116 * @param id
117 * Identifier of animal to kill
118 */
119
120 public void updateDying(int id) {
121 GraphicComponent graphComp = mGraphMap.get(id);
122 if (graphComp != null) {
123 graphComp.willDieMoving();
124 }
125 }
126
127 /**
128 * Notifies the animation that updates were done, and that it can start
129 * animating from now.
130 */
131
132 public void doneUpdating() {
133 mDone = false;
134 }
135
136 /**
137 * Paints the dt-step of the animation.
138 *
139 * @param dt
140 * The elapsed time between two frames
141 * @param g
142 * The graphics environment on which the graphic components will
143 * be painted (assumed non-null)
144 * @param targetWindow
145 * The window on which the graphic components will be painted
146 * (assumed non-null)
147 */
148
149 public void paint(float dt, Graphics2D g, ImageObserver targetWindow) {
150 mRatio += dt;
151 if (mRatio > 1) {
152 mRatio = 1;
153 }
154
155 // paints every graphic component stored so far
156 for (Map.Entry<Integer, GraphicComponent> entry : mGraphMap.entrySet()) {
157 GraphicComponent comp = entry.getValue();