-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.java
executable file
·289 lines (250 loc) · 12.6 KB
/
Grid.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Programmers: GOATS team: Alex David Rex Tom Zihao
// *****************************************************************************
// *****************************************************************************
// **** Grid
// *****************************************************************************
// *****************************************************************************
// Latest Enhancement: Stage 1 Style
// A Grid contains a 2D array of Intersections connected by Lanes.
// A Grid update()s once during each time interval in the Simulation that
// contains it.
import java.util.*;
import java.io.*;
public class Grid
{
public static final int SOUTHWARD = 0;
public static final int EASTWARD = 1;
public static final int NORTHWARD = 2;
public static final int WESTWARD = 3;
// Store Intersections in a 2D array:
// Even though Java is 0-indexed, Intersection (1,2) is i[1][2]
// We achieve this by not initializing row 0 and col 0
private Intersection i[][];
private int numRows;
private int numCols;
private int laneCapacity;
private int minTimeToTravelLane;
private ArrayList<Car> cars;
//Constuctor
public Grid(int numRows, int numCols, int laneCapacity,
int minTimeToTravelLane)
{
this.numRows = numRows;
this.numCols = numCols;
this.laneCapacity = laneCapacity;
this.minTimeToTravelLane = minTimeToTravelLane;
// Allocate space for numRows*numCols intersections
// In nested for loops, construct lanes and intersections appropriately
i = new Intersection[numRows + 1][numCols + 1];
cars = new ArrayList<Car>();
for(int rowNum = 1; rowNum <= numRows; rowNum++)
{
for(int colNum=1; colNum <= numCols; colNum++)
{
//System.out.print("Constructing Intersection ("+rowNum+
// ","+colNum+")...");
// Construct the right amount of lanes for each intersection
// There's a pattern in the number of "new" lane pairs for each:
// 3 2 2 2
// 3 2 2 2
// 3 2 2 2
// 4 3 3 3
if(rowNum==1)
{
if(colNum==1)
{
// Construct 4 new lanes for Intersection [1,1]
shareNoLanes(rowNum, colNum);
} else { //rowNum==1, colNum > 1
// Constructs and shares lanes for the intersection at
// [rowNum, colNum]
shareLanesWithLeft(rowNum,colNum);
} // end of if(colNum==1)
} else { //rowNum > 1
if(colNum==1)
{
shareLanesWithBelow(rowNum,colNum);
} else { //rowNum > 1, colNum > 1
shareLanesWithLeftAndBelow(rowNum,colNum);
} // end of if(colNum==1)
} // end of if(rowNum==1)
//end lane construction
//System.out.println("Success!");
} // end of for(int colNum=0; colNum<numCols; colNum++)
} // end of for(int rowNum = 0; rowNum < numRows; rowNum++)
// The above code constructs lanes and intersections, populates grid */
this.setBoundaryLanes();
} // end of Grid(int numRows, int numCols)
// Processes a list of carParameters for each car
public void insertCars(ArrayList< ArrayList<Integer> > carParameters)
{
//Constructs cars w/ carParameters
for( ArrayList<Integer> d:carParameters )
{
Car c = new Car( d.get(4), d.get(5), d.get(0) );
this.insertCar( d.get(1), d.get(2), d.get(3), c );
//ArrayList carParameters looks like this:
//[0]int carID, [1]int row, [2]int col,
//[3]int laneDirectionCode, [4]int numBlocksBeforeTurning,
//[5]int turnDirectionCode
} // end of for( ArrayList<Integer> d:carParameters )
} // end of insertCars(ArrayList< ArrayList<Integer> > carParameters)
// Perform the events associated with one simulated time interval
public void update()
{
for(Car c : cars)
{
c.setHasChangedLanes(false);
} // end of for(Car c : cars)
for ( int rowNum = 1; rowNum <= numRows; rowNum++ )
{
for ( int colNum = 1; colNum <= numCols; colNum++ )
{
System.out.println("");
System.out.println("At the intersection located at col " +
colNum+ " and row " + rowNum);
i[rowNum][colNum].visit();
} // end of for( int colNum = 0; colNum < i[0].length; colNum++ )
} // end of for( int rowNum = 0; rowNum < i.length; rowNum++ )
} // end of update()
// Inserts Car c at the lane pointing at intersection [row,col] and heading
// in the direction specified by laneDir
private void insertCar(int row, int col, int laneDir, Car c)
{
(i[row][col].getInLane(laneDir)).initialAdd(c);
cars.add(c);
} // end of insertCar(int row, int col, int laneDir, Car c)
// Constructs Intersections that depend on the lanes of the Intersections
// below and left of each; i.e., constructs Intersections in row >1, col >1
private void shareLanesWithLeftAndBelow(int rowNum, int colNum)
{
Lane[] inLanes = new Lane[4];
Lane[] outLanes = new Lane[4];
for(int cardinalDir=0; cardinalDir<4; cardinalDir++)
{
// For rows and columns > 1, lanes need to be shared
// with the intersection at left and with the intersection
// below
if(cardinalDir==SOUTHWARD){
outLanes[SOUTHWARD] = i[rowNum-1][colNum].getInLane(SOUTHWARD);
} else if(cardinalDir==WESTWARD){
outLanes[WESTWARD] = i[rowNum][colNum-1].getInLane(WESTWARD);
} else {
outLanes[cardinalDir] = new Lane(laneCapacity,
minTimeToTravelLane);
} // end of if(cardinalDir==SOUTHWARD)
} // end of for(int cardinalDir=0; cardinalDir<4; cardinalDir++)
for(int cardinalDir=0; cardinalDir<4; cardinalDir++)
{
if(cardinalDir==NORTHWARD)
{
inLanes[NORTHWARD] = i[rowNum-1][colNum].getOutLane(NORTHWARD);
} else if(cardinalDir==EASTWARD){
inLanes[EASTWARD] = i[rowNum][colNum-1].getOutLane(EASTWARD);
} else {
inLanes[cardinalDir] = new Lane(laneCapacity,
minTimeToTravelLane);
} // end of if(cardinalDir==NORTHWARD)
} // end of for(int cardinalDir=0; cardinalDir<4; cardinalDir++)
i[rowNum][colNum] = new Intersection(inLanes,outLanes);
} // end of shareLanesWithLeftAndBelow(int rowNum, int colNum)
// Constructs Intersections that depend on the lanes of the Intersection
// below each; i.e., constructs Intersections in row >1, col 1
private void shareLanesWithBelow(int rowNum, int colNum)
{
Lane[] inLanes = new Lane[4];
Lane[] outLanes = new Lane[4];
for(int cardinalDir=0; cardinalDir<4; cardinalDir++)
{
// For rows > 1, the leftmost column has to share
// a pair of lanes with the intersection below it
if(cardinalDir==SOUTHWARD){
outLanes[SOUTHWARD] = i[rowNum-1][colNum].getInLane(SOUTHWARD);
} else {
outLanes[cardinalDir] = new Lane(laneCapacity,
minTimeToTravelLane);
} // end of if(cardinalDir==SOUTHWARD)
} // end of for(int cardinalDir=0; cardinalDir<4; cardinalDir++)
for(int cardinalDir=0; cardinalDir<4; cardinalDir++)
{
if(cardinalDir==NORTHWARD){
inLanes[NORTHWARD] = i[rowNum-1][colNum].getOutLane(NORTHWARD);
} else {
inLanes[cardinalDir] = new Lane(laneCapacity,
minTimeToTravelLane);
} // end of if(cardinalDir==NORTHWARD)
} // end of for(int cardinalDir=0; cardinalDir<4; cardinalDir++)
i[rowNum][colNum] = new Intersection(inLanes,outLanes);
} // end of shareLanesWithBelow(int rowNum, int colNum)
// Constructs Intersections that depend on the lanes of the Intersection
// at each's left; i.e., constructs Intersections in row 1, col >1
private void shareLanesWithLeft(int rowNum, int colNum)
{
Lane[] inLanes = new Lane[4];
Lane[] outLanes = new Lane[4];
for(int cardinalDir=0; cardinalDir<4; cardinalDir++)
{
// On row 1, each intersection's westward outbound lane is the
// westward inbound lane of the intersection in the previous column
if(cardinalDir==WESTWARD)
{
outLanes[WESTWARD] = i[rowNum][colNum-1].getInLane(WESTWARD);
} else {
// The other 3 outbound lanes are new to this intersection
outLanes[cardinalDir] = new Lane(laneCapacity,
minTimeToTravelLane);
} // end of if(cardinalDir==WESTWARD)
} // end of for(int cardinalDir=0; cardinalDir<4; cardinalDir++)
for(int cardinalDir=0; cardinalDir<4; cardinalDir++)
{
// Similarly, the eastward inbound lane (adjacent to the
// westward outbound lane) has already been constructed
// and assigned to the intersection in the previous col.
if(cardinalDir==EASTWARD)
{
inLanes[EASTWARD] = i[rowNum][colNum-1].getOutLane(EASTWARD);
} else {
inLanes[cardinalDir] = new Lane(laneCapacity,
minTimeToTravelLane);
} // end of if(cardinalDir==EASTWARD)
} // end of for(int cardinalDir=0; cardinalDir<4; cardinalDir++)
i[rowNum][colNum] = new Intersection(inLanes,outLanes);
} // end of shareLanesWithLeft(int rowNum, int colNum)
// Constructs an intersection that does not depend on the lanes of any
// previous intersections; i.e., constructs Intersection [1,1]
private void shareNoLanes(int rowNum, int colNum)
{
Lane[] inLanes = new Lane[4];
Lane[] outLanes = new Lane[4];
for( int cardinalDir=0; cardinalDir<4; cardinalDir++ )
{
//Generate 4 new lane pairs for Intersection [1,1]
inLanes[cardinalDir] = new Lane(laneCapacity, minTimeToTravelLane);
outLanes[cardinalDir] = new Lane(laneCapacity, minTimeToTravelLane);
} // end of for( int cardinalDir=0; cardinalDir<4; cardinalDir++ )
i[rowNum][colNum] = new Intersection(inLanes,outLanes);
} // end of shareNoLanes(int rowNum, int colNum)
// Determines and sets which Lanes enter and exit the Grid
private void setBoundaryLanes(){
//Set boundary lanes for the bottom row
for (int colNum = 1; colNum <= numCols; colNum++){
(i[1][colNum].getOutLane(SOUTHWARD)).setOutboundBoundary();
(i[1][colNum].getInLane(NORTHWARD)).setInboundBoundary();
} //end for (int colNum = 1; colNum <= numCols; colNum++)
//Set boundary lanes for the top row
for (int colNum = 1; colNum <= numCols; colNum++){
(i[numRows][colNum].getOutLane(NORTHWARD)).setOutboundBoundary();
(i[numRows][colNum].getInLane(SOUTHWARD)).setInboundBoundary();
} //end for (int colNum = 1; colNum <= numCols; colNum++)
//Set boundary lanes for the west side
for (int rowNum = 1; rowNum <= numRows; rowNum++){
(i[rowNum][1].getOutLane(WESTWARD)).setOutboundBoundary();
(i[rowNum][1].getInLane(EASTWARD)).setInboundBoundary();
} // end for (int rowNum = 1; rowNum <= numRows; rowNum++)
//Set boundary lanes for the east side
for (int rowNum = 1; rowNum <= numRows; rowNum++){
(i[rowNum][numCols].getOutLane(EASTWARD)).setOutboundBoundary();
(i[rowNum][numCols].getInLane(WESTWARD)).setInboundBoundary();
} // end for (int rowNum = 1; rowNum <= numRows; rowNum++)
} // end of setBoundaryLanes()
}