-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.rb
74 lines (58 loc) · 1.45 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
require_relative 'display'
require_relative 'errors'
class Player
attr_reader :name, :color
def initialize(name, color)
@name = name
@color = color
end
end
class HumanPlayer < Player
def play_turn(display)
[get_start_input(display), get_end_input(display)]
end
def get_start_input(display)
start_input = nil
until start_input
display.render
puts "#{name}'s turn! #{color.capitalize} pieces only!"
start_input = display.cursor.get_input
end
unless display.board.matches_color?(start_input, color)
raise InvalidMoveError, "Not your piece!"
end
start_input
end
def get_end_input(display)
end_input = nil
until end_input
display.render
end_input = display.cursor.get_input
end
end_input
end
end
class ComputerPlayer < Player
def play_turn(display)
piece = choose_piece(display.board)
start_pos = piece.pos
end_pos = choose_move(piece)
raise InvalidMoveError if end_pos.nil? || start_pos.nil?
[start_pos, end_pos]
end
def choose_piece(board)
pieces = board.player_pieces_with_moves(color)
choose_attacking_piece(pieces) || choose_random_piece(pieces)
end
def choose_attacking_piece(pieces)
pieces.select do |piece|
!piece.attacking_moves.empty?
end.sample
end
def choose_random_piece(pieces)
pieces.sample
end
def choose_move(piece)
piece.attacking_moves.sample || piece.valid_moves.sample
end
end