Program Coding

 

Morph subclass: #IronMorph
    instanceVariableNames: ''
    classVariableNames: 'AllIronMorphs '
    poolDictionaries: ''
    category: 'CompSciOlympics'!



!IronMorph methodsFor: 'private' stamp: 'DS 4/10/2002 21:27'!

randomPXPoint
    ^ Point x: 2 y: 0! !


!IronMorph methodsFor: 'drawing' stamp: 'DS 4/10/2002 00:18'!

drawOn: aCanvas
    aCanvas fillRectangle: self bounds color: self color
    "aCanvas fillOval: (self bounds insetBy: 4) color: self color"! !


!IronMorph methodsFor: 'stepping and presenter' stamp: 'DS 4/7/2002 22:29'!

border
    ^ Rectangle origin: (Point x: 100 y: 100) corner: (Point x: 200 y: 200)! !

!IronMorph methodsFor: 'stepping and presenter' stamp: 'DS 4/10/2002 21:30'!
checkBorder
    self checkXBorder.
    self checkYBorder.
    self resetFullBounds! !

!IronMorph methodsFor: 'stepping and presenter' stamp: 'DS 4/7/2002 17:51'!
checkXBorder
    self bounds origin x < self border origin x
        ifTrue: [
            self bounds corner x: self bounds corner x +
                self border origin x - self bounds origin x.
            self bounds origin x: self border origin x].
   
    self bounds corner x > self border corner x
        ifTrue: [
            self bounds origin x: self bounds origin x -
                (self bounds corner x - self border corner x).
            self bounds corner x: self border corner x].! !

!IronMorph methodsFor: 'stepping and presenter' stamp: 'DS 4/10/2002 22:26'!
checkYBorder
    self bounds origin y < self border origin y
        ifTrue: [
            self bounds corner y: self bounds corner y +
                self border origin y - self bounds origin y.
            self bounds origin y: self border origin y].
   
    self bounds corner y > self border corner y
        ifTrue: [
            self bounds origin y: self bounds origin y -
                (self bounds corner y - self border corner y).
            self bounds corner y: self border corner y].! !

!IronMorph methodsFor: 'stepping and presenter' stamp: 'DS 4/10/2002 21:29'!
correctForCollisions
    AllIronMorphs do: [:ironMorph | ironMorph == self ifTrue: [^ nil].
        (ironMorph bounds center x - self bounds center x) abs < 10
            ifTrue: [
                self bounds origin x > ironMorph bounds origin x
                    ifTrue: [self position: self position + (Point x: 2 y: 0)]
                    ifFalse: [self position: self position - (Point x: 2 y: 0)]]]! !

!IronMorph methodsFor: 'stepping and presenter' stamp: 'DS 4/7/2002 23:46'!
move
    ^ nil! !

!IronMorph methodsFor: 'stepping and presenter' stamp: 'DS 4/11/2002 13:28'!
moveDown
    self position: self position + (Point x: 0 y: 2 atRandom).! !

!IronMorph methodsFor: 'stepping and presenter' stamp: 'DS 4/11/2002 13:28'!
moveUp
    self position: self position + (Point x: 0 y: 2 atRandom negated)! !

!IronMorph methodsFor: 'stepping and presenter' stamp: 'DS 4/10/2002 21:30'!
step
    self move.
    self wiggleX.
    self correctForCollisions.
    self checkBorder.! !

!IronMorph methodsFor: 'stepping and presenter' stamp: 'DS 4/7/2002 17:12'!
stepTime
    ^ 100! !

!IronMorph methodsFor: 'stepping and presenter' stamp: 'DS 4/10/2002 18:33'!
wiggleX
    self position: self position + (Point x: 3 atRandom - 2 y: 0).! !

!IronMorph methodsFor: 'stepping and presenter' stamp: 'DS 4/10/2002 22:13'!
wiggleY
    self position: self position + (Point x: 0 y: 3 atRandom - 2)! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

IronMorph class
    instanceVariableNames: ''!



!IronMorph class methodsFor: 'as yet unclassified' stamp: 'DS 4/7/2002 22:13'!

AllIronMorphs
    ^ AllIronMorphs! !

!IronMorph class methodsFor: 'as yet unclassified' stamp: 'DS 4/7/2002 22:15'!
ResetAllIronMorphs
    ^ AllIronMorphs _ nil! !

!IronMorph class methodsFor: 'as yet unclassified' stamp: 'DS 4/7/2002 22:38'!
legalNew
    AllIronMorphs isNil
        ifTrue: [AllIronMorphs _ Bag new].
    ^ AllIronMorphs add: IronMorph new! !


IronMorph subclass: #IronParticle
    instanceVariableNames: 'bin desiredBin '
    classVariableNames: 'Tube '
    poolDictionaries: ''
    category: 'CompSciOlympics'!



