-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.rb
73 lines (63 loc) · 1.53 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
63
64
65
66
67
68
69
70
71
72
73
require "colorize"
require_relative "cursorable"
class Display
include Cursorable
def initialize(board)
@board = board
@cursor_pos = [0, 0]
@highlights = []
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 [i, j] == @cursor_pos
bg = :light_red
elsif @highlights.include?([i,j])
bg = :yellow
elsif (i + j).odd?
bg = :light_blue
else
bg = :green
end
{ background: bg, color: @board.piece_at_position([i,j]).color }
end
def show_options(pos)
piece = @board.piece_at_position(pos)
moves = piece.moves
moves = moves.reject {|move| piece.in_check?(move)}
@highlights = moves
render
end
def reset
@highlights = []
end
def render
system("clear")
puts "Arrow keys to move, space or enter to confirm."
puts " " + ("A".."H").to_a.join(" ") + " " + @board.captured_white.join("")
build_grid.each_with_index { |row,row_idx| puts "#{row_idx} " + row.join + " #{row_idx}" }
puts " " + ("A".."H").to_a.join(" ") + " " + @board.captured_black.join(" ")
if @board.check?(:white)
puts "White is in check!"
elsif @board.check?(:black)
puts "Black is in check!"
end
end
def move
result = nil
until result
render
result = get_input
end
result
end
end