Hello. I'm not really sure on how to explain the problem, but here goes.

I'm creating a Traffic Simulator of a cross junction. I currently have car objects moving, with working traffic lights. I created a variable array for the car class which creates several car objects. Then the lane for the car is randomly chosen, e.g. South road of the junction, left lane or middle lane etc.

I'm now having a problem with trying to identify if there are any cars on a specific lane, if so, then do some stuff, such as stop behind or slow down etc.

I have the classes Traffic, Car and Lane (There are several lane classes, such as SouthLeftLane, SouthMiddleLane). Traffic creates the car objects in the array. Car than chooses a specific lane, in which a Lane class is then called.

I know this may not be a good approach, however I don't know a better one.

Thanks.

I'm now having a problem with trying to identify if there are any cars on a specific lane, if so, then do some stuff, such as stop behind or slow down etc.

It sounds like what I'd be looking for would be for the Lane to tell me whether it has Cars in it. Perhaps it could return an array or other collection of Cars, which would allow you to do what you want. Can you write that method for Lane, and then use it where needed?

Yes that could work, but the thing is, everytime a car chooses a lane, it creates a new instance of the lane class. By having an array holding the cars, the value wouldn't be the same for all instances if it gets changed during runtime right?

No, in that case my thought wouldn't help. I guess I don't know how your classes are set up. If you sketch out the interactions for me, I might have another idea. (Not the full code, at least not yet - we're talking about design, so keep it at the design level for now)

Alright.

Traffic class:
Creates an array

public Car[] testCars = new Car[maxCars];

In the init() method:

for (int carIndex = 0; carIndex < numCars; carIndex++) {
		  		testCars[carIndex] = new Car();
		  	}

In the run() method, it performs the testCar.runCar() method and in the paint() method, it paints the cars.

The Car class has a setLane() method which uses a string array to randomly choose a lane, which is stored in a string "lane". This method also creates a new instance of the lane class according to the string lane.

The Car.runCar() method then uses the new lane instance to call the Lane.runCar() method (Yes, I know I should change the method names and not keep it the same. I will do so after.)

The Lane class then checks the location of the car object and traffic light, which results in the movement of the car by changing the x and y co-ords.

That's pretty much it.

Okay, thanks. I'd made a few incorrect assumptions.
The next idea that comes to me is to maintain an arraylist of Lanes currently in use, and check against that. So when a car creates itself and selects a lane, it registers that lane in the list, and when the car leaves the intersection (and the program) it frees up that lane. Might that work?

I think what you mentioned first would be the best approach to take. Ideally, there should only be one instance of a lane and obviously several instances of cars. So what I have managed to do, as you suggested previously, is create a List of Car objects in the lane class.
I'm now trying to iterate over that list and perform the car movements.

As for the list of lanes being used, it's still a similar approach to above right? I need it so that more than one car can run on a given lane.

I'm gonna see if I can get the movements working, i'll post back with an update later.
In order to check if another car is on the same lane, I would just need to iterate over the whole list and then find a car with similar co-ords right?

I'll try doing that. Thanks for the help.

I tried to get the movements of the cars working using a list containing the car objects in the lane class. Unfortunately, I couldn't do it. The cars all moved at a very fast pace. I'm not exactly sure what the problem is but it's something to do with the iteration between the car objects.

I'll try doing it using an array containing the lanes being used.

My notion is that your Traffic class will contain some sort of registry of Lanes. It might be worth maintaining the list as a pair of Maps, one for Car to Lane and the other for the vice versa. I don't know.
I do think that there might be something in your design that's impeding you. I know that, naively speaking, I think of a Lane as a container for zero, one, or many cars, and that's not how you're using it. I can't help but wonder if you might want to look at this again.

Yes, I agree with you there. That's how it should be ideally, one instance of a lane class containing the cars, but like I said, I couldn't get it to work :S.

What I did was create an instance of the lane class in the Traffic class. When the car chose a lane, it would add itself to Traffic.lane.carList (the list of cars).
Then in the Traffic.run() method, I called the lane.start() method, which iterates through carList and performs the lane.run(car) method.

I thought it would work like that, but what happened was that the cars moved at an incredibly fast pace and the co-ords were also getting messed up.

Solved!
I created a List, specific for each lane in the Traffic class, which stores a list of cars every time a car chooses a lane. Then for each car, I set a lead car which was the car before it in the list. So it moves according to the lead car's position. This works quite well.

thanks to all, giving such a good explanation...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.