-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.rb
82 lines (64 loc) · 1.45 KB
/
canvas.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class Canvas
attr_reader :grid, :cursor_x, :cursor_y
EMPTY_CHARACTER = ' '
WRITTEN_CHARACTER = '*'
def initialize(size: 5, cursor_x: 0, cursor_y: 0)
@grid = initialize_empty_grid(size)
@cursor_x = cursor_x
@cursor_y = cursor_y
end
def draw_right(distance)
write_at_current_location
(distance-1).times do
@cursor_y += 1
write_at_current_location
end
end
def draw_left(distance)
write_at_current_location
(distance-1).times do
@cursor_y -= 1
write_at_current_location
end
end
def draw_down(distance)
write_at_current_location
(distance-1).times do
@cursor_x += 1
write_at_current_location
end
end
def draw_up(distance)
write_at_current_location
(distance-1).times do
@cursor_x -= 1
write_at_current_location
end
end
def pretty
@grid.each do |row|
puts "#{row.join(' ')}"
end
end
def debug(operation)
p operation
p "X: #{@cursor_x} | Y: #{@cursor_y}"
self.pretty
end
private
def write_at_current_location
@grid[@cursor_x][@cursor_y] = WRITTEN_CHARACTER
# debug('Writing') # uncomment to debug for problems
end
def initialize_empty_grid(grid_dimension)
grid_map = []
grid_single_row = []
grid_dimension.times do
grid_single_row << EMPTY_CHARACTER
end
grid_dimension.times do
grid_map << grid_single_row.dup
end
grid_map
end
end