diff --git a/src/Conway-Model-Tests/ConwayCellTest.class.st b/src/Conway-Model-Tests/ConwayCellTest.class.st deleted file mode 100644 index ebb100c..0000000 --- a/src/Conway-Model-Tests/ConwayCellTest.class.st +++ /dev/null @@ -1,11 +0,0 @@ -Class { - #name : #ConwayCellTest, - #superclass : #TestCase, - #category : #'Conway-Model-Tests' -} - -{ #category : #tests } -ConwayCellTest >> testEntityName [ - - self assert: ConwayCell entityName equals: 'Cell' -] diff --git a/src/Conway-Model-Tests/ConwayModelTest.class.st b/src/Conway-Model-Tests/ConwayModelTest.class.st deleted file mode 100644 index 751f806..0000000 --- a/src/Conway-Model-Tests/ConwayModelTest.class.st +++ /dev/null @@ -1,77 +0,0 @@ -Class { - #name : #ConwayModelTest, - #superclass : #TestCase, - #instVars : [ - 'pWindow' - ], - #category : #'Conway-Model-Tests' -} - -{ #category : #tests } -ConwayModelTest >> testACellInConwayModelIsAtDistanceOneToAnotherCell [ - - | randomEntity m | - m := ConwayModel newWithActiveInit: #initSmallGrid. - m initSimulation. - randomEntity := (m allTheEntities: ConwayCell) atRandom. - self - assert: (randomEntity shortestDistanceToCellsVerifying: [ :arg1 | - arg1 state = #dead | (arg1 state = #alive) ]) - equals: 1 -] - -{ #category : #tests } -ConwayModelTest >> testAfterInitializationAConwayModelHas10000Cells [ - - | tmp1 tmp2 | - tmp2 := ConwayModel. - tmp2 initialize. - tmp1 := tmp2 new. - tmp1 initSimulation. - self assert: (tmp1 allTheEntities: ConwayCell) size equals: 10000 -] - -{ #category : #tests } -ConwayModelTest >> testAfterInitializationAConwayModelHas10000Entities [ - - | model modelClass | - modelClass := ConwayModel. - modelClass initialize. - model := modelClass new. - model initSimulation. - self assert: model allTheEntities size equals: 10000 -] - -{ #category : #tests } -ConwayModelTest >> testConwayModelHasNoSocialEntityClass [ - - self assert: ConwayModel socialClasses isEmpty -] - -{ #category : #tests } -ConwayModelTest >> testConwayModelHasOnlyOneSpatialEntityClass [ - - self assert: ConwayModel spatialClasses size equals: 1 -] - -{ #category : #tests } -ConwayModelTest >> testThereIsOneEntityClassInConwayModel [ - - self assert: ConwayModel allEntityClasses size equals: 1 -] - -{ #category : #tests } -ConwayModelTest >> testThereIsTwoClassesInConwayPackage [ - - self assert: ConwayModel allClassesInPackage size equals: 2 -] - -{ #category : #tests } -ConwayModelTest >> testWhenThereIsNoNearestCell [ - - | m randomEntity | - m := (ConwayModel newWithActiveInit: #initSmallGrid). - m initSimulation. - randomEntity := (m allTheEntities: ConwayCell) atRandom. - self should: [ randomEntity shortestDistanceToCellsVerifying: [ :arg1 | arg1 state = #zork ] ] raise: Error -] diff --git a/src/Conway-Model-Tests/package.st b/src/Conway-Model-Tests/package.st index 9ad135a..22f2e38 100644 --- a/src/Conway-Model-Tests/package.st +++ b/src/Conway-Model-Tests/package.st @@ -1 +1 @@ -Package { #name : #'Conway-Model-Tests' } +Package { #name : 'Conway-Model-Tests' } diff --git a/src/Conway-Model/ConwayCell.class.st b/src/Conway-Model/ConwayCell.class.st index ba0838d..220acfe 100644 --- a/src/Conway-Model/ConwayCell.class.st +++ b/src/Conway-Model/ConwayCell.class.st @@ -2,110 +2,65 @@ I'm a cell of a Conway's Game of Life. " Class { - #name : #ConwayCell, - #superclass : #CMSpatialEntityCell, + #name : 'ConwayCell', + #superclass : 'CMSpatialEntityCell', #classVars : [ 'CurrentId' ], - #category : #'Conway-Model' + #category : 'Conway-Model', + #package : 'Conway-Model' } -{ #category : #'pov symbols' } -ConwayCell class >> SpatialEntityPOV_alive [ - - - ^ Array with: 0.0 with: 0.670004 with: 0.0 -] - -{ #category : #'pov symbols' } -ConwayCell class >> SpatialEntityPOV_dead [ - - - ^ Array with: 0.0 with: 0.0 with: 0.0 -] - -{ #category : #'pov symbols' } -ConwayCell class >> SpatialEntityPOV_dead_red [ - - - ^ Array with: 0.5 with: 0.0 with: 0.1 -] - -{ #category : #accessing } -ConwayCell class >> entityName [ - - ^ 'Cell' -] - -{ #category : #'default value' } -ConwayCell class >> state_default [ - - ^ nil -] - -{ #category : #init } +{ #category : 'init' } ConwayCell >> initAllDead [ self state: #dead ] -{ #category : #init } +{ #category : 'init' } ConwayCell >> initRandomly [ - Cormas random < 0.5 + self random < 0.5 ifTrue: [ self state: #dead ] ifFalse: [ self state: #alive ] ] -{ #category : #init } +{ #category : 'init' } ConwayCell >> initTen [ - Cormas random < 0.1 + self random < 0.1 ifTrue: [ self state: #dead ] ifFalse: [ self state: #alive ] ] -{ #category : #init } +{ #category : 'init' } ConwayCell >> initTenPercentDead [ - Cormas random < 0.1 + self random < 0.1 ifTrue: [ self state: #dead ] ifFalse: [ self state: #alive ] ] -{ #category : #probes } -ConwayCell >> isAlive [ - - self state = #alive ifTrue: [ ^ 1 ]. - ^ 0 -] - -{ #category : #control } +{ #category : 'control' } ConwayCell >> newState [ - | tmp1 | - tmp1 := self neighbourhood count: [ :arg1 | arg1 state = #alive ]. - self state = #dead & (tmp1 = 3) ifTrue: [ ^ self bufferState: #alive ]. - (self state = #alive and: [ tmp1 = 2 or: [ tmp1 = 3 ] ]) ifTrue: [ - ^ self bufferState: #alive ]. + | numberOfAliveNeighbours | + numberOfAliveNeighbours := self neighbourhood count: [ :arg1 | arg1 state = #alive ]. + + (self state = #dead and: [ numberOfAliveNeighbours = 3 ]) + ifTrue: [ ^ self bufferState: #alive ]. + + (self state = #alive and: [ #(2 3) includes: numberOfAliveNeighbours ]) + ifTrue: [ ^ self bufferState: #alive ]. + ^ self bufferState: #dead ] -{ #category : #pov } -ConwayCell >> pdv [ - - ^ state -] - -{ #category : #pov } +{ #category : 'pov' } ConwayCell >> pov [ + - ^ self state -] - -{ #category : #pov } -ConwayCell >> pov_red [ - - self state = #dead ifTrue: [ ^ #dead_red ]. - ^ self state + ^ self state = #alive + ifTrue: [ Color white ] + ifFalse: [ Color black ] ] diff --git a/src/Conway-Model/ConwayModel.class.st b/src/Conway-Model/ConwayModel.class.st index 7a039cb..3850ced 100644 --- a/src/Conway-Model/ConwayModel.class.st +++ b/src/Conway-Model/ConwayModel.class.st @@ -38,323 +38,79 @@ Initial state -> Attributes -> state -> Load map Then repeat steps 4 and 5. " Class { - #name : #ConwayModel, - #superclass : #CMAbstractModel, + #name : 'ConwayModel', + #superclass : 'CMAbstractModel', #instVars : [ 'theCells' ], - #category : #'Conway-Model' + #category : 'Conway-Model', + #package : 'Conway-Model' } -{ #category : #description } -ConwayModel class >> aboutThisModel [ - " Answer a which is the main description of the receiver's purpose " - - ^ 'ToDo' - -] - -{ #category : #default } -ConwayModel class >> defaultControl [ - - ^ #stepSynchronously: -] - -{ #category : #default } -ConwayModel class >> defaultInit [ - - ^ #initRandomly -] - -{ #category : #examples } -ConwayModel class >> example1 [ - - | tmp1 tmp2 tmp3 tmp4 tmp5 tmp6 | - self initialize. - tmp1 := self new. - tmp1 initializeSpaceModel. - tmp2 := RTView new. - tmp1 - initSimulation; - runStep. - tmp2 clean. - tmp5 := RTBox new. - tmp5 color: [ :arg1 | - arg1 state == #dead - ifTrue: [ Color white ] - ifFalse: [ Color black ] ]. - tmp3 := (tmp5 size: 25) elementsOn: tmp1 theCells. - tmp2 addAll: tmp3. - tmp6 := RTGridLayout new. - tmp6 - gapSize: 0; - lineItemsCount: tmp1 spaceModel column; - on: tmp2 elements. - tmp2 canvas camera focusOnCenterScaled. - tmp4 := RTActiveAnimation new. - tmp4 - intervalInMilliseconds: 0; - blockToExecute: [ - tmp1 runStep. - tmp2 elements do: #updateShape ]; - inView: tmp2. - tmp2 openWithToolbar -] - -{ #category : #examples } -ConwayModel class >> example2 [ - - | tmp1 tmp2 tmp3 tmp4 tmp5 tmp6 tmp7 tmp8 tmp10 tmp11 | - tmp1 := RTView new. - tmp2 := ConwayModel. - tmp3 := 4. - tmp4 := (1 to: tmp3) collect: [ :arg1 | - tmp2 initialize. - tmp10 := tmp2 new. - tmp10 - initializeSpaceModel; - activeInit: #initSmallGrid; - initSimulation; - runStep. - tmp10 ]. - tmp5 := RTMultiLinearColor new colors: RTPalette c4. - tmp6 := (1 to: tmp3) collect: [ :arg2 | - tmp5 level: (arg2 / tmp3) asFloat ]. - tmp7 := tmp4 with: tmp6 collect: [ :arg3 :arg4 | - | tmp12 tmp13 | - tmp12 := RTBox new. - tmp12 color: [ :arg5 | - arg5 state == #dead - ifTrue: [ Color white ] - ifFalse: [ arg4 ] ]. - tmp11 := (tmp12 size: 25) elementsOn: arg3 theCells. - tmp1 addAll: tmp11. - tmp13 := RTGridLayout new. - tmp13 - gapSize: 0; - lineItemsCount: arg3 spaceModel column; - on: tmp11. - tmp11 ]. - RTGridLayout on: tmp7. - tmp1 canvas camera focusOnCenterScaled. - tmp8 := RTActiveAnimation new. - tmp8 - intervalInMilliseconds: 500; - blockToExecute: [ - tmp4 do: #runStep. - tmp1 elements do: #updateShape ]; - inView: tmp1. - tmp1 openWithToolbar -] - -{ #category : #examples } -ConwayModel class >> example3 [ - - | tmp1 tmp2 tmp3 tmp4 tmp5 tmp6 tmp7 | - self initialize. - tmp1 := self new. - tmp2 := 100. - tmp1 - initializeSpaceModel; - createGridLines: tmp2 - columns: tmp2 - neighbourhood: 4 - closed: true. - tmp3 := RTView new. - tmp1 - initSimulation; - runStep. - tmp3 clean. - tmp6 := RTBox new. - tmp6 color: [ :arg1 | - arg1 state == #dead - ifTrue: [ Color white ] - ifFalse: [ Color black ] ]. - tmp4 := (tmp6 size: 25) elementsOn: tmp1 theCells. - tmp3 addAll: tmp4. - tmp7 := RTGridLayout new. - tmp7 - gapSize: 0; - lineItemsCount: tmp1 spaceModel column; - on: tmp3 elements. - tmp3 canvas camera focusOnCenterScaled. - tmp5 := RTActiveAnimation new. - tmp5 - intervalInMilliseconds: 0; - blockToExecute: [ - tmp1 runStep. - tmp3 elements do: #updateShape ]; - inView: tmp3. - tmp3 openWithToolbar -] - -{ #category : #examples } -ConwayModel class >> example4 [ - - | tmp1 | - tmp1 := self initialize new initSimulation. - (CMSimulationGrid initialize new - on: tmp1 - withCells: tmp1 theESE - withSituatedEntities: tmp1 allTheSituatedEntities) runAndVisualize -] - -{ #category : #examples } -ConwayModel class >> example5 [ - - | tmp1 | - tmp1 := self initialize new. - tmp1 - activeInit: #initGlidersGun; - initSimulation. - (CMSimulationGrid initialize new - on: tmp1 - withCells: tmp1 theESE - withSituatedEntities: tmp1 allTheSituatedEntities) runAndVisualize -] - -{ #category : #examples } -ConwayModel class >> exampleGlidersGun [ - - | tmp1 tmp2 tmp3 tmp4 tmp5 tmp6 | - self initialize. - tmp1 := self new. - tmp1 - initializeSpaceModel; - activeInit: #initGlidersGun. - tmp2 := RTView new. - tmp1 - initSimulation; - runStep. - tmp2 clean. - tmp5 := RTBox new. - tmp5 color: [ :arg1 | - arg1 state == #dead - ifTrue: [ Color white ] - ifFalse: [ Color black ] ]. - tmp3 := (tmp5 size: 25) elementsOn: tmp1 theCells. - tmp2 addAll: tmp3. - tmp6 := RTGridLayout new. - tmp6 - gapSize: 0; - lineItemsCount: tmp1 spaceModel column; - on: tmp2 elements. - tmp2 canvas camera focusOnCenterScaled. - tmp4 := RTActiveAnimation new. - tmp4 - intervalInMilliseconds: 0; - blockToExecute: [ - tmp1 runStep. - tmp2 elements do: #updateShape ]; - inView: tmp2. - tmp2 openWithToolbar -] - -{ #category : #description } -ConwayModel class >> howToRunIt [ - - ^ '1. In the Model zone, double-click on the Cell entity that appears in the spatial entities list, you then access the definition window. - - 1.1. Double-click on the initialize method. A text editor appears with the corresponding code (the state attribute of a Cell can take either the #dead or the #alive value). Close the text editor window. - 1.2. Double-click on the newState method. A text editor appears with the code corresponding to the transition function of the game of life. Close both windows. - -2. In the Model zone, at the bottom-right, in the Define the observation scrolling menu, choose the Space item. A window appears for the definition of viewpoints. - 2.1. In the situated entities list, at the top-left, select Cell - 2.2. In the observation methods list, below, select the pdv method. - 2.3. If you click on the associated symbols in the list (at the top-right), you can visualize the colors corresponding to the different states of the Cell: #alive (white) #dead (black). - 2.4. Close this window. - -3. In the Visualization zone, open a spatial grid (1st icon). A new window opens. By default, it is a 10 * 10 spatial grid composed of 4-connexe squares with toroidal boundaries. - 3.1. Choose an 8-connexity: Topology -> Cell shape -> Square -> 8-connexe. - 3.2. The Grid size item from the Topology menu perm opens a window allowing you to choose the number of lines and columns. For example, choose 50 * 50. - 3.3. Initial state -> Execute method... -> initialize launches the execution of the initialization method (see step 1) for each cell of the grid. -3.4. If you right-click on the grid, you can choose for each entity of the model the observation method or viewpoint (cf. step 2). For the Cell choose the pdv viewpoint. - -4. In the Simulation zone, click on the Initialize... button. - 4.1. Select the init and evoluerSynchrone: methods. - 4.2. Validate and close the window (Apply and close button). - -5. In the Simulation zone, click on the Step button or enter the number of time steps in the Final time field and click Run. - -6. To launch the simulation from a particular initial situation (" glider gun " structure), load the gliderGun.ext file from the spatial grid menu: -Initial state -> Attributes -> state -> Load map -Then repeat steps 4 and 5.' - -] - -{ #category : #probes } +{ #category : 'probes' } ConwayModel >> alive [ - - - ^ (self theCells count: [ :arg1 | arg1 state = #alive ]) - / self theCells size + + + ^ (theCells count: [ :cell | cell state = #alive ]) + / theCells size ] -{ #category : #probes } +{ #category : 'probes' } ConwayModel >> dead [ - - - ^ (self theCells count: [ :arg1 | arg1 state = #dead ]) - / self theCells size -] - -{ #category : #init } -ConwayModel >> initCustomMap [ - - self loadEnvironmentFromDirectory: 'cormas.cells' + + + ^ (theCells count: [ :cell | cell state = #dead ]) + / theCells size ] -{ #category : #init } -ConwayModel >> initGliders [ - - self loadEnvironmentFromDirectory: 'gliders.cells' -] - -{ #category : #init } -ConwayModel >> initGlidersGun [ - - self spaceModel loadEnvironmentFromDirectory: 'glidersGun.cells' -] - -{ #category : #init } +{ #category : 'init' } ConwayModel >> initRandomly [ + self - createGridLines: 100 - columns: 100 + createGridNumberOfRows: 100 + numberOfColumns: 100 neighbourhood: 8 closed: true. - self theCells do: #initRandomly + + theCells do: #initRandomly ] -{ #category : #init } +{ #category : 'init' } ConwayModel >> initSmallGrid [ + self - createGridLines: 10 - columns: 10 + createGridNumberOfRows: 10 + numberOfColumns: 10 neighbourhood: 4 closed: false. - self theCells do: #initRandomly + + theCells do: #initRandomly +] + +{ #category : 'initialization' } +ConwayModel >> initialize [ + + super initialize. + theCells := OrderedCollection new ] -{ #category : #control } +{ #category : 'control' } ConwayModel >> stepSynchronously: arg1 [ + super stepSynchronously: arg1 ] -{ #category : #accessing } +{ #category : 'accessing' } ConwayModel >> theCells [ + - | tmp1 | - tmp1 := theCells. - ^ tmp1 - ifNil: [ theCells := OrderedCollection new ] - ifNotNil: [ tmp1 ] + ^ theCells ] -{ #category : #accessing } +{ #category : 'accessing' } ConwayModel >> theCells: arg1 [ theCells := arg1 diff --git a/src/Conway-Model/package.st b/src/Conway-Model/package.st index 9c67428..299855d 100644 --- a/src/Conway-Model/package.st +++ b/src/Conway-Model/package.st @@ -1 +1 @@ -Package { #name : #'Conway-Model' } +Package { #name : 'Conway-Model' }