-
Notifications
You must be signed in to change notification settings - Fork 0
/
Display.rb
78 lines (71 loc) · 2.14 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
74
75
76
77
78
require 'colorize'
require_relative 'cursor.rb'
require_relative 'board.rb'
class Display
attr_reader :cursor, :board
def initialize(board)
@board = board
@cursor = Cursor.new([0,0], board)
end
def render
(0..7).each do |row|
(0..7).each do |col|
pos = [row,col]
moves = @board[@cursor.cursor_pos].moves
if pos == @cursor.cursor_pos
print @board[pos].symbol.colorize(:background => :green) + " "
elsif moves.include?(pos)
print @board[pos].symbol.colorize(:cyan) + " "
else
print @board[pos].symbol + " "
end
end
puts
end
end
def render_select(frozen)
(0..7).each do |row|
(0..7).each do |col|
pos = [row, col]
moves = @board[frozen].moves
if pos == @cursor.cursor_pos
print @board[pos].symbol.colorize(:color => :magenta, :background => :green) + " "
elsif moves.include?(pos)
print @board[pos].symbol.colorize(:blue) + " "
else
print @board[pos].symbol + " "
end
end
puts
end
end
def render_turn(color)
begin
loop do
a = @cursor.get_input
if @cursor.selected
system("clear")
puts "it is #{color}'s turn"
self.render_select(a)
until !@cursor.selected
b = @cursor.get_input
system("clear")
puts "it is #{color}'s turn"
self.render_select(a)
end
@board.move_piece(color, a, b)
system("clear")
puts "it is #{color}'s turn"
self.render
return
end
system("clear")
puts "it is #{color}'s turn"
self.render
rescue
puts "invalid"
retry
end
end
end
end