!IronParticle methodsFor: 'as yet unclassified' stamp: 'DS 4/6/2002 02:46'!

bin
    ^ bin! !

!IronParticle methodsFor: 'as yet unclassified' stamp: 'DS 4/10/2002 00:01'!
bin: anInteger
    bin _ anInteger! !

!IronParticle methodsFor: 'as yet unclassified' stamp: 'DS 4/7/2002 22:30'!
border
    ^ Tube boundsAtLevel: bin! !

!IronParticle methodsFor: 'as yet unclassified' stamp: 'DS 4/10/2002 22:58'!
checkBorder
    (self border containsPoint: self bounds center)
        ifFalse: [
            (self border corner y < self bounds origin y)
                ifTrue: [self nextBin]
                ifFalse: [self previousBin]].
    ^ super checkBorder! !

!IronParticle methodsFor: 'as yet unclassified' stamp: 'DS 4/10/2002 23:11'!
checkYBorder
    (bin = 1 and: [self bounds origin y < self border origin y])
        ifTrue: [
            self bounds corner y: self bounds corner y +
                self border origin y - self bounds origin y.
            self bounds origin y: self border origin y. self stopStepping].
   
    (bin = Tube levels and: [self bounds corner y > self border corner y])
        ifTrue: [
            self bounds origin y: self bounds origin y -
                (self bounds corner y - self border corner y).
            self bounds corner y: self border corner y. self stopStepping].! !

!IronParticle methodsFor: 'as yet unclassified' stamp: 'DS 4/10/2002 22:13'!
move
    Tube powerOn
        ifFalse: [^ self wiggleY].

    (("avove the powerY"
    (Tube boundsAtLevel: bin) origin corner:
        (Point x: (Tube boundsAtLevel: bin) corner x
            y: (Tube powerYAtLevel: bin)
        )
    ) containsRect: self bounds)
        ifTrue:[^ self moveUp].

    (("below the powerY"
    (Point x: (Tube boundsAtLevel: bin) origin x
        y: (Tube powerYAtLevel: bin)) corner: (Tube boundsAtLevel: bin) corner
    ) containsRect: self bounds)
        ifTrue: [^ self moveDown].

    "only remaining case is that the Particle is ontop of the power"
    ^ self wiggleY.! !

!IronParticle methodsFor: 'as yet unclassified' stamp: 'DS 4/10/2002 23:00'!
nextBin
    (bin = Tube levels)
        ifTrue: [^ bin]
        ifFalse: [bin _ bin + 1].! !

!IronParticle methodsFor: 'as yet unclassified' stamp: 'DS 4/10/2002 23:00'!
previousBin
    (bin = 1)
        ifTrue: [^ bin]
        ifFalse: [bin _ bin - 1].! !

!IronParticle methodsFor: 'as yet unclassified' stamp: 'DS 4/10/2002 23:13'!
resetInBin: anInteger
    | newOrigin |
    self bin: anInteger.
    newOrigin _ (Tube boundsAtLevel: bin) center.
    self bounds: (newOrigin corner: (Point x: newOrigin x + 3 y: newOrigin y + 3)).
    self startStepping.! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

IronParticle class
    instanceVariableNames: ''!



!IronParticle class methodsFor: 'as yet unclassified' stamp: 'DS 4/10/2002 01:43'!

legalNew
    | created |
    created _ IronParticle new.
    created bounds origin x: 0.
    created bounds origin y: 0.
    created bounds corner x: 3.
    created bounds corner y: 3.

    AllIronMorphs isNil
        ifTrue: [AllIronMorphs _ Bag new].
    ^ AllIronMorphs add: created.! !

!IronParticle class methodsFor: 'as yet unclassified' stamp: 'DS 4/10/2002 21:40'!
newIn: testTube numberOfParticles: anInt inBin: aBin
    | bag |
    Tube _ testTube.
    bag _ Bag new.
    anInt timesRepeat:
        [bag add: (IronParticle legalNew bin: aBin)].
    ^ bag.! !

!IronParticle class methodsFor: 'as yet unclassified' stamp: 'DS 4/6/2002 23:16'!
tube
    ^ Tube! !


Morph subclass: #ParadoxicalTestTube
    instanceVariableNames: 'startStopButton isStepping randomPowerButton distanceButton resetButton powerSwitch powerState height powerYs levelBounds plateDistance randomPower '
    classVariableNames: ''
    poolDictionaries: ''
    category: 'CompSciOlympics'!


!ParadoxicalTestTube commentStamp: 'DS 4/11/2002 20:11' prior: 0!
"
(ParadoxicalTestTube createWithLayers: 2)
    setPower: false;
    initilizeAll;
    createIronParticles: 5 onLevel: 3;
    openAllInWorld;
    openInWorld
"!


