mirror of
https://github.com/shlomif/PySolFC.git
synced 2025-04-15 02:54:09 -04:00
Start adding support for handling parse errs.
in the importFile feature. Still does not work fully.
This commit is contained in:
parent
9eab5fe6bf
commit
4b75005b28
4 changed files with 64 additions and 9 deletions
|
@ -716,6 +716,18 @@ class SpiderType_Hint(DefaultHint):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PySolHintLayoutImportError(Exception):
|
||||||
|
|
||||||
|
def __init__(self, msg, cards, line_num):
|
||||||
|
"""docstring for __init__"""
|
||||||
|
self.msg = msg
|
||||||
|
self.cards = cards
|
||||||
|
self.line_num = line_num
|
||||||
|
|
||||||
|
def format(self):
|
||||||
|
return self.msg + ":\n\n" + ', '.join(self.cards)
|
||||||
|
|
||||||
|
|
||||||
# ************************************************************************
|
# ************************************************************************
|
||||||
# * FreeCell-Solver
|
# * FreeCell-Solver
|
||||||
# ************************************************************************
|
# ************************************************************************
|
||||||
|
@ -744,18 +756,21 @@ class Base_Solver_Hint:
|
||||||
def config(self, **kw):
|
def config(self, **kw):
|
||||||
self.options.update(kw)
|
self.options.update(kw)
|
||||||
|
|
||||||
def _card2str_format(self, fmt, card):
|
def _card2str_format(self, fmt, rank, suit):
|
||||||
# row and reserves
|
# row and reserves
|
||||||
rank = (card.rank-self.base_rank) % 13
|
rank = (rank-self.base_rank) % 13
|
||||||
return fmt % {'R': "A23456789TJQK"[rank], 'S': "CSHD"[card.suit]}
|
return fmt % {'R': "A23456789TJQK"[rank], 'S': "CSHD"[suit]}
|
||||||
|
|
||||||
|
def card2str1_(self, rank, suit):
|
||||||
|
# row and reserves
|
||||||
|
return self._card2str_format('%(R)s%(S)s', rank, suit)
|
||||||
|
|
||||||
def card2str1(self, card):
|
def card2str1(self, card):
|
||||||
# row and reserves
|
return self.card2str1_(card.rank, card.suit)
|
||||||
return self._card2str_format('%(R)s%(S)s', card)
|
|
||||||
|
|
||||||
def card2str2(self, card):
|
def card2str2(self, card):
|
||||||
# foundations
|
# foundations
|
||||||
return self._card2str_format('%(S)s-%(R)s', card)
|
return self._card2str_format('%(S)s-%(R)s', card.rank, card.suit)
|
||||||
|
|
||||||
# hard solvable: Freecell #47038300998351211829 (65539 iters)
|
# hard solvable: Freecell #47038300998351211829 (65539 iters)
|
||||||
|
|
||||||
|
@ -853,6 +868,7 @@ class FreeCellSolver_Hint(Base_Solver_Hint):
|
||||||
SUITS_S = "CSHD"
|
SUITS_S = "CSHD"
|
||||||
SUITS_RE = '[' + SUITS_S + ']'
|
SUITS_RE = '[' + SUITS_S + ']'
|
||||||
CARD_RE = r'(?:' + RANKS_RE + SUITS_RE + ')'
|
CARD_RE = r'(?:' + RANKS_RE + SUITS_RE + ')'
|
||||||
|
line_num = 0
|
||||||
|
|
||||||
def cards():
|
def cards():
|
||||||
return game.talon.cards
|
return game.talon.cards
|
||||||
|
@ -860,7 +876,13 @@ class FreeCellSolver_Hint(Base_Solver_Hint):
|
||||||
def put(target, suit, rank):
|
def put(target, suit, rank):
|
||||||
ret = [i for i, c in enumerate(cards())
|
ret = [i for i, c in enumerate(cards())
|
||||||
if c.suit == suit and c.rank == rank]
|
if c.suit == suit and c.rank == rank]
|
||||||
assert len(ret) == 1
|
if len(ret) < 1:
|
||||||
|
raise PySolHintLayoutImportError(
|
||||||
|
"Duplicate cards in input",
|
||||||
|
[solver.card2str1_(rank, suit)],
|
||||||
|
line_num
|
||||||
|
)
|
||||||
|
|
||||||
ret = ret[0]
|
ret = ret[0]
|
||||||
game.talon.cards = \
|
game.talon.cards = \
|
||||||
cards()[0:ret] + cards()[(ret+1):] + [cards()[ret]]
|
cards()[0:ret] + cards()[(ret+1):] + [cards()[ret]]
|
||||||
|
@ -889,6 +911,7 @@ class FreeCellSolver_Hint(Base_Solver_Hint):
|
||||||
|
|
||||||
mytext = mydecode(fh.read())
|
mytext = mydecode(fh.read())
|
||||||
for line_p in mytext.splitlines():
|
for line_p in mytext.splitlines():
|
||||||
|
line_num += 1
|
||||||
line = line_p.rstrip('\r\n')
|
line = line_p.rstrip('\r\n')
|
||||||
m = re.match(r'^(?:Foundations:|Founds?:)\s*(.*)', line)
|
m = re.match(r'^(?:Foundations:|Founds?:)\s*(.*)', line)
|
||||||
if m:
|
if m:
|
||||||
|
|
|
@ -13,6 +13,7 @@ from pysollib.settings import TITLE, WIN_SYSTEM
|
||||||
from pysollib.settings import SELECT_GAME_MENU
|
from pysollib.settings import SELECT_GAME_MENU
|
||||||
from pysollib.gamedb import GI
|
from pysollib.gamedb import GI
|
||||||
from pysollib.settings import USE_FREECELL_SOLVER
|
from pysollib.settings import USE_FREECELL_SOLVER
|
||||||
|
from pysollib.hint import PySolHintLayoutImportError
|
||||||
|
|
||||||
from pysollib.ui.tktile.tkconst import EVENT_HANDLED, EVENT_PROPAGATE, \
|
from pysollib.ui.tktile.tkconst import EVENT_HANDLED, EVENT_PROPAGATE, \
|
||||||
CURSOR_WATCH, COMPOUNDS
|
CURSOR_WATCH, COMPOUNDS
|
||||||
|
@ -1239,7 +1240,16 @@ Unsupported game for import.
|
||||||
if os.path.isfile(filename):
|
if os.path.isfile(filename):
|
||||||
with open(filename, 'r+b') as fh:
|
with open(filename, 'r+b') as fh:
|
||||||
game = self.game
|
game = self.game
|
||||||
game.Solver_Class(game, self).importFile(fh, game, self)
|
try:
|
||||||
|
game.Solver_Class(game, self).importFile(
|
||||||
|
fh, game, self)
|
||||||
|
except PySolHintLayoutImportError as err:
|
||||||
|
self._calc_MfxMessageDialog()(
|
||||||
|
self.top,
|
||||||
|
title=_('Import game error'),
|
||||||
|
text=err.format(),
|
||||||
|
bitmap='error'
|
||||||
|
)
|
||||||
|
|
||||||
def mSaveAs(self, *event):
|
def mSaveAs(self, *event):
|
||||||
if self._cancelDrag(break_pause=False):
|
if self._cancelDrag(break_pause=False):
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from pysollib.acard import AbstractCard
|
from pysollib.acard import AbstractCard
|
||||||
from pysollib.hint import FreeCellSolver_Hint
|
from pysollib.hint import FreeCellSolver_Hint, PySolHintLayoutImportError
|
||||||
import pysollib.stack
|
import pysollib.stack
|
||||||
|
|
||||||
|
|
||||||
|
@ -132,6 +132,20 @@ KD QC 5C QH 6S 3D
|
||||||
8S 7H 3H 2C AC 7D
|
8S 7H 3H 2C AC 7D
|
||||||
''', 'import worked with utf-8 bom')
|
''', 'import worked with utf-8 bom')
|
||||||
|
|
||||||
|
def test_throw_error_on_duplicate_card(self):
|
||||||
|
s_game = Mock_S_Game()
|
||||||
|
h = FreeCellSolver_Hint(s_game, None)
|
||||||
|
fh = open('tests/unit/data/624-with-dup-card.board', 'r+b')
|
||||||
|
try:
|
||||||
|
h.importFileHelper(fh, s_game)
|
||||||
|
except PySolHintLayoutImportError as err:
|
||||||
|
self.assertEqual(err.msg, "Duplicate cards in input")
|
||||||
|
self.assertEqual(err.cards, ["KC"])
|
||||||
|
self.assertEqual(err.line_num, 1)
|
||||||
|
self.assertEqual(err.format(), "Duplicate cards in input:\n\nKC")
|
||||||
|
return
|
||||||
|
self.fail("No exception thrown.")
|
||||||
|
|
||||||
|
|
||||||
def mymain():
|
def mymain():
|
||||||
from pycotap import TAPTestRunner
|
from pycotap import TAPTestRunner
|
||||||
|
|
8
tests/unit/data/624-with-dup-card.board
Normal file
8
tests/unit/data/624-with-dup-card.board
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
: KC KC 4C QS 2D 4S AS
|
||||||
|
: 4H TH 2S JH 2H 9S AH
|
||||||
|
: 3S 6C 9H AD KH QD 7C
|
||||||
|
: 3C JS 5H KS TC 9C 8C
|
||||||
|
: 4D 9D 7S JC 5D TS
|
||||||
|
: KD QC 5C QH 6S 3D
|
||||||
|
: 5S JD 8D 6D TD 8H
|
||||||
|
: 8S 7H 3H 2C AC 7D
|
Loading…
Add table
Reference in a new issue