-
Notifications
You must be signed in to change notification settings - Fork 16
/
grid.rb
49 lines (44 loc) · 1.32 KB
/
grid.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require 'glimmer'
require_relative '../model/game'
require_relative 'cell'
class Snake
module Presenter
class Grid
include Glimmer
attr_reader :game, :cells
def initialize(game = Model::Game.new)
@game = game
@cells = @game.height.times.map do |row|
@game.width.times.map do |column|
Cell.new(grid: self, row: row, column: column)
end
end
observe(@game.snake, :vertebrae) do |new_vertebrae|
occupied_snake_positions = @game.snake.vertebrae.map {|v| [v.row, v.column]}
@cells.each_with_index do |row_cells, row|
row_cells.each_with_index do |cell, column|
if [@game.apple.row, @game.apple.column] == [row, column]
cell.color = Cell::COLOR_APPLE
elsif occupied_snake_positions.include?([row, column])
cell.color = Cell::COLOR_SNAKE
else
cell.clear
end
end
end
end
end
def clear
@cells.each do |row_cells|
row_cells.each do |cell|
cell.clear
end
end
end
# inspect is overridden to prevent printing very long stack traces
def inspect
"#{super[0, 75]}... >"
end
end
end
end