1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00

Compare commits

..

No commits in common. "fd797ffbf53bcc90035b8f636c8f56997b8dcf8a" and "c12be2eaeda5986726bd44a901e863beb1be1ac2" have entirely different histories.

3 changed files with 117 additions and 112 deletions

View file

@ -22,13 +22,12 @@
# ---------------------------------------------------------------------------
# imports
import math
import time
import traceback
from pickle import Pickler, Unpickler, UnpicklingError
from pysol_cards.cards import ms_rearrange
from pysollib.gamedb import GI
from pysollib.help import help_about
from pysollib.hint import DefaultHint
@ -168,93 +167,6 @@ def _updateStatus_process_key_val(tb, sb, k, v):
raise AttributeError(k)
def _stats__is_perfect(stats):
"""docstring for _stats__is_perfect"""
return (stats.undo_moves == 0 and
stats.goto_bookmark_moves == 0 and
# stats.quickplay_moves == 0 and
stats.highlight_piles == 0 and
stats.highlight_cards == 0 and
stats.shuffle_moves == 0)
def _highlightCards__calc_item(canvas, delta, cw, ch, s, c1, c2, color):
assert c1 in s.cards and c2 in s.cards
tkraise = False
if c1 is c2:
# highlight single card
sx0, sy0 = s.getOffsetFor(c1)
x1, y1 = s.getPositionFor(c1)
x2, y2 = x1, y1
if c1 is s.cards[-1]:
# last card in the stack (for Pyramid-like games)
tkraise = True
else:
# highlight pile
if len(s.CARD_XOFFSET) > 1:
sx0 = 0
else:
sx0 = s.CARD_XOFFSET[0]
if len(s.CARD_YOFFSET) > 1:
sy0 = 0
else:
sy0 = s.CARD_YOFFSET[0]
x1, y1 = s.getPositionFor(c1)
x2, y2 = s.getPositionFor(c2)
if sx0 != 0 and sy0 == 0:
# horizontal stack
y2 += ch
if c2 is s.cards[-1]: # top card
x2 += cw
else:
if sx0 > 0:
# left to right
x2 += sx0
else:
# right to left
x1 += cw
x2 += cw + sx0
elif sx0 == 0 and sy0 != 0:
# vertical stack
x2 += cw
if c2 is s.cards[-1]: # top card
y2 += ch
else:
if sy0 > 0:
# up to down
y2 = y2 + sy0
else:
# down to up
y1 += ch
y2 += ch + sy0
else:
x2 += cw
y2 += ch
tkraise = True
# print c1, c2, x1, y1, x2, y2
x1, x2 = x1-delta[0], x2+delta[1]
y1, y2 = y1-delta[2], y2+delta[3]
if TOOLKIT == 'tk':
r = MfxCanvasRectangle(canvas, x1, y1, x2, y2,
width=4, fill=None, outline=color)
if tkraise:
r.tkraise(c2.item)
elif TOOLKIT == 'kivy':
r = MfxCanvasRectangle(canvas, x1, y1, x2, y2,
width=4, fill=None, outline=color)
if tkraise:
r.tkraise(c2.item)
elif TOOLKIT == 'gtk':
r = MfxCanvasRectangle(canvas, x1, y1, x2, y2,
width=4, fill=None, outline=color,
group=s.group)
if tkraise:
i = s.cards.index(c2)
for c in s.cards[i+1:]:
c.tkraise(1)
return r
class Game(object):
# for self.gstats.updated
U_PLAY = 0
@ -1059,8 +971,13 @@ class Game(object):
# get a fresh copy of the original game-cards
cards = list(self.cards)
# init random generator
if isinstance(self.random, LCRandom31):
cards = ms_rearrange(cards)
if isinstance(self.random, LCRandom31) and len(cards) == 52:
# FreeCell mode
fcards = []
for i in range(13):
for j in (0, 39, 26, 13):
fcards.append(cards[i + j])
cards = fcards
self.random.reset() # reset to initial seed
# shuffle
self.random.shuffle(cards)
@ -1896,7 +1813,7 @@ class Game(object):
#
# game changed - i.e. should we ask the player to discard the game
def changed(self, restart=False):
def changed(self, restart=0):
if self.gstats.updated < 0:
return 0 # already won or lost
if self.gstats.loaded > 0:
@ -1915,7 +1832,13 @@ class Game(object):
if not won or self.stats.hints > 0 or self.stats.demo_moves > 0:
# sorry, you lose
return won, 0, self.U_LOST
if _stats__is_perfect(self.stats):
if (self.stats.undo_moves == 0 and
self.stats.goto_bookmark_moves == 0 and
# self.stats.quickplay_moves == 0 and
self.stats.highlight_piles == 0 and
self.stats.highlight_cards == 0 and
self.stats.shuffle_moves == 0):
# perfect !
return won, 2, self.U_PERFECT
return won, 1, self.U_WON
@ -1969,17 +1892,17 @@ You have reached
def checkForWin(self):
won, status, updated = self.getWinStatus()
if not won:
return False
return 0
self.finishMove() # just in case
if self.preview:
return True
return 1
if self.finished:
return True
return 1
if self.demo:
return status
if TOOLKIT == 'kivy':
if not self.app.opt.display_win_message:
return True
return 1
self.top.waitAnimation()
if status == 2:
top_msg = self.updateStats()
@ -2036,7 +1959,7 @@ Congratulations, you did it !
strings=(_("&New game"), _("&Restart"), _("&Cancel")))
self.updateMenus()
if TOOLKIT == 'kivy':
return True
return 1
if d.status == 0 and d.button == 0:
# new game
self.endGame()
@ -2044,7 +1967,7 @@ Congratulations, you did it !
elif d.status == 0 and d.button == 1:
# restart game
self.restartGame()
return True
return 1
#
# Game - subclass overridable methods (but usually not)
@ -2052,8 +1975,10 @@ Congratulations, you did it !
def isGameWon(self):
# default: all Foundations must be filled
return sum([len(s.cards) for s in self.s.foundations]) == \
len(self.cards)
c = 0
for s in self.s.foundations:
c = c + len(s.cards)
return c == len(self.cards)
def getFoundationDir(self):
for s in self.s.foundations:
@ -2063,10 +1988,16 @@ Congratulations, you did it !
# determine the real number of player_moves
def getPlayerMoves(self):
return self.stats.player_moves
player_moves = self.stats.player_moves
# if self.moves.index > 0 and
# self.stats.demo_moves == self.moves.index:
# player_moves = 0
return player_moves
def updateTime(self):
if self.finished or self.pause:
if self.finished:
return
if self.pause:
return
t = time.time()
d = t - self.stats.update_time
@ -2191,9 +2122,80 @@ Congratulations, you did it !
cw, ch = self.app.images.getSize()
items = []
for s, c1, c2, color in info:
items.append(
_highlightCards__calc_item(
self.canvas, delta, cw, ch, s, c1, c2, color))
assert c1 in s.cards and c2 in s.cards
tkraise = False
if c1 is c2:
# highlight single card
sx0, sy0 = s.getOffsetFor(c1)
x1, y1 = s.getPositionFor(c1)
x2, y2 = x1, y1
if c1 is s.cards[-1]:
# last card in the stack (for Pyramid-like games)
tkraise = True
else:
# highlight pile
if len(s.CARD_XOFFSET) > 1:
sx0 = 0
else:
sx0 = s.CARD_XOFFSET[0]
if len(s.CARD_YOFFSET) > 1:
sy0 = 0
else:
sy0 = s.CARD_YOFFSET[0]
x1, y1 = s.getPositionFor(c1)
x2, y2 = s.getPositionFor(c2)
if sx0 != 0 and sy0 == 0:
# horizontal stack
y2 += ch
if c2 is s.cards[-1]: # top card
x2 += cw
else:
if sx0 > 0:
# left to right
x2 += sx0
else:
# right to left
x1 += cw
x2 += cw + sx0
elif sx0 == 0 and sy0 != 0:
# vertical stack
x2 += cw
if c2 is s.cards[-1]: # top card
y2 += ch
else:
if sy0 > 0:
# up to down
y2 = y2 + sy0
else:
# down to up
y1 += ch
y2 += ch + sy0
else:
x2 += cw
y2 += ch
tkraise = True
# print c1, c2, x1, y1, x2, y2
x1, x2 = x1-delta[0], x2+delta[1]
y1, y2 = y1-delta[2], y2+delta[3]
if TOOLKIT == 'tk':
r = MfxCanvasRectangle(self.canvas, x1, y1, x2, y2,
width=4, fill=None, outline=color)
if tkraise:
r.tkraise(c2.item)
elif TOOLKIT == 'kivy':
r = MfxCanvasRectangle(self.canvas, x1, y1, x2, y2,
width=4, fill=None, outline=color)
if tkraise:
r.tkraise(c2.item)
elif TOOLKIT == 'gtk':
r = MfxCanvasRectangle(self.canvas, x1, y1, x2, y2,
width=4, fill=None, outline=color,
group=s.group)
if tkraise:
i = s.cards.index(c2)
for c in s.cards[i+1:]:
c.tkraise(1)
items.append(r)
if not items:
return 0
self.canvas.update_idletasks()

