rubylabs 0.9.5 → 0.9.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/data/huffman/hafreq.txt +2 -2
- data/lib/iterationlab.rb +87 -5
- data/lib/lifelab.rb +181 -0
- data/lib/rubylabs.rb +1 -1
- metadata +10 -9
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.6
|
data/data/huffman/hafreq.txt
CHANGED
data/lib/iterationlab.rb
CHANGED
@@ -19,6 +19,23 @@ documented here.
|
|
19
19
|
|
20
20
|
module IterationLab
|
21
21
|
|
22
|
+
ArrayView = Struct.new(:array, :rects, :bar, :palette, :history, :options)
|
23
|
+
|
24
|
+
@@viewOptions = {
|
25
|
+
:array_fill => 'lightblue',
|
26
|
+
:bar_fill => 'darkblue',
|
27
|
+
:canvas_fill => 'white',
|
28
|
+
:mark_color => 'blue',
|
29
|
+
:x0 => 10, # left edge of leftmost bar
|
30
|
+
:dx => 10, # distance between left edges of adjacent bars
|
31
|
+
:y0 => 50, # top edege of tallest bar
|
32
|
+
:y1 => 150, # bottom edge of array bars
|
33
|
+
:ymin => 3, # minimum height of bar
|
34
|
+
:delay => 0.01,
|
35
|
+
}
|
36
|
+
|
37
|
+
@@drawing = nil
|
38
|
+
|
22
39
|
# The RubyLabs implementation of Ruby's <tt>include?</tt> method.
|
23
40
|
# Does a linear search of array +a+ to find item +k+, returning
|
24
41
|
# +true+ or +false+ depending on whether the item was found.
|
@@ -101,17 +118,45 @@ module IterationLab
|
|
101
118
|
# and reinsert it at a location between 0 and +i+ (i.e. move the item
|
102
119
|
# to the left in the array).
|
103
120
|
#--
|
121
|
+
# updated 10/9/2012 for visualization
|
104
122
|
# :begin :move_left
|
105
123
|
def move_left(a, i)
|
106
|
-
x = a.slice!(i) # remove the item at location i
|
107
|
-
j = i-1 # start scanning from the left of i
|
108
|
-
while j >= 0 && less(x, a[j])
|
109
|
-
|
124
|
+
# x = a.slice!(i) # remove the item at location i
|
125
|
+
# j = i-1 # start scanning from the left of i
|
126
|
+
# while j >= 0 && less(x, a[j])
|
127
|
+
# j = j-1 # move left
|
128
|
+
# end
|
129
|
+
# a.insert(j+1, x) # insert x back into a at location j
|
130
|
+
if @@drawing
|
131
|
+
touch(i)
|
132
|
+
set_region(i+1)
|
133
|
+
sleep(@@drawing.options[:delay])
|
134
|
+
end
|
135
|
+
while i > 0 && less(a[i], a[i-1])
|
136
|
+
swap(a, i-1, i)
|
137
|
+
i -= 1
|
110
138
|
end
|
111
|
-
a.insert(j+1, x) # insert x back into a at location j
|
112
139
|
end
|
113
140
|
# :end :move_left
|
114
141
|
|
142
|
+
# Helper method called by +move_left+ to exchange the items at specified
|
143
|
+
# locations in an array.
|
144
|
+
#--
|
145
|
+
# :begin :swap
|
146
|
+
def swap(a, i, j)
|
147
|
+
a[i], a[j] = a[j], a[i]
|
148
|
+
if @@drawing.class == ArrayView
|
149
|
+
dist = (j - i) * @@drawing.options[:dx]
|
150
|
+
ri = @@drawing.rects[i]
|
151
|
+
rj = @@drawing.rects[j]
|
152
|
+
Canvas.move(ri, dist, 0)
|
153
|
+
Canvas.move(rj, -dist, 0)
|
154
|
+
@@drawing.rects[i], @@drawing.rects[j] = @@drawing.rects[j], @@drawing.rects[i]
|
155
|
+
sleep(@@drawing.options[:delay])
|
156
|
+
end
|
157
|
+
end
|
158
|
+
# :end :swap
|
159
|
+
|
115
160
|
# +less+ is called by +isort+ to compare two items.
|
116
161
|
# It is implemented as a helper method in order to allow users to
|
117
162
|
# attach a probe to count the number of comparisons.
|
@@ -153,6 +198,43 @@ module IterationLab
|
|
153
198
|
end
|
154
199
|
end
|
155
200
|
|
201
|
+
# Visualization
|
202
|
+
|
203
|
+
def view_array(a, userOptions = {})
|
204
|
+
Canvas.init(800, 200, "IterationLab")
|
205
|
+
options = @@viewOptions.merge(userOptions)
|
206
|
+
|
207
|
+
amax = a.max
|
208
|
+
rects = []
|
209
|
+
a.each_with_index do |val, i|
|
210
|
+
rx = options[:x0] + i * options[:dx]
|
211
|
+
y = Float(val)
|
212
|
+
dy = options[:y1] - options[:y0] - options[:ymin] # height of tallest bar
|
213
|
+
ry = options[:y1] - options[:ymin] - (y/amax)*dy # height of this bar
|
214
|
+
rects << Canvas::Rectangle.new( rx, ry, rx + options[:dx], options[:y1], :fill => options[:array_fill], :outline => options[:canvas_fill] )
|
215
|
+
end
|
216
|
+
bar = Canvas::Rectangle.new( options[:x0], options[:y1] + 10, options[:x0] + a.length*options[:dx], options[:y1]+15, :fill => options[:bar_fill] )
|
217
|
+
|
218
|
+
@@drawing = ArrayView.new(a, rects, bar, ['#000080','#BADFEB'], [], options)
|
219
|
+
return true
|
220
|
+
end
|
221
|
+
|
222
|
+
def touch(loc)
|
223
|
+
rect = @@drawing.rects[loc]
|
224
|
+
pmax = @@drawing.palette.length
|
225
|
+
@@drawing.history.pop if @@drawing.history.length >= pmax
|
226
|
+
@@drawing.history.insert(0, rect)
|
227
|
+
@@drawing.history.each_with_index do |r, i|
|
228
|
+
r.fill = @@drawing.palette[i]
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
def set_region(loc)
|
233
|
+
x1, y1, x2, y2 = @@drawing.bar.coords
|
234
|
+
x1 = @@drawing.options[:x0] + loc * @@drawing.options[:dx]
|
235
|
+
@@drawing.bar.coords = [x1, y1, x2, y2]
|
236
|
+
end
|
237
|
+
|
156
238
|
end # IterationLab
|
157
239
|
|
158
240
|
end # RubyLabs
|
data/lib/lifelab.rb
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
module RubyLabs
|
2
|
+
|
3
|
+
=begin rdoc
|
4
|
+
|
5
|
+
== LifeLab
|
6
|
+
|
7
|
+
Conway's Game of Life.
|
8
|
+
#--
|
9
|
+
TODO all of it...
|
10
|
+
#++
|
11
|
+
=end
|
12
|
+
|
13
|
+
module LifeLab
|
14
|
+
|
15
|
+
# -- Initializations -- These are "global" vars in the outer LifeLab scope that are
|
16
|
+
# accessible to all the classes and modules defined inside LifeLab
|
17
|
+
|
18
|
+
@@lifeDirectory = File.join(File.dirname(__FILE__), '..', 'data', 'life')
|
19
|
+
|
20
|
+
@@rule = "B3/S23"
|
21
|
+
|
22
|
+
@@params = {
|
23
|
+
:rounds => 1000,
|
24
|
+
:rows => 20,
|
25
|
+
:cols => 20,
|
26
|
+
:pause => 0.01,
|
27
|
+
}
|
28
|
+
|
29
|
+
LifeView = Struct.new(:rows, :cols, :options)
|
30
|
+
|
31
|
+
@@viewOptions = {
|
32
|
+
:cellSize => 2,
|
33
|
+
:cellRows => 100,
|
34
|
+
:cellCols => 200,
|
35
|
+
:padding => 20,
|
36
|
+
:traceSize => 10,
|
37
|
+
:cellColor => '#DDDDDD',
|
38
|
+
}
|
39
|
+
|
40
|
+
@@drawing = nil
|
41
|
+
|
42
|
+
=begin rdoc
|
43
|
+
|
44
|
+
== Life
|
45
|
+
|
46
|
+
The Life class defines a singleton object that has the methods used to initialize and run
|
47
|
+
simulations.
|
48
|
+
|
49
|
+
=end
|
50
|
+
|
51
|
+
class Life
|
52
|
+
|
53
|
+
# Execute one round.
|
54
|
+
|
55
|
+
def Life.step
|
56
|
+
puts "step"
|
57
|
+
return true
|
58
|
+
end
|
59
|
+
|
60
|
+
# Print information about the system.
|
61
|
+
|
62
|
+
def Life.state
|
63
|
+
puts "state"
|
64
|
+
return true
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# Set the value of one of the run-time options. The options, and their default values, are:
|
69
|
+
# [TBD]
|
70
|
+
#
|
71
|
+
# Example:
|
72
|
+
# >>!blue Life.set_option(:name, value)
|
73
|
+
# => value
|
74
|
+
#
|
75
|
+
# Note: Call Life.state to see the current settings for the options.
|
76
|
+
|
77
|
+
def Life.set_option(key, val)
|
78
|
+
case key
|
79
|
+
# the branches of the case statement are places to validate parameter
|
80
|
+
# values -- see examples from MARSLab below
|
81
|
+
when :todo
|
82
|
+
puts "options TBD"
|
83
|
+
return nil
|
84
|
+
# when :memSize
|
85
|
+
# if val.class != Fixnum || val < 1024 || val > 16536
|
86
|
+
# puts ":memSize must be an integer between 1024 and 16536"
|
87
|
+
# return nil
|
88
|
+
# end
|
89
|
+
# when :maxRounds, :buffer
|
90
|
+
# if val.class != Fixnum
|
91
|
+
# puts ":#{key} must be an integer"
|
92
|
+
# return nil
|
93
|
+
# end
|
94
|
+
else
|
95
|
+
puts "Unknown option: #{key}"
|
96
|
+
puts "Call Life.state to see a list of options and their current settings."
|
97
|
+
return nil
|
98
|
+
end
|
99
|
+
@@params[key] = val
|
100
|
+
end
|
101
|
+
|
102
|
+
# Initialize the RubyLabs canvas with a drawing of the Life board. The
|
103
|
+
# display will show one rectangle for each cell.
|
104
|
+
|
105
|
+
def Life.view(userOptions = {} )
|
106
|
+
|
107
|
+
options = @@viewOptions.merge(userOptions)
|
108
|
+
|
109
|
+
cellsize = options[:cellSize]
|
110
|
+
padding = options[:padding]
|
111
|
+
|
112
|
+
width = options[:cellCols] * cellsize + 2*padding
|
113
|
+
height = options[:cellRows] * cellsize + 2*padding
|
114
|
+
Canvas.init(width, height, "LifeLab")
|
115
|
+
|
116
|
+
cells = []
|
117
|
+
for i in 0...options[:cellRows]
|
118
|
+
for j in 0...options[:cellCols]
|
119
|
+
x = j * cellsize + padding
|
120
|
+
y = i * cellsize + padding
|
121
|
+
cells << Canvas::Rectangle.new( x, y, x+cellsize, y+cellsize, :outline => "#888888", :fill => options[:cellColor] )
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
@@drawing = LifeView.new(cells, options)
|
126
|
+
return true
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
# Close the RubyLabs Canvas window.
|
131
|
+
|
132
|
+
def Life.close_view
|
133
|
+
Canvas.close
|
134
|
+
end
|
135
|
+
|
136
|
+
# Reset the game state.
|
137
|
+
|
138
|
+
def Life.reset
|
139
|
+
puts "fix this!"
|
140
|
+
# if @@drawing
|
141
|
+
# @@drawing.cells.each do |x|
|
142
|
+
# x.fill = @@drawing.options[:cellColor]
|
143
|
+
# end
|
144
|
+
# end
|
145
|
+
return true
|
146
|
+
end
|
147
|
+
|
148
|
+
# Print a list of programs in the LifeLab data directory.
|
149
|
+
|
150
|
+
def Life.dir
|
151
|
+
# puts "Redcode programs in #{@@marsDirectory}:"
|
152
|
+
# Dir.open(@@marsDirectory).each do |file|
|
153
|
+
# next if file[0] == ?.
|
154
|
+
# file.slice!(/\.txt/)
|
155
|
+
# puts " " + file
|
156
|
+
# end
|
157
|
+
return nil
|
158
|
+
end
|
159
|
+
|
160
|
+
private
|
161
|
+
|
162
|
+
# Drawing hook, called from MARS.step when the canvas is active.
|
163
|
+
|
164
|
+
# def MARS.updateCells(pc)
|
165
|
+
# id = pc.id
|
166
|
+
# a = pc.history[pc.current[:thread]]
|
167
|
+
# d = @@drawing.palettes[id].length - a.length
|
168
|
+
# a.each_with_index do |x, i|
|
169
|
+
# @@drawing.cells[x].fill = @@drawing.palettes[id][i+d]
|
170
|
+
# end
|
171
|
+
# end
|
172
|
+
|
173
|
+
end # class Life
|
174
|
+
|
175
|
+
# Initialize any class variables here...
|
176
|
+
|
177
|
+
# @@entries = Array.new
|
178
|
+
|
179
|
+
end # LifeLab
|
180
|
+
|
181
|
+
end # RubyLabs
|
data/lib/rubylabs.rb
CHANGED
@@ -8,6 +8,7 @@ autoload :IterationLab, "iterationlab.rb"
|
|
8
8
|
autoload :RecursionLab, "recursionlab.rb"
|
9
9
|
autoload :HashLab, "hashlab.rb"
|
10
10
|
autoload :BitLab, "bitlab.rb"
|
11
|
+
autoload :LifeLab, "lifelab.rb"
|
11
12
|
autoload :MARSLab, "marslab.rb"
|
12
13
|
autoload :RandomLab, "randomlab.rb"
|
13
14
|
autoload :EncryptionLab, "encryptionlab.rb"
|
@@ -325,7 +326,6 @@ call <tt>a.random(:success)</tt> to get a value that is in the array +a+, or cal
|
|
325
326
|
|
326
327
|
end # class TestArray
|
327
328
|
|
328
|
-
|
329
329
|
# Equivalent to calling <tt>TestArray.new</tt>.
|
330
330
|
#
|
331
331
|
# Examples:
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubylabs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 55
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
9
|
+
- 6
|
10
|
+
version: 0.9.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- conery
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-10-10 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rubygems-test
|
@@ -33,8 +33,8 @@ dependencies:
|
|
33
33
|
version_requirements: *id001
|
34
34
|
description: A set of modules for interactive experiments in an introductory computer science class.
|
35
35
|
email: conery@cs.uoregon.edu
|
36
|
-
executables:
|
37
|
-
|
36
|
+
executables: []
|
37
|
+
|
38
38
|
extensions: []
|
39
39
|
|
40
40
|
extra_rdoc_files:
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- lib/hashlab.rb
|
91
91
|
- lib/introlab.rb
|
92
92
|
- lib/iterationlab.rb
|
93
|
+
- lib/lifelab.rb
|
93
94
|
- lib/marslab.rb
|
94
95
|
- lib/permute.rb
|
95
96
|
- lib/randomlab.rb
|
@@ -115,8 +116,8 @@ homepage: http://github.com/conery/rubylabs
|
|
115
116
|
licenses: []
|
116
117
|
|
117
118
|
post_install_message:
|
118
|
-
rdoc_options:
|
119
|
-
|
119
|
+
rdoc_options: []
|
120
|
+
|
120
121
|
require_paths:
|
121
122
|
- lib
|
122
123
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -140,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
141
|
requirements: []
|
141
142
|
|
142
143
|
rubyforge_project: rubylabs
|
143
|
-
rubygems_version: 1.8.
|
144
|
+
rubygems_version: 1.8.24
|
144
145
|
signing_key:
|
145
146
|
specification_version: 3
|
146
147
|
summary: Software and data for lab projects for Explorations in Computing.
|