Class: Grid
- Inherits:
-
Object
- Object
- Grid
- Defined in:
- lib/cem/cruzzles.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #[](y, x = nil) ⇒ Object
-
#[]=(y, x, assign = nil) ⇒ Object
Array assignment to grid.
- #adja_index(x, y = nil) ⇒ Object
- #each(&block) ⇒ Object
- #each_with_index(&block) ⇒ Object
-
#initialize(x, y, default = 0, &block) ⇒ Grid
constructor
A new instance of Grid.
- #initialize_copy(orig) ⇒ Object
-
#map_a(&block) ⇒ Object
Maps each cell of the grid by the given block.
- #minmax(null = nil) ⇒ Object
-
#nsew_index(x, y = nil) ⇒ Object
Returns the Point2Ds in the directions NSEW as an array (no diagonals, see adja_index for this).
- #printBoard(overlay = nil) ⇒ Object
-
#select_valid(a) ⇒ Object
Will remove all invalid indices from the array.
- #sum(&block) ⇒ Object
-
#to_a ⇒ Object
returns a copy of the underlying 2D array, with linking to the content (rather than dup the content also).
- #to_s ⇒ Object
Constructor Details
#initialize(x, y, default = 0, &block) ⇒ Grid
Returns a new instance of Grid.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/cem/cruzzles.rb', line 187 def initialize(x, y, default=0, &block) @data = [] if block case block.arity when 0 y.times { row = [] x.times { row << block.call() } @data << row } when 2 y.times { |iy| row = [] x.times { |ix| row << block.call(iy, ix) } @data << row } else raise end else # if no block given y.times { row = [] x.times { row << default } @data << row } # @data = [[default] * x] * y end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
185 186 187 |
# File 'lib/cem/cruzzles.rb', line 185 def data @data end |
Instance Method Details
#[](y, x = nil) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/cem/cruzzles.rb', line 254 def [](y, x=nil) if x == nil # puts y.inspect if y.respond_to?(:x) && y.respond_to?(:y) return @data[y.y][y.x] end return @data[y] end if y.is_a? Range return @data[y].map { |row| row[x] } end return @data[y][x] end |
#[]=(y, x, assign = nil) ⇒ Object
Array assignment to grid.
grid[y,x] = a # Store a in row y and column x
grid[point2d] = a # Use point2d.y and point2d.x to store a
275 276 277 278 279 280 281 282 283 |
# File 'lib/cem/cruzzles.rb', line 275 def []=(y, x, assign=nil) if assign == nil if y.respond_to?(:x) && y.respond_to?(:y) return @data[y.y][y.x] = x end return @data[y] = x end return @data[y][x] = assign end |
#adja_index(x, y = nil) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/cem/cruzzles.rb', line 332 def adja_index(x,y=nil) if y == nil if x.respond_to?(:x) && x.respond_to?(:y) y = x.y x = x.x else raise "Need Point2D with :x and :y or two parameters" end end result = [] if x > 0 if y > 0 result << Point2D.new(x-1,y-1) end result << Point2D.new(x-1,y) if y + 1 < @data.size - 1 result << Point2D.new(x-1, y+1) end end if y > 0 result << Point2D.new(x,y-1) end # result << Point2D.new(x,y) if y + 1 < @data.size - 1 result << Point2D.new(x, y+1) end if x + 1 < @data[0].size - 1 if y > 0 result << Point2D.new(x+1,y-1) end result << Point2D.new(x+1,y) if y + 1 < @data.size - 1 result << Point2D.new(x+1, y+1) end end return result end |
#each(&block) ⇒ Object
412 413 414 415 416 417 418 |
# File 'lib/cem/cruzzles.rb', line 412 def each(&block) @data.each { |row| row.each { |cell| block.call(cell) } } end |
#each_with_index(&block) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/cem/cruzzles.rb', line 420 def each_with_index(&block) case block.arity when 2 @data.each_with_index { |l, y| l.each_with_index { |c, x| block.call(c, Point2D.new(x,y)) } } when 3 @data.each_with_index { |l, y| l.each_with_index { |c, x| block.call(c, y, x) } } else raise end end |
#initialize_copy(orig) ⇒ Object
227 228 229 230 231 232 |
# File 'lib/cem/cruzzles.rb', line 227 def initialize_copy(orig) super @data = Marshal.load(Marshal.dump(orig.data)) # Do custom initialization for self end |
#map_a(&block) ⇒ Object
Maps each cell of the grid by the given block.
If block has…
- one parameter, passes the cell value
- two parameters, passes y and x
- three parameteres, passes y, x and the cell value
397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/cem/cruzzles.rb', line 397 def map_a(&block) case block.arity when 1 @data.map { |row| row.map { |value| block.call(value) } } when 2 @data.each_with_index.map { |row, y| row.each_index.map { |x| block.call(y,x) } } when 3 @data.each_with_index.map { |row, y| row.each_with_index.map { |cell, x| block.call(y,x,cell) } } else raise end end |
#minmax(null = nil) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/cem/cruzzles.rb', line 234 def minmax(null=nil) min = nil max = nil @data.each_with_index { |l, y| l.each_with_index { |c, x| if c != null if min == nil || max == nil min = max = Point2D.new(x,y) end min = Point2D.new([min.x, x].min, [min.y, y].min) max = Point2D.new([max.x, x].max, [min.y, y].max) end } } return min, max end |
#nsew_index(x, y = nil) ⇒ Object
Returns the Point2Ds in the directions NSEW as an array (no diagonals, see adja_index for this)
Will not return Point2Ds in case we are at the boundary of the Grid
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/cem/cruzzles.rb', line 294 def nsew_index(x,y=nil) # puts "#{@data.size} x #{@data[0].size}" if y == nil if x.respond_to?(:x) && x.respond_to?(:y) y = x.y x = x.x else raise "Need Point2D with :x and :y or two parameters" end end result = [] if x > 0 result << Point2D.new(x-1,y) end if y > 0 result << Point2D.new(x,y-1) end if y + 1 < @data.size - 1 result << Point2D.new(x, y+1) end if x + 1 < @data[0].size - 1 result << Point2D.new(x+1,y) end return result end |
#printBoard(overlay = nil) ⇒ Object
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/cem/cruzzles.rb', line 448 def printBoard( = nil) result = "" @data.each_with_index { |l, y| l.each_with_index { |c, x| pos = Point2D.new(x,y) if && .has_key?(pos) result += [pos] else result += c.to_s end } result += "\r\n" } return result end |
#select_valid(a) ⇒ Object
Will remove all invalid indices from the array
326 327 328 329 330 |
# File 'lib/cem/cruzzles.rb', line 326 def select_valid(a) a.select { |p| (p.x >= 0) && (p.y >= 0) && (p.y <= @data.size-1) && (p.x <= @data[0].size-1) } end |
#sum(&block) ⇒ Object
440 441 442 443 444 445 446 |
# File 'lib/cem/cruzzles.rb', line 440 def sum(&block) sum = 0 each { |cell| sum += block.call(cell) } return sum end |
#to_a ⇒ Object
returns a copy of the underlying 2D array, with linking to the content (rather than dup the content also)
381 382 383 |
# File 'lib/cem/cruzzles.rb', line 381 def to_a return @data.flatten end |
#to_s ⇒ Object
373 374 375 |
# File 'lib/cem/cruzzles.rb', line 373 def to_s printBoard() end |