-
Notifications
You must be signed in to change notification settings - Fork 17
/
apple.rb
33 lines (29 loc) · 928 Bytes
/
apple.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
class Snake
module Model
class Apple
attr_reader :game
attr_accessor :row, :column
def initialize(game)
@game = game
end
# generates a new location from scratch or via dependency injection of what cell is (for testing purposes)
def generate(initial_row: nil, initial_column: nil)
if initial_row && initial_column
self.row, self.column = initial_row, initial_column
else
self.row, self.column = @game.height.times.zip(@game.width.times).reject do |row, column|
@game.snake.vertebrae.map {|v| [v.row, v.column]}.include?([row, column])
end.sample
end
end
def remove
self.row = nil
self.column = nil
end
# inspect is overridden to prevent printing very long stack traces
def inspect
"#{super[0, 120]}... >"
end
end
end
end