I agree that many objects in an OO approach suggest themselves, that would be of general use. "piles" that behave as stacks (push/pop) and show the top card or show no cards would be very common for all draw/discard piles. A "hand" of cards that presents the UI would also be good, but not needed for solitare. A deck would be derived from the stack and include shuffle and deal methods.

As for rules... that's the hard part. Writing code that uses these objects is straightforward. But describing it more formally so the objects simply interact the way they are required to? Might not be the way to go, since that's not how they operate in real life. The piles are not cogs in a clock or parts in a simulation; they are dumb data and an outside agent manipulates them and knows all the rules and plays.

However, I like the idea of putting in formal rules of play. This can be done with an expert system and backward-chaining logic. For example, you can only add a card to a pile if specific conditions are met, and this behaves as an exception if the play logic violates it. Then the rules of play are written in procedural logic, but can access the formal rules; e.g. obtain a list of legal discards before deciding which one to play. Then when it does discard (remove card from hand, push onto discard pile) it makes sure that this meets the formal criteria.


In reply to Re: PerlSol (solitaire in Perl) by John M. Dlugosz
in thread PerlSol (solitaire in Perl) by dimmesdale

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.