@@ -16,26 +16,70 @@ class Buggy
16
16
private int laps = 0 ;
17
17
private Station station ;
18
18
private int requiredLaps = 0 ;
19
+ private volatile bool motion = false ;
20
+ private Thread onlineThread = null ;
21
+ private volatile bool onlineThreadShouldRun = false ;
19
22
20
23
public Buggy ( int ID , Direction direction , Station station , Communications comms )
21
24
{
22
25
this . ID = ID ;
23
26
this . direction = direction ;
24
27
this . comms = comms ;
25
28
this . station = station ;
26
-
27
- sendDirection ( ) ;
29
+ }
30
+ public void startOnlineCheck ( )
31
+ {
32
+ if ( onlineThread != null )
33
+ return ;
34
+ onlineThread = new Thread ( new ThreadStart ( ( ) =>
35
+ {
36
+ while ( onlineThreadShouldRun )
37
+ {
38
+ try
39
+ {
40
+ Thread . Sleep ( 1000 ) ;
41
+ bool firstTry = syn ( ) ;
42
+ if ( ! firstTry )
43
+ {
44
+ buggyAction ( "is back online!" ) ;
45
+ if ( motion )
46
+ {
47
+ go ( ) ;
48
+ }
49
+ }
50
+ } catch ( ThreadInterruptedException e ) { }
51
+ }
52
+ } ) ) ;
53
+ onlineThreadShouldRun = true ;
54
+ onlineThread . Start ( ) ;
55
+ }
56
+ public void stopOnlineCheck ( )
57
+ {
58
+ if ( onlineThread == null )
59
+ return ;
60
+ onlineThreadShouldRun = false ;
61
+ onlineThread . Interrupt ( ) ;
62
+ onlineThread . Join ( ) ;
28
63
}
29
64
public void setRequiredLaps ( int laps )
30
65
{
31
66
requiredLaps = laps ;
32
67
}
68
+ public ConsoleColor getColour ( )
69
+ {
70
+ if ( ID == 1 )
71
+ return ConsoleColor . DarkBlue ;
72
+ else
73
+ return ConsoleColor . DarkRed ;
74
+ }
33
75
public void go ( )
34
76
{
77
+ motion = true ;
35
78
comms . send ( ID , "GO" ) ;
36
79
}
37
80
public void stop ( )
38
81
{
82
+ motion = false ;
39
83
comms . send ( ID , "STOP" ) ;
40
84
}
41
85
public void sendPing ( )
@@ -46,20 +90,28 @@ public void sendPong()
46
90
{
47
91
comms . send ( ID , "PONG" ) ;
48
92
}
93
+ public bool syn ( bool silent = false )
94
+ {
95
+ return comms . send ( ID , "SYN" , ( ) =>
96
+ {
97
+ if ( ! silent )
98
+ buggyAction ( "is Offline! \n Will keep pinging buggy " + ID ) ;
99
+ } ) ;
100
+ }
49
101
public void onGantry ( int gantry_num )
50
102
{
51
103
stop ( ) ;
52
104
Thread . Sleep ( 1000 ) ;
53
105
54
106
if ( ( ( direction == Direction . Clockwise ) && ( gantry_num == 2 ) ) || ( ( direction == Direction . AntiClockwise ) && ( gantry_num == 1 ) ) )
55
107
laps ++ ;
56
- else if ( ( ( direction == Direction . Clockwise ) && ( last_gantry == 1 ) && ( gantry_num != 2 ) )
108
+ else if ( ( ( direction == Direction . Clockwise ) && ( last_gantry == 1 ) && ( gantry_num != 2 ) )
57
109
|| ( ( direction == Direction . AntiClockwise ) && ( last_gantry == 2 ) && ( gantry_num != 1 ) ) ) {
58
110
laps ++ ;
59
111
}
60
112
go ( ) ;
61
113
last_gantry = gantry_num ;
62
- trackState ( "Gantry" , gantry_num ) ;
114
+ trackState ( "Gantry" ) ;
63
115
if ( station . getNumberOfBuggies ( ) < 2 )
64
116
{
65
117
if ( laps >= requiredLaps && gantry_num == 2 )
@@ -88,99 +140,97 @@ public void onGantry(int gantry_num)
88
140
}
89
141
public void goPark ( )
90
142
{
91
- comms . send ( ID , "PARK" ) ;
143
+ comms . send ( ID , "PARK" ) ;
144
+ }
145
+ public void obstacle ( string condition )
146
+ {
147
+ trackState ( condition ) ;
92
148
}
93
149
public void buggyParked ( )
94
150
{
151
+ motion = false ;
95
152
if ( direction == Direction . AntiClockwise )
96
153
{
97
154
station . buggySwitch ( ID ) ;
98
- Program . print ( "Buggy " + ID + " is in the park lane" ) ;
155
+ buggyAction ( " is in the park lane" ) ;
99
156
}
100
157
else
101
158
{
102
159
if ( station . getNumberOfBuggies ( ) == 1 )
103
- Program . print ( "Buggy " + ID + " parked! " + ( laps ) + " lap(s) completed!" ) ;
160
+ buggyAction ( " parked! " + ( laps ) + " lap(s) completed!" ) ;
104
161
else
105
- Program . print ( "Buggy " + ID + " parked! " + ( laps - 1 ) + " lap(s) completed!" ) ;
162
+ buggyAction ( " parked! " + ( laps - 1 ) + " lap(s) completed!" ) ;
106
163
}
107
164
}
108
- private void sendDirection ( )
109
- {
110
- if ( direction == Direction . AntiClockwise )
111
- {
112
- comms . send ( ID , "ACLOCK" ) ;
113
- }
114
- else
115
- {
116
- comms . send ( ID , "CLOCK" ) ;
117
- }
118
- }
119
-
120
165
public void pingRecieved ( )
121
166
{
122
- Program . print ( "PING recieved" ) ;
167
+ buggyAction ( "PING recieved" ) ;
123
168
}
124
169
public void pongRecieved ( )
125
170
{
126
- Program . print ( "PONG recieved" ) ;
171
+ buggyAction ( "PONG recieved" ) ;
127
172
}
128
173
public void going ( )
129
174
{
130
-
175
+ buggyAction ( "GOING" ) ;
131
176
}
132
177
public void stopped ( )
133
178
{
134
-
179
+ buggyAction ( "STOPPED" ) ;
135
180
}
136
- private void buggyAction ( )
181
+ private void buggyAction ( String command = "" )
137
182
{
138
- Console . Write ( "> Buggy " + ID + ": " ) ;
183
+ Program . print ( " Buggy " + ID + " " + command , getColour ( ) ) ;
139
184
}
140
- private void trackState ( string call , int num )
185
+ private void trackState ( string call )
141
186
{
142
- buggyAction ( ) ;
143
- if ( direction == Direction . AntiClockwise )
187
+ int section ;
188
+ if ( direction == Direction . Clockwise )
189
+ section = last_gantry ;
190
+ else
144
191
{
145
- if ( num == 1 )
146
- num = num + 2 ;
192
+ if ( last_gantry == 1 )
193
+ section = 3 ;
147
194
else
148
- num -- ;
195
+ section = last_gantry - 1 ;
149
196
}
150
197
if ( call == "Gantry" ) {
151
198
onLap ( ) ;
152
- Console . Write ( ( " stopped at gantry " + last_gantry + " Entering track section: " ) ) ;
153
- if ( ( laps >= requiredLaps && last_gantry == 2 )
154
- || ( direction == Direction . AntiClockwise && last_gantry == 1 ) ) {
155
- Console . WriteLine ( ( "Park Lane" ) ) ;
156
- return ;
199
+ if ( last_gantry == - 10 )
200
+ buggyAction ( "gantry interpreted as invalid" ) ;
201
+ else
202
+ {
203
+ buggyAction ( "stopped at gantry " + last_gantry ) ;
204
+
205
+ string sectionString ;
206
+ if ( ( direction == Direction . Clockwise && laps >= requiredLaps && last_gantry == 2 )
207
+ || ( direction == Direction . AntiClockwise && last_gantry == 1 ) )
208
+ sectionString = "Park Lane" ;
209
+ else
210
+ sectionString = section . ToString ( ) ;
211
+ buggyAction ( "entering track section: " + sectionString ) ;
157
212
}
158
- Console . WriteLine ( ( num . ToString ( ) ) ) ;
159
213
}
160
214
else if ( call == "Stop" )
161
215
{
162
- Console . WriteLine ( ( " has stopped in section " + num ) ) ;
216
+ buggyAction ( " has stopped in section " + section ) ;
163
217
}
164
218
else if ( call == "Go" )
165
219
{
166
- Console . WriteLine ( ( " is on the move in section " + num ) ) ;
220
+ buggyAction ( " is on the move in section " + section ) ;
167
221
}
168
- }
169
- private void onLap ( )
170
- {
171
- buggyAction ( ) ;
172
- Console . WriteLine ( "is on lap " + laps ) ;
173
- }
174
- private int earlyAction ( )
175
- {
176
- int a ;
222
+ else if ( call == "OBSTACLE" )
177
223
{
178
- if ( direction == Direction . AntiClockwise )
179
- a = 2 ;
180
- else
181
- a = 3 ;
224
+ buggyAction ( "has detected an obstacle in section " + section ) ;
182
225
}
183
- return a ;
226
+ else if ( call == "PATHCLEAR" )
227
+ {
228
+ buggyAction ( "is now able to progress in section " + section ) ;
229
+ }
230
+ }
231
+ private void onLap ( )
232
+ {
233
+ buggyAction ( "is on lap " + laps ) ;
184
234
}
185
235
}
186
236
}
0 commit comments