-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.rb
62 lines (50 loc) · 1.12 KB
/
display.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
require "colorize"
require_relative "cursor"
class Display
attr_reader :board, :notifications, :cursor
def initialize(board)
@board = board
@cursor = Cursor.new([0, 0], board)
@notifications = {}
end
def build_grid
@board.rows.map.with_index do |row, i|
build_row(row, i)
end
end
def build_row(row, i)
row.map.with_index do |piece, j|
color_options = colors_for(i, j)
piece.to_s.colorize(color_options)
end
end
def colors_for(i, j)
if cursor.cursor_pos == [i, j] && cursor.selected
bg = :light_green
elsif cursor.cursor_pos == [i, j]
bg = :light_red
elsif (i + j).odd?
bg = :light_blue
else
bg = :light_yellow
end
{ background: bg }
end
def reset!
@notifications.delete(:error)
end
def uncheck!
@notifications.delete(:check)
end
def set_check!
@notifications[:check] = "Check!"
end
def render
system("clear")
puts "Arrow keys, WASD, or vim to move, space or enter to confirm."
build_grid.each { |row| puts row.join }
@notifications.each do |_key, val|
puts val
end
end
end