View file

@ -34,7 +34,7 @@ except ImportError:
"You need to install " +
"https://pypi.python.org/pypi/random2 using pip or similar.")
from pysol_cards.random_base import RandomBase # noqa: I100
from pysol_cards.random import RandomBase # noqa: I100
# ************************************************************************
@ -277,7 +277,7 @@ PysolRandom = MTRandom
def _match_ms(s):
"""match an ms based seed string."""
return re.match(r"ms([0-9]+)\n?\Z", str(s))
return re.match(r"ms([0-9]+)\n?\Z", s)
# construct Random from seed string

View file

@ -53,8 +53,6 @@
# imports
import unittest
from pysol_cards.cards import ms_rearrange
# So the localpaths will be overrided.
from pysollib.pysolrandom import LCRandom31, constructRandom, \
random__long2str, random__str2long
@ -246,8 +244,13 @@ def flip_card(card_str, flip):
def shuffle(orig_cards, rand):
shuffled_cards = list(orig_cards)
if isinstance(rand, LCRandom31):
shuffled_cards = ms_rearrange(shuffled_cards)
if isinstance(rand, LCRandom31) and len(shuffled_cards) == 52:
# FreeCell mode
fcards = []
for i in range(13):
for j in (0, 39, 26, 13):
fcards.append(shuffled_cards[i + j])
shuffled_cards = fcards
# rand.shuffle works in place
rand.shuffle(shuffled_cards)
return shuffled_cards