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

Compare commits

...

4 commits

Author SHA1 Message Date
Shlomi Fish
6bc5b53995 test presence of a <move/> element
add a testcase for the XML output
2025-02-01 13:52:43 +02:00
Shlomi Fish
dd42ac10ea test presence of a <move/> element
add a testcase for the XML output
2025-01-31 09:05:30 +02:00
Shlomi Fish
d6ca6d226b add a testcase for the XML output 2025-01-31 08:39:36 +02:00
Shlomi Fish
e4e2e99031 [Wip] kpatience xml exporting (Jan 2025 try) 2025-01-31 08:39:36 +02:00
2 changed files with 86 additions and 0 deletions

View file

@ -26,6 +26,8 @@ import os
import re import re
import subprocess import subprocess
import time import time
from collections import OrderedDict
from io import BytesIO from io import BytesIO
from pysollib.mfxutil import destruct from pysollib.mfxutil import destruct
@ -996,6 +998,72 @@ class FreeCellSolver_Hint(Base_Solver_Hint):
-1 -1
) )
def calcBoardXML(self):
from io import StringIO
from xml.sax.saxutils import XMLGenerator
game = self.game
self.board = ''
is_simple_simon = self._isSimpleSimon()
b = []
for s in game.s.foundations:
if s.cards:
b.append(s.cards[0 if is_simple_simon else -1])
assert len(b) == 0
b = []
for s in game.s.reserves:
b.append((s.cards[-1]) if s.cards else None)
assert all(x is None for x in b)
nextid = 1
ids = {}
out = StringIO("")
xmler = XMLGenerator(out=out, encoding='UTF-8')
xmler.startDocument()
xmler.startElement(name='state', attrs={})
for row_idx, s in enumerate(game.s.rows):
moveattrs = []
moveattrs.append(('pile', 'store{}'.format(row_idx)))
moveattrs.append(('position', '{}'.format(0)))
moveattrs.sort()
dmoveattrs = OrderedDict(moveattrs)
xmler.startElement(name='move', attrs=dmoveattrs)
b = []
for c in s.cards:
cardattrs = []
cs = self.card2str1(c)
if cs in ids:
this_id = ids[cs]
else:
this_id = nextid
nextid += 1
ids[cs] = this_id
cardattrs.append(('id', '{}'.format(this_id)))
this_rank = [
"ace", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "jack", "queen", "king"
][c.rank]
cardattrs.append(('rank', '{}'.format(this_rank)))
this_suit = [
"clubs", "spades", "hearts", "diamonds",
][c.suit]
cardattrs.append(('suit', '{}'.format(this_suit)))
cardattrs.append(('turn', '{}'.format("face-up")))
cardattrs.sort()
dcardattrs = OrderedDict(cardattrs)
xmler.startElement(name='card', attrs=dcardattrs)
if not c.face_up:
cs = '<%s>' % cs
xmler.endElement(name='card')
xmler.endElement(name='move')
xmler.endElement(name='state')
xmler.endDocument()
ret = out.getvalue()
return ret
def calcBoardString(self): def calcBoardString(self):
game = self.game game = self.game
self.board = '' self.board = ''

View file

@ -50,6 +50,24 @@ class ImportFileTests(unittest.TestCase):
def _successful_import(self, fn, want_s, blurb): def _successful_import(self, fn, want_s, blurb):
self.assertEqual(self._calc_hint(fn).calcBoardString(), want_s, blurb) self.assertEqual(self._calc_hint(fn).calcBoardString(), want_s, blurb)
def _successful_import__XML_test(self, fn, expected_regex, blurb):
hint = self._calc_hint(fn)
xml_output = hint.calcBoardXML()
# import sys
# print(xml_output, file=sys.stderr)
self.assertRegex(
text=xml_output, expected_regex=expected_regex, msg=blurb)
def test_import_XML(self):
return self._successful_import__XML_test(
fn='tests/unit/data/with-10-for-rank.txt',
expected_regex=(
'''<state><move pile="store0" position="0">'''
'''<card id="[0-9]+" rank="four"'''
''' suit="clubs" turn="face-up"></card>'''
),
blurb='xml import worked')
def test_import(self): def test_import(self):
return self._successful_import('tests/unit/data/with-10-for-rank.txt', return self._successful_import('tests/unit/data/with-10-for-rank.txt',
'''FC: - - - - '''FC: - - - -