Commit 18aeec63 authored by Adrian Kosmaczewski's avatar Adrian Kosmaczewski

Style similar to that seen in other apps

parent 35c24ca4
......@@ -44,7 +44,6 @@ FreeBASIC 5 37 4 290
PHP 5 34 11 252
Go 5 42 21 252
Rust 5 32 3 252
Smalltalk 5 44 0 250
Visual Basic .NET 5 32 1 248
C++ 5 35 8 234
......@@ -54,6 +53,7 @@ C# 5 33 1 229
TypeScript 5 28 6 216
Lua 5 28 6 215
Smalltalk 5 41 0 208
Swift 5 33 29 204
Ruby 5 28 1 192
Kotlin 5 26 1 186
......
......@@ -6,12 +6,9 @@ Object subclass: Cell [
| c |
c := super new.
c initAlive: alive.
^c
]
^c ]
initAlive: alive [
state := alive.
]
initAlive: alive [ state := alive. ]
aliveValue := Cell newAlive: true.
deadValue := Cell newAlive: false.
......@@ -19,8 +16,7 @@ Object subclass: Cell [
printOn: stream [
state
ifTrue: [ ' x |' displayOn: stream ]
ifFalse: [ ' |' displayOn: stream ].
]
ifFalse: [ ' |' displayOn: stream ] ]
]
Cell class extend [
......
......@@ -6,19 +6,16 @@ Object subclass: Coord [
| c |
c := super new.
c initWithX:x y:y.
^c
]
^c ]
initWithX:xValue y:yValue [
x := xValue.
y := yValue.
]
y := yValue ]
printOn: stream [
x displayOn: stream.
':' displayOn: stream.
y displayOn: stream.
]
y displayOn: stream ]
x [ ^x ]
......
......@@ -25,15 +25,12 @@ Object subclass: Conway [
generation printNl.
(Delay forMilliseconds:500) wait.
world := world evolve.
] repeat
]
] repeat ]
Conway class >> clrscr [
<comment: 'I clear the screen, courtesy of https://stackoverflow.com/a/24833963 and https://stackoverflow.com/a/37778152/133764'>
('%1[2J' % #($<16r1B>)) displayNl.
('%1[H' % #($<16r1B>)) displayNl.
]
]
('%1[H' % #($<16r1B>)) displayNl ] ]
Conway main.
......@@ -10,16 +10,14 @@ TestCase subclass: ConwayTests [
alive := World block:(Coord newWithX:0 y:0).
original := World newWithSize:5 aliveCells:alive.
next := original evolve.
self assert: next = original.
]
self assert: next = original ]
testTub [
| alive original next |
alive := World tub:(Coord newWithX:0 y:0).
original := World newWithSize:5 aliveCells:alive.
next := original evolve.
self assert: next = original.
]
self assert: next = original ]
testBlinker [
| alive original gen1 gen2 expectedAlive expected |
......@@ -33,9 +31,7 @@ TestCase subclass: ConwayTests [
expected := World newWithSize:3 aliveCells:expectedAlive.
self assert: expected = gen1.
gen2 := gen1 evolve.
self assert: original = gen2.
]
]
self assert: original = gen2 ] ]
ConwayTests suite run.
......@@ -6,8 +6,7 @@ Object subclass: World [
| w |
w := super new.
w initWithSize:size aliveCells:cells.
^w
]
^w ]
initWithSize:newSize aliveCells:aliveCells [
| coord interval |
......@@ -20,10 +19,7 @@ Object subclass: World [
coord := Coord newWithX:a y:b.
(aliveCells includes:coord)
ifTrue: [ cells at:coord put:Cell alive ]
ifFalse: [ cells at:coord put:Cell dead ].
]
]
]
ifFalse: [ cells at:coord put:Cell dead ] ] ] ]
evolve [
| alive |
......@@ -39,19 +35,11 @@ Object subclass: World [
(coord = currentCoord) ifFalse: [ (cells includesKey:currentCoord) ifTrue: [
| currentCell |
currentCell := cells at: currentCoord.
currentCell = Cell alive ifTrue: [ count := count + 1 ]
] ]
]
].
currentCell = Cell alive ifTrue: [ count := count + 1 ] ] ] ] ].
cell = Cell alive ifTrue: [
(count = 2 or: [ count = 3 ]) ifTrue: [ alive add: coord ].
].
cell = Cell alive ifTrue: [ (count = 2 or: [ count = 3 ]) ifTrue: [ alive add: coord ] ].
cell = Cell dead ifTrue: [ count = 3 ifTrue: [ alive add: coord ] ] ].
cell = Cell dead ifTrue: [
count = 3 ifTrue: [ alive add: coord ].
]
].
^ World newWithSize: size aliveCells: alive.
]
......@@ -64,34 +52,24 @@ Object subclass: World [
' ' displayOn: stream.
interval do: [ :b |
(b printPaddedWith: Character space to:3) displayOn: stream.
'|' displayOn: stream.
].
'|' displayOn: stream ].
Character cr displayOn: stream.
Character lf displayOn: stream.
].
Character lf displayOn: stream ].
(a printPaddedWith: Character space to:3) displayOn: stream.
'|' displayOn: stream.
interval do: [ :b |
| cell coord |
coord := Coord newWithX:b y:a.
cell := cells at:coord.
cell displayOn: stream.
].
cell displayOn: stream ].
Character cr displayOn: stream.
Character lf displayOn: stream.
]
]
Character lf displayOn: stream ] ]
size [ ^size ]
cells [ ^cells ]
= arg [
size = arg size ifFalse: [ ^false ].
cells = arg cells ifFalse: [ ^false ].
^true.
]
]
= arg [ ^ size = arg size and: [ cells = arg cells ] ] ]
World class extend [
blinker:coord [
......@@ -100,8 +78,7 @@ World class extend [
result add:(Coord newWithX:coord x y:coord y).
result add:(Coord newWithX:coord x + 1 y:coord y).
result add:(Coord newWithX:coord x + 2 y:coord y).
^result.
]
^result ]
beacon:coord [
| result |
......@@ -114,8 +91,7 @@ World class extend [
result add:(Coord newWithX:coord x + 3 y:coord y + 2).
result add:(Coord newWithX:coord x + 2 y:coord y + 3).
result add:(Coord newWithX:coord x + 3 y:coord y + 3).
^result.
]
^result ]
glider:coord [
| result |
......@@ -125,8 +101,7 @@ World class extend [
result add:(Coord newWithX:coord x y:coord y + 2).
result add:(Coord newWithX:coord x + 2 y:coord y + 1).
result add:(Coord newWithX:coord x + 1 y:coord y).
^result.
]
^result ]
block:coord [
| result |
......@@ -135,8 +110,7 @@ World class extend [
result add:(Coord newWithX:coord x + 1 y:coord y).
result add:(Coord newWithX:coord x y:coord y + 1).
result add:(Coord newWithX:coord x + 1 y:coord y + 1).
^result.
]
^result ]
tub:coord [
| result |
......@@ -145,7 +119,4 @@ World class extend [
result add:(Coord newWithX:coord x y:coord y + 1).
result add:(Coord newWithX:coord x + 2 y:coord y + 1).
result add:(Coord newWithX:coord x + 1 y:coord y + 2).
^result.
]
]
^result ] ]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment