forked from Metalab/elle-and-the-spooky-arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.rb
85 lines (71 loc) · 1.77 KB
/
screen.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
83
84
85
class Screen
NINE_BITS = /........./
EIGHT_BITS = /......../
def initialize(panels = 4, width = 8, height = 9)
@width = width
@height = height
@panels = panels
@screen = [ Array.new(@panels * @width, false),
Array.new(@panels * @width, false),
Array.new(@panels * @width, false),
Array.new(@panels * @width, false),
Array.new(@panels * @width, false),
Array.new(@panels * @width, false),
Array.new(@panels * @width, false),
Array.new(@panels * @width, false),
Array.new(@panels * @width, false)]
@blank_screen = Marshal.dump(@screen)
@blink_bit = "1"
end
def height
@height
end
def width
@panels * @width
end
def col(col, value)
@screen.each { |row| row[col]= value}
end
def row(row, value)
@screen[row].map! { |p| p = value}
end
def shift_cols
@screen.each { |row| row << row.shift }
end
def shift_rows
@screen << @screen.shift
end
def []=(row, col, value)
@screen[col][row] = value
end
# convert to bit stream
def ascii_bit_stream
s = ""
0.upto((@panels * @width)-1) do |col|
@screen.each do |row|
s << (row[col] ? "1" : "0")
end
end
s
end
def ascii_bit_stream2
s = ""
0.upto((@panels * @width)-1) do |col|
s << @blink_bit
@screen.each do |row|
s << (row[col] ? "1" : "0")
end
end
s
end
# add blink bit (first bit)
def ascii_bit_stream_with_blink
ascii_bit_stream.scan(NINE_BITS).map { |b| (@blink_bit + b) }.join("")
end
def bit_stream
ascii_bit_stream_with_blink.scan(EIGHT_BITS).map { |b| b.to_i(2) }
end
def reset
@screen = Marshal.load(@blank_screen)
end
end