#!/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; }