-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuman_player.rb
More file actions
47 lines (37 loc) · 990 Bytes
/
human_player.rb
File metadata and controls
47 lines (37 loc) · 990 Bytes
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
class HumanPlayer
attr_reader :color
def initialize(color)
@color = color
end
def play_turn
move = {}
move['start'] = get_pos('start')
move['end'] = get_pos('end')
move
end
def get_pos(turn)
puts "Enter your " + turn + " position: b2"
pos = gets.chomp.split("")
error_handling(pos)
pos[0] = letter_to_num(pos[0].downcase)
pos.map!(&:to_i)
end
private
def error_handling(pos)
format_error = ArgumentError.new "Please use this format: b2"
letter_error = ArgumentError.new "First character must be a letter!"
number_error = ArgumentError.new "Second character must be a number!"
raise format_error unless pos.length == 2
raise letter_error unless is_let(pos[0])
raise number_error unless is_int(pos[1])
end
def letter_to_num(let)
let.ord - 97
end
def is_int(str)
str == str.to_i.to_s
end
def is_let(let)
!(let =~ /[[:alpha:]]/).nil?
end
end