-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.rb
71 lines (62 loc) · 1.82 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
class Display
attr_reader :debug, :board, :cursor
COLUMNS = ("a".."h").to_a.freeze
ROWS = (1..8).to_a.reverse!.freeze
def initialize(board = Board.new, debug = false)
@cursor = Cursor.new([0,0], board)
@board = board
@debug = debug
end
def render
system("clear")
render_alphabet_labels
puts
build_grid.each do |row|
puts row
puts
end
render_alphabet_labels
if debug
puts
curr_piece = board[cursor.cursor_pos]
opponent_color = curr_piece.color == :white ? :black : :white
puts "Piece: #{curr_piece.color == :white ? "White" : "Black"} #{curr_piece.class}"
puts "All moves #{curr_piece.moves.inspect}"
puts "Valid moves #{curr_piece.valid_moves.inspect}"
puts "Oponent in check: #{board.in_check?(opponent_color)}"
end
end
private
def render_alphabet_labels
alphabet_labels = " "
COLUMNS.each { |letter| alphabet_labels << " " + letter + " " }
puts alphabet_labels
end
def build_grid
@board.rows.map.with_index do |row, i|
build_row(row, i)
end
end
def build_row(row_array, row_num)
row_string = ROWS[row_num].to_s + " "
processed_row = row_array.map.with_index do |piece, col_num|
pos = [row_num, col_num]
piece_char = " " + piece.symbol + " "
piece_char.colorize(color_values(pos)) + " "
end
row_string << processed_row.join(" ") + " " + ROWS[row_num].to_s
row_string
end
def color_values(pos)
if board[pos].selected
bg = :red
elsif pos == cursor.cursor_pos
bg = :light_green
elsif (pos.first + pos.last).even?
bg = :light_blue
else
bg = :light_yellow
end
{ :background => bg }
end
end