RubyGrid 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/rubygrid/grid.rb +348 -0
- data/lib/rubygrid/version.rb +4 -0
- data/lib/rubygrid.rb +44 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 63c31ea89ff50e5ed3aea13d735dd7da612ea187
|
4
|
+
data.tar.gz: cd2f68ec4783d291ce4d02594393a6b62afbbe1e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9882afb2fc490723dc72b46ddfa2e8aff36bf15c90daf88d8ec62b781262e66e5334e823dcc8a98bb7ce8c8774fe922383412d3b695ff10c7c23d05b41ebb1e7
|
7
|
+
data.tar.gz: fffe1ce1a8f852db1e94ad4ed9b4f33bf79ce04307768ca375fc8b1273445361df136b4bad154e02d3909d9ab139fa5a680300807afeffde8fb464250ca5fe97
|
@@ -0,0 +1,348 @@
|
|
1
|
+
module RubyGrid
|
2
|
+
class Grid
|
3
|
+
# Grid constructor.
|
4
|
+
# +sizex+ and +sizey+::
|
5
|
+
# These set the desired size of the grid, 4 by default.
|
6
|
+
# +def_value+::
|
7
|
+
# The default data to store in a cell by default.
|
8
|
+
def initialize(sizex=4, sizey=4, def_value=nil)
|
9
|
+
@grid = []
|
10
|
+
|
11
|
+
# Default Grid size is 4x4
|
12
|
+
sizex = 4 unless is_number?(sizex)
|
13
|
+
sizey = 4 unless is_number?(sizey)
|
14
|
+
|
15
|
+
@size_x = sizex
|
16
|
+
@size_y = sizey
|
17
|
+
@def_value = def_value
|
18
|
+
|
19
|
+
# Build the grid and insert the default values.
|
20
|
+
@size_x.times do |x|
|
21
|
+
@grid.push([])
|
22
|
+
|
23
|
+
@size_y.times do |y|
|
24
|
+
@grid[x].push(def_value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
public
|
30
|
+
|
31
|
+
def sizex; @size_x; end
|
32
|
+
def sizey; @size_y; end
|
33
|
+
def grid; @grid; end
|
34
|
+
|
35
|
+
# This checks to see if a given x,y pair are within
|
36
|
+
# the boundaries of the grid.
|
37
|
+
def is_valid?(x, y)
|
38
|
+
unless (is_number?(x) and is_number?(y))
|
39
|
+
return false
|
40
|
+
end
|
41
|
+
return ((x >= 0 and x < @size_x) and (y >= 0 and y < @size_y))
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns the center coordinates of the grid
|
45
|
+
def center
|
46
|
+
x = (@size_x - 1)/2
|
47
|
+
y = (@size_y - 1)/2
|
48
|
+
return x,y
|
49
|
+
end
|
50
|
+
|
51
|
+
# Get the data in a given x,y cell.
|
52
|
+
# Returns nil if the cell is not valid.
|
53
|
+
def get_cell(x, y)
|
54
|
+
return @grid[x][y] if is_valid?(x,y)
|
55
|
+
end
|
56
|
+
|
57
|
+
# This method will return a set of cell data in a table.
|
58
|
+
# The 'cells' argument should be an array of x,y pairs of
|
59
|
+
# the cells being requested.
|
60
|
+
def get_cells(cells) # :yields: cell_data
|
61
|
+
data = []
|
62
|
+
|
63
|
+
return data unless cells.respond_to?(:collect)
|
64
|
+
|
65
|
+
cells.each do |x,y|
|
66
|
+
if is_valid?(x,y)
|
67
|
+
if block_given?
|
68
|
+
yield @grid[x][y]
|
69
|
+
else
|
70
|
+
data << @grid[x][y]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
return block_given? ? nil : data
|
76
|
+
end
|
77
|
+
|
78
|
+
# Sets a given x,y cell to the data object
|
79
|
+
def set_cell(x,y, obj)
|
80
|
+
@grid[x][y] = obj if is_valid?(x,y)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Resets a given x,y cell to the grid default value
|
84
|
+
def reset_cell(x,y)
|
85
|
+
@grid[x][y] = @def_value if is_valid?(x,y)
|
86
|
+
end
|
87
|
+
|
88
|
+
# Resets the entire grid to the default value
|
89
|
+
def reset_all
|
90
|
+
@size_x.times do |x|
|
91
|
+
@size_y.times do |y|
|
92
|
+
@grid[x][y] = @def_value
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# This method is used to populate multiple cells at once.
|
98
|
+
# +data+::
|
99
|
+
# This argument must be an Array, with each element
|
100
|
+
# consisting of three values: x, y, and the data to
|
101
|
+
# set the cell to.
|
102
|
+
#
|
103
|
+
# Example:
|
104
|
+
#
|
105
|
+
# d = [[4, 4, "X"], [4, 5, "O"], [5, 4, "O"], [5, 5, "X"]]
|
106
|
+
# grid.populate(d)
|
107
|
+
#
|
108
|
+
# If the object to be populated is nil, it is replaced with
|
109
|
+
# the default value.
|
110
|
+
def populate(data)
|
111
|
+
return unless data.respond_to?(:collect)
|
112
|
+
|
113
|
+
data.each do |x, y, obj|
|
114
|
+
if is_valid?(x,y)
|
115
|
+
obj = @def_value if obj.nil?
|
116
|
+
@grid[x][y] = obj
|
117
|
+
end
|
118
|
+
end
|
119
|
+
return
|
120
|
+
end
|
121
|
+
|
122
|
+
# This method returns the entire grid's contents in a
|
123
|
+
# flat array suitable for feeding to #populate above.
|
124
|
+
# Useful for recreating a grid layout.
|
125
|
+
# If the +no_default+ argument is non-false, then the
|
126
|
+
# returned data Array only contains elements who's
|
127
|
+
# cells are not the default value.
|
128
|
+
# Returns +nil+ if a block is given.
|
129
|
+
def get_contents(no_default=false) # :yields: x, y, cell_data
|
130
|
+
data = []
|
131
|
+
cell_obj = nil
|
132
|
+
|
133
|
+
@size_x.times do |x|
|
134
|
+
@size_y.times do |y|
|
135
|
+
cell_obj = @grid[x][y]
|
136
|
+
unless no_default and cell_obj == @def_value
|
137
|
+
if block_given?
|
138
|
+
yield x, y, cell_obj
|
139
|
+
else
|
140
|
+
data.push([x,y,cell_obj])
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
return block_given? ? nil : data
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
# Convienence method to return an x,y vector pair from the
|
151
|
+
# GRID_* vector constants. Or nil if there is no such
|
152
|
+
# constant.
|
153
|
+
def get_vector(vector)
|
154
|
+
case vector
|
155
|
+
when TOP_LEFT then return -1, -1
|
156
|
+
when TOP then return -1, 0
|
157
|
+
when TOP_RIGHT then return -1, 1
|
158
|
+
when LEFT then return 0, -1
|
159
|
+
when CENTER then return 0, 0
|
160
|
+
when RIGHT then return 0, 1
|
161
|
+
when BOTTOM_LEFT then return 1, -1
|
162
|
+
when BOTTOM then return 1, 0
|
163
|
+
when BOTTOM_RIGHT then return 1, 1
|
164
|
+
else
|
165
|
+
return nil, nil
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# Get a cell's neighbor in a given vector.
|
170
|
+
def get_neighbor(x,y, vector)
|
171
|
+
vx, vy = get_vector(vector)
|
172
|
+
|
173
|
+
if vx and vy
|
174
|
+
x = x + vx
|
175
|
+
y = y + vy
|
176
|
+
return @grid[x][y] if is_valid?(x,y)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
# Will return an Array of 8 elements, with each element
|
181
|
+
# representing one of the 8 neighbors for the given
|
182
|
+
# x,y cell. Each element of the returned Array will consist
|
183
|
+
# of the x,y cell pair, plus the data stored there, suitable
|
184
|
+
# for use of the populate method. If the neighbor cell is
|
185
|
+
# outside the grid, then [nil, nil, OUTSIDE] is used
|
186
|
+
# for the value.
|
187
|
+
# If the given x,y values are not sane, an empty Array
|
188
|
+
# is returned instead.
|
189
|
+
def get_neighbors(x,y) # :yields: x, y, cell_data
|
190
|
+
data = []
|
191
|
+
|
192
|
+
return data unless is_valid?(x,y)
|
193
|
+
|
194
|
+
# The vectors used are x,y pairs between -1 and +1
|
195
|
+
# for the given x,y cell.
|
196
|
+
# IE:
|
197
|
+
# (-1, -1) (0, -1) (1, -1)
|
198
|
+
# (-1, 0) (0, 0) (1, 0)
|
199
|
+
# (-1, 1) (0, 1) (1, 1)
|
200
|
+
# Value of 0,0 is ignored, since that is the cell
|
201
|
+
# we are working with! :D
|
202
|
+
for gx in -1..1
|
203
|
+
for gy in -1..1
|
204
|
+
vx = x + gx
|
205
|
+
vy = y + gy
|
206
|
+
|
207
|
+
unless gx == 0 and gy == 0
|
208
|
+
if is_valid?(vx,vy)
|
209
|
+
if block_given?
|
210
|
+
yield vx, vy, @grid[vx][vy]
|
211
|
+
else
|
212
|
+
data.push([vx, vy, @grid[vx][vy]])
|
213
|
+
end
|
214
|
+
else
|
215
|
+
if block_given?
|
216
|
+
yield nil, nil, OUTSIDE
|
217
|
+
else
|
218
|
+
data.push([nil, nil, OUTSIDE])
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
return block_given? ? nil : data
|
226
|
+
end
|
227
|
+
|
228
|
+
# This method will change the grid size. If the new size is
|
229
|
+
# smaller than the old size, data in the cells now 'outside'
|
230
|
+
# the grid is lost. If the grid is now larger, new cells are
|
231
|
+
# filled with the default value given when the grid was first
|
232
|
+
# created.
|
233
|
+
def resize(newx, newy)
|
234
|
+
unless is_number?(newx) and is_number?(newy)
|
235
|
+
return false
|
236
|
+
end
|
237
|
+
|
238
|
+
# Save old data
|
239
|
+
c = get_contents
|
240
|
+
|
241
|
+
# Reset grid.
|
242
|
+
@grid.clear
|
243
|
+
|
244
|
+
# Set the new sizes.
|
245
|
+
@size_x = newx
|
246
|
+
@size_y = newy
|
247
|
+
|
248
|
+
newx.times do |x|
|
249
|
+
@grid.push([])
|
250
|
+
|
251
|
+
newy.times do |y|
|
252
|
+
@grid[x].push(@def_value)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
# Insert the old contents
|
257
|
+
populate(c)
|
258
|
+
|
259
|
+
return true
|
260
|
+
end
|
261
|
+
|
262
|
+
# This method returns an Array of all values in a given
|
263
|
+
# row 'x' value.
|
264
|
+
# If given a block, then the data in each cell in the row
|
265
|
+
# is passed to the block.
|
266
|
+
def get_row(x) # :yields: cell_data
|
267
|
+
row = []
|
268
|
+
|
269
|
+
if is_number?(x) and (x >= 0 and x < @size_x)
|
270
|
+
row = @grid[x]
|
271
|
+
end
|
272
|
+
|
273
|
+
if block_given?
|
274
|
+
row.each do |obj|
|
275
|
+
yield obj
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
return block_given? ? nil : row
|
280
|
+
end
|
281
|
+
|
282
|
+
# This method returns an Array of all values in a given
|
283
|
+
# column 'y' value.
|
284
|
+
# If given a block, then the data in each cell in the column
|
285
|
+
# is passed to the block.
|
286
|
+
def get_column(y) # :yields: cell_data
|
287
|
+
col = []
|
288
|
+
|
289
|
+
if is_number?(y) and (y >= 0 and y < @size_y)
|
290
|
+
@size_x.times do |x|
|
291
|
+
if block_given?
|
292
|
+
yield @grid[x][y]
|
293
|
+
else
|
294
|
+
col.push(@grid[x][y])
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
return block_given? ? nil : col
|
300
|
+
end
|
301
|
+
|
302
|
+
# This method traverses a line of cells, from a given x,y
|
303
|
+
# going in +vector+ direction. The vector arg is one of the
|
304
|
+
# Grid::* traversal constants. This will return an Array of
|
305
|
+
# the data of the cells along the traversal path or nil if
|
306
|
+
# the original x,y is not valid or if the vector is not one
|
307
|
+
# of the constant values. The first element of the Array
|
308
|
+
# will be the first cell -after- the x, y cell given as the
|
309
|
+
# argument.
|
310
|
+
# In the returned Array, each element will be in the format
|
311
|
+
# of [x, y, obj], suitable for #populate.
|
312
|
+
# If a block is given, then each x, y, and data is passed to
|
313
|
+
# the block, and nil is returned.
|
314
|
+
def traverse(x, y, vector) # :yields: x, y, cell_data
|
315
|
+
data = []
|
316
|
+
|
317
|
+
if is_valid?(x,y)
|
318
|
+
vx, vy = get_vector(vector)
|
319
|
+
|
320
|
+
return data unless vx and vy
|
321
|
+
|
322
|
+
gx = x + vx
|
323
|
+
gy = y + vy
|
324
|
+
|
325
|
+
while is_valid?(gx, gy)
|
326
|
+
obj = @grid[gx,gy]
|
327
|
+
|
328
|
+
if block_given?
|
329
|
+
yield gx, gy, obj
|
330
|
+
else
|
331
|
+
data.push([gx, gy, obj])
|
332
|
+
end
|
333
|
+
|
334
|
+
gx = gx + vx
|
335
|
+
gy = gy + vy
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
return block_given? ? nil : data
|
340
|
+
end
|
341
|
+
|
342
|
+
private
|
343
|
+
def is_number?(n)
|
344
|
+
(n.respond_to?(:even?) and not n.nil?)
|
345
|
+
end
|
346
|
+
|
347
|
+
end
|
348
|
+
end
|
data/lib/rubygrid.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygrid/version'
|
2
|
+
require 'rubygrid/grid'
|
3
|
+
|
4
|
+
module RubyGrid
|
5
|
+
|
6
|
+
# Some constant values. These are set as strings instead of
|
7
|
+
# integer values to prevent clashes of data.
|
8
|
+
OUTSIDE = "OUTSIDE".freeze
|
9
|
+
NOT_VALID = "NOT_VALID".freeze
|
10
|
+
NIL_VALUE = "NIL_VALUE".freeze
|
11
|
+
|
12
|
+
# Traversal Vector Constants
|
13
|
+
TOP_LEFT = 1
|
14
|
+
TOP = 2
|
15
|
+
TOP_RIGHT = 3
|
16
|
+
LEFT = 4
|
17
|
+
CENTER = 5
|
18
|
+
RIGHT = 6
|
19
|
+
BOTTOM_LEFT = 7
|
20
|
+
BOTTOM = 8
|
21
|
+
BOTTOM_RIGHT = 9
|
22
|
+
|
23
|
+
# Wrapper for Grid#new
|
24
|
+
def RubyGrid.create(x, y, value)
|
25
|
+
return Grid.new(x, y, value)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Yields each of the vector constants in turn, from top-left
|
29
|
+
# to bottom-right.
|
30
|
+
def RubyGrid.get_all_vectors # :yields: vector_constant
|
31
|
+
[TOP_LEFT,
|
32
|
+
TOP,
|
33
|
+
TOP_RIGHT,
|
34
|
+
LEFT,
|
35
|
+
CENTER,
|
36
|
+
RIGHT,
|
37
|
+
BOTTOM_LEFT,
|
38
|
+
BOTTOM,
|
39
|
+
BOTTOM_RIGHT
|
40
|
+
].each do |v|
|
41
|
+
yield v # :yields: vector_constant
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: RubyGrid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Randy Carnahan
|
8
|
+
- yacn
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.3'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.3'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: minitest
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: guard
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: guard-minitest
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
description: A simple gem for grids in Ruby
|
85
|
+
email: admin@yacn.pw
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- lib/rubygrid.rb
|
91
|
+
- lib/rubygrid/version.rb
|
92
|
+
- lib/rubygrid/grid.rb
|
93
|
+
homepage: https://github.com/yacn/Ruby-Grid
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.0.6
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Grid gem for Games or whatever else you can think of
|
117
|
+
test_files: []
|