Skip to content

Commit e67e267

Browse files
committed
Fixed merge conflicts.
2 parents a9b71f6 + 5309e82 commit e67e267

File tree

9 files changed

+134
-44
lines changed

9 files changed

+134
-44
lines changed

.classpath

Lines changed: 0 additions & 6 deletions
This file was deleted.

.project

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Car/Car.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,17 @@ public int getDestY()
5353

5454
public boolean move() // plug off the first item of the arraylist and execute that
5555
{
56-
Tile nextTile= map.getInDir(map.get(xPos, yPos), path.get(0));
57-
if(nextTile instanceof TrafficLight) { //No red traffic lights
58-
TrafficLight l = (TrafficLight)nextTile;
59-
if(l.isRed() && !(map.get(xPos,yPos) instanceof TrafficLight)) {
60-
hasMoved=true;
61-
return false;
62-
}
63-
}
64-
6556
Road road = (Road)map.get(xPos, yPos);
66-
int speedInit = road.getSpeed();
67-
for(int remaining = road.getSpeed(); remaining>=0; remaining--) {
57+
for(int remaining = road.getSpeed(); remaining>0 && path.size()>0; remaining--) {
58+
Tile nextTile=getNextTile(xPos, yPos, path.get(0));
59+
if(nextTile instanceof TrafficLight) { //No red traffic lights
60+
TrafficLight l = (TrafficLight)nextTile;
61+
if(l.isRed() && !(map.get(xPos,yPos) instanceof TrafficLight)) {
62+
hasMoved=true;
63+
return false;
64+
}
65+
}
66+
int speedInit = road.getSpeed();
6867
road = (Road)map.get(xPos, yPos);
6968
if(road.getSpeed()!=speedInit) break;
7069
if(nextTile.getX()>=0 && nextTile.getY()>=0 && nextTile.getX()<map.getLengthX() && nextTile.getY()<map.getLengthY()) {
@@ -76,11 +75,12 @@ public boolean move() // plug off the first item of the arraylist and execute th
7675
road = (Road) map.get(xPos, yPos);
7776
road.carIncrement(this);
7877
path.remove(0);
79-
return true;
78+
//return true;
8079
}
8180

8281
}
8382
}
83+
if(hasMoved) return true;
8484
return false;
8585
}
8686
public boolean turn()

src/Constants/Directions.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ public class Directions {
1010

1111
public static final int[] dx = { 0, 1, 0, -1 };
1212
public static final int[] dy = { -1, 0, 1, 0 };
13+
14+
public static int getOppositeDir(int dir) {
15+
int val = dir + 2;
16+
if(val >= 4) val -= 4;
17+
return val;
18+
}
1319
}

src/Graphics/Runner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Runner extends JFrame{
3939
static int cars=100;//number of cars
4040
static int sizeBox=14;//size of each box
4141
static Map map;
42-
static JFrame f ;
42+
static JFrame f ;
4343
public static void main(String[] args) throws InterruptedException {
4444

4545
gui.setBorder(new EmptyBorder(2, 3, 2, 3));

src/Map/Map.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,13 @@ void placeCar(Car c) {
112112
cars.add(c);
113113
}
114114

115+
116+
117+
115118
void createHorizontalRoad(int startX, int startY, int length, int lanes, int speed){
116119
startX += startX % 2;
117120
startY += startY % 2;
118-
length += length % 2;
121+
length += length % 2;
119122

120123
for (int i = 0; i < length; i++) {
121124
if (startX + i >= getLengthX()) break;

src/Map/RandomMapGenerator.java

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import Constants.Directions;
44
import Map.NonRoad.House;
5+
import Map.NonRoad.*;
56
import Car.Car;
67
import Map.Road.Road;
78
import Map.Road.TrafficLight;
@@ -45,7 +46,7 @@ public Map generateMap() {
4546
int start = (int) (Math.random() * length / 2) * 2;
4647
int width = (int) (Math.random() * (length - start - 6) / 2) * 2 + 6;
4748

48-
m.createHorizontalRoad(start, i, width, 2, 4);
49+
m.createHorizontalRoad(start, i, width, 2, 1);
4950
}
5051
}
5152

@@ -54,13 +55,11 @@ public Map generateMap() {
5455
int start = (int) (Math.random() * length / 2) * 2;
5556
int width = (int) (Math.random() * (length - start - 6) / 2) * 2 + 6;
5657

57-
m.createVerticalRoad(i, start, width, 2, 4);
58+
m.createVerticalRoad(i, start, width, 2, 1);
5859
}
5960
}
6061
}
6162

62-
removeIslands(m);
63-
6463
ArrayList<Road> roads = new ArrayList<Road>();
6564

6665
for (Tile[] a : grid) {
@@ -70,6 +69,8 @@ public Map generateMap() {
7069
}
7170
}
7271
}
72+
if(!assignSpeeds(m, roads))
73+
for(int i = 0; i < 30; i++) System.out.println("MAPGEN DONE GOOFED");
7374

