Module: ConwayDeathmatch::Shapes
- Defined in:
- lib/conway_deathmatch/shapes.rb
Class Method Summary collapse
-
.add(board, str, val = BoardState::ALIVE) ⇒ Object
parse a string like “acorn 12 22 block 5 0 p 1 2 p 3 4 p 56 78” add known shapes.
-
.classic ⇒ Object
memoize shapes/classic.yaml.
-
.discovered ⇒ Object
memoize shapes/discovered.yaml.
- .load_yaml(filename) ⇒ Object
Class Method Details
.add(board, str, val = BoardState::ALIVE) ⇒ Object
parse a string like “acorn 12 22 block 5 0 p 1 2 p 3 4 p 56 78” add known shapes
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/conway_deathmatch/shapes.rb', line 21 def self.add(board, str, val = BoardState::ALIVE) tokens = str.split points = [] classic = self.classic while !tokens.empty? shape = tokens.shift.downcase raise "no coordinates for #{shape}" if tokens.length < 2 x = tokens.shift.to_i y = tokens.shift.to_i case shape.downcase when 'p' points << [x, y] else found = classic[shape] || self.discovered.fetch(shape) board.add_points(found, x, y, val) end end board.add_points(points, 0, 0, val) end |
.classic ⇒ Object
memoize shapes/classic.yaml
10 11 12 |
# File 'lib/conway_deathmatch/shapes.rb', line 10 def self.classic @@classic ||= self.load_yaml('classic.yaml') end |
.discovered ⇒ Object
memoize shapes/discovered.yaml
15 16 17 |
# File 'lib/conway_deathmatch/shapes.rb', line 15 def self.discovered @@disovered ||= self.load_yaml('discovered.yaml') end |
.load_yaml(filename) ⇒ Object
5 6 7 |
# File 'lib/conway_deathmatch/shapes.rb', line 5 def self.load_yaml(filename) YAML.load_file(File.join(__dir__, 'shapes', filename)) end |