-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.rb
46 lines (40 loc) · 1.11 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
require 'colorize'
require 'byebug'
require_relative 'board'
require_relative 'cursor'
class Display
attr_accessor :selected_pos, :current_color
attr_reader :cursor
def initialize(board)
@board = board
@cursor = Cursor.new([0,0], board)
@selected_pos = nil
@current_color = nil
end
def render
system('clear')
selected_piece = @selected_pos ? @board[@selected_pos]
: @board[@cursor.cursor_pos]
moves = selected_piece.valid_moves
(0..7).each do |row|
(0..7).each do |column|
pos = [row, column]
piece = @board[pos]
colors(pos, moves, selected_piece, piece)
end
puts ""
end
end
private
def colors(pos, moves, selected_piece, piece)
if pos == @selected_piece || pos == @cursor.cursor_pos
print " #{piece.to_s} ".colorize(background: :red)
elsif moves.include?(pos) && selected_piece.color == @current_color
print " #{piece.to_s} ".colorize(background: :blue ).blink
elsif (pos[0] + pos[1]).odd?
print " #{piece.to_s} ".colorize(background: :white)
else
print " #{piece.to_s} "
end
end
end