|
| 1 | +from math import sqrt |
| 2 | + |
| 3 | +class Person: |
| 4 | + def __init__(self,route): |
| 5 | + self.arrived = False |
| 6 | + self.travelTime = 0 |
| 7 | + self.route = route |
| 8 | + self.currentRouteSegment = self.route.pop() |
| 9 | + self.positionOnCurrentRouteSegment = 0 |
| 10 | + self.width = 2 |
| 11 | + self.length = 2 |
| 12 | + def Walk(self): |
| 13 | + self.travelTime += 1 |
| 14 | + if isinstance(self.currentRouteSegment,FourWayStopSignIntersection): |
| 15 | + pass |
| 16 | + elif isinstance(self.currentRouteSegment,FourWaySignalIntersection): |
| 17 | + pass |
| 18 | + elif isinstance(self.currentRouteSegment,RoadSegment): |
| 19 | + self.positionOnCurrentRouteSegment += 3.7 |
| 20 | + if self.positionOnCurrentRouteSegment >= self.currentRouteSegment.length: # we reached the end of the segment and are at a signal |
| 21 | + if self.currentRouteSegment.signal.color == 'green': # if green we go |
| 22 | + if self.route: # more routeSegments in our route |
| 23 | + self.routePosition = self.routePosition - self.routeSegment.length |
| 24 | + self.routeSegment = self.route.pop() |
| 25 | + else: |
| 26 | + self.arrived = True |
| 27 | + else: # red/yellow we stop |
| 28 | + self.routePosition = self.routeSegment.length |
| 29 | + elif isinstance(self.currentRouteSegment,Crosswalk): |
| 30 | + pass |
| 31 | + |
| 32 | +class Vehicle: |
| 33 | + def __init__(self,driver): |
| 34 | + self.driver = driver |
| 35 | + self.speed = 0.0 |
| 36 | + self.acceleration = 0.0 |
| 37 | + |
| 38 | +class Bicycle(Vehicle): |
| 39 | + def __init__(self,driver): |
| 40 | + Vehicle.__init__(self,driver) |
| 41 | + self.width = 3 |
| 42 | + self.lenght = 6 |
| 43 | + |
| 44 | +class Car(Vehicle): |
| 45 | + def __init__(self,driver): |
| 46 | + Vehicle.__init__(self,driver) |
| 47 | + |
| 48 | +class Carpool(Car): |
| 49 | + def __init__(self,driver): |
| 50 | + Car.__init__(self,driver) |
| 51 | + |
| 52 | +class Bus(Vehicle): |
| 53 | + pass |
| 54 | + |
| 55 | +class Train: |
| 56 | + pass |
| 57 | + |
| 58 | +class Ferry: |
| 59 | + pass |
| 60 | + |
| 61 | +class Place: |
| 62 | + def __init__(self, x, y): |
| 63 | + self.x = x |
| 64 | + self.y = y |
| 65 | + |
| 66 | +class Signal: # red = stop, yellow = stop if able, green = go |
| 67 | + def __init__(self,color,greenTime,redTime,yellowTime): |
| 68 | + self.color = color |
| 69 | + self.greenTime = greenTime |
| 70 | + self.redTime = redTime |
| 71 | + self.yellowTime = yellowTime |
| 72 | + def countdown(self): |
| 73 | + if self.color == 'red': |
| 74 | + if self.redTime: |
| 75 | + self.redTime -= 1 |
| 76 | + else: self.color = 'green' |
| 77 | + elif self.color == 'green': |
| 78 | + if self.greenTime: |
| 79 | + self.greenTime -= 1 |
| 80 | + else: self.color = 'yellow' |
| 81 | + elif self.color == 'yellow': |
| 82 | + if self.yellowTime: |
| 83 | + self.yellowTime -= 1 |
| 84 | + else: self.color = 'red' |
| 85 | + |
| 86 | +class FourWayStopSign: # this direction stop, then yeild, then go |
| 87 | + def __init__(self): |
| 88 | + pass |
| 89 | + |
| 90 | +class FourWayStopSignIntersection: |
| 91 | + def __init__(self,nRoadSegment,sRoadSegment,eRoadSegment,wRoadSegment): |
| 92 | + self.stopSign = FourWayStopSign() |
| 93 | + self.nRoadSegment = nRoadSegment |
| 94 | + self.sRoadSegment = sRoadSegment |
| 95 | + self.eRoadSegment = eRoadSegment |
| 96 | + self.wRoadSegment = wRoadSegment |
| 97 | + nQueque = False |
| 98 | + sQueque = False |
| 99 | + eQueque = False |
| 100 | + wQueque = False |
| 101 | + |
| 102 | +class TwoWayStopSign: # this direction stop, then yeild, then go |
| 103 | + def __init__(self): |
| 104 | + |
| 105 | + |
| 106 | +class YeildSign: # yeild, then go |
| 107 | + def __init__(self): |
| 108 | + pass |
| 109 | + |
| 110 | +class = Crosswalk: # |
| 111 | + def __init__(self): |
| 112 | + pedestrian = False |
| 113 | + |
| 114 | +Signals = [] |
| 115 | + |
| 116 | +class FourWaySignal: |
| 117 | + def __init__(self,majorTime,minorTime,yellowTime): |
| 118 | + self.major = Signal('green',majorTime,minorTime+yellowTime,yellowTime) |
| 119 | + Signals.append(self.major) |
| 120 | + self.minor = Signal('red',minorTime,majorTime+yellowTime,yellowTime) |
| 121 | + Signals.append(self.minor) |
| 122 | + |
| 123 | +class FourWaySignalIntersection: |
| 124 | + def __init__(self) |
| 125 | + self.pedestrianSignal1 = FourWaySignal(13,3,12) |
| 126 | + self.trafficSignal = FourWaySignal(20,10,5) |
| 127 | + self.pedestrianSignal2 = FourWaySignal(13,3,12) |
| 128 | + |
| 129 | +class RoadSegment: |
| 130 | + def __init__(self, origin, destination, signal, type): |
| 131 | + self.origin = origin |
| 132 | + self.destination = destination |
| 133 | + self.length = sqrt((self.origin.x+self.destination.x)**2+(self.origin.y+self.destination.y)**2) |
| 134 | + self.signal = signal |
| 135 | + self.type = type |
| 136 | + |
| 137 | +class TwoLaneRoad: |
| 138 | + def __init__(self,end1,end2): |
| 139 | + self.sidewalk1a = RoadSegment(end1,end2,'person') |
| 140 | + self.sidewalk1b = RoadSegment(end2,end1,'person') |
| 141 | + self.roada = RoadSegment(end1,end2,'vehicle') |
| 142 | + self.roadb = RoadSegment(end2,end1,'vehicle') |
| 143 | + self.sidewalk2a = RoadSegment(end1,end2,'person') |
| 144 | + self.sidewalk2b = RoadSegment(end2,end1,'person') |
| 145 | + |
| 146 | +p1 = Place(0,0) |
| 147 | +p2 = Place(10,10) |
| 148 | +p3 = Place(20,20) |
| 149 | + |
| 150 | +rs1 = RoadSegment(p1,p2,s1) |
| 151 | +rs2 = RoadSegment(p2,p3,s2) |
| 152 | + |
| 153 | +rs3 = RoadSegment(p3,p2,s1) |
| 154 | +rs4 = RoadSegment(p2,p1,s2) |
| 155 | + |
| 156 | +P1 = Person([rs2,rs1]) |
| 157 | +P2 = Person([rs4,rs3]) |
| 158 | +People = [P1,P2] |
| 159 | + |
| 160 | +while P1.arrived == False: |
| 161 | + for S in Signals: |
| 162 | + print S.color |
| 163 | + S.countdown() |
| 164 | + for P in People: |
| 165 | + print str(P.arrived) + " " + str(P.travelTime) + " " + str(P.routeSegment) + " " + str(P.routePosition) + "\n" |
| 166 | + P.Walk() |
0 commit comments