-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.rb
54 lines (47 loc) · 978 Bytes
/
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
require 'colorize'
require_relative "cursorable"
class Display
include Cursorable
def initialize(board)
@board = board
@cursor_pos = [board.grid.length - 1, 0]
@selected = nil
@second_selection = nil
end
def build_grid
@board.grid.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 ||= " "
piece.to_s.colorize(color_options)
end
end
def colors_for(i, j)
if [i, j] == @cursor_pos
bg = :light_red
elsif [i, j] == @selected
bg = :blue
elsif (i + j).odd?
bg = :light_black
else
bg = :light_white
end
{ background: bg }
end
def render
system("clear")
puts "Arrow keys to move, space or enter to confirm, s to save."
build_grid.each { |row| puts row.join }
nil
end
def run
while true
render
get_input
end
end
end