Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'From VisualWorks® NonCommercial, 7.7 of December 15, 2009 on November 19, 2010 at 10:00:25 PM'!
- CodeComponent create: #package named: 'CS535Demos2010 - direction problem'!"Package CS535Demos2010 - direction problem(1.9,malasig)*"!
- CodeComponent type: #package named: 'CS535Demos2010 - direction problem' property: #parcelName value: 'CS535Demos2010'!
- CodeComponent type: #package named: 'CS535Demos2010 - direction problem' property: #packageName value: 'CS535Demos2010 - direction problem'!
- CodeComponent type: #package named: 'CS535Demos2010 - direction problem' property: #version value: '1.2'!
- CodeComponent type: #package named: 'CS535Demos2010 - direction problem' property: #comment value: '"CS535 Project SimuAnts"
- '!
- Smalltalk defineClass: #DrawExampleApplication
- superclass: #{UI.ApplicationModel}
- indexedType: #none
- private: false
- instanceVariableNames: 'view antColony '
- classInstanceVariableNames: ''
- imports: ''
- category: 'UIApplications-New'!
- DrawExampleApplication comment:
- 'Application for the demo. The method canvas shows how to link the model and the view. See the DrawExampleView class for example of custom view.
- Execute the following to run:
- "DrawExampleApplication openWithSpec: #windowSpec"
- '!
- Smalltalk defineClass: #Pond
- superclass: #{Graphics.Rectangle}
- indexedType: #none
- private: false
- instanceVariableNames: 'width height rectangle '
- classInstanceVariableNames: 'pond '
- imports: ''
- category: ''!
- Pond comment:
- ''!
- Smalltalk defineClass: #Colony
- superclass: #{Core.Object}
- indexedType: #none
- private: false
- instanceVariableNames: 'ants '
- classInstanceVariableNames: ''
- imports: ''
- category: ''!
- Colony comment:
- 'Colony of ants.
- Instance Variables
- ants <(Collection of: (Ant))> All the ants the belong to the colony
- '!
- Smalltalk defineClass: #Ant
- superclass: #{Core.Object}
- indexedType: #none
- private: false
- instanceVariableNames: 'location random direction '
- classInstanceVariableNames: ''
- imports: ''
- category: ''!
- Ant comment:
- 'Represents one Ant in the simulation.
- Instance Variables
- direction <Point> direction ant is headed
- location <Point> location of ant in wold
- random <Stream> Generates random numbers between 0 and 1
- '!
- Smalltalk defineClass: #World
- superclass: #{Core.Object}
- indexedType: #none
- private: false
- instanceVariableNames: 'pond antColony '
- classInstanceVariableNames: ''
- imports: ''
- category: ''!
- World comment:
- ''!
- Smalltalk defineClass: #DrawExampleView
- superclass: #{UI.View}
- indexedType: #none
- private: false
- instanceVariableNames: 'antColony pond world '
- classInstanceVariableNames: ''
- imports: ''
- category: ''!
- DrawExampleView comment:
- 'A subclass of View. Must implement displayOn:. In the UIPainter add a canvas widget to your view. The application model must return a View subclass in the view Holder method given in the UIPainter.
- '!
- !DrawExampleApplication class methodsFor: 'interface specs'!
- windowSpec
- "Tools.UIPainter new openOnClass: self andSelector: #windowSpec"
- <resource: #canvas>
- ^#(#{UI.FullSpec}
- #window:
- #(#{UI.WindowSpec}
- #label: 'Motion Demo'
- #bounds: #(#{Graphics.Rectangle} 640 389 1083 670 ) )
- #component:
- #(#{UI.SpecCollection}
- #collection: #(
- #(#{UI.ActionButtonSpec}
- #layout: #(#{Graphics.Rectangle} 19 5 87 35 )
- #name: #move
- #model: #move
- #label: 'Go'
- #defaultable: true )
- #(#{UI.ArbitraryComponentSpec}
- #layout: #(#{Graphics.Rectangle} 109 4 434 268 )
- #name: #canvas
- #component: #canvas ) ) ) )! !
- !DrawExampleApplication methodsFor: 'actions'!
- move
- [600 timesRepeat:
- [
- (view world antColony) move.
- [view invalidate]
- uiEventFor: self mainWindow.
- (Delay forMilliseconds: 500) wait]]
- fork! !
- !DrawExampleApplication methodsFor: 'accessing'!
- canvas
- view
- ifNil:
- [view := DrawExampleView new.
- "antColony := Colony new."
- view model:(view world antColony).].
- ^view! !
- !DrawExampleView class methodsFor: 'interface specs'!
- windowSpec
- "Tools.UIPainter new openOnClass: self andSelector: #windowSpec"
- <resource: #canvas>
- ^#(#{UI.FullSpec}
- #window:
- #(#{UI.WindowSpec}
- #label: ''
- #bounds: #(#{Graphics.Rectangle} 1280 512 1480 712 ) )
- #component:
- #(#{UI.SpecCollection}
- #collection: #() ) )! !
- !DrawExampleView methodsFor: 'displaying'!
- displayOn: aGraphicsContext
- "aGraphicsContext pond."
- | rectangle |
- aGraphicsContext paint: ColorValue red.
- (world antColony) ants do: [:each | aGraphicsContext displayDotOfDiameter: 5 at: each location].
- aGraphicsContext paint: ColorValue blue.
- "rectangle := Rectangle origin: 50 @ 50 corner: 100 @ 100."
- aGraphicsContext displayRectangle: world pond rectangle.! !
- !DrawExampleView methodsFor: 'initialize-release'!
- world
- ^world!
- initialize
- world := World new.! !
- !World class methodsFor: 'instance creation'!
- new
- "Answer a newly created and initialized instance."
- ^super new initialize! !
- !World methodsFor: 'initialize-release'!
- antColony
- ^antColony!
- initialize
- "Initialize a newly created instance. This method must answer the receiver."
- super initialize.
- " *** Replace this comment with the appropriate initialization code *** "
- pond := Pond new.
- "location := 160 @ 130."
- antColony := Colony new.
- ^self!
- pond
- ^pond! !
- !Ant class methodsFor: 'instance creation'!
- new
- "Answer a newly created and initialized instance."
- ^super new initialize! !
- !Ant methodsFor: 'initialize-release'!
- move
- | move |
- move := random next.
- location x <= 150 | (location x >= 170) | (location y <= 120)
- | (location y >= 140)
- ifTrue:
- [location x <= 150
- ifTrue:
- [move <= 0.33 ifTrue: [direction := direction east].
- 0.33 < move & (move < 0.66) ifTrue: [direction := direction south].
- 0.66 <= move ifTrue: [direction := direction north].
- Transcript
- clear;
- print: location].
- location x >= 170
- ifTrue:
- [move <= 0.33 ifTrue: [direction := direction west].
- 0.33 < move & (move < 0.66) ifTrue: [direction := direction north].
- 0.66 <= move ifTrue: [direction := direction south].
- Transcript
- clear;
- print: location].
- location y <= 120
- ifTrue:
- [move <= 0.33 ifTrue: [direction := direction south].
- 0.33 < move & (move < 0.66) ifTrue: [direction := direction east].
- 0.66 <= move ifTrue: [direction := direction west].
- Transcript
- clear;
- print: location].
- location y >= 140
- ifTrue:
- [move <= 0.33 ifTrue: [direction := direction north].
- 0.33 < move & (move < 0.66) ifTrue: [direction := direction east].
- 0.66 <= move ifTrue: [direction := direction west].
- Transcript
- clear;
- print: location].
- location x <= 150 | (location y <= 120)
- ifTrue:
- [move <= 0.50 ifTrue: [direction := direction east].
- move >= 0.51 ifTrue: [direction := direction south].
- Transcript
- clear;
- print: location].
- location x >= 170 | (location y <= 120)
- ifTrue:
- [move <= 0.50 ifTrue: [direction := direction west].
- move >= 0.51 ifTrue: [direction := direction south].
- Transcript
- clear;
- print: location].
- location x <= 150 | (location y >= 140)
- ifTrue:
- [move <= 0.50 ifTrue: [direction := direction east].
- move >= 0.51 ifTrue: [direction := direction north].
- Transcript
- clear;
- print: location].
- location x >= 170 | (location y >= 140)
- ifTrue:
- [move <= 0.50 ifTrue: [direction := direction west].
- move >= 0.51 ifTrue: [direction := direction north].
- Transcript
- clear;
- print: location]]
- ifFalse:
- [move := random next.
- move < 0.05 ifTrue: [direction := direction left].
- move > 0.85 ifTrue: [direction := direction right]].
- location := location + (direction * 2)!
- location
- ^location!
- displayOnn: aGraphicsContext
- aGraphicsContext displayDotOfDiameter: 5 at: location!
- initialize
- "Initialize a newly created instance. This method must answer the receiver."
- super initialize.
- location := 160 @ 130.
- direction := 1 @ 0.
- random := Random new! !
- !Pond class methodsFor: 'instance creation'!
- new
- "Answer a newly created and initialized instance."
- ^super new initialize! !
- !Pond methodsFor: 'initialize-release'!
- rectangle
- rectangle := Rectangle origin: origin corner: corner.
- ^rectangle!
- initialize
- "Initialize a newly created instance. This method must answer the receiver."
- super initialize.
- origin:= 50@50.
- corner:=100@100.
- " *** Replace this comment with the appropriate initialization code *** "
- ^self! !
- !Colony class methodsFor: 'instance creation'!
- new
- "Answer a newly created and initialized instance."
- ^super new initialize! !
- !Colony methodsFor: 'initialize-release'!
- displayOnn: aGraphicsContext
- ants do: [:each | each displayOn: aGraphicsContext]!
- initialize
- ants := OrderedCollection new.
- 50 timesRepeat:[ants add: Ant new].!
- move
- ants do: [:each | each move].!
- ants
- ^ants! !
- !Core.Point methodsFor: 'converting'!
- left
- x = 1 ifTrue: [^0 @ -1].
- x = -1 ifTrue: [^0 @ 1].
- y = 1 ifTrue: [^1 @ 0].
- ^-1 @ 0!
- south
- ^0 @ 1!
- north
- ^0 @ -1!
- west
- ^-1 @ 0!
- east
- ^1 @ 0!
- right
- x = 1 ifTrue: [^0 @ 1].
- x = -1 ifTrue: [^0 @ ( -1)].
- y = 1 ifTrue: [^(-1) @ 0].
- ^1 @ 0! !
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement