Dark Horse programmer--traffic light management

Source: Internet
Author: User

The simulation realizes traffic light management system logic at intersection, the specific requirements are as follows:  randomly generates vehicles that follow each route. For example,:        Southbound vehicles----straight vehicles         from West to south----right turn vehicles         from east to south----left-turn vehicle         ...   Lights ignore the yellow light, only consider the red and green light.   should consider turning left vehicle control signal, right turn vehicle is not controlled by signal.   Specific signal control logic is the same as the ordinary traffic light control logic in real life, regardless of the control logic under special circumstances. Note: North-South vehicles and things to the vehicle alternately released, the same direction waiting for the vehicle should be released straight vehicles and then released left-turn vehicles.   1 seconds per car crossing time (hint: can be simulated by thread sleep).   Randomly generated vehicle time interval and traffic light Exchange interval custom, can be set.   does not require the implementation of the GUI, only consider the system logic implementation, log mode can show the results of the program operation.    there will be multiple cars on each route, a new car will be added randomly on the route, and a car will be reduced every second during the green light. A road class is designed to represent the route, and each road object represents a route with a total of 12 routes, that is, a total of 12 road instance objects are generated in the system. Each route is randomly added to a new vehicle, which is added to a collection to save. Each route is checked every second to see if the light that controls the route is green, and the first car in the collection of this route is removed, which means the car has crossed the junction. Every second of each route checks whether the light that controls this route is green, and when a light turns red from green, the light in the next direction should turn green. To design a lamp class to represent a traffic light, each traffic light maintains a state: bright (green) or not bright (red), each traffic light should have a way to lighten and darken, and can return to their own bright black state. There are 12 routes in total, so there are 12 traffic lights to be generated in a total system. The right turn of the route is not controlled by the lights, but in order to allow the program to adopt a unified processing method, it is assumed that there are four right turning lights, but these lights are solid state, that is never black. In addition to the lights on the other 8 routes in the direction of the right corner, they are 22 pairs, can be grouped into 4 groups, so, in the programming process, as long as from each of the 4 groups to take out a lamp, the 4 lights in turn to turn on the light, and the 4 lamp direction corresponding to the lamp with the change, so the lamp class to have a variable to remember the opposite sideTo the lamp, in a lamp object in the light and darken method, the corresponding direction of the light is also lightened and blackened. When each lamp turns black, the next light becomes brighter, and the lamp class uses a variable to remember its next light. No matter where in the program to get a light in a certain direction, each time you get the same instance object, so the lamp class instead of using enumerations to do is obviously very convenient, always only the 12-directional lamp instance object. Design a Lampcontroller class that periodically turns the current green light red. Road class writing each road object has a name member variable to represent the direction, and there is a vehicles member variable to represent the vehicle collection in the direction. In the construction method of the Road object, start a thread every other random time to add a car to the vehicles collection (represented by a string in the form of a "route name _id"). Start a timer in the construction method of the road object, checking that the light is green in that direction every second, and then printing the vehicle collection and removing the first car in the collection. Lamp class in the writing system has 12 directions in the light, in other parts of the program to be based on the name of the lamp can obtain the corresponding lamp instance object, the combination of these factors, the lamp class with JAVA5 in the enumeration form definition is simpler. The light black state in each lamp object is represented by the lighted variable, and the lamp objects in the four directions of s2n, s2w, e2w and e2n are then polled to lighten, and there is a oppositelampname variable in the Lamp object to indicate their opposite direction, A nextlampname variable is used to represent the next lighted light after the light is turned on. These three variables are assigned in the form of a constructor, because the enumeration elements must be referenced after the definition, so they cannot be referenced to each other in the constructed method, so the lights in the opposite direction and the next direction are represented in string form. To increase the lamp to lighten and darken the method: Light and Blackout, for s2n, s2w, e2w, e2n in the four direction of lamp objects, the two methods inside to let the opposite direction of the lights and darken, blackout method also to make the next light to lighten. In addition to the lamp objects in the four directions of s2n, s2w, e2w, e2n, the Nextlampname and Oppositelampname properties of lamp objects in other directions are set to NULL, and s2n, s2w, e2w, E2n the Nextlampname and Oppositelampname properties of lamp objects in these four directions must be set to NULL to prevent light and blackout from entering a dead loop. The Lampcontroller class can only be written in the entire systemA traffic light control system, so the Lampcontroller class is best designed as a single case. Lampcontroller the first green light to be set in the construction method. The Start method of the Lampcontroller object turns the current light green and then starts a timer that turns the current light red and turns the next light green every 10 seconds. The MainClass class is written with a for loop to create an object that represents 12 alignments. It then obtains the Lampcontroller object and invokes its Start method.

Dark Horse programmer-traffic lights management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.