7475
for (int i = 0; i < cars; i++) {
7576
Road start, end;
@@ -85,7 +86,106 @@ public Map generateMap() {
8586

8687
return m;
8788
}
89+
private static Tile[] getAdjTiles(Tile[][] grid, Tile x){
90+
Tile[] y = new Tile[4];
91+
if(!(x.getX() == 0))
92+
y[3] = grid[x.getX() - 1][x.getY()];
93+
if(!(x.getY() == 0))
94+
y[0] = grid[x.getX()][x.getY() - 1];
95+
if(!(x.getX() == grid.length - 1))
96+
y[1] = grid[x.getX() + 1][x.getY()];
97+
if(!(x.getY() == grid[0].length - 1))
98+
y[2] = grid[x.getX()][x.getY() + 1];
99+
100+
101+
return y;
102+
}
103+
private boolean assignSpeeds(Map m, ArrayList<Road> roads){
104+
int[][] speeds = new int[m.getLengthX()][m.getLengthY()];
105+
int count = 0;
106+
for(Road r : roads){
107+
108+
Tile[] adjTiles = getAdjTiles(m.grid, m.grid[r.getX()][r.getY()]);
109+
Road start = null;
110+
for(int i = 0; i < 4; i++){
111+
if(adjTiles[i] instanceof TrafficLight){
112+
start = r;
113+
}
114+
if(adjTiles[i] == null || adjTiles[i] instanceof NonRoad){
115+
if(i == r.getDirection() || i == Math.abs(r.getDirection() - 4)){
116+
start = r;
117+
}
118+
}
119+
}
120+
121+
if(start != null){
122+
System.out.println("Start point XY:" + start.getX() + " " + start.getY());
123+
int dir = start.getDirection();
124+
int length = 1;
125+
Tile t = r;
126+
System.out.println("XY:" + t.getX() + " " + t.getY());
127+
128+
// if(m.getInDir(r,dir) != null){
129+
t = m.getInDir(r, dir);
130+
131+
132+
133+
if(t instanceof TrafficLight || t == null || t instanceof NonRoad){
134+
135+
int reverseDir = (dir + 2 > 3)? dir - 2 : dir + 2;
136+
if(m.getInDir(r, reverseDir) != null)
137+
t = m.getInDir(r, reverseDir);
138+
139+
while(!(t == null || t instanceof TrafficLight || t instanceof NonRoad)){
140+
System.out.println("Count: " + count + "XY:" + t.getX() + " " + t.getY());
88141

142+
length++;
143+
if(m.getInDir(t, reverseDir) != null)
144+
t = m.getInDir(t, reverseDir);
145+
else
146+
break;
147+
}
148+
System.out.println("Length:" + length);
149+
for(int i = 0; i < length; i++){
150+
t = m.getInDir(t, dir); // reverse direction
151+
speeds[t.getX()][t.getY()] = determineSpeed(length);
152+
}
153+
154+
} else {
155+
156+
while(!(t == null || t instanceof TrafficLight || t instanceof NonRoad)){
157+
System.out.println("Count: " + count + "XY:" + t.getX() + " " + t.getY());
158+
159+
length++;
160+
if(m.getInDir(t, dir) != null)
161+
t = m.getInDir(t, dir);
162+
else
163+
break;
164+
}
165+
int reverseDir = (dir + 2 > 3)? dir - 2: dir + 2;
166+
System.out.println("Length:" + length);
167+
168+
for(int i = 0; i < length; i ++){
169+
t = m.getInDir(t, reverseDir); //reverse directions
170+
speeds[t.getX()][t.getY()] = determineSpeed(length);
171+
}
172+
173+
}
174+
}
175+
// }
176+
count++;
177+
}
178+
for(Road r : roads){
179+
r.setSpeed(speeds[r.getX()][r.getY()]);
180+
}
181+
return true;
182+
}
183+
private int determineSpeed(int length){
184+
if(length <= 6) return 1;
185+
if(length <= 10) return 2;
186+
return 3;
187+
188+
}
89189
private void removeIslands(Map m) {
90190
int[][] dp = new int[length][length];
91191

@@ -188,7 +288,7 @@ public String toString() {
188288
}
189289

190290
public static void main(String[] args) {
191-
MapGenerator mapGen = new RandomMapGenerator(30, 1);
291+
MapGenerator mapGen = new RandomMapGenerator(10, 1);
192292

193293
System.out.println(mapGen.generateMap());
194294
}

src/Map/Road/Road.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public class Road extends Tile
77
{
8-
private final int speedLimit;
8+
private int speedLimit;
99
protected final int dir;
1010
private int max;
1111
protected Car[] occupants;
@@ -22,7 +22,9 @@ public Road(int x, int y, int speed, int direction, int lanes) {
2222
public int getSpeed() {
2323
return speedLimit;
2424
}
25-
25+
public void setSpeed(int speed){
26+
speedLimit = speed;
27+
}
2628
public int getDirection() {
2729
return dir;
2830
}

src/Map/Tile.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
public class Tile implements java.io.Serializable{
44
protected final int x, y;
5+
public double density;
56

67
public Tile(int x, int y) {
78
this.x = x;
89
this.y = y;
10+
density = 0;
911
}
1012

1113
public int getX()

0 commit comments

Comments
 (0)