This program is an object-oriented course assignment for my first teachers, younger siblings last semester. At that time, I saw two or three C ++ books. Although I was busy with projects, however, I tried to test myself in a hurry. From design to implementation, it took about a week to complete at the end of 2013.
Although it is an object-oriented course assignment, it only uses "encapsulation" and does not touch on "inheritance" and "polymorphism". It is only based on objects.
I used to read a lot of materials when I was doing one thing. This is simply an imaginary task.
The source code is hosted on GitHub: Click to enter the link. The name follows the "Hungary naming method" and try to implement "self-documenting". Of course, it is also necessary to note.
Executable program: Click to enter the link
Question (complete PDF ):
Design Description:
Traffic lights, saving the color, total duration, current countdown, etc. Operation countdown and color conversion;
Saves the road endpoint with an enumeration type;
Path class. You can use a map to save all the path endpoints and their corresponding discount points. You can use the route endpoints to obtain their corresponding route discount points;
When a vehicle is randomly generated, its color and route are randomly set. In this type, the vehicle's route endpoint, current travel interval, and current location are saved to obtain the vehicle status or update the location.
Road class, which is a two-dimensional matrix with multiple traffic lights installed and each point has a mark. If a vehicle is in this position, it is activated.
Simulation illustration:Periodically scan all points in the road class, obtain the corresponding traffic lights or vehicles, and then draw them on the interface.
Coordinate hypothesis:The origin is in the upper left corner, the right is the positive direction of the X axis, and the downward direction is the positive direction of the Y axis.
Difficulties:
1. Traffic Light replacement rules are encapsulated in traffic light to simplify the process;
2. Complicated turning and Route Assignment rules are solved by saving routes;
3. Update for 1 second:
1 // vehicle travel 1 second 2 void ctrafficsimulationdlg: moveonesec (cmypoint & ptcarpos) 3 {4 for (Auto it = m_veccars.begin (); it! = M_veccars.end (); ++ it) {5 If (IT-> getpos () = ptcarpos) {// locate the vehicle 6 pair in the vehicle collection <cmypoint, cmypoint> paircurinterval = it-> getcurinterval (); 7 cmypoint ptfrom = paircurinterval. first; 8 cmypoint ptto = paircurinterval. second; 9 10 if (ptto. getx () = ptcarpos. getx () 11 & ptto. gety () = ptcarpos. gety () {// travel to the end of a range of 12 13 vector <cmypoint> & vecturningpoints14 = m_paths.getturningpoints (IT-> getpath (); 15 16 if (IT-> m_nptidx <vecturningpoints. size () {// update range 17 ptfrom = ptto; 18 ptto = vecturningpoints. at (IT-> m_nptidx ++); 19 it-> setcurinterval (ptfrom, ptto); 20} 21 else {// travel to the end of the route, delete 22 m_veccars.erase (it); 23 m_roadmain.deactivatepoint (ptcarpos); 24 return; 25} 26} 27 28 cmypoint ptnewcarpos; 29 If (ptfrom. getx ()! = Ptto. getx () {// travel 30 int n = ptto. getx ()-ptfrom. getx ()> 0? + 1:-1; 31 ptnewcarpos = cmypoint (ptcarpos. getx ()/gc_nscalar + N, ptcarpos. gety ()/gc_nscalar); 32} 33 else {// travel along Y axis 34 int n = ptto. gety ()-ptfrom. gety ()> 0? + 1:-1; 35 ptnewcarpos = cmypoint (ptcarpos. getx ()/gc_nscalar, ptcarpos. gety ()/gc_nscalar + n); 36} 37 38 bool bgreen = true; 39 if (ptnewcarpos! = Ptto // if you do not turn, view traffic light 40 & m_setlightpos.find (ptcarpos )! = M_setlightpos.end () // encounter traffic lights 41 bgreen = m_roadmain.isgreen (ptcarpos); 42 43 // vehicle travel 1 second 44 If (bgreen &&! M_roadmain.ispointactivated (ptnewcarpos) {45 m_roadmain.deactivatepoint (ptcarpos); 46 m_roadmain.activatepoint (ptnewcarpos); 47 It-> setpos (ptnewcarpos); 48} 49 return; // Update completed 51} 52} 53}View code
Whether to change the driving range (whether the current vehicle position is the same as the end of the range) and whether to make the trip to the end (delete the vehicle );
Determine whether the current interval is horizontal (the X axis coordinates are not equal) or vertical (the Y axis coordinates are not equal), and then decide whether to follow the positive or backward direction of X or Y;
Determines whether to ignore the traffic lights by determining whether to turn at the intersection (where the new vehicle is located and the end of the intersection.
Run: