-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin_max.py
More file actions
92 lines (79 loc) · 2.26 KB
/
min_max.py
File metadata and controls
92 lines (79 loc) · 2.26 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
from __future__ import absolute_import
from __future__ import division
from test import *
def count_non_empty(board):
result = 0
for row in board:
for el in row:
if el > 0:
result += 1
return result
def board_modifier(x, y):
result = 1.0
if x == 0 or x == 3:
result *= 1.5
if y == 0 or y == 3:
result *= 1.5
return result
def heuristic_value(board):
result = 0.0
x, y = 0, 0
for row in board:
x = 0
for el in row:
factor = Threes._scoringFactor(el)
if factor > 0:
result += (3 ** factor) * board_modifier(x, y)
x += 1
y += 1
return result / count_non_empty(board)
def rec_best_move(model, moves, depth):
if depth == 0:
return model
score = float("-inf")
result = model
for move in moves:
if not model.canMove(move):
continue
model_c = copy.deepcopy(model)
model_c.makeMove(move)
model_c = rec_best_move(model_c, moves, depth - 1)
new_score = heuristic_value(model_c.stateInfo().board)
if new_score > score:
result = model_c
score = new_score
return result
def best_move(model, moves=list(MoveEnum), depth=3):
result = None
score = float("-inf")
for move in moves:
if not model.canMove(move):
continue
model_c = copy.deepcopy(model)
model_c.makeMove(move)
model_c = rec_best_move(model_c, moves, depth)
new_score = heuristic_value(model_c.stateInfo().board)
if new_score > score:
score = new_score
result = move
return result
if __name__ == u'__main__':
seed = int(time.time())
random.seed(seed)
game = Threes()
printer(game)
moves_dict = {u"w": MoveEnum.Up,
u"a": MoveEnum.Left,
u"s": MoveEnum.Down,
u"d": MoveEnum.Right}
while True:
any_move = False
for m in moves_dict.values():
any_move = any_move or game.canMove(m)
if not any_move:
break
m = best_move(game, moves_dict.values())
game.makeMove(m)
print
printer(game)
print game.stateInfo().score