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
|
import json
import math
import os
import sys
import atexit
from subprocess import run, TimeoutExpired, CalledProcessError, DEVNULL
from abc import ABCMeta, abstractmethod
from prettytable import PrettyTable
tempRulesFilename = "temp_rules.json"
timeoutsLimit = 4
timeoutSeconds = 1
runtype = "release"
if (len(sys.argv) >= 2
and (sys.argv[1] == "--debug" or sys.argv[1] == "-d")):
runtype = "debug"
print("Running in debug mode...")
flag = ""
if (len(sys.argv) >= 2
and (sys.argv[1] == "--simple-hash" or sys.argv[1] == "-sh")):
flag = "-simple-hash"
print("Running in simple-hash mode...")
class SolitaireRulesGen(metaclass=ABCMeta):
def __init__(self, name):
self.name = name
self.baseJson = json.load(open('resources/' + name + '.json'))
def genRules(self, level):
self.alterFieldsToChange(level)
with open(tempRulesFilename, "w") as tempOut:
json.dump(self.baseJson, tempOut)
@abstractmethod
def alterFieldsToChange(self, level):
pass
def __str__(self):
return json.dumps(self.baseJson, indent=4)
class SpanishPatienceRulesGen(SolitaireRulesGen):
def __init__(self):
super().__init__("spanish-patience")
def alterFieldsToChange(self, level):
self.baseJson["max rank"] = level
self.baseJson["tableau piles"]["count"] = level
class FreeCellRulesGen(SolitaireRulesGen):
def __init__(self):
super().__init__("free-cell")
def alterFieldsToChange(self, level):
self.baseJson["max rank"] = level
self.baseJson["tableau piles"]["count"] = math.ceil(level * 0.61)
self.baseJson["cells"] = math.ceil(level * 0.3)
class BlackHoleRulesGen(SolitaireRulesGen):
def __init__(self):
super().__init__("black-hole")
def alterFieldsToChange(self, level):
self.baseJson["max rank"] = level
self.baseJson["tableau piles"]["count"] = math.ceil((4*level - 1)/3)
class BakersDozenRulesGen(SolitaireRulesGen):
def __init__(self):
super().__init__("bakers-dozen")
def alterFieldsToChange(self, level):
self.baseJson["max rank"] = level
self.baseJson["tableau piles"]["count"] = level
class FortunesFavorRulesGen(SolitaireRulesGen):
def __init__(self):
super().__init__("fortunes-favor")
def alterFieldsToChange(self, level):
# Because aces start up we must skip lv 1
if (level == 1): level = 2;
self.baseJson["max rank"] = level
self.baseJson["tableau piles"]["count"] = math.ceil(12*level/13)
self.baseJson["stock size"] = level * 4 - 4 - self.baseJson["tableau piles"]["count"]
class FlowerGardenRulesGen(SolitaireRulesGen):
def __init__(self):
super().__init__("flower-garden")
def alterFieldsToChange(self, level):
self.baseJson["max rank"] = level
self.baseJson["tableau piles"]["count"] = math.ceil(6*level/13)
self.baseJson["reserve size"] = math.ceil(16*level/13)
class CanfieldRulesGen(SolitaireRulesGen):
def __init__(self):
super().__init__("canfield")
def alterFieldsToChange(self, level):
self.baseJson["max rank"] = level
cardsNotInTableau = (level - 1) * 4
reserveSize = math.ceil(cardsNotInTableau * 0.7)
stockSize = (level - 1) * 4 - reserveSize
self.baseJson["reserve size"] = reserveSize
self.baseJson["stock size"] = stockSize
class SomersetRulesGen(SolitaireRulesGen):
def __init__(self):
super().__init__("somerset")
def alterFieldsToChange(self, level):
self.baseJson["max rank"] = level
self.baseJson["tableau piles"]["count"] = math.ceil(10*level/13)
class AlphaStarRulesGen(SolitaireRulesGen):
def __init__(self):
super().__init__("alpha-star")
def alterFieldsToChange(self, level):
self.baseJson["max rank"] = level
self.baseJson["tableau piles"]["count"] = math.ceil(12*level/13)
class SpiderRulesGen(SolitaireRulesGen):
def __init__(self):
super().__init__("spider")
def alterFieldsToChange(self, level):
self.baseJson["max rank"] = level
self.baseJson["tableau piles"]["count"] = math.ceil(10*level/13)
self.baseJson["stock size"] = math.ceil(60*level/13)
def cleanup():
os.remove(tempRulesFilename)
run("pkill solvitaire", shell=True)
atexit.register(cleanup)
summaryResults = {}
# Loops through each canonical solitaire
for rulesGen in [SpanishPatienceRulesGen(),
FreeCellRulesGen(),
BlackHoleRulesGen(),
BakersDozenRulesGen(),
FortunesFavorRulesGen(),
FlowerGardenRulesGen(),
CanfieldRulesGen(),
SomersetRulesGen(),
AlphaStarRulesGen(),
SpiderRulesGen()]:
# Loops through the levels
for level in range(1, 14):
# Calls the solitaire rules generator function with the level
rulesGen.genRules(level)
# Runs the solver with the rules and 20 random seeds. If more than 4
# don't return in 5 seconds, output that this is the max level
timeouts = 0
for attempt in range(20):
try:
run("./cmake-build-" + runtype + "/bin/solvitaire" + flag
+ " --rules " + tempRulesFilename + " --random "
+ str(attempt + 1),
shell=True,
check=True,
stdout=DEVNULL,
stderr=DEVNULL,
timeout=timeoutSeconds)
except TimeoutExpired:
timeouts += 1
except CalledProcessError:
print("Error running " + rulesGen.name + " at level "
+ str(level) + " with seed " + str(attempt + 1) + "!")
print(str(rulesGen) + "\n")
sys.exit()
except:
print("Unknown error in script")
sys.exit()
run("pkill solvitaire", shell=True)
if timeouts > timeoutsLimit:
break
if timeouts > timeoutsLimit:
print("Could not solve " + rulesGen.name + " at level " + str(level)
+ " within time constraint\n")
print("Game description at level " + str(level) + ":")
print(str(rulesGen) + "\n")
summaryResults[rulesGen.name] = level
break
else:
print("Solved " + rulesGen.name + " at level " + str(level)
+ " ...")
summaryResults[rulesGen.name] = level
# Prints results summary table
t = PrettyTable(['Solitaire', 'Level'])
for name, level in summaryResults.items():
t.add_row([name, level])
print(t)
|