-
Notifications
You must be signed in to change notification settings - Fork 0
/
v6_customcache.py
147 lines (117 loc) · 4.02 KB
/
v6_customcache.py
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
import udebs
import functools
import udebs_config
from collections import OrderedDict
def cache(f, maxsize=2**23):
storage = OrderedDict()
empty = (-float("inf"), float("inf"))
@functools.wraps(f)
def cache_wrapper(self, alpha, beta):
key = self.hash()
a_, b_ = storage.get(key, empty)
if a_ > alpha:
alpha = a_
if b_ < beta:
beta = b_
if alpha >= beta:
storage.move_to_end(key)
return alpha
result = f(self, alpha, beta)
if result <= alpha:
storage[key] = (a_, result)
elif result >= beta:
storage[key] = (result, b_)
else:
storage[key] = (result, result)
storage.move_to_end(key)
if storage.__len__() > maxsize:
storage.popitem(False)
return result
return cache_wrapper
class Connect4(udebs.State):
win_cond = 4
def hash(self):
map_ = self.getMap()
mappings = {
"empty": "0",
"x": "1",
"o": "2",
}
data = []
sym = []
for y in range(map_.y):
buf = [mappings[map_[x,y]] for x in range(map_.x)]
data.extend(buf)
sym.extend(reversed(buf))
return min(int("".join(data), 3), int("".join(sym), 3))
def legalMoves(self):
map_ = self.map["map"]
player = "xPlayer" if self.getStat("xPlayer", "ACT") >= 2 else "oPlayer"
other = self.getStat("oPlayer" if player == "xPlayer" else "xPlayer", "token")
token = self.getStat(player, "token")
options = []
forced = None
backup = None
for x in range(map_.x):
y = udebs_config.BOTTOM(map_, x)
if y is not None:
loc = (x, y)
if udebs_config.win(map_, other, loc) >= self.win_cond:
if forced is not None:
yield 1
return
forced = loc
if y > 0:
if udebs_config.win(map_, other, (x, y - 1)) >= self.win_cond:
# We cannot play here unless it is our only option
backup = 1
if forced == loc:
yield 1
return
continue
# finally these are our only good options
if forced is None:
options.append(loc)
if forced:
yield player, forced, "drop"
elif len(options) > 0:
const = (map_.x - 1) / 2
huristic = lambda x: abs(const - x[0])
for loc in sorted(options, key=huristic):
yield player, loc, "drop"
else:
yield backup if backup else 0
def result(self, alpha=-1, beta=1):
if self.value is not None:
return -abs(self.value)
map_ = self.getMap()
player = "xPlayer" if self.getStat("xPlayer", "ACT") >= 2 else "oPlayer"
token = "x" if player == "xPlayer" else "o"
for x in range(map_.x):
y = udebs_config.BOTTOM(map_, x)
if y is not None:
if udebs_config.win(map_, token, (x,y)) >= self.win_cond:
return 1
return self.negamax(alpha, beta)
@udebs.countrecursion
@cache
def negamax(self, alpha, beta):
current = -float("inf")
for child, e in self.substates():
if child is e:
result = -child
else:
result = -child.negamax(-beta, -alpha)
if result > current:
current = result
if result > alpha:
alpha = result
if alpha >= beta:
break
return current
if __name__ == "__main__":
main_map = udebs.battleStart(udebs_config.config, field=Connect4())
main_map.printMap()
with udebs.Timer():
result = main_map.result()
print(result)