-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.rb
42 lines (35 loc) · 876 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
require_relative 'board'
require_relative 'cursor'
require 'colorize'
require 'byebug'
class Display
attr_reader :board, :cursor
def initialize(board)
@cursor = Cursor.new([0,0], board)
@board = board
end
def render
system("clear")
formatted_board.each_with_index do |row, idx|
puts "#{8 - idx} #{row.join('')}"
end
puts " #{('a'..'h').to_a.join(' ')}"
end
def cursor_pos
@cursor.cursor_pos
end
def formatted_board
display = Array.new(8) { Array.new(8) { [] } }
(0..7).each do |row_i|
(0..7).each do |col_i|
pos = [row_i, col_i]
color = (row_i + col_i).odd? ? :white : :grey
if pos == cursor_pos
color = cursor.selected? ? :yellow : :blue
end
display[row_i][col_i] << board[pos].to_s.colorize(background: color)
end
end
display
end
end