-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboardSolver.rb
168 lines (130 loc) · 4.03 KB
/
boardSolver.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
require_relative 'board'
class BoardSolver
DIRECTIONS = [:up, :left, :down, :right]
TURN = [:player, :computer]
def initialize(board, turn=:player)
@board = board
@turn = turn
end
def player_turn?
@turn == :player
end
def maximise_heuristic(heuristic, board, directions=[nil, :up, :left, :down, :right])
return directions if directions.size == 1
result = []
best_value = -Float::INFINITY
directions.compact.each do |direction|
new_situation = board.move(direction)
next if board == new_situation
new_value = new_situation.method(heuristic).call
if new_value > best_value
result = [direction]
best_value = new_value
elsif new_value == best_value
result = result << direction
end
end
if directions.include? nil
current_value = board.method(heuristic).call
if current_value > best_value
directions
elsif current_value == best_value
result << nil
else
result
end
else
result
end
end
def dfs(max_depth, &heuristic)
return [nil, yield(@board)] if max_depth == 0
results = {}
DIRECTIONS.each do |direction|
new_board = @board.move(direction)
next if new_board == @board
results[direction] = BoardSolver.new(new_board).dfs(max_depth-1, &heuristic)[1]
end
results.max_by{ |k, v| v }
end
def minmax(max_depth, alpha, beta, &player_heuristic)
if self.player_turn?
return [nil, yield(@board)] if max_depth == 0
return [nil, -Float::INFINITY] if @board.empty_count == 0 && @board.smoothness == 0
best_result = [nil, alpha]
DIRECTIONS.each do |direction|
new_board = @board.move(direction)
next if new_board == @board
result = [direction,
BoardSolver.new(new_board, :computer)
.minmax(max_depth-1, best_result[1], beta, &player_heuristic)[1]]
if result[1] > best_result[1]
if result[1] > beta
return [direction, beta]
end
best_result = result
end
end
return best_result
else
moves = {}
[2, 4].each do |value|
@board.size.times do |row|
@board.size.times do |col|
next unless @board[row, col].nil?
@board[row, col] = value
(moves[computer_heuristic] ||= []) << [row, col, value]
@board[row, col] = nil
# (moves[0] ||= []) << [row, col, value]
end
end
end
candidates = moves[moves.keys.max]
best_result = beta
candidates.each do |(row, col, value)|
(new_board = @board.deep_copy)[row, col] = value
result = BoardSolver.new(new_board, :player)
.minmax(max_depth, alpha, best_result, &player_heuristic)[1]
if result < best_result
if result < alpha
return [nil, alpha]
end
best_result = result
end
end
return [nil, best_result]
end
end
def nearest_improvement(max_depth, &heuristic)
threshold = yield(@board)
best_non_improvement = [nil, -Float::INFINITY]
(1..max_depth).each do |depth|
result = dfs(depth, &heuristic)
if result[1] > threshold
return result[0]
elsif result[1] > best_non_improvement[1]
best_non_improvement = result
end
end
puts "No improvement found!"
return best_non_improvement[0]
end
def timed_improvement(min_time_s, &heuristic)
best_improvement = [nil, -Float::INFINITY]
time_start = Time.now.to_f
1.upto(Float::INFINITY) do |depth|
break unless Time.now.to_f - time_start < min_time_s
puts "Depth: #{depth}."
result = minmax(depth, -Float::INFINITY, Float::INFINITY, &heuristic)
if result[1] == -Float::INFINITY
return nil
end
best_improvement = result
end
puts "Took #{Time.now.to_f - time_start - min_time_s} extra seconds..!"
return best_improvement[0]
end
def computer_heuristic
end
end