-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.rb
73 lines (64 loc) · 1.73 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_relative 'board'
require 'colorize'
class Display
BG_COLORS = [
:magenta,
:blue
]
def initialize(board)
@board = board
@cursor = [6,4] # king pawn
end
def update_cursor(direction)
row, col = @cursor[0] + direction[0], @cursor[1] + direction[1]
@cursor = [row, col].map { |coord| clamp(coord) }
end
def get_cursor_position
@cursor
end
def clamp(coord)
[0, [coord, 7].min].max
end
def print_board_status(color)
if @board.checkmate?(color)
print "Checkmate!"
elsif @board.stalemate?(color)
print "Stalemate!"
elsif @board.in_check?(color)
print "Check!"
end
end
def render(color)
system("clear")
piece_at_cursor = @board.piece_at(@cursor)
valid_moves = piece_at_cursor.moves.select{ |move| @board.is_valid_move?(piece_at_cursor,move)}
display_grid = @board.get_icons
puts " #{("A".."H").to_a.join(" ")} "
display_grid.each_with_index do |row, i|
print " #{8 - i} "
row.each_with_index do |icon, j|
curr_pos = [i, j]
icon_string = " #{icon} "
if curr_pos == @cursor
print icon_string.on_green
elsif valid_moves.include?(curr_pos)
print icon_string.on_yellow
else
print icon_string.colorize(:background => BG_COLORS[(i + j) % 2])
end
end
print " #{8 - i} "
case i
when 0
print " " + @board.get_captured_pieces(:white).map(&:get_icon).join(" ")
when 4
print_board_status(color)
when 7
print " " + @board.get_captured_pieces(:black).map(&:get_icon).join(" ")
end
puts
end
puts " #{("A".."H").to_a.join(" ")} "
puts "#{color.to_s.capitalize}'s turn!"
end
end