Sha256: 3232e1493e9f5c4f159b74e7362ab05e7959ebc0d5389bbc13053d55a568138c
Contents?: true
Size: 1.06 KB
Versions: 6
Compression:
Stored size: 1.06 KB
Contents
module Diagrams class Grid attr_accessor :width, :height def initialize(width, height, init) @width, @height = width, height @data = Array.new height.times do @data.push [init]*@width end @init = init end def get(x, y) @data[y][x] end def set(x, y, v) @data[y][x] = v || @init end def inspect @data.map{|x| x.map{|y| case y when true; '#' when false, nil; ' ' when Fixnum; y.chr end }.join }.join("\n") end def inspect2(valid) s = "" s += "+" + "-"*@width + "+\n" for y in 0..(@height-1) s += "|" for x in 0..(@width-1) s += valid.get(x,y) ? get(x,y).chr : " " end s += "|" s += "\n" end s += "+" + "-"*@width + "+\n" s end def each for y in 0..(height-1) for x in 0..(width-1) e = get(x,y) yield x,y,e end end end def set_area(x,y,w,h,e) for i in (x..x+w-1) for j in (y..y+h-1) set(i,j,e) end end end def read_area(x,y,w,h) s = "" for j in (y..y+h-1) for i in (x..x+w-1) s << get(i,j) end s << "\n" end s end end end
Version data entries
6 entries across 6 versions & 1 rubygems