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

Start implementing board import.

See https://github.com/shlomif/PySolFC/issues/47 .
This commit is contained in:
Shlomi Fish 2017-12-18 20:47:10 +02:00
parent b87e8dd5a8
commit 3fc989ae0a
3 changed files with 97 additions and 4 deletions

View file

@ -484,20 +484,25 @@ class Game(object):
#
# start a new name
def newGame(self, random=None, restart=0, autoplay=1):
def newGame(self, random=None, restart=0, autoplay=1, shuffle=True,
s_game=0):
# print 'Game.newGame'
self.finished = False
old_busy, self.busy = self.busy, 1
self.setCursor(cursor=CURSOR_WATCH)
self.stopWinAnimation()
self.disableMenus()
self.redealAnimation()
if shuffle:
self.redealAnimation()
self.reset(restart=restart)
self.resetGame()
self.createRandom(random)
# print self.random, self.random.__dict__
self.shuffle()
assert len(self.s.talon.cards) == self.gameinfo.ncards
if shuffle:
self.shuffle()
assert len(self.s.talon.cards) == self.gameinfo.ncards
else:
self.s = s_game
for stack in self.allstacks:
stack.updateText()
self.updateText()

View file

@ -814,6 +814,59 @@ class FreeCellSolver_Hint(Base_Solver_Hint):
self._addBoardLine(prefix + b)
return
def _getNextId(self):
self._id += 1
return self._id
def importFile(solver, fh, s_game, self):
game = s_game.s
solver._id = 1000
stack_idx = 0
def crCard(id, deck, suit, rank):
return s_game._createCard(id, deck, suit, rank, 0, 0)
for line_p in fh:
line = line_p.rstrip('\r\n')
m = re.match(r'^(?:Foundations:|Founds?:)\s*(.*)', line)
if m:
g = re.findall(r'\b([HCDS])-([0A23456789TJQK])\b', m.group(1))
for gm in g:
for foundat in game.foundations:
suit = foundat.cap.suit
if "CSHD"[suit] == gm[0]:
foundat.cards = [crCard(
solver._getNextId(), 0, suit, r
) for r in range(
"A23456789TJQK".index(gm[1]))
]
break
continue
m = re.match(r'^(?:FC:|Freecells:)\s*(.*)', line)
if m:
g = re.findall(
r'\b((?:[A23456789TJQK][HCDS])|\-)\b', m.group(1))
while len(g) < len(game.reserves):
g.append(('-'))
for i, gm in enumerate(g):
str_ = gm
if str_ == '-':
game.reserves[i].cards = []
else:
print(str_)
game.reserves[i].cards = [crCard(
solver._getNextId(), 0, "CSHD".index(str_[1]),
"A23456789TJQK".index(str_[0]))]
continue
g = re.findall(r'\b((?:[A23456789TJQK][HCDS]))\b', line)
game.rows[stack_idx].cards = [
crCard(solver._getNextId(), 0, "CSHD".index(str_[1]),
"A23456789TJQK".index(str_[0]))
for str_ in g
]
stack_idx += 1
s_game.endGame()
s_game.newGame(shuffle=False, s_game=game)
def calcBoardString(self):
game = self.game
self.board = ''

View file

@ -334,6 +334,9 @@ class PysolMenubarTkCommon:
menu.add_command(
label=n_("E&xport current layout..."),
command=self.mExportCurrentLayout)
menu.add_command(
label=n_("I&mport starting layout..."),
command=self.mImportStartingLayout)
menu.add_separator()
menu.add_command(
label=n_("&Hold and quit"),
@ -1201,6 +1204,38 @@ Unsupported game for export.
fh.write(game.Solver_Class(game, self).calcBoardString())
self.updateMenus()
def mImportStartingLayout(self, *event):
if self._cancelDrag(break_pause=False):
return
game = self.game
if not game.Solver_Class:
d = self._calc_MfxMessageDialog()(
self.top, title=_('Import game error'),
text=_('''
Unsupported game for import.
'''),
bitmap='error')
return
filename = self.game.filename
if filename:
idir, ifile = os.path.split(os.path.normpath(filename))
else:
idir, ifile = "", ""
if not idir:
idir = self.app.dn.savegames
d = tkinter_tkfiledialog.Open()
filename = d.show(filetypes=self.FILETYPES,
defaultextension=self.DEFAULTEXTENSION,
initialdir=idir, initialfile=ifile)
if filename:
filename = os.path.normpath(filename)
# filename = os.path.normcase(filename)
if os.path.isfile(filename):
with open(filename, 'r') as fh:
game = self.game
game.Solver_Class(game, self).importFile(fh, game, self)
def mSaveAs(self, *event):
if self._cancelDrag(break_pause=False):
return