-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputer_player.rb
More file actions
65 lines (55 loc) · 1.29 KB
/
computer_player.rb
File metadata and controls
65 lines (55 loc) · 1.29 KB
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
class ComputerPlayer
VALS = {
King => 1000000,
Queen => 925,
Rook => 500,
Bishop => 325,
Knight => 300,
Pawn => 100
}
def initialize(color,board)
@color = color
@board = board
end
def get_source(display)
move_hash = {}
@board.get_pieces(@color).each do |piece|
move_hash[piece] = piece.moves.select { |move| @board.is_valid_move?(piece, move) }
end
capture_hash = Hash.new { |h,v| h[v] = []}
move_hash.each do |piece, moves|
moves.each do |move|
capture_hash[piece] << move if @board.color_at(move) == Board.opposite_color(@color)
end
end
if capture_hash.size == 0
random_move
@move = @selected_piece.moves.sample
else
max_val = 0
capture_hash.each do |piece, moves|
moves.each do |move|
if VALS[@board.piece_at(move).class] > max_val
max_val = VALS[@board.piece_at(move).class]
@move = move
@selected_piece = piece
end
end
end
end
@selected_piece.get_position
end
def get_promotion
Queen
end
def random_move
@selected_piece = @board.get_pieces(@color).sample
@selected_piece.get_position
end
def get_destination(display)
@move
end
def get_color
@color
end
end