Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'From VisualWorks® NonCommercial, 7.7 of December 15, 2009 on November 19, 2010 at 10:00:25 PM'!
  2.  
  3.  
  4. CodeComponent create: #package named: 'CS535Demos2010 - direction problem'!"Package CS535Demos2010 - direction problem(1.9,malasig)*"!
  5.  
  6.  
  7. CodeComponent type: #package named: 'CS535Demos2010 - direction problem' property: #parcelName value: 'CS535Demos2010'!
  8.  
  9. CodeComponent type: #package named: 'CS535Demos2010 - direction problem' property: #packageName value: 'CS535Demos2010 - direction problem'!
  10.  
  11. CodeComponent type: #package named: 'CS535Demos2010 - direction problem' property: #version value: '1.2'!
  12.  
  13. CodeComponent type: #package named: 'CS535Demos2010 - direction problem' property: #comment value: '"CS535 Project SimuAnts"
  14. '!
  15.  
  16. Smalltalk defineClass: #DrawExampleApplication
  17.     superclass: #{UI.ApplicationModel}
  18.     indexedType: #none
  19.     private: false
  20.     instanceVariableNames: 'view antColony '
  21.     classInstanceVariableNames: ''
  22.     imports: ''
  23.     category: 'UIApplications-New'!
  24.  
  25. DrawExampleApplication comment:
  26. '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.
  27.  
  28. Execute the following to run:
  29. "DrawExampleApplication openWithSpec: #windowSpec"
  30. '!
  31.  
  32. Smalltalk defineClass: #Pond
  33.     superclass: #{Graphics.Rectangle}
  34.     indexedType: #none
  35.     private: false
  36.     instanceVariableNames: 'width height rectangle '
  37.     classInstanceVariableNames: 'pond '
  38.     imports: ''
  39.     category: ''!
  40.  
  41. Pond comment:
  42. ''!
  43.  
  44. Smalltalk defineClass: #Colony
  45.     superclass: #{Core.Object}
  46.     indexedType: #none
  47.     private: false
  48.     instanceVariableNames: 'ants '
  49.     classInstanceVariableNames: ''
  50.     imports: ''
  51.     category: ''!
  52.  
  53. Colony comment:
  54. 'Colony of ants.
  55.  
  56. Instance Variables
  57.     ants    <(Collection of: (Ant))>    All the ants the belong to the colony
  58.  
  59. '!
  60.  
  61. Smalltalk defineClass: #Ant
  62.     superclass: #{Core.Object}
  63.     indexedType: #none
  64.     private: false
  65.     instanceVariableNames: 'location random direction '
  66.     classInstanceVariableNames: ''
  67.     imports: ''
  68.     category: ''!
  69.  
  70. Ant comment:
  71. 'Represents one Ant in the simulation.
  72.  
  73. Instance Variables
  74.     direction   <Point> direction ant is headed
  75.     location    <Point> location of ant in wold
  76.     random  <Stream>    Generates random numbers between 0 and 1
  77.  
  78. '!
  79.  
  80. Smalltalk defineClass: #World
  81.     superclass: #{Core.Object}
  82.     indexedType: #none
  83.     private: false
  84.     instanceVariableNames: 'pond antColony '
  85.     classInstanceVariableNames: ''
  86.     imports: ''
  87.     category: ''!
  88.  
  89. World comment:
  90. ''!
  91.  
  92. Smalltalk defineClass: #DrawExampleView
  93.     superclass: #{UI.View}
  94.     indexedType: #none
  95.     private: false
  96.     instanceVariableNames: 'antColony pond world '
  97.     classInstanceVariableNames: ''
  98.     imports: ''
  99.     category: ''!
  100.  
  101. DrawExampleView comment:
  102. '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.
  103. '!
  104.  
  105. !DrawExampleApplication class methodsFor: 'interface specs'!
  106.  
  107. windowSpec
  108.     "Tools.UIPainter new openOnClass: self andSelector: #windowSpec"
  109.  
  110.     <resource: #canvas>
  111.     ^#(#{UI.FullSpec}
  112.         #window:
  113.         #(#{UI.WindowSpec}
  114.             #label: 'Motion Demo'
  115.             #bounds: #(#{Graphics.Rectangle} 640 389 1083 670 ) )
  116.         #component:
  117.         #(#{UI.SpecCollection}
  118.             #collection: #(
  119.                 #(#{UI.ActionButtonSpec}
  120.                     #layout: #(#{Graphics.Rectangle} 19 5 87 35 )
  121.                     #name: #move
  122.                     #model: #move
  123.                     #label: 'Go'
  124.                     #defaultable: true )
  125.                 #(#{UI.ArbitraryComponentSpec}
  126.                     #layout: #(#{Graphics.Rectangle} 109 4 434 268 )
  127.                     #name: #canvas
  128.                     #component: #canvas ) ) ) )! !
  129.  
  130.  
  131. !DrawExampleApplication methodsFor: 'actions'!
  132.  
  133. move
  134.  
  135.    
  136.     [600 timesRepeat:
  137.             [
  138.             (view world antColony) move.
  139.             [view invalidate]
  140.                     uiEventFor: self mainWindow.
  141.             (Delay forMilliseconds: 500) wait]]
  142.             fork! !
  143.  
  144. !DrawExampleApplication methodsFor: 'accessing'!
  145.  
  146. canvas
  147.  
  148.     view
  149.         ifNil:
  150.             [view := DrawExampleView new.
  151.             "antColony := Colony new."
  152.             view model:(view world antColony).].
  153.     ^view! !
  154.  
  155.  
  156. !DrawExampleView class methodsFor: 'interface specs'!
  157.  
  158. windowSpec
  159.     "Tools.UIPainter new openOnClass: self andSelector: #windowSpec"
  160.  
  161.     <resource: #canvas>
  162.     ^#(#{UI.FullSpec}
  163.         #window:
  164.         #(#{UI.WindowSpec}
  165.             #label: ''
  166.             #bounds: #(#{Graphics.Rectangle} 1280 512 1480 712 ) )
  167.         #component:
  168.         #(#{UI.SpecCollection}
  169.             #collection: #() ) )! !
  170.  
  171.  
  172. !DrawExampleView methodsFor: 'displaying'!
  173.  
  174. displayOn: aGraphicsContext
  175.     "aGraphicsContext pond."
  176.  
  177.     | rectangle |
  178.     aGraphicsContext paint: ColorValue red.
  179.     (world antColony) ants do: [:each | aGraphicsContext displayDotOfDiameter: 5 at: each location].
  180.     aGraphicsContext paint: ColorValue blue.
  181.     "rectangle := Rectangle origin: 50 @ 50 corner: 100 @ 100."
  182.     aGraphicsContext displayRectangle: world pond rectangle.! !
  183.  
  184. !DrawExampleView methodsFor: 'initialize-release'!
  185.  
  186. world
  187.     ^world!
  188.  
  189. initialize
  190.  
  191.     world := World new.! !
  192.  
  193.  
  194. !World class methodsFor: 'instance creation'!
  195.  
  196. new
  197.     "Answer a newly created and initialized instance."
  198.  
  199.     ^super new initialize! !
  200.  
  201.  
  202. !World methodsFor: 'initialize-release'!
  203.  
  204. antColony
  205.     ^antColony!
  206.  
  207. initialize
  208.     "Initialize a newly created instance. This method must answer the receiver."
  209.  
  210.     super initialize.
  211.     " *** Replace this comment with the appropriate initialization code *** "
  212.     pond := Pond new.
  213.     "location := 160 @ 130."
  214.     antColony := Colony new.
  215.     ^self!
  216.  
  217. pond
  218.     ^pond! !
  219.  
  220.  
  221. !Ant class methodsFor: 'instance creation'!
  222.  
  223. new
  224.     "Answer a newly created and initialized instance."
  225.  
  226.     ^super new initialize! !
  227.  
  228.  
  229. !Ant methodsFor: 'initialize-release'!
  230.  
  231. move
  232.  
  233.     | move |
  234.     move := random next.
  235.     location x <= 150 | (location x >= 170) | (location y <= 120)
  236.         | (location y >= 140)
  237.             ifTrue:
  238.                 [location x <= 150
  239.                     ifTrue:
  240.                         [move <= 0.33 ifTrue: [direction := direction east].
  241.                         0.33 < move & (move < 0.66) ifTrue: [direction := direction south].
  242.                         0.66 <= move ifTrue: [direction := direction north].
  243.                         Transcript
  244.                             clear;
  245.                             print: location].
  246.                 location x >= 170
  247.                     ifTrue:
  248.                         [move <= 0.33 ifTrue: [direction := direction west].
  249.                         0.33 < move & (move < 0.66) ifTrue: [direction := direction north].
  250.                         0.66 <= move ifTrue: [direction := direction south].
  251.                         Transcript
  252.                             clear;
  253.                             print: location].
  254.                 location y <= 120
  255.                     ifTrue:
  256.                         [move <= 0.33 ifTrue: [direction := direction south].
  257.                         0.33 < move & (move < 0.66) ifTrue: [direction := direction east].
  258.                         0.66 <= move ifTrue: [direction := direction west].
  259.                         Transcript
  260.                             clear;
  261.                             print: location].
  262.                 location y >= 140
  263.                     ifTrue:
  264.                         [move <= 0.33 ifTrue: [direction := direction north].
  265.                         0.33 < move & (move < 0.66) ifTrue: [direction := direction east].
  266.                         0.66 <= move ifTrue: [direction := direction west].
  267.                         Transcript
  268.                             clear;
  269.                             print: location].
  270.                 location x <= 150 | (location y <= 120)
  271.                     ifTrue:
  272.                         [move <= 0.50 ifTrue: [direction := direction east].
  273.                         move >= 0.51 ifTrue: [direction := direction south].
  274.                         Transcript
  275.                             clear;
  276.                             print: location].
  277.                 location x >= 170 | (location y <= 120)
  278.                     ifTrue:
  279.                         [move <= 0.50 ifTrue: [direction := direction west].
  280.                         move >= 0.51 ifTrue: [direction := direction south].
  281.                         Transcript
  282.                             clear;
  283.                             print: location].
  284.                 location x <= 150 | (location y >= 140)
  285.                     ifTrue:
  286.                         [move <= 0.50 ifTrue: [direction := direction east].
  287.                         move >= 0.51 ifTrue: [direction := direction north].
  288.                         Transcript
  289.                             clear;
  290.                             print: location].
  291.                 location x >= 170 | (location y >= 140)
  292.                     ifTrue:
  293.                         [move <= 0.50 ifTrue: [direction := direction west].
  294.                         move >= 0.51 ifTrue: [direction := direction north].
  295.                         Transcript
  296.                             clear;
  297.                             print: location]]
  298.             ifFalse:
  299.                 [move := random next.
  300.                 move < 0.05 ifTrue: [direction := direction left].
  301.                 move > 0.85 ifTrue: [direction := direction right]].
  302.     location := location + (direction * 2)!
  303.  
  304. location
  305.  
  306.     ^location!
  307.  
  308.  displayOnn: aGraphicsContext
  309.  
  310.     aGraphicsContext displayDotOfDiameter: 5 at: location!
  311.  
  312. initialize
  313.     "Initialize a newly created instance. This method must answer the receiver."
  314.  
  315.     super initialize.
  316.     location := 160 @ 130.
  317.     direction := 1 @ 0.
  318.     random := Random new! !
  319.  
  320.  
  321. !Pond class methodsFor: 'instance creation'!
  322.  
  323. new
  324.     "Answer a newly created and initialized instance."
  325.  
  326.     ^super new initialize! !
  327.  
  328.  
  329. !Pond methodsFor: 'initialize-release'!
  330.  
  331. rectangle
  332.     rectangle := Rectangle origin: origin corner: corner.
  333.     ^rectangle!
  334.  
  335. initialize
  336.     "Initialize a newly created instance. This method must answer the receiver."
  337.  
  338.     super initialize.
  339.     origin:= 50@50.
  340.     corner:=100@100.
  341.     " *** Replace this comment with the appropriate initialization code *** "
  342.     ^self! !
  343.  
  344.  
  345. !Colony class methodsFor: 'instance creation'!
  346.  
  347. new
  348.     "Answer a newly created and initialized instance."
  349.  
  350.     ^super new initialize! !
  351.  
  352.  
  353. !Colony methodsFor: 'initialize-release'!
  354.  
  355. displayOnn: aGraphicsContext
  356.  
  357.     ants do: [:each | each displayOn: aGraphicsContext]!
  358.  
  359. initialize
  360.  
  361.     ants := OrderedCollection new.
  362.     50 timesRepeat:[ants add: Ant new].!
  363.  
  364. move
  365.  
  366.     ants do: [:each | each move].!
  367.  
  368. ants
  369.     ^ants! !
  370.  
  371.  
  372. !Core.Point methodsFor: 'converting'!
  373.  
  374. left
  375.  
  376.     x = 1 ifTrue: [^0 @ -1].
  377.     x = -1 ifTrue: [^0 @ 1].
  378.     y = 1 ifTrue: [^1 @ 0].
  379.     ^-1 @ 0!
  380.  
  381. south
  382.     ^0 @ 1!
  383.  
  384. north
  385.     ^0 @ -1!
  386.  
  387. west
  388.     ^-1 @ 0!
  389.  
  390. east
  391.     ^1 @ 0!
  392.  
  393. right
  394.  
  395.     x = 1 ifTrue: [^0 @ 1].
  396.     x = -1 ifTrue: [^0 @ ( -1)].
  397.     y = 1 ifTrue: [^(-1) @ 0].
  398.     ^1 @ 0! !
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement