-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallclasses.rb
425 lines (367 loc) · 9.9 KB
/
allclasses.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#Enum by classes
class Class
def directSubclassesSince
#puts "#{self}->#@directSubclassesSince"
@directSubclassesSince ||= []
end
def allDescendantsSince
all = []
#puts "1#{self}->#@directSubclassesSince"
#puts "2#{self}->#{directSubclassesSince}"
directSubclassesSince.each {|subclass|
all = all << subclass
all = all | subclass.allDescendantsSince}
return all
end
def randomSubclass
allDescendantsSince().randomElement;
end
def inherited(subclass)
#not sure how to force any other implementation of inherited to be called as well
self.directSubclassesSince << subclass
#puts "inherited #{self}->#@directSubclassesSince"
end
end
class Array
def collect_if_not_nil(&elementToCollectBlock)
return self.collect(&elementToCollectBlock).select {|e| !e.nil?}
end
def randomElement
return self[rand(self.size)]
end
end
class String
def asCommand
return Command.fromStringOrNil(self)
end
def randomishChange
return randomOneCharChange if rand(5)==0
return ""
end
def randomOneCharChange
changed=self.clone
if rand(3)==0 # 1 time in 3 replace a character
changed[rand(changed.size)]= String.randomAsciiOfLength(1)
elsif rand(2)==0 # 1 time in 3 delete a character - there are two chances left
changed[rand(changed.size)]=''
else # 1 time in 3 add a character
index = rand(changed.size)
changed[index]= (changed[index]<< String.randomAsciiOfLength(1))
end
return changed
end
def self.randomAsciiOfLength(length)
raise "cant be zero or negative length" if length<1
return rand(128).chr if length ==1
return randomAsciiOfLength(length-1)<< randomAsciiOfLength(1)
end
end
class Command
def self.fromStringOrNil(s)
commandElements = s.strip.split(/[ ,]/)
return nil if commandElements.size<1
commandName = commandElements.first.strip
commands = self.allDescendantsSince.collect_if_not_nil {
|clazz|
clazz.fromCommandNameAndCommandElementsOrNil(commandName,commandElements)
}
raise if commands.size>1
return commands.first if commands.size==1
return nil
end
def asCommand()
return self
end
def self.randomCommandInputLine
randomCommandType= self.randomSubclass
while randomCommandType.equal?(Place) && rand(10)!=0
randomCommandType= self.randomSubclass #only choose place 1 in 10 times it is chosen
end
inputLine = randomCommandType.randomInputLine()
inputLine=inputLine.randomishChange() if rand(10)==0
return inputLine
end
end
class Place < Command
def initialize(heading ,position)
@heading,@position=heading,position
end
def executeOn(robot)
return robot.place(@heading,@position)
end
def self.fromCommandNameAndCommandElementsOrNil(commandName,commandElements)
return nil if commandName != "PLACE"
return nil if commandElements.size!= 4
position = PositionOnTable.fromTwoStringsAndOnTableOrNil(commandElements[1],commandElements[2]);
heading= Heading.fromStringOrNil(commandElements[3].strip)
return nil if heading.nil? || position.nil? || position.notOnTable?
return self.new(heading,position)
end
def self.randomInputLine()
return "PLACE #{rand(8)-1},#{rand(8)-1},#{Heading.random}"
end
end
class Move < Command
def executeOn(robot)
return robot.moveIfOnTable()
end
def self.fromCommandNameAndCommandElementsOrNil(commandName,commandElements)
return nil if commandName != "MOVE" || commandElements.size>1
return Move.new()
end
def self.randomInputLine()
return "MOVE"
end
end
class Report < Command
def executeOn(robot)
return robot.reportIfOnTable()
end
def self.fromCommandNameAndCommandElementsOrNil(commandName,commandElements)
return nil if commandName != "REPORT" || commandElements.size>1
return Report.new()
end
def self.randomInputLine()
return "REPORT"
end
end
class Left < Command
def executeOn(robot)
return robot.turnLeftIfOnTable()
end
def self.fromCommandNameAndCommandElementsOrNil(commandName,commandElements)
return nil if commandName != "LEFT" || commandElements.size>1
return Left.new()
end
def self.randomInputLine()
return "LEFT"
end
end
class Right < Command
def executeOn(robot)
return robot.turnRightIfOnTable()
end
def self.fromCommandNameAndCommandElementsOrNil(commandName,commandElements)
return nil if commandName != "RIGHT" || commandElements.size>1
return Right.new()
end
def self.randomInputLine()
return "RIGHT"
end
end
#Enum by singleton classes
class Heading
def self.headings
@@headings ||=[]
return @@headings
end
def self.random
return @@headings.randomElement
end
def initialize()
self.class.headings<<self
end
def self.fromStringOrNil(s)
heading = self.headings.detect { |h| h.name == s.strip }
return heading
end
def to_s
return "#{name}"
end
end
$north=Heading.new
class << $north
def turnedLeft
$west
end
def turnedRight
$east
end
def moveXy
[0,1]
end
def name
return "NORTH"
end
end
$south=Heading.new
class << $south
def turnedLeft
$east
end
def turnedRight
$west
end
def moveXy
[0,-1]
end
def name
return "SOUTH"
end
end
$east=Heading.new
class << $east
def turnedLeft
$north
end
def turnedRight
$south
end
def moveXy
[1,0]
end
def name
return "EAST"
end
end
$west=Heading.new
class << $west
def turnedLeft
$south
end
def turnedRight
$north
end
def moveXy
[-1,0]
end
def name
return "WEST"
end
end
class PositionOnTable
def initialize(x,y)
@xy=[x,y]
end
def moved(heading)
x= @xy.first+heading.moveXy.first
[email protected]+heading.moveXy.last
return PositionOnTable.new(x,y) unless self.class.notOnTable?(x,y)
return self
end
def to_s_unbracketed
return "#{@xy.first.to_s},#{@xy.last.to_s}"
end
def to_s
"#{@xy}"
end
def self.fromTwoStringsAndOnTableOrNil(xs,ys)
x =xs.to_i
y =ys.to_i
#make sure the number strings are just numbers
return nil if x.to_s != xs.strip || y.to_s != ys.strip
return nil if self.notOnTable?(x,y)
return self.new(x,y)
end
def notOnTable?
return self.class.notOnTable?(@xy.first,@xy.last)
end
def self.notOnTable?(x,y)
return x<0 || x>5 ||y<0 ||y>5
end
end
class Robot2
def self.run
r= self.new
ARGF.each_line do |s|
cmd = Command.fromStringOrNil(s)
report=nil
report = cmd.executeOn(r) unless cmd.nil?
puts report unless report.nil?
end
end
def to_s
return "#{@heading} #{@position}"
end
def onTable
return [email protected]?
end
def executeCommands(commands)
return commands.executeOn(self) if commands.respond_to?(:executeOn)
return self.executeCommands(Commands.new(commands)) if commands.respond_to?(:each)
return self.executeCommands(Commands.fromString(commands))
end
=begin
(def heading() @heading end
def heading=(headingX)
#raise "heading must be a Heading" if headingX.class.equals Heading
#raise "heading cant be nil" if headingX.nil?
@heading=headingX
end
def position() @position end
def position=(position) @position=position end
=end
def place(headingX,positionX)
@heading= headingX
@position=positionX
#puts "at end of #{__method__} robot = #{self}"
return nil
end
def moveIfOnTable()
@position = @position.moved(@heading) if onTable
#puts "at end of #{__method__} robot = #{self.to_s}"
return nil
end
def turnLeftIfOnTable()
@[email protected] if onTable
#puts "at end of #{__method__} robot = #{self.to_s}"
return nil
end
def turnRightIfOnTable()
@[email protected] if onTable
#puts "at end of #{__method__} robot = #{self.to_s}"
return nil
end
def reportIfOnTable()
return "#{@position.to_s_unbracketed},#{@heading.name}" if onTable
#puts "at end of #{__method__} robot = #{self.to_s}"
return nil
end
end
class Commands
include Enumerable
def initialize(commands)
@commands=commands.collect_if_not_nil{|c|
if c.nil?
puts c
end
c.asCommand
}
end
def each(&block)
@commands.each(&block)
end
def executeOn(robot)
return @commands.collect_if_not_nil {|c| c.executeOn(robot)}
#puts "hi"
end
def self.fromString(s)
commands = s.split("\n")
return self.new(commands)
end
def self.newRandom
cmds = 1.to(rand(200)).collect {Command.newRandom}
return Commands.new(cmds)
end
def self.randomCommandsInput
input = ""
lines = (1..rand(200)).collect {|i| Command.randomCommandInputLine}
if (lines.select {|line| !line.index("PLACE").nil?}).size==0
lines[rand(lines.size)] = Place.randomInputLine unless rand(5)==0 # ensure there is a place command most of the time
end
lines.each {
|cmdInput|
if input.nil?
input=cmdInput
else
input=input << "\n#{cmdInput}"
end
}
return input
end
end
if __FILE__ == $0
#puts Robot2.new.executeCommands ["PLACE 0,1,WEST", "MOVE", "REPORT"]
# puts String.randomAsciiOfLength(1)
Robot2.run
end