MC logo

Frog Chorus Simulation

  Smalltalk Frog Simulation

<<Verbose Frog Simulation frogchor.st
Frog subclass: #FrogChorusMember
    instanceVariableNames: 'nextFrog'
    classVariableNames: ''
    poolDictionaries: ''
    category: nil !

FrogChorusMember comment:
'I am a member of the frog chorus.  I croak with my friends.' !

!FrogChorusMember class methodsFor: 'instance creation'!
new
     | r |

     r := super new.
     ^r
!!

!FrogChorusMember methodsFor: 'instance initialization'!
init
     super init.
     nextFrog := nil
!!

!FrogChorusMember methodsFor: 'croaking'!
croak
     super croak.
     (nextFrog = nil) 
        ifFalse: [ nextFrog croak ]
!!

!FrogChorusMember methodsFor: 'croak count extraction'!
getCroakCount
     | downCount |

     downCount := croakCount.
     (nextFrog = nil) 
        ifFalse: [ downCount := downCount + (nextFrog getCroakCount) ].
     ^downCount
!!

!FrogChorusMember methodsFor: 'ordering'!
setNextFrog: otherFrog
    nextFrog := otherFrog
!!

Object subclass: #FrogChorus
    instanceVariableNames: 'firstFrog'
    classVariableNames: ''
    poolDictionaries: ''
    category: nil !

FrogChorus comment:
'We are the frog chorus.  What more can be said?' !

!FrogChorus class methodsFor: 'instance creation'!
new
     | r |

     r := super new.
     r init.
     ^r
!!

!FrogChorus methodsFor: 'instance initialization'!
init
     firstFrog := nil
!!


!FrogChorus methodsFor: 'croaking'!
croak
     (firstFrog = nil) 
        ifFalse: [ firstFrog croak ]
!!

!FrogChorus methodsFor: 'croak count extraction'!
getCroakCount
     (firstFrog = nil) 
        ifTrue: [ ^0 ]
        ifFalse: [ ^(firstFrog getCroakCount) ]
!!

!FrogChorus methodsFor: 'recruitment'!
addFrog: newFrog
    newFrog setNextFrog: firstFrog.
    firstFrog := newFrog
!!
<<Verbose Frog Simulation