-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuman_player.rb
53 lines (48 loc) · 930 Bytes
/
human_player.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
class HumanPlayer
DIRECTIONS = {
"w" => [-1,0],
"a" => [0,-1],
"s" => [1,0],
"d" => [0,1]
}
PROMOTIONS = {
"q" => Queen,
"k" => Knight,
"b" => Bishop,
"r" => Rook
}
def initialize(color)
@color = color
end
def get_color
@color
end
def get_source(display)
user_input(display)
end
def get_destination(display)
user_input(display)
end
def get_promotion
puts "Type {Q, R, B, K} to select the promotion."
while true
c = $stdin.getch
if PROMOTIONS.has_key?(c)
return PROMOTIONS[c]
end
exit if c == "\u0003"
end
end
def user_input(display)
while true
c = $stdin.getch
if DIRECTIONS.has_key?(c)
display.update_cursor(DIRECTIONS[c])
display.render(get_color)
elsif c == "\r"
return display.get_cursor_position
end
exit if c == "\u0003"
end
end
end