!ParadoxicalTestTube methodsFor: 'accessing' stamp: 'DS 4/7/2002 22:02'!

boundsAtLevel: anInteger
    (levelBounds isNil or:
        [levelBounds first origin ~= self bounds origin |
        levelBounds last corner ~= self bounds corner])
            ifTrue: [self initilizeLevelBounds].
    ^ levelBounds at: anInteger! !

!ParadoxicalTestTube methodsFor: 'accessing' stamp: 'DS 4/11/2002 10:41'!
curretPowerState
    powerState
        ifTrue: [^ 'Power is on']
        ifFalse: [^ 'Power is off']! !

!ParadoxicalTestTube methodsFor: 'accessing' stamp: 'DS 4/10/2002 01:29'!
levels
    ^ height ! !

!ParadoxicalTestTube methodsFor: 'accessing' stamp: 'DS 4/10/2002 21:53'!
powerOn
    ^ powerState! !

!ParadoxicalTestTube methodsFor: 'accessing' stamp: 'DS 4/10/2002 16:06'!
powerYAtLevel: anIntger
    powerYs isNil
        ifTrue: [self initilizePowerYs].
    ^ powerYs at: anIntger! !

!ParadoxicalTestTube methodsFor: 'accessing' stamp: 'DS 4/11/2002 20:11'!
setPower: aBoolean
    powerState _ aBoolean.
    powerSwitch notNil
        ifTrue: [powerSwitch label: self curretPowerState]! !

!ParadoxicalTestTube methodsFor: 'accessing' stamp: 'DS 4/11/2002 10:43'!
togglePower
    self setPower: powerState not.! !

!ParadoxicalTestTube methodsFor: 'accessing' stamp: 'DS 4/11/2002 10:47'!
togglePower: iHateSimpleSwitchMorphs
    self setPower: powerState not.! !


!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/10/2002 01:42'!

createIronParticles: aNumber onLevel: aLevel
    IronParticle newIn: self numberOfParticles: aNumber inBin: aLevel! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/10/2002 01:28'!
height: anInt
    height _ anInt! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/11/2002 20:10'!
initilizeAll
    self initilizeDistanceButton.
    self initilizeLevelBounds.
    self initilizePowerSwitch.
    self initilizePowerYs.
    self initilizeRandomPowerButton.
    self initilizeResetButton.
    self initilizeStartStopButton.! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/11/2002 14:05'!
initilizeDistanceButton
    distanceButton isNil
        ifTrue: [distanceButton _ SimpleButtonMorph new].
    distanceButton initializeWithLabel: 'Change plate distance'.
    distanceButton target: self.
    distanceButton actionSelector: #inputNewPlateDistance.
    distanceButton arguments: #().
    distanceButton openInWorld.! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/10/2002 01:29'!
initilizeLevelBounds
    | yCordinate |
    levelBounds _ OrderedCollection new.
    yCordinate _ self bounds origin y.
    height timesRepeat: [levelBounds add:
        ((Point x: self bounds origin x y: yCordinate + 1) corner:
        (Point x: self bounds corner x
            y: (yCordinate _ yCordinate +
                ((self bounds corner y - self bounds origin y) / height) asInteger)))]! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/11/2002 10:51'!
initilizePowerSwitch
    powerSwitch isNil
        ifTrue: [powerSwitch _ SimpleSwitchMorph new].
    powerSwitch initializeWithLabel: self curretPowerState.
    powerSwitch target: self.
    powerSwitch actionSelector: #togglePower:.
    powerSwitch arguments: #().
    powerSwitch openInWorld.! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/11/2002 13:45'!
initilizePowerYs
    powerYs _ OrderedCollection new.
    1 to: height do: [:myLevel |
        powerYs add:
            (((self boundsAtLevel: myLevel) corner y -
            (self boundsAtLevel: myLevel) origin y) * self plateDistance) abs asInteger +
            (self boundsAtLevel: myLevel) origin y]! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/11/2002 19:46'!
initilizeRandomPowerButton
    randomPowerButton isNil
        ifTrue: [randomPowerButton _ SimpleButtonMorph new].
    randomPowerButton initializeWithLabel: 'Random power toggling on'.
    randomPowerButton target: self.
    randomPowerButton actionSelector: #toggleRandomPower.
    randomPowerButton arguments: #().
    randomPowerButton openInWorld.! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/11/2002 10:36'!
initilizeResetButton
    resetButton isNil
        ifTrue: [resetButton _ SimpleButtonMorph new].
    resetButton initializeWithLabel: 'Reset particle locations'.
    resetButton target: self.
    resetButton actionSelector: #putAllIronRandom.
    resetButton arguments: #().
    resetButton openInWorld.! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/11/2002 19:38'!
