-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplay.rb
executable file
·66 lines (54 loc) · 1.34 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
require_relative 'cursorable'
require_relative 'player'
class Display
include Cursorable
attr_accessor :selected
def initialize(board)
@board = board
@cursor = [0, 0]
@selected = false
end
def render
system("clear")
puts "Use WASD to move, enter to confirm."
grid = build_grid
load_path_space(grid)
grid.each_with_index { |row, i| puts "#{i+1} #{row.join}" }
puts " #{('a'..'h').to_a.join(' ')}"
end
private
attr_reader :board
def build_grid
@board.grid.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 possible_moves
board[@cursor].valid_moves
end
def colors_for(i, j)
if [i, j] == @cursor
bg = :light_cyan
elsif (i + j).odd?
bg = :light_red
else
bg = :light_brown
end
{ background: bg, color: :black, mode: :bold }
end
def load_path_space(grid)
possible_moves.each do |move|
piece = grid[move[0]][move[1]]
if @board.current_player_color == @board[@cursor].color
grid[move[0]][move[1]] = piece.to_s.colorize({ background: :light_magenta,
color: :light_white})
end
end
end
end