Here's a quick transformation of that into some code (not complete). Some issues I'm trying to think about now are:

When a value in the card is changed (for example, the x,y coordinates) it needs to be updated (perhaps I should get rid of this and handle it on the Tk/GUI end; possible advantage is that it would extend the module to work with other GUIs or even with text)

Hmm. . . actually, that idea would make the other issues I was working with go away too. Comments?

#!/usr/bin/perl ###################################### # cards.pm -- a perl module to allow # # for the easy creation of solitare # # games (easily expanded to others) # ###################################### package cards; use strict; use warnings; ###################################### # Card type and methods # ###################################### sub new { # usage: $caller->new([\%attr]) # keys for %attr: x_pos, y_pos, # suit, value, where, state, # gif, deck (for future use) my $proto = shift; my $self = shift; my $class = ref($proto) || $proto; bless ($self, $class); return $self; } sub show { # usage: $caller->show([\%pos]) # keys for %pos: x_pos, y_pos # return: hash with keys x_pos, # y_pos (of upper-left corner) my $self = shift; if (@_) { $self->{x_pos} = $_->{x_pos}; $self->{y_pos} = $_->{y_pos}; } return { x_pos => $self->{x_pos}, y_pos => $self->{y_pos} }; } sub suit { # usage: $caller->suit([$suit]) # $suit =~ /[CDHScdhs]/ # return: suit of card my $self = shift; if (@_) { $self->{suit} = shift } return $self->{suit}; } sub value { # usage: $caller->value([$val]) # $val =~ /[1-13AKQJ]/ # return: value of card my $self = shift; if (@_) { $self->{value} = shift } return $self->{value}; } sub where { # usage: $caller->where([$pile]) # $pile is of type Pile # return: Pile reference where # card is located my $self = shift; if (@_) { $self->{where} = shift } return $self->{where}; } sub state { # usage: $caller->state([$state]) # $state =~ (up)|(down) # return: returns state of card my $self = shift; if (@_) { $self->{state} = shift } return $self->{state}; } ###################################### # "Friend" functions for rules # ###################################### sub IsAlternatingColor { # usage: IsAlternatingColor(@cards) # where for all i less than $#cards # $cards[i] is of type Card # return: true or false (1 or 0) my $cards = shift; # A zero represents black, a 1 red; orignially set # $old_color to opposite of that of the first card my $old_color = $cards->[0]->suit() =~ /CScs/ ? 1 : 0; my $cur_color; for (@$cards) { $cur_color = $_->suit() =~ /CScs/ ? 0 : 1; return 0 if $cur_color = $old_color; } return 1; } sub IsSameColor { # usage: IsSameColor(@cards) # where for all i less than $#cards # $cards[i] is of type Card # return: true or false (1 or 0) my $cards = shift; # A zero represents black, a 1 red; orignially set # $old_color to the same of that of the first card my $old_color = $cards->[0]->suit() =~ /CScs/ ? 0 : 1; my $cur_color; for (@$cards) { $cur_color = $_->suit() =~ /CScs/ ? 0 : 1; return 0 if $cur_color != $old_color; } return 1; }

In reply to Re: PerlSol (solitaire in Perl) by dimmesdale
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.