-
Notifications
You must be signed in to change notification settings - Fork 0
/
human_player.rb
62 lines (53 loc) · 1.3 KB
/
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
54
55
56
57
58
59
60
61
62
class HumanPlayer
COL_LETTER_TO_NUM = {
"a" => 0,
"b" => 1,
"c" => 2,
"d" => 3,
"e" => 4,
"f" => 5,
"g" => 6,
"h" => 7
}
attr_reader :color
def initialize(color)
@color = color
end
def get_move
print "Move from: "
first_input = gets.chomp
abort if first_input.downcase == "exit"
return :save if first_input.downcase == "save"
start_pos = convert(first_input.split(""))
print "Move to: "
end_pos = convert(gets.chomp.split(""))
unless start_pos.length == 2 && end_pos.length == 2
raise InvalidMoveError.new "Use a letter and a number when making a move!"
end
[start_pos, end_pos]
end
def convert(array)
col = COL_LETTER_TO_NUM[array[0].downcase]
row = array[1].to_i - 1
unless row.is_a?(Fixnum) && col.is_a?(Fixnum)
raise InvalidMoveError.new "Use letter-number coordinates! I.e. a5"
end
[row, col]
end
def get_promotion
begin
puts "What would you like your pawn to become?"
piece_class = Object.const_get(gets.chomp.capitalize)
unless Board::PROMOTION_PIECE_TYPES.include?(piece_class)
raise NameError
end
rescue NameError => e
puts "Not a possible promotion."
retry
end
piece_class
end
def to_s
@color.to_s.capitalize
end
end