File Coverage

blib/lib/Games/Euchre/Team.pm
Criterion Covered Total %
statement 6 51 11.7
branch n/a
condition 0 6 0.0
subroutine 2 16 12.5
pod 14 14 100.0
total 22 87 25.2


line stmt bran cond sub pod time code
1             package Games::Euchre::Team;
2              
3             =head1 NAME
4              
5             Games::Euchre::Team - Team class for Euchre card game
6              
7             =head1 DESCRIPTION
8              
9             The Team object is used to keep track of bidding information and
10             current score for the two teams in a Euchre game.
11              
12             =cut
13              
14 1     1   5 use strict;
  1         2  
  1         31  
15 1     1   6 use warnings;
  1         1  
  1         494  
16              
17             =head1 CLASS METHODS
18              
19             =over 4
20              
21             =item new GAME NUMBER NAME PLAYER1 PLAYER2
22              
23             Create and initialize a new Euchre team for the specified game. The
24             number is "1" or "2". The name is a string used to identify this
25             team. The players are instantiated Games::Euchre::Player instances.
26              
27             =cut
28              
29             sub new {
30 0     0 1   my $pkg = shift;
31 0           my $game = shift;
32 0           my $number = shift; # 1-based
33 0           my $name = shift;
34 0           my $player1 = shift;
35 0           my $player2 = shift;
36 0           return bless({
37             game => $game,
38             number => $number,
39             name => $name,
40             players => [$player1,$player2],
41             points => undef,
42             tricks => undef,
43             otherTeam => undef,
44             }, $pkg);
45             }
46              
47             =back
48              
49             =head1 INSTANCE METHODS
50              
51             =over 4
52              
53             =item getGame
54              
55             Return the Euchre game instance to which this team belongs.
56              
57             =cut
58              
59             sub getGame {
60 0     0 1   my $self = shift;
61 0           return $self->{game};
62             }
63              
64             =item getOtherTeam
65              
66             Returns the team recorded in the setOtherTeam() method.
67              
68             =cut
69              
70             sub getOtherTeam {
71 0     0 1   my $self = shift;
72             # Other team's 0-based index is this team's 1-based number mod 2
73             # i.e. 1 -> 1, 2-> 0
74 0           return ($self->getGame()->getTeams())[$self->getNumber()%2];
75             }
76              
77             =item addScore SCORE
78              
79             Increment this team's game score by this amount.
80              
81             =cut
82              
83             sub addScore {
84 0     0 1   my $self = shift;
85 0           my $score = shift;
86 0           $self->{points} += $score;
87 0           return $self;
88             }
89              
90             =item addTrick
91              
92             Increment this team's trick count by one.
93              
94             =cut
95              
96             sub addTrick {
97 0     0 1   my $self = shift;
98 0           $self->{tricks}++;
99 0           return $self;
100             }
101              
102             =item getNumber
103              
104             Return this team's number, between 1 and 2
105              
106             =cut
107              
108             sub getNumber {
109 0     0 1   my $self = shift;
110 0           return $self->{number};
111             }
112              
113             =item getName
114              
115             Return the team name.
116              
117             =cut
118              
119             sub getName {
120 0     0 1   my $self = shift;
121 0           return $self->{name};
122             }
123              
124             =item getPlayers
125              
126             Return an array of the two players on this team.
127              
128             =cut
129              
130             sub getPlayers {
131 0     0 1   my $self = shift;
132 0           return @{$self->{players}};
  0            
133             }
134              
135             =item getScore
136              
137             Return the current game score for this team.\
138              
139             =cut
140              
141             sub getScore {
142 0     0 1   my $self = shift;
143 0           return $self->{points};
144             }
145              
146             =item getTricks
147              
148             Return the number of tricks won by this team in the current hand.
149              
150             =cut
151              
152             sub getTricks {
153 0     0 1   my $self = shift;
154 0           return $self->{tricks};
155             }
156              
157             =item wentAlone
158              
159             Returns a boolean indicating whether a member of this team chose to go
160             alone on a bid.
161              
162             =cut
163              
164             sub wentAlone {
165 0     0 1   my $self = shift;
166 0           my $alone = undef;
167 0           foreach my $player ($self->getPlayers()) {
168 0   0       $alone ||= $player->wentAlone();
169             }
170 0           return $alone;
171             }
172              
173             =item isBidder
174              
175             Returns a boolean indicating whether a member of this team called the
176             trump suit during bidding.
177              
178             =cut
179              
180             sub isBidder {
181 0     0 1   my $self = shift;
182 0           my $bid = undef;
183 0           foreach my $player ($self->getPlayers()) {
184 0   0       $bid ||= $player->isBidder();
185             }
186 0           return $bid;
187             }
188              
189             =item resetGame
190              
191             Clear all of the state for the current game and get ready for the next one.
192              
193             =cut
194              
195             sub resetGame {
196 0     0 1   my $self = shift;
197 0           $self->{points} = 0;
198 0           return $self->resetHand();
199             }
200              
201             =item resetHand
202              
203             Clear all of the state for the current hand and get ready for the next one.
204              
205             =cut
206              
207             sub resetHand {
208 0     0 1   my $self = shift;
209 0           $self->{tricks} = 0;
210 0           return $self;
211             }
212              
213             1;
214             __END__