-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard_node.rb
More file actions
106 lines (82 loc) · 2.13 KB
/
Copy pathboard_node.rb
File metadata and controls
106 lines (82 loc) · 2.13 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
class BoardNode
PIECE_VALUES ={
Pawn => 1,
Bishop => 3,
Knight => 3,
Rook => 5,
Queen => 9,
King => 20
}
MAX_DEPTH = 2
attr_reader :prev_move, :board
def initialize(board, color, depth = 0, prev_move = nil)
@board = board
@color = color
@depth = depth
@prev_move = prev_move
end
def children
children = []
movable_pieces = @board.pieces(@color).select do |piece|
piece.valid_moves.count > 0
end
movable_pieces.each do |piece|
piece.valid_moves.each do |move|
duped_board = @board.dup
duped_board.move(piece.pos, move)
other_color = Piece.other_color(@color)
children << BoardNode.new(
duped_board,
other_color,
@depth + 1,
[piece.pos, move]
)
end
end
children
end
def value(color)
other_color = Piece.other_color(color)
value = @board.pieces(color).inject(0) do |sum, piece|
sum + PIECE_VALUES[piece.class]
end
value -= @board.pieces(other_color).inject(0) do |sum, piece|
sum + PIECE_VALUES[piece.class]
end
value
end
def deep_search(color)
return [self, self.value(color)] if @depth == MAX_DEPTH
child_values = Hash.new
self.children.each do |child|
child_values[child] = child.deep_search(color).last
end
if @depth == 0
child_values.max_by { |child, val| val }
else
sum = child_values.values.inject(0,&:+)
[self, sum / (child_values.count + 1)]
end
end
def best_children(color)
best_children = []
self.children.each do |child|
if best_children.empty?
best_children << child
elsif best_children.last.value(color) == child.value(color)
best_children << child
elsif best_children.last.value(color) < child.value(color)
best_children = [child]
end
end
best_children
end
def checkmate_children
checkmate_children = []
other_color = Piece.other_color(@color)
self.children.each do |child|
checkmate_children << child if child.board.checkmate?(other_color)
end
checkmate_children
end
end