initilizeStartStopButton
    startStopButton isNil
        ifTrue: [startStopButton _ SimpleButtonMorph new].
    startStopButton initializeWithLabel: 'Pause'.
    startStopButton target: self.
    startStopButton actionSelector: #toggleMovement.
    startStopButton arguments: #().
    startStopButton openInWorld.! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/11/2002 14:29'!
inputNewPlateDistance
    | input |
    input _ (FillInTheBlank request: 'Imput new plate distance (1 - 100)' initialAnswer: '50') asInteger.
    input isNil
        ifTrue: [input _ 50].
    input > 0
        ifFalse: [input _ 1].
    input < 101
        ifFalse: [input _ 100].
    self plateDistance: input / 100.
    self resetFullBounds.! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/10/2002 01:25'!
openAllInWorld
    IronMorph AllIronMorphs do: [:particle | particle openInWorld]! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/11/2002 13:45'!
plateDistance
    plateDistance isNil
        ifTrue: [plateDistance _ 9/110].
    ^ plateDistance.! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/11/2002 13:49'!
plateDistance: anInteger
    (anInteger < 1) & (anInteger > 0)
        ifTrue: [plateDistance _ anInteger].! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/10/2002 01:26'!
putAllIronParticlesIntoBin: anInteger
    (anInteger >= 1) & (anInteger <= levels size)
        IronMorph AllIronMorphs do: [:ironP | ironP resetInBin: anInteger]! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/10/2002 01:26'!
putAllIronRandom
    IronParticle AllIronMorphs do: [:ironP | ironP resetInBin: (1 to: self levels) atRandom]! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/11/2002 19:40'!
toggleMovement
    isStepping isNil
        ifTrue: [isStepping _ true].
    isStepping
        ifTrue: [self stop]
        ifFalse: [self start].
    isStepping _ isStepping not.
    isStepping
        ifTrue: [startStopButton label: 'Pause']
        ifFalse: [startStopButton label: 'Play']! !

!ParadoxicalTestTube methodsFor: 'initialization' stamp: 'DS 4/11/2002 19:51'!
toggleRandomPower
    randomPower isNil
        ifTrue: [randomPower _ true].
    randomPower _ randomPower not.
    randomPower
        ifTrue: [randomPowerButton label: 'Random power toggling on']
        ifFalse: [randomPowerButton label: 'Random power toggling off']! !


!ParadoxicalTestTube methodsFor: 'drawing' stamp: 'DS 4/11/2002 13:41'!

checkForMovement
    self bounds origin = (self boundsAtLevel: 1) origin
        ifFalse: [self initilizeLevelBounds. self initilizePowerYs].
    self bounds corner = (self boundsAtLevel: self levels) corner
        ifFalse: [self initilizeLevelBounds. self initilizePowerYs].! !

!ParadoxicalTestTube methodsFor: 'drawing' stamp: 'DS 4/11/2002 13:41'!
drawOn: aCanvas
    | powerLineColor |
    self powerOn
        ifTrue: [powerLineColor _ Color red]
        ifFalse: [powerLineColor _ Color white].
   
    self checkForMovement.

    1 to: height do: [:levelIndex |
        aCanvas fillRectangle: (self boundsAtLevel: levelIndex) color: Color gray].
    1 to: height do: [:levelIndex |
        aCanvas fillRectangle:(
            (Point x: self bounds origin x
                y: (self powerYAtLevel: levelIndex))
            corner: (Point x: self bounds corner x
                y: (self powerYAtLevel: levelIndex) + 1)
        ) color: powerLineColor].! !


!ParadoxicalTestTube methodsFor: 'stepping and presenter' stamp: 'DS 4/11/2002 16:58'!

start
    self startStepping.
    IronMorph AllIronMorphs do: [:iron | iron startStepping]! !

!ParadoxicalTestTube methodsFor: 'stepping and presenter' stamp: 'DS 4/11/2002 19:42'!
step
    (randomPower notNil and: [randomPower])
        ifTrue: [self setPower: powerState not]! !

!ParadoxicalTestTube methodsFor: 'stepping and presenter' stamp: 'DS 4/11/2002 12:38'!
stepTime
    ^ 3000! !

!ParadoxicalTestTube methodsFor: 'stepping and presenter' stamp: 'DS 4/11/2002 16:58'!
stop
    self stopStepping.
    IronMorph AllIronMorphs do: [:iron | iron stopStepping]! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

ParadoxicalTestTube class
    instanceVariableNames: ''!



!ParadoxicalTestTube class methodsFor: 'as yet unclassified' stamp: 'DS 4/7/2002 22:55'!

createWithLayers: anInt
    | created |
    created _ ParadoxicalTestTube new height: anInt * 3.
    created bounds origin x: 100.
    created bounds origin y: 100.
    created bounds corner x: 200.
    created bounds corner y: 400.
    created resetFullBounds.
    ^ created! !

Home ] Project Simulations ] [ Program Coding ]