-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchars_array.rb
More file actions
executable file
·69 lines (55 loc) · 1.49 KB
/
chars_array.rb
File metadata and controls
executable file
·69 lines (55 loc) · 1.49 KB
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
require_relative 'board'
require_relative 'piece'
require_relative 'plane_like'
class CharsArray
include PlaneLike
BG_COLORS = [:light_red, :light_black]
BG_SWAP = { BG_COLORS[0] => BG_COLORS[1],
BG_COLORS[1] => BG_COLORS[0] }
attr_accessor :board, :turn
def initialize(board, turn)
@rows = Array.new(8) { Array.new(8) }
@board = board
@turn = turn
@bg_color = :light_red
convert_without_highlight
highlight_squares
end
def highlight_squares
unless board.selected_piece.nil?
hold_highlight_on_selected_piece
highlight_available_moves(board.selected_piece)
end
highlight_cursor
end
def hold_highlight_on_selected_piece
pos = board.prev_pos
self[pos] = self[pos].colorize(:background => :light_white)
end
def highlight_available_moves(selected_piece)
selected_piece.valid_moves.each do |move|
self[move] = self[move].colorize(:background => :white)
end
end
def highlight_cursor
pos = board.cursor.pos
self[pos] = self[pos].colorize(:background => :light_white)
end
def convert_without_highlight
self.rows.count.times do |y|
self.rows[y] = board.rows[y].render
self.rows[y].each_with_index do |char, x|
self[[y, x]] = char.colorize( :background => background_color_swap )
end
background_color_swap
end
end
def background_color_swap
@bg_color = BG_SWAP[@bg_color]
end
end
class Array
def render
self.map { |piece| piece.render }
end
end