-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.rb
253 lines (212 loc) · 7.13 KB
/
solver.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
require './2048.rb'
class Solver
attr_accessor :game
def initialize(strategy=RandomSolver)
@strategy = strategy.new
@game = Game.new
end
def solve!
while !@game.win_or_lose?
@game.play_single(@strategy.pick_move(@game))
end
end
def solve_to_file!(filename)
output = File.open( "./game_logs/#{filename}","w" )
game_log = ""
while !@game.win_or_lose?
game_log << @game.board.to_s
move = @strategy.pick_move(@game)
game_log << "#{move}\n\n"
@game.play_single(move)
end
output << game_log
output.close
end
def won?
@game.win?
end
def lost?
@game.lose?
end
def to_s
@strategy.class.to_s
end
end
class Strategy
def pick_move(game)
raise "Not Implemented!"
end
end
# Always chooses randomly
class RandomSolver < Strategy
def pick_move(game)
game.possible_commands.sample
end
end
# Chosses the move that maximizes a function of the board after the move.
# Evaludate function is board sum (and it sucks since it prefers not to merge.)
class D1 < Strategy
# sum of all tiles
def evaluate(game)
game.board.inject(0) { |sum, tile| sum + tile.val}
end
def pick_move(game)
moves_n_scores = game.possible_commands.collect do |move|
game_copy = Marshal::load(Marshal.dump(game))
game_copy.simulate_single(move)
[move, evaluate(game_copy)]
end
max = moves_n_scores.max_by{ |k,v| v }[1]
moves_n_scores.select{ |k,v| v == max }.map{|k,v| k }.sample
end
end
class D2 < D1
# sum of all tiles, scores higher for more empty tiles and thus choosing to merge
def evaluate(game)
sum_of_tiles = game.board.inject(0) { |sum, tile| sum + tile.val}
empty_tiles_factor = (game.board.select{|t| t.empty?}.count * 15)
sum_of_tiles + empty_tiles_factor
end
end
# Evaluate function is a sum of all tiles, empty tile factor and a highest tile factor (thus choosing to merge
# hisgher rather than a few lower merges)
class D3 < D1
# sum of all tiles, scores higher for more empty tiles and thus choosing to merge
def evaluate(game)
sum_of_tiles = game.board.inject(0) { |sum, tile| sum + tile.val}
empty_tiles_factor = (game.board.select{|t| t.empty?}.count * 15)
max_tile = game.board.max_by { |tile| tile.val}.val
highest_tile_factor = max_tile * max_tile
sum_of_tiles + empty_tiles_factor + highest_tile_factor
end
end
# Does not go UP unless no other choice.
class NeverGoesUp < D1
def pick_move(game)
moves = (game.possible_commands - [:up]).empty? ? game.possible_commands : game.possible_commands - [:up]
moves_n_scores = moves.collect do |move|
game_copy = Marshal::load(Marshal.dump(game))
game_copy.simulate_single(move)
[move, evaluate(game_copy)]
end
max = moves_n_scores.max_by{ |k,v| v }[1]
moves_n_scores.select{ |k,v| v == max }.map{|k,v| k }.sample
end
# sum of all tiles, scores higher for more empty tiles and thus choosing to merge
def evaluate(game)
sum_of_tiles = game.board.inject(0) { |sum, tile| sum + tile.val}
empty_tiles_factor = (game.board.select{|t| t.empty?}.count * 15)
max_tile = game.board.max_by { |tile| tile.val}.val
highest_tile_factor = max_tile * max_tile
sum_of_tiles + empty_tiles_factor + highest_tile_factor
end
end
# Maximizes board score two steps ahead
class OneMoveAhead < D1
def pick_move(game)
moves_n_scores = game.possible_commands.collect do |move|
game_copy = Marshal::load(Marshal.dump(game))
game_copy.simulate_single(move)
next_move_scores = []
game_copy.possible_commands.collect do |next_move|
second_copy = Marshal::load(Marshal.dump(game_copy))
second_copy.simulate_single(next_move)
next_move_scores << evaluate(second_copy)
end
[move, next_move_scores.max]
end
max = moves_n_scores.max_by{ |k,v| v }[1]
moves_n_scores.select{ |k,v| v == max }.map{|k,v| k }.sample
end
# sum of all tiles, scores higher for more empty tiles and thus choosing to merge
def evaluate(game)
sum_of_tiles = game.board.inject(0) { |sum, tile| sum + tile.val}
empty_tiles_factor = (game.board.select{|t| t.empty?}.count * 15)
max_tile = game.board.max_by { |tile| tile.val}.val
highest_tile_factor = max_tile * max_tile * max_tile * max_tile
sum_of_tiles + empty_tiles_factor + highest_tile_factor
end
end
class PenaltyForOrphanTiles < OneMoveAhead
def evaluate(game)
sum_of_tiles = game.board.inject(0) { |sum, tile| sum + tile.val}
empty_tiles_factor = (game.board.select{|t| t.empty?}.count * 15)
max_tile = game.board.max_by { |tile| tile.val}.val
# highest_tile_factor = max_tile * max_tile
orphan_tiles_penalty = game.board.select { |t| orphan_tile?(t, game) }.count * 20
sum_of_tiles + empty_tiles_factor - orphan_tiles_penalty
end
def orphan_tile?(tile, game)
deltas = [ [1, 0], [-1, 0], [0 , 1], [0, -1] ]
ne = []
deltas.each do |d|
if [tile.row + d[0], tile.column + d[1]].all? { |x| x >= 0 && x < game.board.size}
ne << game.board.tiles[tile.row + d[0]][tile.column + d[1]]
end
end
ne.all? { |n| n.val > tile.val }
end
end
class MixMaxAlphaBeta < D1
def alphabeta(game, depth, alpha, beta, is_max)
return evaluate(game) if depth == 0 or game.win_or_lose?
if is_max # max player
game.possible_commands.collect do |move|
game_copy = Marshal::load(Marshal.dump(game))
game_copy.simulate_single(move)
alpha = [alpha, alphabeta(game_copy, depth - 1, alpha, beta, false)].max
if beta <= alpha
# puts "cutoff`"
break
end
end
return alpha
else # min player
a = min_player_moves(game)
min_player_moves(game).collect do |move|
game_copy = Marshal::load(Marshal.dump(game))
game_copy.simulate_single_min(move)
beta = [beta, alphabeta(game_copy, depth - 1, alpha, beta, true)].min
if beta <= alpha
break
end
end
return beta
end
end
def min_player_moves(game)
empty_tiles = game.board.select{|t| t.empty? }
[2, 4].flat_map do |val|
empty_tiles.map{|t| [t.row, t.column, val]}
end
end
@@counter = 0
def pick_move(game)
@@counter += 1
moves_n_scores = game.possible_commands.collect do |move|
game_copy = Marshal::load(Marshal.dump(game))
game_copy.simulate_single(move)
[move, alphabeta(game_copy, 4, -Float::INFINITY, Float::INFINITY, false)]
end
max = moves_n_scores.max_by{ |k,v| v }[1]
puts "Round Number #{@@counter}\nMax Tile: #{game.board.max_by { |tile| tile.val}.val}\n\n"
moves_n_scores.select{ |k,v| v == max }.map{|k,v| k }.sample
end
def evaluate(game)
sum_of_tiles = game.board.inject(0) { |sum, tile| sum + tile.val}
empty_tiles_factor = (game.board.select{|t| t.empty?}.count * 15)
max_tile = game.board.max_by { |tile| tile.val}.val
highest_tile_factor = max_tile * max_tile * max_tile * max_tile
sum_of_tiles + empty_tiles_factor + highest_tile_factor
end
def orphan_tile?(tile, game)
deltas = [ [1, 0], [-1, 0], [0 , 1], [0, -1] ]
ne = []
deltas.each do |d|
if [tile.row + d[0], tile.column + d[1]].all? { |x| x >= 0 && x < game.board.size}
ne << game.board.tiles[tile.row + d[0]][tile.column + d[1]]
end
end
ne.all? { |n| n.val > tile.val }
end
end