Games-Euchre

 view release on metacpan or  search on metacpan

lib/Games/Euchre/Trick.pm  view on Meta::CPAN

package Games::Euchre::Trick;

=head1 NAME

Games::Euchre::Trick - Trick class for Euchre card game

=head1 DESCRIPTION

Only one Trick instance is alive at one time per Euchre game.  The
Trick keeps track of which cards have been played, and provides useful
functions to determine which cards are legal plays, as well as who
is the winner of the trick.  The trick class makes the determination
of which card beats which card, given the current trump and lead.  The
trick class knows how to handle an alone hand and it calls the
playCard() method for each player in turn in it's play() method,
usually called from the Games::Euchre->playHand() method.

=cut

use strict;
use warnings;
use Games::Cards;

=head1 CLASS METHODS

=over 4

=item new GAME LEAD NAME NUMBER

Create and initialize a new Euchre trick.  The lead is a
Games::Euchre::Player instance.  The name is any string.  The number
is a one-based index of which trick this is (from 1 to 5).

=cut

sub new {
   my $pkg = shift;
   my $game = shift;
   my $lead = shift;
   my $name = shift;
   my $number = shift;  # 1-based
   my $self =  bless({
      game => $game,
      name => $name,
      number => $number,
      players => [$game->getPlayers()],
      hand => Games::Cards::Queue->new($game->{game}, $name),
      play => 0,
      leadIndex => undef,
   }, $pkg);
   for (my $i=$#{$self->{players}}; $i >= 0; $i--) {
      my $player = $self->{players}->[$i];
      if ((!$player->wentAlone()) && $player->getTeam()->wentAlone()) {
         # Remove teammate of alone-goer
         splice @{$self->{players}}, $i, 1;
      }
   }

   for (my $i=0; $i < @{$self->{players}}; $i++) {
      last if ($lead->getName() eq $self->{players}->[0]->getName());
      push @{$self->{players}}, shift(@{$self->{players}}); # rotate
   }
   return $self;
}

=back

=head1 CLASS METHODS



( run in 1.146 second using v1.01-cache-2.11-cpan-14f38c9f855 )