-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarsRoversSinIfs.st
519 lines (344 loc) · 14.6 KB
/
MarsRoversSinIfs.st
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
!classDefinition: #TestMarsRovers category: #MarsRovers!
TestCase subclass: #TestMarsRovers
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'MarsRovers'!
!TestMarsRovers methodsFor: 'cardinal points' stamp: 'ml 10/13/2024 21:37:43'!
east
^ East new.
! !
!TestMarsRovers methodsFor: 'cardinal points' stamp: 'ml 10/13/2024 21:38:00'!
north
^ North new.
! !
!TestMarsRovers methodsFor: 'cardinal points' stamp: 'ml 10/13/2024 21:38:13'!
south
^ South new.
! !
!TestMarsRovers methodsFor: 'cardinal points' stamp: 'ml 10/13/2024 21:37:27'!
west
^ West new.
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:39:45'!
test01anEmptyCommandLineShouldNotMoveMarsRover
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self west.
robotito receiveCommandLine: ''.
self assert: (robotito hasPosition: 10@10 lookingAt: self west).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:40:12'!
test02rotateRight
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self west.
robotito receiveCommandLine: 'r'.
self assert: (robotito hasPosition: 10@10 lookingAt: self north).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:40:34'!
test03rotateRight180degrees
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self west.
robotito receiveCommandLine: 'rr'.
self assert: (robotito hasPosition: 10@10 lookingAt: self east).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:40:58'!
test04rotateRight270degrees
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self west.
robotito receiveCommandLine: 'rrr'.
self assert: (robotito hasPosition: 10@10 lookingAt: self south).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:41:20'!
test05rotateRight360degrees
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self west .
robotito receiveCommandLine: 'rrrr'.
self assert: (robotito hasPosition: 10@10 lookingAt: self west ).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:41:43'!
test06rotateLeft
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self west .
robotito receiveCommandLine: 'l'.
self assert: (robotito hasPosition: 10@10 lookingAt: self south ).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:42:11'!
test07rotateLeft180degrees
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self west .
robotito receiveCommandLine: 'll'.
self assert: (robotito hasPosition: 10@10 lookingAt: self east ).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:42:30'!
test08fowardLookingWest
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self west .
robotito receiveCommandLine: 'f'.
self assert: (robotito hasPosition: 9@10 lookingAt: self west ).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:42:49'!
test09fowardLookingSouth
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self south .
robotito receiveCommandLine: 'f'.
self assert: (robotito hasPosition: 10@9 lookingAt: self south ).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:43:09'!
test10fowardLookingNorth
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self north .
robotito receiveCommandLine: 'f'.
self assert: (robotito hasPosition: 10@11 lookingAt: self north ).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:43:29'!
test11fowardLookingEast
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self east .
robotito receiveCommandLine: 'f'.
self assert: (robotito hasPosition: 11@10 lookingAt: self east ).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:43:48'!
test12backwardLookingEast
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self east .
robotito receiveCommandLine: 'b'.
self assert: (robotito hasPosition: 9@10 lookingAt: self east ).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:44:09'!
test13backwardLookingWest
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self west .
robotito receiveCommandLine: 'b'.
self assert: (robotito hasPosition: 11@10 lookingAt: self west ).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:44:28'!
test14backwardLookingNorth
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self north .
robotito receiveCommandLine: 'b'.
self assert: (robotito hasPosition: 10@9 lookingAt: self north ).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:44:51'!
test15backwardLookingSouth
| robotito |
robotito := MarsRover inPosition: 10@10 lookingAt: self south .
robotito receiveCommandLine: 'b'.
self assert: (robotito hasPosition: 10@11 lookingAt: self south ).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:45:17'!
test16makeAnLFormFoward
| robotito |
robotito := MarsRover inPosition: 0@0 lookingAt: self south .
robotito receiveCommandLine: 'fflf'.
self assert: (robotito hasPosition: 1@-2 lookingAt: self east ).
! !
!TestMarsRovers methodsFor: 'tests' stamp: 'ml 10/13/2024 21:45:45'!
test17makeAnLFormReverse
| robotito |
robotito := MarsRover inPosition: 1@-2 lookingAt: self east .
robotito receiveCommandLine: 'brbb'.
self assert: (robotito hasPosition: 0@0 lookingAt: self south ).
! !
!classDefinition: #MarsRover category: #MarsRovers!
Object subclass: #MarsRover
instanceVariableNames: 'position lookingDirection'
classVariableNames: ''
poolDictionaries: ''
category: 'MarsRovers'!
!MarsRover methodsFor: 'initialize' stamp: 'ml 10/13/2024 20:34:37'!
startingAt: aPosition lookingAt: aCardinalPoint
position := aPosition .
lookingDirection := aCardinalPoint.! !
!MarsRover methodsFor: 'testing' stamp: 'ml 10/13/2024 20:40:46'!
hasPosition: aPositionToCompare lookingAt: aCardinalPointToCompare
^(position = aPositionToCompare) and: (lookingDirection class = aCardinalPointToCompare class)! !
!MarsRover methodsFor: 'receive commands' stamp: 'ml 10/12/2024 01:02:31'!
receiveCommandLine: aCommandString
aCommandString collect: [:aCommand |
aCommand = $r ifTrue: [self rotateRight].
aCommand = $l ifTrue: [self rotateLeft].
aCommand = $f ifTrue: [self goFoward].
aCommand = $b ifTrue: [self goBackward]
].! !
!MarsRover methodsFor: 'execute commands' stamp: 'ml 10/13/2024 21:14:32'!
goBackward
lookingDirection movingBackwardBy: self at: position.
"lookingDirection = #East ifTrue: [self moveToWest: position].
lookingDirection = #West ifTrue: [self moveToEast: position].
lookingDirection = #North ifTrue: [self moveToSouth: position].
lookingDirection = #South ifTrue: [self moveToNorth: position]."! !
!MarsRover methodsFor: 'execute commands' stamp: 'ml 10/13/2024 21:11:42'!
goFoward
lookingDirection movingFowardBy: self at: position.
"lookingDirection = #West ifTrue: [self moveToWest: position].
lookingDirection = #South ifTrue: [self moveToSouth: position].
lookingDirection = #East ifTrue: [self moveToEast: position].
lookingDirection = #North ifTrue: [self moveToNorth: position]."! !
!MarsRover methodsFor: 'execute commands' stamp: 'ml 10/13/2024 21:11:46'!
rotateLeft
lookingDirection rotatingLeftBy: self.
"lookingDirection = #North ifTrue: [lookingDirection := #West. ^self].
lookingDirection = #East ifTrue: [lookingDirection := #North. ^self].
lookingDirection = #South ifTrue: [lookingDirection := #East. ^self].
lookingDirection = #West ifTrue: [lookingDirection := #South. ^self]."
! !
!MarsRover methodsFor: 'execute commands' stamp: 'ml 10/13/2024 21:11:49'!
rotateRight
lookingDirection rotatingRigthBy: self.
"lookingAt = #South ifTrue: [lookingAt := #West. ^self.].
lookingAt = #East ifTrue: [lookingAt := #South. ^self.].
lookingAt = #North ifTrue: [lookingAt := #East. ^self.].
lookingAt = #West ifTrue: [lookingAt := #North. ^self.]."! !
!MarsRover methodsFor: 'rotate left' stamp: 'ml 10/13/2024 21:12:06'!
eastRotateLeft
lookingDirection := North new.
"lookingDirection = #North ifTrue: [lookingDirection := #West. ^self].
lookingDirection = #East ifTrue: [lookingDirection := #North. ^self].
lookingDirection = #South ifTrue: [lookingDirection := #East. ^self].
lookingDirection = #West ifTrue: [lookingDirection := #South. ^self]."! !
!MarsRover methodsFor: 'rotate left' stamp: 'ml 10/13/2024 21:12:03'!
northRotateLeft
lookingDirection := West new.
"lookingDirection = #North ifTrue: [lookingDirection := #West. ^self].
lookingDirection = #East ifTrue: [lookingDirection := #North. ^self].
lookingDirection = #South ifTrue: [lookingDirection := #East. ^self].
lookingDirection = #West ifTrue: [lookingDirection := #South. ^self]."
! !
!MarsRover methodsFor: 'rotate left' stamp: 'ml 10/13/2024 20:59:02'!
southRotateLeft
lookingDirection := East new.
"lookingDirection = #North ifTrue: [lookingDirection := #West. ^self].
lookingDirection = #East ifTrue: [lookingDirection := #North. ^self].
lookingDirection = #South ifTrue: [lookingDirection := #East. ^self].
lookingDirection = #West ifTrue: [lookingDirection := #South. ^self]."! !
!MarsRover methodsFor: 'rotate left' stamp: 'ml 10/13/2024 21:11:58'!
westRotateLeft
lookingDirection := South new.
"lookingDirection = #North ifTrue: [lookingDirection := #West. ^self].
lookingDirection = #East ifTrue: [lookingDirection := #North. ^self].
lookingDirection = #South ifTrue: [lookingDirection := #East. ^self].
lookingDirection = #West ifTrue: [lookingDirection := #South. ^self]."! !
!MarsRover methodsFor: 'rotate right' stamp: 'ml 10/13/2024 21:12:20'!
eastRotateRight
lookingDirection := South new.
! !
!MarsRover methodsFor: 'rotate right' stamp: 'ml 10/13/2024 20:43:51'!
northRotateRight
lookingDirection := East new.
! !
!MarsRover methodsFor: 'rotate right' stamp: 'ml 10/13/2024 21:12:13'!
southRotateRight
lookingDirection := West new.
! !
!MarsRover methodsFor: 'rotate right' stamp: 'ml 10/13/2024 20:24:26'!
westRotateRight
lookingDirection := North new.! !
!MarsRover methodsFor: 'move' stamp: 'ml 10/12/2024 01:12:35'!
moveToEast: actualPosition
position := (actualPosition x + 1)@(actualPosition y).! !
!MarsRover methodsFor: 'move' stamp: 'ml 10/12/2024 01:10:24'!
moveToNorth: actualPosition
position := (actualPosition x)@(actualPosition y + 1)! !
!MarsRover methodsFor: 'move' stamp: 'ml 10/12/2024 01:12:38'!
moveToSouth: actualPosition
position := (actualPosition x)@(actualPosition y - 1)! !
!MarsRover methodsFor: 'move' stamp: 'ml 10/12/2024 01:12:41'!
moveToWest: actualPosition
position := ((actualPosition x) - 1)@(actualPosition y).! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
!classDefinition: 'MarsRover class' category: #MarsRovers!
MarsRover class
instanceVariableNames: ''!
!MarsRover class methodsFor: 'as yet unclassified' stamp: 'ml 10/11/2024 20:44:51'!
inPosition: aPosition lookingAt: aCardinalPoint
^ MarsRover new startingAt: aPosition lookingAt: aCardinalPoint .! !
!classDefinition: #MarsRoverDirection category: #MarsRovers!
Object subclass: #MarsRoverDirection
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'MarsRovers'!
!MarsRoverDirection methodsFor: 'rotating' stamp: 'ml 10/13/2024 20:56:10'!
rotatingLeftBy: aMarsRover
self subclassResponsibility! !
!MarsRoverDirection methodsFor: 'rotating' stamp: 'ml 10/13/2024 20:29:45'!
rotatingRigthBy: aMarsRover
self subclassResponsibility! !
!MarsRoverDirection methodsFor: 'moving' stamp: 'ml 10/13/2024 21:16:35'!
movingBackwardBy: aMarsRover at: actualPosition
self subclassResponsibility! !
!MarsRoverDirection methodsFor: 'moving' stamp: 'ml 10/13/2024 21:10:51'!
movingFowardBy: aMarsRover at: actualPosition
self subclassResponsibility! !
!classDefinition: #East category: #MarsRovers!
MarsRoverDirection subclass: #East
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'MarsRovers'!
!East methodsFor: 'rotating' stamp: 'ml 10/13/2024 20:57:40'!
rotatingLeftBy: aMarsRover
aMarsRover eastRotateLeft .! !
!East methodsFor: 'rotating' stamp: 'ml 10/13/2024 20:42:10'!
rotatingRigthBy: aMarsRover
aMarsRover eastRotateRight .! !
!East methodsFor: 'moving' stamp: 'ml 10/13/2024 21:17:18'!
movingBackwardBy: aMarsRover at: actualPosition
aMarsRover moveToWest: actualPosition .! !
!East methodsFor: 'moving' stamp: 'ml 10/13/2024 21:10:40'!
movingFowardBy: aMarsRover at: actualPosition
aMarsRover moveToEast: actualPosition .! !
!classDefinition: #North category: #MarsRovers!
MarsRoverDirection subclass: #North
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'MarsRovers'!
!North methodsFor: 'rotating' stamp: 'ml 10/13/2024 20:57:22'!
rotatingLeftBy: aMarsRover
aMarsRover northRotateLeft .! !
!North methodsFor: 'rotating' stamp: 'ml 10/13/2024 20:41:59'!
rotatingRigthBy: aMarsRover
aMarsRover northRotateRight .! !
!North methodsFor: 'moving' stamp: 'ml 10/13/2024 21:17:58'!
movingBackwardBy: aMarsRover at: actualPosition
aMarsRover moveToSouth: actualPosition .! !
!North methodsFor: 'moving' stamp: 'ml 10/13/2024 21:09:38'!
movingFowardBy: aMarsRover at: actualPosition
aMarsRover moveToNorth: actualPosition .! !
!classDefinition: #South category: #MarsRovers!
MarsRoverDirection subclass: #South
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'MarsRovers'!
!South methodsFor: 'rotating' stamp: 'ml 10/13/2024 20:57:09'!
rotatingLeftBy: aMarsRover
aMarsRover southRotateLeft .! !
!South methodsFor: 'rotating' stamp: 'ml 10/13/2024 20:41:43'!
rotatingRigthBy: aMarsRover
aMarsRover southRotateRight .! !
!South methodsFor: 'moving' stamp: 'ml 10/13/2024 21:18:14'!
movingBackwardBy: aMarsRover at: actualPosition
aMarsRover moveToNorth: actualPosition .! !
!South methodsFor: 'moving' stamp: 'ml 10/13/2024 21:09:09'!
movingFowardBy: aMarsRover at: actualPosition
aMarsRover moveToSouth: actualPosition .! !
!classDefinition: #West category: #MarsRovers!
MarsRoverDirection subclass: #West
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'MarsRovers'!
!West methodsFor: 'rotating' stamp: 'ml 10/13/2024 20:56:56'!
rotatingLeftBy: aMarsRover
aMarsRover westRotateLeft. ! !
!West methodsFor: 'rotating' stamp: 'ml 10/13/2024 20:30:05'!
rotatingRigthBy: aMarsRover
aMarsRover westRotateRight .! !
!West methodsFor: 'moving' stamp: 'ml 10/13/2024 21:18:32'!
movingBackwardBy: aMarsRover at: actualPosition
aMarsRover moveToEast: actualPosition .! !
!West methodsFor: 'moving' stamp: 'ml 10/13/2024 21:08:34'!
movingFowardBy: aMarsRover at: actualPosition
aMarsRover moveToWest: actualPosition.! !