forked from shlomif/PySolFC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunionsquare.py
219 lines (182 loc) · 7.49 KB
/
unionsquare.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
#!/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/>.
#
# ---------------------------------------------------------------------------##
import pysollib.game
from pysollib.game import Game
from pysollib.gamedb import GI, GameInfo, registerGame
from pysollib.hint import CautiousDefaultHint
from pysollib.layout import Layout
from pysollib.mfxutil import kwdefault
from pysollib.stack import \
AbstractFoundationStack, \
OpenStack, \
Stack, \
StackWrapper, \
UD_SS_RowStack, \
WasteStack, \
WasteTalonStack
from pysollib.util import ACE, ANY_RANK, NO_RANK
# ************************************************************************
# *
# ************************************************************************
class UnionSquare_Foundation(AbstractFoundationStack):
def acceptsCards(self, from_stack, cards):
if not AbstractFoundationStack.acceptsCards(self, from_stack, cards):
return False
# check the rank
if len(self.cards) > 12:
return cards[0].rank == 25 - len(self.cards)
else:
return cards[0].rank == len(self.cards)
class UnionSquare_RowStack(OpenStack):
def __init__(self, x, y, game, **cap):
kwdefault(cap, mod=8192, dir=0, base_rank=ANY_RANK,
max_accept=1, max_move=1)
OpenStack.__init__(self, x, y, game, **cap)
# self.CARD_YOFFSET = 1
def acceptsCards(self, from_stack, cards):
if not OpenStack.acceptsCards(self, from_stack, cards):
return False
if not self.cards:
return True
if cards[0].suit != self.cards[0].suit:
return False
if len(self.cards) == 1:
card_dir = cards[0].rank - self.cards[-1].rank
return card_dir == 1 or card_dir == -1
else:
stack_dir = (self.cards[1].rank - self.cards[0].rank) % \
self.cap.mod
return (self.cards[-1].rank + stack_dir) % \
self.cap.mod == cards[0].rank
getBottomImage = Stack._getReserveBottomImage
# ************************************************************************
# *
# ************************************************************************
class UnionSquare(pysollib.game.StartDealRowAndCards, Game):
Hint_Class = CautiousDefaultHint
Foundation_Class = StackWrapper(UnionSquare_Foundation, max_cards=26)
RowStack_Class = UnionSquare_RowStack
#
# game layout
#
def createGame(self, rows=16):
# create layout
l, s = Layout(self, card_y_space=20), self.s
# set window
self.setSize(l.XM + (5+rows//4)*l.XS, l.YM + 4*l.YS)
# create stacks
x, y, = l.XM, l.YM
s.talon = WasteTalonStack(x, y, self, max_rounds=1)
l.createText(s.talon, "s")
x = x + l.XS
s.waste = WasteStack(x, y, self)
l.createText(s.waste, "s")
for i in range(4):
x = 3*l.XS
for j in range(rows//4):
stack = self.RowStack_Class(x, y, self)
stack.CARD_XOFFSET, stack.CARD_YOFFSET = 0, 1
s.rows.append(stack)
x = x + l.XS
y = y + l.YS
x, y = self.width-l.XS, l.YM
for i in range(4):
stack = self.Foundation_Class(x, y, self, suit=i,
max_move=0, dir=0)
l.createText(stack, "sw")
s.foundations.append(stack)
y = y + l.YS
# define stack-groups
l.defaultStackGroups()
#
# game overrides
#
shallHighlightMatch = Game._shallHighlightMatch_SS
def getHighlightPilesStacks(self):
return ()
# ************************************************************************
# * Solid Square
# ************************************************************************
class SolidSquare(UnionSquare):
RowStack_Class = StackWrapper(UD_SS_RowStack, base_rank=NO_RANK,
max_accept=1, max_move=1, mod=13)
def createGame(self):
UnionSquare.createGame(self, rows=20)
def _shuffleHook(self, cards):
return self._shuffleHookMoveToTop(
cards,
lambda c: (c.rank == ACE and c.deck == 0, c.suit))
def startGame(self):
self.s.talon.dealRow(rows=self.s.foundations, frames=0)
UnionSquare.startGame(self)
def fillStack(self, stack):
if stack in self.s.rows and not stack.cards:
old_state = self.enterState(self.S_FILL)
if not self.s.waste.cards:
self.s.talon.dealCards()
if self.s.waste.cards:
self.s.waste.moveMove(1, stack)
self.leaveState(old_state)
shallHighlightMatch = Game._shallHighlightMatch_SSW
# ************************************************************************
# * Boomerang
# ************************************************************************
class Boomerang_Foundation(AbstractFoundationStack):
def acceptsCards(self, from_stack, cards):
if not AbstractFoundationStack.acceptsCards(self, from_stack, cards):
return False
# check the rank
# 7, 8, 9, 10, J, Q, K, A, K, Q, J, 10, 9, 8, 7, A
if len(self.cards) < 7:
return cards[0].rank - 6 == len(self.cards)
elif len(self.cards) == 7:
return cards[0].rank == ACE
elif len(self.cards) < 15:
return cards[0].rank == 20 - len(self.cards)
else: # len(self.cards) == 15
return cards[0].rank == ACE
class Boomerang(UnionSquare):
Foundation_Class = StackWrapper(Boomerang_Foundation,
base_rank=6, max_cards=16)
RowStack_Class = StackWrapper(UnionSquare_RowStack, base_rank=NO_RANK)
def createGame(self):
UnionSquare.createGame(self, rows=12)
def fillStack(self, stack):
if stack in self.s.rows and not stack.cards:
old_state = self.enterState(self.S_FILL)
if not self.s.waste.cards:
self.s.talon.dealCards()
if self.s.waste.cards:
self.s.waste.moveMove(1, stack)
self.leaveState(old_state)
# register the game
registerGame(GameInfo(35, UnionSquare, "Union Square",
GI.GT_2DECK_TYPE, 2, 0, GI.SL_MOSTLY_SKILL,
altnames=('British Square',),
))
registerGame(GameInfo(439, SolidSquare, "Solid Square",
GI.GT_2DECK_TYPE, 2, 0, GI.SL_BALANCED))
registerGame(GameInfo(738, Boomerang, "Boomerang",
GI.GT_2DECK_TYPE, 2, 0, GI.SL_BALANCED,
ranks=(0, 6, 7, 8, 9, 10, 11, 12),
))