Skip to content

Commit 1fb39d9

Browse files
committed
Fixed merge conflicts.
2 parents e67e267 + 169eed3 commit 1fb39d9

File tree

7 files changed

+116
-230
lines changed

7 files changed

+116
-230
lines changed

maps/map1.map

9.76 KB
Binary file not shown.

src/Car/Car.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Car implements java.io.Serializable {
2323
private int destY;
2424
public boolean hasMoved=false;
2525

26-
public Car(int x, int y, int destX, int destY, Map map) {
26+
public Car(int x, int y, int destX, int destY, Map map, int pathNumber) {
2727
xPos = x;
2828
yPos = y;
2929
this.destX = destX;
@@ -32,7 +32,11 @@ public Car(int x, int y, int destX, int destY, Map map) {
3232
road.carIncrement(this);
3333
this.dir = road.getDirection();
3434
this.map = map;
35-
path = PathGenerator.minimizeTime(map, this);
35+
switch (pathNumber){
36+
case 0: path = PathGenerator.minimizeTime(map, this); break;
37+
case 1: path = PathGenerator.minimizeDistance(map, this); break;
38+
case 2: path = PathGenerator.base(map, this);
39+
}
3640
}
3741
public int getXPos()
3842
{
@@ -50,7 +54,13 @@ public int getDestY()
5054
{
5155
return destY;
5256
}
53-
57+
public void setPath(int pathNumber){
58+
switch (pathNumber){
59+
case 0: path = PathGenerator.minimizeTime(map, this); break;
60+
case 1: path = PathGenerator.minimizeDistance(map, this); break;
61+
case 2: path = PathGenerator.base(map, this);
62+
}
63+
}
5464
public boolean move() // plug off the first item of the arraylist and execute that
5565
{
5666
Road road = (Road)map.get(xPos, yPos);

src/Graphics/Runner.java

Lines changed: 84 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@
1111

1212
import javax.swing.Icon;
1313
import javax.swing.JButton;
14+
import javax.swing.JCheckBoxMenuItem;
1415
import javax.swing.JFrame;
1516
import javax.swing.JLabel;
1617
import javax.swing.JMenu;
1718
import javax.swing.JMenuBar;
1819
import javax.swing.JMenuItem;
20+
import javax.swing.JOptionPane;
1921
import javax.swing.JPanel;
2022
import javax.swing.JToolBar;
2123
import javax.swing.border.EmptyBorder;
2224

2325
import Car.Car;
26+
import FileIO.MapIO;
2427
import Map.Map;
2528
import Map.RandomMapGenerator;
26-
import Map.Tile;
2729
import Map.NonRoad.NonRoad;
2830
import Map.Road.Intersection;
2931
import Map.Road.Road;
@@ -39,12 +41,17 @@ public class Runner extends JFrame{
3941
static int cars=100;//number of cars
4042
static int sizeBox=14;//size of each box
4143
static Map map;
42-
static JFrame f ;
44+
static JFrame f ;
45+
static RandomMapGenerator generator;
46+
static boolean tLOptimized;
47+
48+
static boolean stopped = true;
49+
4350
public static void main(String[] args) throws InterruptedException {
4451

4552
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
4653
gui.setBackground(Color.WHITE.darker().darker());
47-
RandomMapGenerator generator = new RandomMapGenerator(size, cars);
54+
generator = new RandomMapGenerator(size, cars);
4855

4956
map = generator.generateMap();
5057
int w = map.getLengthX();
@@ -73,7 +80,9 @@ public static void main(String[] args) throws InterruptedException {
7380
private static void run() throws InterruptedException{
7481
while(run){
7582
gui.removeAll();
76-
step(map);
83+
if(!stopped) {
84+
step(map);
85+
}
7786
color(gui, map);
7887
f.pack();
7988
gui.updateUI();
@@ -173,17 +182,22 @@ public static void step(Map map) {
173182
//Print out state
174183
iterations++;
175184
ArrayList<Intersection> intersections=map.getHandler().intersections;
176-
//if(iterations % 10 == 0) { //Toggle all lights every ten iterations
185+
if(tLOptimized) {
177186
for(int i = 0; i < intersections.size(); i++) {
178-
179-
187+
if(intersections.get(i).shouldToggle()) {
188+
intersections.get(i).toggle();
189+
}
190+
}
191+
}
192+
else {
193+
if(iterations % 10 == 0) { //Toggle all lights every ten iterations
194+
for(int i = 0; i < intersections.size(); i++) {
180195
if(intersections.get(i).shouldToggle()) {
181196
intersections.get(i).toggle();
182-
183-
197+
}
184198
}
185199
}
186-
//}
200+
}
187201
int t=0;
188202
while(!haveAllMoved()&&t<=5)
189203
{
@@ -224,12 +238,18 @@ private static boolean haveAllMoved()
224238
}
225239
public static void Jbutton(final JFrame f) {
226240
JToolBar vertical = new JToolBar(JToolBar.VERTICAL);
241+
JButton start = new JButton("Start");
227242
JButton stop = new JButton("Stop");
228243
JButton step = new JButton("Step");
229244

230-
stop.addActionListener(new ActionListener() {
245+
start.addActionListener(new ActionListener() {
246+
public void actionPerformed(ActionEvent ae) {
247+
stopped = false;
248+
}
249+
});
250+
stop.addActionListener(new ActionListener() {
231251
public void actionPerformed(ActionEvent ae) {
232-
run=false;
252+
stopped = true;
233253
}
234254
});
235255

@@ -249,6 +269,7 @@ public void actionPerformed(ActionEvent ae) {
249269
}
250270
});
251271

272+
vertical.add(start);
252273
vertical.add(stop);
253274
vertical.add(step);
254275
// add(label, BorderLayout.WEST);
@@ -261,29 +282,69 @@ public void actionPerformed(ActionEvent ae) {
261282
public static void addMenus(final JFrame frame) {
262283
JMenuBar menubar = new JMenuBar();
263284
JMenu fileMenu = new JMenu("File");
285+
JMenu optimizations = new JMenu("Optimizations");
264286
menubar.add(fileMenu);
265-
266-
JMenuItem StopItem = new JMenuItem("Stop");
267-
JMenuItem StartItem = new JMenuItem("Start (doesnt work yet)");
268287

269-
fileMenu.add(StopItem);
270-
StopItem.addActionListener(new ActionListener() {
288+
JMenuItem save = new JMenuItem("Save");
289+
JMenuItem load = new JMenuItem("Load");
290+
fileMenu.add(save);
291+
fileMenu.add(load);
292+
293+
save.addActionListener(new ActionListener() {
271294
public void actionPerformed(ActionEvent ae) {
272-
run=false;
295+
String path = getFilePath(frame);
296+
MapIO.saveMap(map, path);
273297
}
274298
});
275-
fileMenu.add(StartItem);
276-
StartItem.addActionListener(new ActionListener() {
299+
load.addActionListener(new ActionListener() {
277300
public void actionPerformed(ActionEvent ae) {
278-
run=true;
279-
System.out.println("should i run again?"+run+" but i dont");
301+
String path = getFilePath(frame);
302+
map = MapIO.loadMap(path);
280303
}
281304
});
305+
JMenu settingsMenu = new JMenu("Settings");
306+
JMenuItem StopItem = new JMenuItem("Stop");
307+
JMenuItem StartItem = new JMenuItem("Start (doesnt work yet)");
308+
JMenuItem minimizeTime = new JMenuItem("Time");
309+
JMenuItem minimzeDistance = new JMenuItem("Distance");
310+
JMenuItem base = new JMenuItem("Baseline");
282311

312+
settingsMenu.add(optimizations);
313+
optimizations.add(base);
314+
optimizations.addActionListener(new ActionListener() {
315+
public void actionPerformed(ActionEvent ae) {
316+
map.setPath(2);
317+
}
318+
});
319+
optimizations.add(minimzeDistance);
320+
optimizations.addActionListener(new ActionListener() {
321+
public void actionPerformed(ActionEvent ae) {
322+
map.setPath(1);
323+
}
324+
});
325+
optimizations.add(minimizeTime);
326+
optimizations.addActionListener(new ActionListener() {
327+
public void actionPerformed(ActionEvent ae) {
328+
map.setPath(0);
329+
}
330+
});
331+
332+
menubar.add(settingsMenu);
283333

334+
final JCheckBoxMenuItem tL = new JCheckBoxMenuItem("TrafficLight Optimizations", tLOptimized);
335+
tL.addActionListener(new ActionListener() {
336+
public void actionPerformed(ActionEvent ae) {
337+
tLOptimized = !tLOptimized;
338+
tL.setState(tLOptimized);
339+
}
340+
});
341+
settingsMenu.add(tL);
284342
frame.setJMenuBar(menubar);
285343
frame.pack();
286344

287345
}
288-
346+
public static String getFilePath(JFrame frame) {
347+
String s = (String)JOptionPane.showInputDialog(frame, "Enter a file path and name", "File Path", JOptionPane.PLAIN_MESSAGE, null, null, "maps/map1.map");
348+
return s;
349+
}
289350
}

src/Map/DensityBasedMapGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public Map generateMap() {
9999
end = roads.get((int) (roads.size() * Math.random()));
100100
} while (start == end);
101101

102-
m.placeCar(new Car(start.x, start.y, end.x, end.y, m));
102+
m.placeCar(new Car(start.x, start.y, end.x, end.y, m, 0));
103103
}
104104

105105
for (int i = 0; i < grid.length; i++) {

src/Map/Map.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public int getLengthY() {
4343
public ArrayList<Car> getCars() {
4444
return this.cars;
4545
}
46-
46+
public void setPath(int path){
47+
for (int i =0; i< cars.size();i++)
48+
cars.get(i).setPath(path);
49+
}
4750
@Override
4851
public String toString() {
4952
StringBuilder total = new StringBuilder();

src/Optimization/Dijkstra.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,26 @@ public static void relax(int[] current, graphNode[] graph, PriorityQueue pq, int
7171
if (graph[pos].getUp() != null && graph[pos].getVertical() == Directions.UP && indexInCompleted(completed, graph[pos].getUp().getIndex()) == -1) {
7272
int tempWeight = weight + (graph[pos].getY() - graph[pos].getUp().getY())*6/speedVertical;
7373
if (weightUse == 2)tempWeight = 0;
74-
pq.add(tempWeight, graph[pos].getUp().getIndex(), pos);
74+
if ((weightUse == 3 && speedVertical != 3) || weightUse != 3)
75+
pq.add(tempWeight, graph[pos].getUp().getIndex(), pos);
7576
}
7677
if (graph[pos].getRight() != null && graph[pos].getHorizontal() == Directions.RIGHT && indexInCompleted(completed, graph[pos].getRight().getIndex()) == -1) {
7778
int tempWeight = weight + (graph[pos].getRight().getX() - graph[pos].getX())*6/speedHorizontal;
7879
if (weightUse == 2)tempWeight = 0;
79-
pq.add(tempWeight, graph[pos].getRight().getIndex(), pos);
80+
if ((weightUse == 3 && speedHorizontal != 3) || weightUse != 3)
81+
pq.add(tempWeight, graph[pos].getRight().getIndex(), pos);
8082
}
8183
if (graph[pos].getDown() != null && graph[pos].getVertical() == Directions.DOWN && indexInCompleted(completed, graph[pos].getDown().getIndex()) == -1) {
8284
int tempWeight = weight + (graph[pos].getDown().getY() - graph[pos].getY())*6/speedVertical;
8385
if (weightUse == 2)tempWeight = 0;
84-
pq.add(tempWeight, graph[pos].getDown().getIndex(), pos);
86+
if ((weightUse == 3 && speedVertical != 3) || weightUse != 3)
87+
pq.add(tempWeight, graph[pos].getDown().getIndex(), pos);
8588
}
8689
if (graph[pos].getLeft() != null && graph[pos].getHorizontal() == Directions.LEFT && indexInCompleted(completed, graph[pos].getLeft().getIndex()) == -1) {
8790
int tempWeight = weight + (graph[pos].getX() - graph[pos].getLeft().getX())*6/speedHorizontal;
8891
if (weightUse == 2)tempWeight = 0;
89-
pq.add(tempWeight, graph[pos].getLeft().getIndex(), pos);
92+
if ((weightUse == 3 && speedHorizontal != 3) || weightUse != 3)
93+
pq.add(tempWeight, graph[pos].getLeft().getIndex(), pos);
9094
}
9195
}
9296
public static int indexInCompleted(int[][] completed, int item)
@@ -106,13 +110,6 @@ public static int[][] completedItem(int[][] completed, int[] current) {
106110
temp[1][completed[0].length] = current[2];
107111
return temp;
108112
}
109-
110-
// public static boolean finalDestOneDirection(int start, int end) {
111-
// if (start == end || start + 1 == end || start - 1 == end)
112-
// return true;
113-
// return false;
114-
// }
115-
116113
// function to add one direction to the list at a time
117114
public static void moveInDirection(graphNode a, graphNode b, ArrayList<Integer> path) {
118115
if (a.getX() == b.getX()) {

0 commit comments

Comments
 (0)