forked from shlomif/PySolFC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bakersgame.py
330 lines (269 loc) · 11.2 KB
/
bakersgame.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python
# -*- mode: python; coding: utf-8; -*-
# ---------------------------------------------------------------------------##
#
# Copyright (C) 1998-2003 Markus Franz Xaver Johannes Oberhumer
# Copyright (C) 2003 Mt. Hood Playing Card Co.
# Copyright (C) 2005-2009 Skomoroh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ---------------------------------------------------------------------------##
from pysollib.game import Game
from pysollib.gamedb import GI, GameInfo, registerGame
from pysollib.games.freecell import FreeCell
from pysollib.hint import DefaultHint
from pysollib.hint import FreeCellSolverWrapper, FreeCellType_Hint
from pysollib.layout import Layout
from pysollib.stack import \
AC_RowStack, \
FreeCell_SS_RowStack, \
InitialDealTalonStack, \
KingSS_RowStack, \
OpenStack, \
ReserveStack, \
SS_FoundationStack, \
SS_RowStack, \
StackWrapper, \
SuperMoveSS_RowStack
from pysollib.util import KING
# ************************************************************************
# * Baker's Game
# ************************************************************************
class BakersGame(FreeCell):
RowStack_Class = SuperMoveSS_RowStack
Solver_Class = FreeCellSolverWrapper(sbb='suit')
shallHighlightMatch = Game._shallHighlightMatch_SS
# ************************************************************************
# *
# ************************************************************************
class KingOnlyBakersGame(BakersGame):
RowStack_Class = StackWrapper(FreeCell_SS_RowStack, base_rank=KING)
Solver_Class = FreeCellSolverWrapper(sbb='suit', esf='kings')
# ************************************************************************
# * Eight Off (Baker's Game in a different layout)
# ************************************************************************
class EightOff(KingOnlyBakersGame):
#
# game layout
#
def createGame(self, rows=8, reserves=8):
# create layout
l, s = Layout(self), self.s
# set window
# (piles up to 16 cards are playable without
# overlap in default window size)
h = max(2*l.YS, l.YS+(16-1)*l.YOFFSET)
maxrows = max(rows, reserves)
self.setSize(l.XM + maxrows*l.XS, l.YM + l.YS + h + l.YS)
# create stacks
x, y = l.XM + (maxrows-4)*l.XS//2, l.YM
for i in range(4):
s.foundations.append(SS_FoundationStack(x, y, self, i))
x = x + l.XS
x, y = l.XM + (maxrows-rows)*l.XS//2, y + l.YS
for i in range(rows):
s.rows.append(self.RowStack_Class(x, y, self))
x = x + l.XS
x, y = l.XM + (maxrows-reserves)*l.XS//2, self.height - l.YS
for i in range(reserves):
s.reserves.append(ReserveStack(x, y, self))
x = x + l.XS
self.setRegion(s.reserves, (-999, y - l.CH // 2, 999999, 999999))
s.talon = InitialDealTalonStack(l.XM, l.YM, self)
# define stack-groups
l.defaultStackGroups()
#
# game overrides
#
def startGame(self):
self._startDealNumRows(5)
self.s.talon.dealRow()
r = self.s.reserves
self.s.talon.dealRow(rows=[r[0], r[2], r[4], r[6]])
# ************************************************************************
# * Seahaven Towers (Baker's Game in a different layout)
# ************************************************************************
class SeahavenTowers(KingOnlyBakersGame):
#
# game layout
#
def createGame(self):
# create layout
l, s = Layout(self), self.s
# set window
# (piles up to 20 cards are playable in default window size)
h = max(3*l.YS, 20*l.YOFFSET)
self.setSize(l.XM + 10*l.XS, l.YM + l.YS + h)
# create stacks
x, y = l.XM, l.YM
for i in range(4):
s.reserves.append(ReserveStack(x + (i+3)*l.XS, y, self))
for suit in range(4):
i = (9, 0, 1, 8)[suit]
s.foundations.append(SS_FoundationStack(x + i*l.XS, y, self, suit))
x, y = l.XM, l.YM + l.YS
for i in range(10):
s.rows.append(self.RowStack_Class(x, y, self))
x = x + l.XS
self.setRegion(s.rows, (-999, y - l.CH // 2, 999999, 999999))
s.talon = InitialDealTalonStack(l.XM, self.height-l.YS, self)
# define stack-groups
self.sg.openstacks = s.foundations + s.rows + s.reserves
self.sg.talonstacks = [s.talon]
self.sg.dropstacks = s.rows + s.reserves
self.sg.reservestacks = s.reserves
#
# game overrides
#
def startGame(self):
self._startDealNumRows(4)
self.s.talon.dealRow()
self.s.talon.dealRow(rows=(self.s.reserves[1:3]))
# ************************************************************************
# *
# ************************************************************************
class RelaxedSeahavenTowers(SeahavenTowers):
RowStack_Class = KingSS_RowStack
Solver_Class = FreeCellSolverWrapper(
sbb='suit', esf='kings', sm='unlimited')
# ************************************************************************
# * Tuxedo
# * Penguin
# * Opus
# ************************************************************************
class Tuxedo(Game):
RowStack_Class = StackWrapper(SS_RowStack, mod=13)
ReserveStack_Class = ReserveStack
Foundation_Class = StackWrapper(SS_FoundationStack, mod=13, max_move=0)
Hint_Class = FreeCellType_Hint
Solver_Class = FreeCellSolverWrapper(sbb='suit', sm='unlimited')
def createGame(self, rows=7, reserves=7):
# create layout
l, s = Layout(self), self.s
# set window
# (piles up to 16 cards are playable without
# overlap in default window size)
h = max(3*l.YS, l.YS+(16-1)*l.YOFFSET)
maxrows = max(rows, reserves)
self.setSize(l.XM + (maxrows+1)*l.XS, l.YM + h + l.YS)
# extra settings
self.base_card = None
# create stacks
x, y = self.width - l.XS, l.YM
for i in range(4):
s.foundations.append(self.Foundation_Class(x, y, self, suit=i))
y = y + l.YS
self.setRegion(s.foundations, (x - l.CW//2, -999, 999999, 999999))
x, y = l.XM + (maxrows-rows)*l.XS//2, l.YM
for i in range(rows):
s.rows.append(self.RowStack_Class(x, y, self))
x = x + l.XS
x, y = l.XM + (maxrows-reserves)*l.XS//2, self.height - l.YS
for i in range(reserves):
s.reserves.append(self.ReserveStack_Class(x, y, self))
x = x + l.XS
self.setRegion(s.reserves, (-999, y - l.CH // 2, 999999, 999999))
s.talon = InitialDealTalonStack(l.XM+1, y, self)
# define stack-groups
l.defaultStackGroups()
def startGame(self):
self._startDealNumRows(6)
self.s.talon.dealRow()
self.s.talon.dealRow(rows=self.s.rows[::3])
shallHighlightMatch = Game._shallHighlightMatch_SSW
class Penguin(Tuxedo):
GAME_VERSION = 2
Solver_Class = FreeCellSolverWrapper(
sbb='suit', esf='kings', sm='unlimited')
def _shuffleHook(self, cards):
# move base cards to top of the Talon (i.e. first cards to be dealt)
return self._shuffleHookMoveToTop(
cards,
lambda c, rank=cards[-1].rank: (c.rank == rank, 0))
def _updateStacks(self):
for s in self.s.foundations:
s.cap.base_rank = self.base_card.rank
for s in self.s.rows:
s.cap.base_rank = (self.base_card.rank - 1) % 13
def startGame(self):
self.base_card = self.s.talon.cards[-4]
self._updateStacks()
# deal base cards to Foundations
for i in range(3):
c = self.s.talon.getCard()
assert c.rank == self.base_card.rank
to_stack = self.s.foundations[c.suit * self.gameinfo.decks]
self.flipMove(self.s.talon)
self.moveMove(1, self.s.talon, to_stack, frames=0)
# deal rows
self._startDealNumRowsAndDealSingleRow(6)
def _restoreGameHook(self, game):
self.base_card = self.cards[game.loadinfo.base_card_id]
self._updateStacks()
def _loadGameHook(self, p):
self.loadinfo.addattr(base_card_id=None) # register extra load var.
self.loadinfo.base_card_id = p.load()
def _saveGameHook(self, p):
p.dump(self.base_card.id)
class Opus(Penguin):
def createGame(self):
Tuxedo.createGame(self, reserves=5)
# ************************************************************************
# * Flipper
# ************************************************************************
class Flipper_Row(AC_RowStack):
def canFlipCard(self):
if not OpenStack.canFlipCard(self):
return False
i = list(self.game.s.rows).index(self)
return len(self.game.s.reserves[i].cards) == 0
class Flipper(Tuxedo):
RowStack_Class = Flipper_Row
Foundation_Class = SS_FoundationStack
Hint_Class = DefaultHint
Solver_Class = None
def fillStack(self, stack):
i = 0
for s in self.s.reserves:
r = self.s.rows[i]
if r.cards:
if ((s.cards and r.cards[-1].face_up) or
(not s.cards and not r.cards[-1].face_up)):
r.flipMove(animation=True)
i += 1
shallHighlightMatch = Game._shallHighlightMatch_AC
# register the game
registerGame(GameInfo(45, BakersGame, "Baker's Game",
GI.GT_FREECELL | GI.GT_OPEN, 1, 0, GI.SL_SKILL))
registerGame(GameInfo(26, KingOnlyBakersGame, "King Only Baker's Game",
GI.GT_FREECELL | GI.GT_OPEN, 1, 0, GI.SL_SKILL))
registerGame(GameInfo(258, EightOff, "Eight Off",
GI.GT_FREECELL | GI.GT_OPEN, 1, 0, GI.SL_MOSTLY_SKILL))
registerGame(GameInfo(9, SeahavenTowers, "Seahaven Towers",
GI.GT_FREECELL | GI.GT_OPEN, 1, 0, GI.SL_SKILL,
altnames=("Sea Towers", "Towers")))
registerGame(GameInfo(6, RelaxedSeahavenTowers, "Relaxed Seahaven Towers",
GI.GT_FREECELL | GI.GT_RELAXED | GI.GT_OPEN, 1, 0,
GI.SL_SKILL))
registerGame(GameInfo(64, Penguin, "Penguin",
GI.GT_FREECELL | GI.GT_OPEN, 1, 0, GI.SL_MOSTLY_SKILL,
altnames=("Beak and Flipper",)))
registerGame(GameInfo(427, Opus, "Opus",
GI.GT_FREECELL | GI.GT_OPEN, 1, 0, GI.SL_MOSTLY_SKILL))
registerGame(GameInfo(629, Tuxedo, "Tuxedo",
GI.GT_FREECELL | GI.GT_OPEN, 1, 0, GI.SL_MOSTLY_SKILL))
registerGame(GameInfo(713, Flipper, "Flipper",
GI.GT_FREECELL | GI.GT_ORIGINAL, 1, 0,
GI.SL_MOSTLY_SKILL))