| Games-Solitaire-Verify documentation | Contained in the Games-Solitaire-Verify distribution. |
Games::Solitaire::Verify::Card - a class wrapper for an individual Solitaire card.
Version 0.0101
use Games::Solitaire::Verify::Card;
# Initialise a Queen-of-Hearts
my $queen_of_hearts = Games::Solitaire::Verify::Card->new(
{
string => "QH",
},
);
Returns the expected sequence of the suits - "H", "S", "C", "D".
Calculates the numerical rank of the string passed as argument.
Example:
my $ten = Games::Solitaire::Verify::Card->calc_rank("T")
# Prints 10.
print "$ten\n";
Same as calc_rank only supporting "0" as the zero'th card.
Returns the rank of the card as an integer. Ace is 1, 2-10 are 2-20; J is 11, Q is 12 and K is 13.
Returns "H", "C", "D" or "S" depending on the suit.
Returns "red" or "black" depending on the rank of the card.
Clones the card into a new copy.
Converts the card to a string representation.
Converts the rank to a string.
Shlomi Fish, <shlomif at iglu.org.il>
Please report any bugs or feature requests to bug-games-solitaire-verifysolution-move at rt.cpan.org, or through
the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Solitaire-Verify. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc Games::Solitaire::Verify::Card
You can also look for information at:
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-Solitaire-Verify
Copyright 2008 Shlomi Fish.
This program is released under the following license: MIT/X11 ( http://www.opensource.org/licenses/mit-license.php ).
| Games-Solitaire-Verify documentation | Contained in the Games-Solitaire-Verify distribution. |
package Games::Solitaire::Verify::Card; use warnings; use strict;
our $VERSION = '0.0901'; use base 'Games::Solitaire::Verify::Base'; use Games::Solitaire::Verify::Exception; __PACKAGE__->mk_acc_ref([qw( rank suit _game )]);
sub _card_num_normalize { my $arg = shift; if (ref($arg) eq "") { return +{ map { $_ => $arg } (qw(t non_t)) }; } else { return $arg } } my @card_nums = (map { _card_num_normalize($_) } ("A", (2 .. 9), { 't' => "T", 'non_t' => "10", }, , "J", "Q", "K")); my %ranks_map = (map { $card_nums[$_]->{t} => ($_+1) } (0 .. $#card_nums)); my @suits_map_proto = ( ["H" => { name => "hearts", color => "red", },], ["C" => { name => "clubs", color => "black", },], ["D" => { name => "diamonds", color => "red", },], ["S" => { name => "spades", color => "black", },], ); my %suits_map = (map {@$_} @suits_map_proto);
sub get_suits_seq { my $class = shift; return [map { $_->[0] } @suits_map_proto]; }
sub calc_rank { my ($self, $s) = @_; return $ranks_map{$s}; }
sub calc_rank_with_0 { my ($self, $str) = @_; if ($str eq "0") { return 0; } else { return $self->calc_rank($str); } } sub _from_string { my ($self, $str) = @_; if (length($str) != 2) { Games::Solitaire::Verify::Exception::Parse::Card->throw( error => "string length is too long", ); } my ($rank, $suit) = split(//, $str); if (! defined($self->rank($self->calc_rank($rank)))) { Games::Solitaire::Verify::Exception::Parse::Card::UnknownRank->throw( error => "unknown rank", ); } if (exists($suits_map{$suit})) { $self->suit($suit); } else { Games::Solitaire::Verify::Exception::Parse::Card::UnknownSuit->throw( error => "unknown suit", ); } } sub _init { my ($self, $args) = @_; if (exists($args->{string})) { return $self->_from_string($args->{string}); } }
sub color { my ($self) = @_; return $suits_map{$self->suit()}->{'color'}; }
sub clone { my $self = shift; my $new_card = Games::Solitaire::Verify::Card->new(); $new_card->suit($self->suit()); $new_card->rank($self->rank()); return $new_card; }
sub to_string { my $self = shift; return $self->rank_to_string($self->rank()) . $self->suit(); }
sub rank_to_string { my ($class, $rank) = @_; if ($rank == 0) { return '0'; } else { return $card_nums[$rank-1]->{t}; } }
1; # End of Games::Solitaire::Verify::Move