You could do an object hierarchy where specific cards inherit from an abstract Card class, but I think that's overkill. You're probably right to differentiate between cards based on instance variables. However, I would suggest modifying your Card class such that you separate the internal data, e.g., suit, color, number, from methods for displaying and manipulating cards.

CardModel : ->suit([val]); # returns (and optionally sets) the # suit to value (data type undecided) ->value([val]); # returns (and optionally sets) the # card number to value (1-13,A,K,Q,J) ->isSameColor(CardModel c); ->isSameSuit(CardModel c); CardController: # returns (and optionally sets) the # pile where card is placed ->where(cardmodel, [pile]); # Sets card to be face up/face down ->state(cardmodel, [state]); CardView : # displays card on screen, optionally with the # upper-left coordinate being (x,y) ->show(cardmodel, [x,y]);
Sorry to sound like a Smalltalk textbook, but this way you have a clean separation of responsibilities, rather than lumping them into a single class.

My other suggestion is to introduce the concept of the Set, in addition to the Pile. Recall that in Solitaire you can move cards in groups so long as they conform to the rule of descending, alternating order. This could be implemented best I think as a blessed array.


In reply to Re: PerlSol by djantzen
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.