I want to design a data structure with OOP for emulating traffic situation in a racing game. The requirements are as below:
- Each vehicle can know on which position of a lane, which lane, and which section of road it is driving.
- For each road, it can know how many vehicles are on it, and how many vehicles on each lane on the road.
- (plus) Each vehicle has it's driving strategy. For example, some vehicles like to drive fast while others like slow.
I use java to implement this topic. Currently my objects are as below. I just know that this could need a bi-direction relationship between Vehicle
and Road
/RoadSection
, but I don't know how to implement it.
class Lane {
List<Vehicle> vehicleDrivingOnMe = new ArrayList<Vehicle>()
}
class RoadSection {
int roadSectionLengthByKM
/**
* Integer: LaneID, example: 0 for overspeed
*/
Map<Integer, Lane> lanes = new HashMap<Integer, Lane>()
}
class Road {
List<RoadSection> roadSectionList = new ArrayList<RoadSection>()
}
class Vehicle {
int drivingSpeedByKM
}
Then, my problem is, what elements should I add into what object for fulfilling the requirements 1 and 2? Any suggestion is appreciated.