blib/lib/Games/Cards.pm

filter: Statementstmt 47.3% 135 / 285 Branchbran 26.3% 19 / 72 Conditioncond 22.8% 8 / 35 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 44.0% 26 / 59 Podpod n/a totaltotal 39.6% 188 / 474 CC 57 SCAR 23.0
CC
57
Cov
41%
print
56.0
index
30.0
print
30.0
CRAP 10.0
« blib/lib/Games/Cards/Undo.pm ↑ Summary blib/lib/Games/Cards/Tk.pm »
1 package Games::Cards;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Games::Cards -- Perl module for writing and playing card games
8  
9 =head1 SYNOPSIS
10  
11 use Games::Cards;
12 my $Rummy = new Games::Cards::Game;
13  
14 # Create the correct deck for a game of Rummy.
15 my $Deck = new Games::Cards::Deck ($Rummy, "Deck");
16  
17 # shuffle the deck and create the discard pile
18 $Deck->shuffle;
19 my $Discard = new Games::Cards::Queue "Discard Pile";
20  
21 # Deal out the hands
22 foreach my $i (1 .. 3) {
23 my $hand = new Games::Cards::Hand "Player $i" ;
24 $Deck->give_cards($hand, 7);
25 $hand->sort_by_value;
26 push @Hands, $hand;
27 }
28  
29 # print hands (e.g. "Player 1: AS 2C 3C 3H 10D QS KH")
30 foreach (@Hands) { print ($_->print("short"), "\n") }
31
32 $Hands[1]->give_a_card ($Discard, "8D"); # discard 8 of diamonds
33  
34 =head1 DESCRIPTION
35  
36 This module creates objects and methods to allow easier programming of card
37 games in Perl. It allows you to do things like create decks of cards,
38 have piles of cards, hands, and other sets of cards, turn cards face-up
39 or face-down, and move cards from one set to another. Which is pretty much
40 all you need for most card games.
41  
42 Sub-packages include:
43  
44 =over 4
45  
46 =item Games::Cards::Undo
47  
48 This package handles undoing and redoing moves (important for solitaire).
49  
50 =item and Games::Cards::Tk
51  
52 This package allows you to write games that use a Tk graphical interface.
53 It's designed so that it will be relatively easy to write a game that uses
54 i<either> a GUI or a simple text interface, depending on the player's
55 circumstances (availability of Tk, suspicious boss, etc.). See
56 L<Games::Cards::Tk> for more details on writing Tk games.
57  
58 =back
59  
60 =head2 Quick Overview of Classes
61  
62 A GC::Game stores information like what cards are in the starting deck,
63 plus pointers to the various Cards and CardSets.
64  
65 A GC::Card represents one playing card. Every Card must belong to one
66 (and only one) CardSet at every point during the game.
67  
68 A GC::CardSet is mostly just a set of GC::Cards. A CardSet has a unique
69 name. Many also have short nicknames, which make it easier to write games
70 that move cards between the sets. (See Klondike or FreeCell, for example.)
71  
72 There are many sorts of CardSet. The basic differentiation is Piles,
73 for which you only access the top or bottom card (or cards) and Hands,
74 where you might access any one of the cards in the Hand. Piles are
75 broken up into Stacks and Queues, and every game starts with a Deck of
76 cards (or more than one).
77  
78 =cut
79  
80 # TODO get rid of size, have cards return wantarray ? array of cards : size
81 #
82 # TODO Games::Cards::Undo::Exists. If not defined, don't bother calling
83 # GC::Undo::store etc. on every turn. Then each game can "use GCU" or not.
84  
85 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 require 5.004; # I use 'foreach my'
88  
89 # Stolen from `man perlmod`
90 2 $VERSION = do { my @r = (q$Revision: 1.45 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
91  
92 # Handle undoing/redoing moves
93 2 ▶use Games::Cards::Undo;
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 # sub-packages
96 {
97 package Games::Cards::Game;
98  
99 package Games::Cards::Deck;
100 package Games::Cards::Queue;
101 package Games::Cards::Stack;
102 package Games::Cards::Pile;
103 package Games::Cards::Hand;
104 package Games::Cards::CardSet;
105  
106 package Games::Cards::Card;
107 }
108  
109  
110 =head2 Class Games::Cards::Game
111  
112 This class represents a certain game, like War, or Solitaire. This is
113 necessary to store the various rules for a given game, like the ranking
114 of the cards. (Or, for more exotic games, how many cards of what type are
115 in the deck.) Methods:
116  
117 =over 4
118  
119 =cut
120  
121 {
122 2 package Games::Cards::Game;
123 # suits is a reference to an array listing the suits in the deck
124 # cards_in_suit is a reference to a hash whose keys are the names of the
125 # cards in each suit, and values are the (default) values of those cards
126 # (Card names will be strings, although they might be "2". Values are
127 # integers, so that we can compare cards with other cards.)
128 #
129 # cardset_by_nickname is a hash whose keys are short (unique) nicknames and
130 # values are the CardSets (e.g., player's Hands, Piles, etc.) so nicknamed
131 # cardset_by_name is the same with the CardSet names
132 # card_by_truename stores Cards via their truenames. (See Card::truename)
133  
134 2 my $Default_Suits = [qw(Clubs Diamonds Hearts Spades)];
135 # (Parts of) this hash will need to be reset in lots of games.
136 2 my $Default_Cards_In_Suit = {
137 "Ace" => 1,
138 2 => 2,
139 3 => 3,
140 4 => 4,
141 5 => 5,
142 6 => 6,
143 7 => 7,
144 8 => 8,
145 9 => 9,
146 10 => 10,
147 "Jack" => 11,
148 "Queen" => 12,
149 "King" => 13,
150 };
151  
152 =item current_game
153  
154 Returns the current Game object. In almost every case, you'll only be
155 working with one at a time.
156  
157 =item set_current_game(GAME)
158  
159 In theory, these subs let you handle multiple Games at once, as long
160 as you set_current_game to the right one. Note that Game->new automatically
161 sets the current Game to be that game, so in 99% of cases, you won't have to
162 call set_current_game.
163  
164 =cut
165  
166 2 my $_Current_Game;
167 0 ▶sub current_game { return $_Current_Game; }
Subroutine current_game: not called CC=1 SCAR=6.9
168 1 ▶sub set_current_game {$_Current_Game = shift;}
Subroutine set_current_game: called CC=1 SCAR=0.0
169  
170 =item new(HASHREF)
171  
172 creates a new game. HASHREF is a reference to a hash containing zero or more
173 of the keys "suits" and "cards_in_suit". "suits" is a list of the suits in a
174 deck, "cards_in_suit" is a reference to a hash whose keys are the names
175 of the cards in one suit and whose values are the values (or ranks) of those
176 cards. If "suits" is not given, the default suits (Clubs, Diamonds, Hearts,
177 Spades) are used. If "cards_in_suit" is not given, the default cards
178 (Ace, 2..10, Jack, Queen, King with values 1..13) are used.
179 For example, war would require "Ace"=>14.
180  
181 =cut
182  
183 1 ▶ sub new {
Subroutine new: called CC=3 SCAR=12.0
184 1 my $class = shift;
185 1 my $hashref = shift;
186 my $cardgame = {
187 "suits" => $hashref->{"suits"} || $Default_Suits,
188 1 ▶ "cards_in_suit" => $hashref->{"cards_in_suit"} ||
Condition33%2/6
$hashref->{'suits'} || $Default_Suits
l!l&&r!l&&!r
010
$hashref->{'cards_in_suit'} || $Default_Cards_In_Suit
l!l&&r!l&&!r
100
MC/DC0%0/2
$hashref->{'suits'} || $Default_Suits
$hashref->{'suits'} $Default_Suits
MC/DC0%0/2
$hashref->{'cards_in_suit'} || $Default_Cards_In_Suit
$hashref->{'cards_in_suit'} $Default_Cards_In_Suit
Truth table33%1/3
A || B
A= $hashref->{'suits'}B= $Default_Suits
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $hashref->{'cards_in_suit'}B= $Default_Cards_In_Suit
ABresult
1X1
011
000
189 $Default_Cards_In_Suit,
190 "cardset_by_name" => {},
191 "cardset_by_nickname" => {},
192 };
193  
194 1 bless $cardgame, $class;
195 # For now, this game will be the current game
196 1 $cardgame->set_current_game;
197  
198 1 return $cardgame;
199 } # end sub Games::Cards::Game::new
200  
201 # Store a CardSet. Use separate hashes for cardset's name and nickname,
202 # for convenience.
203 5 ▶ sub store_cardset {
Subroutine store_cardset: called CC=2 SCAR=7.6
204 5 my ($self, $cardset) = @_;
205 5 $self->{"cardset_by_name"}->{$cardset->name} = $cardset;
206 5 ▶ if (defined (my $nick = $cardset->nickname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $nick = $cardset->nickname)) 0 5 5
207 0 $self->{"cardset_by_nickname"}->{$nick} = $cardset;
208 }
209 }
210  
211 =item get_cardset_by_name(NAME)
212  
213 Returns the CardSet with name NAME
214  
215 =cut
216  
217 0 ▶ sub get_cardset_by_name {
Subroutine get_cardset_by_name: not called CC=2 SCAR=17.9
218 0 my ($self, $name) = @_;
219 0 ▶ if (exists ($self->{"cardset_by_name"}->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'cardset_by_name'}{$name}) { } 0 0 0
220 0 return $self->{"cardset_by_name"}->{$name};
221 } else {
222 0 return undef;
223 }
224 }
225  
226 =item get_cardset_by_nickname(NAME)
227  
228 Returns the CardSet with nickname NAME
229  
230 =cut
231  
232 0 ▶ sub get_cardset_by_nickname {
Subroutine get_cardset_by_nickname: not called CC=2 SCAR=17.9
233 0 my ($self, $nickname) = @_;
234 0 ▶ if (exists ($self->{"cardset_by_nickname"}->{$nickname})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'cardset_by_nickname'}{$nickname}) { } 0 0 0
235 0 return $self->{"cardset_by_nickname"}->{$nickname};
236 } else {
237 0 return undef;
238 }
239 }
240  
241 # Store a Card
242 52 ▶ sub store_card {
Subroutine store_card: called CC=1 SCAR=0.0
243 52 my ($self, $card) = @_;
244 52 my $truename = $card->truename;
245 52 $self->{"card_by_truename"}->{$truename} = $card;
246 }
247  
248 =item get_card_by_truename(NAME)
249  
250 Returns the Card with truename NAME
251  
252 =cut
253  
254 0 ▶ sub get_card_by_truename {
Subroutine get_card_by_truename: not called CC=2 SCAR=17.9
255 0 my ($self, $truename) = @_;
256 0 ▶ if (exists ($self->{"card_by_truename"}->{$truename})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'card_by_truename'}{$truename}) { } 0 0 0
257 0 return $self->{"card_by_truename"}->{$truename};
258 } else {
259 0 return undef;
260 }
261 }
262  
263 } # end package Games::Cards::Game
264  
265 ######################################################################
266 # CardSet and its subclasses
267  
268 =head2 Games::Cards::Deck
269  
270 A deck is a deck of cards. The number of cards and identities of the cards in
271 the deck depend on the particular Game for which the deck is used.
272  
273 =over 4
274  
275 =cut
276  
277 {
278 2 package Games::Cards::Deck;
279 2 @Games::Cards::Deck::ISA = qw (Games::Cards::Queue);
280  
281 =item new (GAME, NAME)
282  
283 creates an I<unshuffled> deck of cards. For each card in the deck it creates
284 a name, suit, value, and suit value. GAME is the GC::Game this deck
285 belongs to, and it stipulates the number of cards in the deck, etc. NAME is the
286 name to give the deck, e.g. "Deck".
287  
288 =back
289  
290 =cut
291  
292 1 ▶ sub new {
Subroutine new: called CC=4 SCAR=13.9
293 1 my ($class, $game, $deckname) = @_;
294 0 ▶ if (ref($class)) {$class = ref($class)}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 1 1
295 # This allows us to get Tk or non-Tk automatically
296 1 (my $qclass = $class) =~ s/::Deck/::Queue/;
297 1 my $deck = $qclass->new($game, $deckname);
298 1 my %cards = %{$game->{"cards_in_suit"}};
299  
300 # make an unshuffled deck
301 1 (my $cclass = $class) =~ s/::Deck/::Card/;
302 1 foreach my $suit_value (1..@{$game->{"suits"}}) {
303 4 my $suit = $game->{"suits"}->[$suit_value-1];
304 4 foreach my $name (keys %cards) {
305 my $arg = {
306 "suit"=>$suit, "name"=> $name,
307 52 "suit_value" => $suit_value, "value" => $cards{$name}
308 };
309 52 my $new_card = $cclass->new($game, $arg);
310 52 push @{$deck->{"cards"}}, $new_card;
311 52 $new_card->set_owning_cardset($deck);
312 }
313 }
314  
315 1 bless $deck, $class;
316 } # end sub Games::Cards::Deck::new
317 } # end package Games::Cards::Deck
318  
319 =head2 Class Games::Cards::Queue
320  
321 A Queue (cf. computer science terminology, or the C++ stdlib) is a first-in
322 first-out pile of cards. Cards are removed from the top of the pile, but new
323 cards are added to the bottom of the pile. This might represent, say, a pile
324 of face-down cards, like the player's hand in War.
325  
326 =cut
327  
328 {
329 2 package Games::Cards::Queue;
330 # cards array has 0 as the top card, -1 as the bottom card (opposite of Queue,
331 # for convenience when moving cards from a Queue to a stack or vice versa).
332 # We push to add cards, but shift to remove cards.
333 2 @Games::Cards::Queue::ISA = qw(Games::Cards::Pile);
334  
335 # inherit SUPER::new
336  
337 274 ▶ sub remove_cards {
Subroutine remove_cards: called CC=1 SCAR=0.0
338 # remove (and return a ref to) top arg1 cards from the Queue
339 274 my ($thing, $number) = @_;
340 274 return $thing->splice (0, $number);
341 } # end sub Games::Cards::Queue::remove_cards
342  
343 238 ▶ sub add_cards {
Subroutine add_cards: called CC=1 SCAR=0.0
344 # Add array of Cards arg1 to the Queue
345 238 my ($thing, $cards) = @_;
346 238 $thing->splice ($thing->size, 0, $cards);
347 } # end sub Games::Cards::Queue::add_cards
348  
349 0 ▶ sub top_card {
Subroutine top_card: not called CC=2 SCAR=17.9
350 0 my $set = shift;
351 0 ▶ return $set->size ? $set->{"cards"}->[0] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$set->size ? : 0 0 0
352 } # end sub Games::Cards::Queue::top_card
353  
354 0 ▶ sub print_ordered_cards {
Subroutine print_ordered_cards: not called CC=1 SCAR=6.9
355 # returns the cards in the set in the correct order to be printed
356 0 return shift->{"cards"};
357 } # end sub Games::Cards::Queue::print_ordered_cards
358  
359 } #end package Games::Cards::Queue
360  
361 =head2 Class Games::Cards::Stack
362  
363 A stack (cf. computer science terminology, or the C++ stdlib) is a last-in
364 first-out pile of cards. Cards are removed from the top of the pile, and new
365 cards are also added to the top of the pile. This would usually represent a
366 pile of cards with its top card (and perhaps all cards) face up.
367  
368 =cut
369  
370 {
371 2 package Games::Cards::Stack;
372 # cards array has -1 as the top card, 0 as the bottom card (opposite of Queue,
373 # for convenience when moving cards from a Queue to a stack or vice versa).
374 # We only access the top of the stack, pushing to add and popping to remove.
375 2 @Games::Cards::Stack::ISA = qw(Games::Cards::Pile);
376  
377 # inherit SUPER::new
378  
379 236 ▶ sub remove_cards {
Subroutine remove_cards: called CC=1 SCAR=0.0
380 # remove (and return a ref to) top arg1 cards from the Stack
381 236 my ($thing, $number) = @_;
382 236 return $thing->splice (-$number);
383 } # end sub Games::Cards::Stack::remove_cards
384  
385 272 ▶ sub add_cards {
Subroutine add_cards: called CC=1 SCAR=0.0
386 # Add array of Cards arg1 to the Stack
387 272 my ($thing, $cards) = @_;
388 272 $thing->splice($thing->size, 0, $cards);
389 } # end sub Games::Cards::Stack::add_cards
390  
391 254 ▶ sub top_card {
Subroutine top_card: called CC=2 SCAR=7.2
392 254 my $set = shift;
393 254 ▶ return $set->size ? $set->{"cards"}->[-1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$set->size ? : 254 0 254
394 } # end sub Games::Cards::Stack::top_card
395  
396 # Use "reverse" to print the top card of the Set first
397 # (makes for easier reading when lists are long, since you usually
398 # care more about the next card to be played)
399 0 ▶ sub print_ordered_cards {
Subroutine print_ordered_cards: not called CC=1 SCAR=6.9
400 # returns the cards in the set in the correct order to be printed
401 0 return [reverse (@{shift->{"cards"}})];
402 } # end sub Games::Cards::Queue::print_ordered_cards
403  
404 } #end package Games::Cards::Stack
405  
406 #####################
407  
408 =head2 Class Games::Cards::Pile
409  
410 A Pile is a pile of cards. That is, it is a CardSet where we will only access
411 the beginning or end of the set. (This may include the first N cards in the
412 set, but we will never reference the 17'th card.) This is a super class of
413 Queue and Stack, and those classes should be used instead, so that we know
414 whether the cards in the pile are FIFO or LIFO. Methods:
415  
416 =over 4
417  
418 =cut
419  
420 {
421 2 package Games::Cards::Pile;
422 # The cards array is LIFO for the Stack subclass and FIFO for the Queue
423 # subclass. We always push things onto Queues or Stacks, but
424 # we use "pop", for Stacks, and "shift" for the Queues.
425  
426 2 @Games::Cards::Pile::ISA = qw(Games::Cards::CardSet);
427 # inherit SUPER::new
428  
429 =item give_cards(RECEIVER, NUMBER)
430  
431 Transfers NUMBER cards from the donor (the object on which this method was
432 called) to the CardSet RECEIVER. This method can used for dealing cards from
433 a deck, giving cards to another player (Go Fish), putting cards on the table
434 (War), or transferring a card or cards between piles in solitaire.
435  
436 If NUMBER is "all", then the donor gives all of its cards.
437  
438 Returns 1 usually. If the donor has too few cards, it returns 0 and does not
439 transfer any cards.
440  
441 =cut
442  
443 511 ▶ sub give_cards {
Subroutine give_cards: called CC=3 SCAR=11.0
444 #TODO if called with a subref instead of a scalar, then sort the
445 #cards to the top of the Set using the sub, and then set $number!
446  
447 # If we're going from a Stack to a Queue, we would normally need to flip
448 # the stack of cards over. E.g. if you deal three cards from the stock to
449 # the waste pile in Solitaire, the top card of the stock becomes the
450 # *bottom* card of the waste. However, the cards arrays in Stacks and
451 # Queues are stored in opposite directions, so this works automatically!
452 # If we're giving to a Hand, which doesn't have a top card, it doesn't
453 # matter
454  
455 511 my ($donor, $receiver) = (shift, shift);
456 511 my $number = shift;
457 511 ▶ $number = $donor->size if $number eq "all";
Branch: 1 branch
BranchTrueFalseTotal
if $number eq "all" 236 275 511
458  
459 # Remove the cards if we can
460 511 ▶ if ($donor->size < $number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($donor->size < $number) 1 510 511
461 #print $donor->{"name"} . " is out of cards\n";
462 1 return 0;
463 }
464 510 my $cards_ref = $donor->remove_cards($number);
465 #print $donor->{"name"}, " gives ";
466 #print map {$_->print("short")} @$cards_ref;
467 #print " to ", $receiver->{"name"}, "\n";
468  
469 # Add the cards
470 510 $receiver->add_cards($cards_ref);
471  
472 510 return 1;
473 } # end sub Games::Cards::Pile::give_cards
474  
475  
476 =item top_card
477  
478 Returns the top Card in the CardSet (or 0 if CardSet is empty)
479  
480 =cut
481  
482 # This sub is actually found in the subclasses, since their
483 # arrays are stored in different orders
484 } #end package Games::Cards::Pile
485  
486 #####################
487  
488 =head2 Class Games::Cards::Hand
489  
490 A Hand represents a player's hand. Most significantly, it's a CardSet which
491 is different from a Pile because the Cards in it are unordered. We may
492 reference any part of the CardSet, not just the top or bottom.
493 Methods:
494  
495 =over 4
496  
497 =cut
498  
499 {
500 2 package Games::Cards::Hand;
501  
502 2 @Games::Cards::Hand::ISA = qw(Games::Cards::CardSet);
503 # Use SUPER::new
504  
505 =item give_a_card(RECEIVER, DESCRIPTION)
506  
507 Transfers Card described by DESCRIPTION from the donor (the Hand on which
508 this method was called) to the CardSet RECEIVER. This method can used for
509 discarding a card from a hand, e.g.
510  
511 If DESCRIPTION matches /^-?\d+$/, then it is the index in the cards array of the
512 Card to give. Otherwise, DESCRIPTION is passed to Hand::index.
513  
514 Returns 1 usually. If the donor does not have the card, it returns 0 and does
515 not transfer anything.
516  
517 =cut
518  
519 0 ▶ sub give_a_card {
Subroutine give_a_card: not called CC=4 SCAR=30.0
520 0 my ($donor, $receiver) = (shift, shift);
521 0 my $description = shift;
522  
523 # Which card to remove?
524 0 ▶ my $donor_index = $description =~ /^-?\d+$/ ?
Branch: 1 branch
BranchTrueFalseTotal
$description =~ /^-?\d+$/ ? : 0 0 0
525 $description :
526 $donor->index($description);
527  
528 0 ▶ unless (defined $donor_index && $donor_index < $donor->size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $donor_index and $donor_index < $donor->size) 0 0 0
Condition0%0/3
defined $donor_index and $donor_index < $donor->size
!ll&&!rl&&r
000
MC/DC0%0/2
defined $donor_index and $donor_index < $donor->size
defined $donor_index $donor_index < $donor->size
Truth table0%0/3
A and B
A= defined $donor_indexB= $donor_index < $donor->size
ABresult
0X0
100
111
529 #print $donor->name . " does not have that card\n";
530 0 return;
531 }
532  
533 # Remove the card
534 0 my $card_ref = $donor->remove_a_card($donor_index);
535 #print $donor->name, " gives ";
536 #print map {$_->print("short") . " "} @$cards_ref;
537 #print " to ", $receiver->name, "\n";
538  
539 # Add the card
540 0 $receiver->add_cards([$card_ref]); # add_cards takes an array ref
541  
542 0 return 1;
543 } # end sub Games::Cards::Hand::give_card
544  
545 =item move_card(DESCRIPTION, INDEX)
546  
547 Rearrange a Hand by putting Card described by DESCRIPTION at index INDEX.
548  
549 If DESCRIPTION matches /^-?\d+$/, then it is the index in the cards array of the
550 Card to give. Otherwise, DESCRIPTION is passed to Hand::index.
551  
552 Returns 1 usually. If the donor does not have the card, it returns 0 and does
553 not transfer anything.
554  
555 =cut
556  
557 0 ▶ sub move_card {
Subroutine move_card: not called CC=3 SCAR=24.8
558 0 my $hand = shift;
559 0 my ($description, $final) = @_;
560  
561 # Which card to remove?
562 0 ▶ my $initial = $description =~ /^-?\d+$/ ?
Branch: 1 branch
BranchTrueFalseTotal
$description =~ /^-?\d+$/ ? : 0 0 0
563 $description :
564 $hand->index($description);
565  
566 # don't have that card!
567 0 ▶ return unless defined $initial;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $initial 0 0 0
568  
569 # Remove the card
570 0 my $card_ref = $hand->remove_a_card($initial);
571  
572 # Add the card
573 0 $hand->add_a_card($card_ref, $final);
574  
575 0 return 1;
576 } # end sub Games::Cards::Hand::move_card
577  
578 0 ▶ sub remove_a_card {
Subroutine remove_a_card: not called CC=1 SCAR=6.9
579 # remove (and return a ref to an array with) card number arg1 of the Hand
580 0 my ($thing, $number) = @_;
581 # splice returns an array ref
582 0 my $listref = $thing->splice ($number,1);
583 0 return $listref->[0];
584 } # end sub Games::Cards::Stack::remove_cards
585  
586 0 ▶ sub add_a_card {
Subroutine add_a_card: not called CC=1 SCAR=6.9
587 # add card arg1 at position arg2 number arg1 of the Hand arg0
588 0 my ($thing, $card, $number) = @_;
589 0 $thing->splice ($number,0,[$card]);
590 } # end sub Games::Cards::Stack::remove_cards
591  
592 0 ▶ sub add_cards {
Subroutine add_cards: not called CC=1 SCAR=6.9
593 # Add array of Cards arg1 to the Hand
594 # This sub is called by Pile::give_cards & doesn't care where in the
595 # Hand the cards end up. So just put 'em at the end
596 0 my ($thing, $cards) = @_;
597 0 $thing->splice($thing->size, 0, $cards);
598 } # end sub Games::Cards::Hand::add_cards
599  
600 =item index(DESCRIPTION)
601  
602 Given a card description DESCRIPTION return the index of that Card
603 in the Hand, or undef if it was not found. DESCRIPTION may be a Card or
604 a string (like "8H" or "KC").
605  
606 =cut
607  
608 0 ▶ sub index {
Subroutine index: not called CC=5 SCAR=34.0
609 # Depending on the nature of the description arg1, we create a sub
610 # to match that description with a Card. Then we search among the
611 # cards in Hand arg0's cards array with that sub
612 0 my ($set, $description) = @_;
613 0 my $number;
614 my $find; # sub whose arg0 is a card to compare to
615  
616 0 ▶ if (ref $description eq "Games::Cards::Card") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $description eq 'Games::Cards::Card') { } 0 0 0
elsif ($description =~ /^[\dakqj]+[chds]/i) { } 0 0 0
617 0 ▶ $find = sub {shift == $description};
Subroutine __ANON__: not called CC=1 SCAR=6.9
618  
619 # but it matches 2-10 or AKQJ of CHDS
620 # TODO need to change this for multiple decks!
621 } elsif ($description =~ /^[\dakqj]+[chds]/i) {
622 0 ▶ $find = sub {shift->truename eq uc($description)};
Subroutine __ANON__: not called CC=1 SCAR=6.9
623 } else {
624 0 my $caller = (caller(0))[3];
625 0 die "$caller called with unknown description $description\n";
626 }
627  
628 0 foreach my $i (0..$#{$set->{"cards"}}){
629 0 my $card = $set->{"cards"}->[$i];
630 0 ▶ $number = $i if &$find($card);
Branch: 1 branch
BranchTrueFalseTotal
if &$find($card) 0 0 0
631 }
632  
633 0 return $number; # will return undef if card wasn't found
634 }
635  
636 0 ▶ sub print_ordered_cards {
Subroutine print_ordered_cards: not called CC=1 SCAR=6.9
637 # returns the cards in the set in the correct order to be printed
638 0 return shift->{"cards"};
639 } # end sub Games::Cards::Hand::print_ordered_cards
640  
641 } #end package Games::Cards::Hand
642  
643 ##################
644  
645 =head2 Class Games::Cards::CardSet
646  
647 A CardSet is just an array of cards (stored in the "cards" field). It could be
648 a player's hand, a deck, or a discard pile, for instance. This is a super class
649 of a number of other classes, and those subclasses should be used instead.
650  
651 =over 4
652  
653 =cut
654  
655 #####################
656  
657 {
658 2 package Games::Cards::CardSet;
659 # Fields:
660 # cards - array of Cards
661 # name - "Joe's Hand" for Joe's hand, "discard" for a
662 # discard pile, etc.
663  
664 =item new(GAME, NAME, NICKNAME)
665  
666 create a new (empty) CardSet. GAME is the Game object that this set belongs
667 to. NAME is a unique string that e.g. can be output when you print the CardSet.
668 Optionally, NICKNAME is a (unique!) short name that will be used to reference
669 the set.
670  
671 =cut
672  
673 5 ▶ sub new {
Subroutine new: called CC=3 SCAR=11.3
674 5 my $self = shift;
675 # so we can say $foo->new or new Bar
676 5 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
677 5 my $game = shift;
678 # TODO use carp!
679 5 ▶ my $name = shift || die "new $class must be called with a 'name' arg";
Condition50%1/2
shift() || die("new $class must be called with a 'name' arg")
l!l
50
MC/DC0%0/1
shift() || die("new $class must be called with a 'name' arg")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
680 5 my $nickname = shift; # may be undef
681 5 my $set = {
682 "cards" => [],
683 "name" => $name,
684 "nickname" => $nickname,
685 };
686 5 bless $set, $class;
687  
688 # If this set is named "a" in this Game, then store
689 # "a"=>$set in the Game object. Same for nickname
690 5 $game->store_cardset($set);
691  
692 5 return $set;
693 } # end sub Games::Cards::CardSet::new
694  
695 # Splice cards into/out of a set
696 # Just like Perl's splice (with different argument types!)
697 # RESULT = splice(ARRAY, OFFSET, LENGTH, LIST);
698 # ARRAY is a CardSet,
699 # OFFSET is the index in the "cards" array
700 # LENGTH is the number of cards spliced out,
701 # LIST is a reference to an array of Cards to splice in
702 # RESULT is (empty or) a ref to an array of Cards that were spliced out
703 # (LENGTH and LIST are optional)
704 #
705 # This sub is private. People should use add_cards et al., which call
706 # this sub
707 1020 ▶ sub splice {
Subroutine splice: called CC=7 SCAR=19.6
708 1020 my ($set, $offset, $length, $in_cards) = @_;
709 # set in_cards to empty list if undef. Otherwise, we'd splice in (undef)
710 1020 ▶ $in_cards = [] unless defined $in_cards;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in_cards 510 510 1020
711  
712 # Negative offsets will break if we try to undo them
713 1020 ▶ $offset += $set->size if $offset < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $offset < 0 236 784 1020
714  
715 # If we didn't get length, splice to end of array
716 1020 ▶ $length = $set->size - $offset unless defined $length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 236 784 1020
717 # print $set->name, ": ",$set->size,
718 # " cards - $length starting at $offset",
719 # " + ", scalar(@$in_cards)," = ";
720  
721 # Can't splice in past position #$cards+1==foo->size
722 # Can't splice out more cards than we have
723 1020 ▶ warn "illegal splice!\n" if $offset > $set->size ||
Branch: 1 branch
BranchTrueFalseTotal
if $offset > $set->size or $length + $offset > $set->size 0 1020 1020
Condition33%1/3
$offset > $set->size or $length + $offset > $set->size
l!l&&r!l&&!r
001020
MC/DC0%0/2
$offset > $set->size or $length + $offset > $set->size
$offset > $set->size $length + $offset > $set->size
Truth table33%1/3
A or B
A= $offset > $set->sizeB= $length + $offset > $set->size
ABresult
1X1
011
000
724 $length + $offset > $set->size;
725  
726 # Do the splice
727 1020 my $out_cards = [splice (@{$set->{"cards"}}, $offset,
728 $length, @$in_cards)];
729  
730 # Store the splice & its result for Undo
731 1020 my $atom = new Games::Cards::Undo::Splice {
732 "set" => $set,
733 "offset" => $offset,
734 "length" => $length,
735 "in_cards" => $in_cards,
736 "out_cards" => $out_cards,
737 };
738 1020 $atom->store; # store the atom in the Undo List
739  
740 # in_cards now belong to this set
741 # out_cards will be handled by another splice, presumably
742 1020 foreach (@$in_cards) { $_->set_owning_cardset($set) }
743  
744 # print $set->size,"\n";
745 1020 return $out_cards;
746 } # end sub Games::Cards::CardSet::splice
747  
748 =item shuffle
749  
750 shuffles the cards in the CardSet. Shuffling is not undoable.
751  
752 =cut
753  
754 1 ▶ sub shuffle {
Subroutine shuffle: called CC=1 SCAR=0.0
755 # shuffle the deck (or subset thereof)
756 1 my $deck = shift;
757  
758 # "Random Schwartz"
759 # Replace the cards in the deck with shuffled cards
760 # (Just pick N random numbers & sort them)
761 1 @{$deck->{"cards"}} =
762 52 map { $_->[0] }
763 234 sort { $a->[1] <=> $b->[1] }
764 52 map { [$_, rand] }
765 1 @{$deck->{"cards"}};
766  
767 1 return;
768 } # end sub CardSet::Shuffle
769  
770 =item sort_by_value
771  
772 Sorts the Set by value. This and other sort routines will probably be used
773 mostly on Hands, which are "ordered sets", but you might want to reorder a deck
774 or something. Sorting is not undoable.
775  
776 =item sort_by_suit
777  
778 Sorts the Set by suit, but not by value within the suit.
779  
780 =item sort_by_suit_and_value
781  
782 Sorts the Set by suit, then by value within the suit.
783  
784 =cut
785  
786 0 ▶ sub sort_by_value {
Subroutine sort_by_value: not called CC=1 SCAR=6.9
787 0 my $set = shift;
788 0 @{$set->{"cards"}} = sort {$a->value <=> $b->value} @{$set->{"cards"}}
789 } # end sub Games::Cards::CardSet::sort_by_value
790  
791 0 ▶ sub sort_by_suit {
Subroutine sort_by_suit: not called CC=1 SCAR=6.9
792 0 my $set = shift;
793 0 @{$set->{"cards"}} = sort {$a->suit_value <=> $b->suit_value}
794 0 @{$set->{"cards"}}
795 } # end sub Games::Cards::CardSet::sort_by_suit
796  
797 0 ▶ sub sort_by_suit_and_value {
Subroutine sort_by_suit_and_value: not called CC=2 SCAR=17.9
798 0 my $set = shift;
799 0 ▶ @{$set->{"cards"}} = sort {$a->suit_value <=> $b->suit_value ||
Condition0%0/3
$a->suit_value <=> $b->suit_value || $a->value <=> $b->value
l!l&&r!l&&!r
000
MC/DC0%0/2
$a->suit_value <=> $b->suit_value || $a->value <=> $b->value
$a->suit_value <=> $b->suit_value $a->value <=> $b->value
Truth table0%0/3
A || B
A= $a->suit_value <=> $b->suit_valueB= $a->value <=> $b->value
ABresult
1X1
011
000
800 $a->value <=> $b->value}
801 0 @{$set->{"cards"}}
802 } # end sub Games::Cards::CardSet::sort_by_suit_and_value
803  
804 =item clone(GAME, NAME, NICKNAME)
805  
806 Create a copy of this CardSet. That is, create an object with the same class
807 as arg0. Then make a copy of each Card in the CardSet (true copy, not a
808 reference). arg1 is the Game that the set belongs to. arg2 is the name to give
809 the new CardSet. arg3 (optional) is the nickname.
810  
811 =cut
812  
813 0 ▶ sub clone {
Subroutine clone: not called CC=2 SCAR=17.9
814 0 my $this = shift;
815 0 my $clone = $this->new(@_);
816 0 my $game = shift; # shift *after* using @_!
817  
818 0 $clone->{"cards"} = [map {$_->clone($game)} @{$this->cards}];
819 0 foreach (@{$clone->cards}) {$_->set_owning_cardset($clone)};
820  
821 0 return $clone;
822 } # end sub Games::Cards::CardSet::clone
823  
824 =item face_down
825  
826 Makes a whole CardSet face down
827  
828 =cut
829  
830 0 ▶ sub face_down {
Subroutine face_down: not called CC=2 SCAR=17.9
831 0 foreach (@{shift->{"cards"}}) {$_->face_down}
832 } # end sub Games::Cards::CardSet::face_down
833  
834 =item face_up
835  
836 Makes a whole CardSet face up
837  
838 =cut
839  
840 0 ▶ sub face_up {
Subroutine face_up: not called CC=2 SCAR=17.9
841 0 foreach (@{shift->{"cards"}}) {$_->face_up}
842 } # end sub Games::Cards::CardSet::face_up
843  
844 =item print(LENGTH)
845  
846 Returns a string containing a printout of the Cards in the CardSet. Prints
847 a long printout if LENGTH is "long", short if "short" (or nothing).
848 The CardSet is printed out in reverse order, so that the top card of the set is
849 printed first.
850  
851 =cut
852  
853 0 ▶ sub print {
Subroutine print: not called CC=7 SCAR=40.3
854 0 my $set = shift;
855 0 my $length = shift;
856 0 ▶ my $long = $length && $length eq "long";
Condition0%0/3
$length && $length eq 'long'
!ll&&!rl&&r
000
MC/DC0%0/2
$length && $length eq 'long'
$length $length eq 'long'
Truth table0%0/3
A && B
A= $lengthB= $length eq 'long'
ABresult
0X0
100
111
857 0 my $max_per_line = 10;
858 0 my $i = 0;
859 0 my $to_print = "";
860 #print $set->{"name"}." has " . $set->size . " cards\n";
861  
862 0 ▶ $to_print .= $set->{"name"} . ":" . ($long ? "\n" : " ");
Branch: 1 branch
BranchTrueFalseTotal
$long ? : 0 0 0
863  
864 # Print. Different types of Sets are printed in different order
865 0 foreach my $card (@{$set->print_ordered_cards}) {
866 0 $to_print .= $card->print($length);
867 0 ▶ if ($long) {
Branch: 1 branch
BranchTrueFalseTotal
if ($long) { } 0 0 0
868 0 $to_print .= "\n";
869 } else { # short printout
870 0 ▶ if (++$i % $max_per_line) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i % $max_per_line) { } 0 0 0
871 0 $to_print .= " ";
872 } else {
873 0 $to_print .= "\n";
874 0 $to_print .= " " x (length($set->{"name"}) + 1);
875 }
876 } # end if (short or long printout?)
877 }
878 # Or, if there are no cards...
879 0 ▶ $to_print .= "(none)" unless $set->size;
Branch: 1 branch
BranchTrueFalseTotal
unless $set->size 0 0 0
880  
881 # Always print \n at end, but don't print 2
882 0 chomp($to_print);
883 0 $to_print .= "\n";
884  
885 0 return $to_print;
886 } # end sub CardSet::Print
887  
888 =item name
889  
890 Returns the name of the Set
891  
892 =cut
893  
894 725 ▶ sub name {return shift->{"name"}}
Subroutine name: called CC=1 SCAR=0.0
895  
896 =item nickname
897  
898 Returns the nickname of the Set (or undef if there is none)
899  
900 =cut
901  
902 5 ▶ sub nickname {return shift->{"nickname"}}
Subroutine nickname: called CC=1 SCAR=0.0
903  
904 =item cards
905  
906 Returns a reference to the array of Cards in the set
907  
908 =cut
909  
910 0 ▶ sub cards { return shift->{"cards"}; }
Subroutine cards: not called CC=1 SCAR=6.9
911  
912 =item size
913  
914 Tells how many cards are in the set
915  
916 =cut
917  
918 4261 ▶ sub size { return scalar(@{shift->{"cards"}}); }
Subroutine size: called CC=1 SCAR=0.0
919  
920 =back
921  
922 =cut
923  
924 } # end package Games::Cards::CardSet
925  
926 ######################################################################
927  
928 =head2 Class Games::Cards::Card
929  
930 A Card is a playing card. Methods:
931  
932 =over 4
933  
934 =cut
935  
936 {
937 2 package Games::Cards::Card;
938 # One playing card
939 # name is the name of the card (2-9, ace, king, queen, jack)
940 # value is the value of the card: e.g. ace may be 14 or 1. king may be 13 or 10.
941 # suit is the suit
942 # suit_value is the value of the suit: e.g. in bridge spades is 4, clubs 1
943 # (although that may change after bidding!)
944 # face_up tells whether the player can see the card
945 # owner is the name of the CardSet that this Card belongs to. A Card can
946 # only belong to one CardSet! (We store the name because storing a pointer
947 # might screw up garbage collection.)
948  
949 =item new(GAME, HASHREF)
950  
951 creates a new card. GAME is the Game this card is being created in. HASHREF
952 references a hash with keys "suit" and "name".
953  
954 =cut
955  
956 52 ▶ sub new {
Subroutine new: called CC=2 SCAR=7.1
957 52 my $a = shift;
958 52 ▶ my $class = ref($a) || $a;
Condition33%1/3
ref $a || $a
l!l&&r!l&&!r
0520
MC/DC0%0/2
ref $a || $a
ref $a $a
Truth table33%1/3
A || B
A= ref $aB= $a
ABresult
1X1
011
000
959 52 my $game = shift;
960 52 my $hashref = shift;
961 my $card = {
962 "name" => $hashref->{"name"},
963 "suit" => $hashref->{"suit"},
964 "value" => $hashref->{"value"},
965 52 "suit_value" => $hashref->{"suit_value"},
966 "face_up" => 1, # by default, you can see a card
967 "owner" => undef,
968 };
969  
970 # turn it into a playing card
971 52 bless $card, $class;
972  
973 # store a pointer to the card in the Game object
974 52 $game->store_card($card);
975  
976 52 return $card;
977 } # end sub Games::Cards::Card::new
978  
979 =item clone(GAME)
980  
981 makes a copy of the Card (not just a reference to it).
982  
983 =cut
984  
985 0 ▶ sub clone {
Subroutine clone: not called CC=2 SCAR=17.9
986 0 my $old_card = shift;
987 0 my $game = shift;
988 0 my $class = ref($old_card);
989 0 my $suit = $old_card->suit("long");
990 0 my $name = $old_card->name("long");
991 0 my $value = $old_card->value;
992 0 my $suit_value = $old_card->suit_value;
993 0 my $new_card = $old_card->new ($game, {
994 "suit"=>$suit, "name"=> $name,
995 "suit_value" => $suit_value, "value" => $value
996 });
997  
998 0 ▶ $old_card->is_face_up ? $new_card->face_up : $new_card->face_down;
Branch: 1 branch
BranchTrueFalseTotal
$old_card->is_face_up ? : 0 0 0
999 # Don't set owner, because it may be different
1000
1001 0 return $new_card;
1002 } # end sub Games::Cards::Card::clone
1003  
1004 =item print(LENGTH)
1005  
1006 returns a string with the whole card name ("King of Hearts") if LENGTH is
1007 "long", otherwise a short version ("KH").
1008  
1009 =cut
1010  
1011 0 ▶ sub print {
Subroutine print: not called CC=5 SCAR=34.0
1012 0 my $card = shift;
1013 0 my $length = shift;
1014 0 ▶ my $long = $length && $length eq "long";
Condition0%0/3
$length && $length eq 'long'
!ll&&!rl&&r
000
MC/DC0%0/2
$length && $length eq 'long'
$length $length eq 'long'
Truth table0%0/3
A && B
A= $lengthB= $length eq 'long'
ABresult
0X0
100
111
1015 0 my ($name, $suit) = ($card->name($length), $card->suit($length));
1016 0 my $face_up = $card->{"face_up"};
1017  
1018 0 ▶ $long ? (
Branch: 3 branches
BranchTrueFalseTotal
$long ? : 0 0 0
$face_up ? : 0 0 0
$face_up ? : 0 0 0
1019 $face_up ?
1020 $name . " of " . $suit :
1021 "(Face down card)"
1022 ) : ( # long
1023 $face_up ?
1024 sprintf("%3s ", $name . $suit) :
1025 "*** "
1026 )
1027 ;
1028  
1029 } # end sub Card::print
1030  
1031 =item truename
1032  
1033 Gives a unique description of this card, i.e., you're guaranteed that no
1034 other card in the Game will have the same description.
1035  
1036 =cut
1037  
1038 52 ▶ sub truename {
Subroutine truename: called CC=1 SCAR=0.0
1039 52 my $self = shift;
1040 52 return join("", $self->name, $self->suit);
1041 } # end sub Games::Cards::Card::truename
1042
1043 =item name(LENGTH)
1044  
1045 prints the name of the card. The full name ("King") if LENGTH is "long";
1046 otherwise a short version ("K");
1047  
1048 =cut
1049  
1050 52 ▶ sub name {
Subroutine name: called CC=4 SCAR=14.3
1051 52 my $name = shift->{"name"};
1052 52 my $length = shift;
1053 52 ▶ my $long = $length && $length eq "long";
Condition33%1/3
$length && $length eq 'long'
!ll&&!rl&&r
5200
MC/DC0%0/2
$length && $length eq 'long'
$length $length eq 'long'
Truth table33%1/3
A && B
A= $lengthB= $length eq 'long'
ABresult
0X0
100
111
1054
1055 52 ▶ if ($name =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\d+$/) { } 36 16 52
1056 36 return $name;
1057 } else {
1058 16 ▶ return $long ? $name : uc(substr($name, 0, 1));
Branch: 1 branch
BranchTrueFalseTotal
$long ? : 0 16 16
1059 }
1060 } # end sub Games::Cards::Card::name
1061  
1062 =item suit(LENGTH)
1063  
1064 Returns the suit of the card. Returns the long version ("Diamonds") if LENGTH
1065 is "long", else a short version ("D").
1066  
1067 =cut
1068  
1069 52 ▶ sub suit {
Subroutine suit: called CC=3 SCAR=12.0
1070 52 my $suit = shift->{"suit"};
1071 52 my $length = shift;
1072 52 ▶ my $long = $length && $length eq "long";
Condition33%1/3
$length && $length eq 'long'
!ll&&!rl&&r
5200
MC/DC0%0/2
$length && $length eq 'long'
$length $length eq 'long'
Truth table33%1/3
A && B
A= $lengthB= $length eq 'long'
ABresult
0X0
100
111
1073 52 ▶ return $long ? $suit : uc(substr($suit,0,1));
Branch: 1 branch
BranchTrueFalseTotal
$long ? : 0 52 52
1074 } # end sub Games::Cards::Card::suit
1075  
1076 =item color
1077  
1078 Is the card "red" or "black"? Returns the color or undef for unknown color.
1079  
1080 =cut
1081  
1082 0 ▶ sub color {
Subroutine color: not called CC=2 SCAR=17.9
1083 0 my $suit = shift->suit("long");
1084 0 my %color_map = (
1085 "Diamonds" => "red",
1086 "Hearts" => "red",
1087 "Spades" => "black",
1088 "Clubs" => "black",
1089 );
1090  
1091 0 ▶ if (exists ($color_map{$suit})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $color_map{$suit}) { } 0 0 0
1092 0 return $color_map{$suit};
1093 } else {
1094 0 warn "unknown suit '$suit'";
1095 0 return;
1096 }
1097 } # end sub Games::Cards::Card::color
1098  
1099 =item value
1100  
1101 Calculates the value of a card
1102  
1103 =cut
1104  
1105 254 ▶ sub value { return shift->{"value"}}
Subroutine value: called CC=1 SCAR=0.0
1106  
1107 =item suit_value
1108  
1109 Returns the suit_value of a card (1..number of suits)
1110  
1111 =cut
1112  
1113 0 ▶ sub suit_value { return shift->{"suit_value"}}
Subroutine suit_value: not called CC=1 SCAR=6.9
1114  
1115 =item is_face_up
1116  
1117 Returns true if a card is face up
1118  
1119 =cut
1120  
1121 0 ▶ sub is_face_up { return shift->{"face_up"} }
Subroutine is_face_up: not called CC=1 SCAR=6.9
1122  
1123 =item is_face_down
1124  
1125 Returns true if a card is face down
1126  
1127 =cut
1128  
1129 0 ▶ sub is_face_down { return !shift->{"face_up"} }
Subroutine is_face_down: not called CC=1 SCAR=6.9
1130  
1131 =item face_up
1132  
1133 Makes a card face up
1134  
1135 =cut
1136  
1137 0 ▶ sub face_up {
Subroutine face_up: not called CC=2 SCAR=17.9
1138 0 my $card = shift;
1139 0 ▶ unless ($card->{"face_up"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($card->{"face_up"}) 0 0 0
1140 0 $card->{"face_up"} = 1;
1141 0 my $atom = new Games::Cards::Undo::Face {
1142 "card" => $card,
1143 "direction" => "up",
1144 };
1145 0 $atom->store; # store the atom in the Undo List
1146 }
1147 } # end sub Games::Cards::Card::face_up
1148  
1149 =item face_down
1150  
1151 Makes a card face down
1152  
1153 =cut
1154  
1155 0 ▶ sub face_down {
Subroutine face_down: not called CC=2 SCAR=17.9
1156 0 my $card = shift;
1157 0 ▶ if ($card->{"face_up"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card->{"face_up"}) 0 0 0
1158 0 $card->{"face_up"} = 0;
1159 0 my $atom = new Games::Cards::Undo::Face {
1160 "card" => $card,
1161 "direction" => "down",
1162 };
1163 0 $atom->store; # store the atom in the Undo List
1164 }
1165 } # end sub Games::Cards::Card::face_down
1166  
1167 =item owning_cardset
1168  
1169 Returns the CardSet which this Card is a part of
1170  
1171 =item set_owning_cardset(SET_OR_NAME)
1172  
1173 Makes the Card a part of a CardSet. Arg0 is either an actual CardSet ref, or
1174 the name of a CardSet.
1175  
1176 =cut
1177  
1178 0 ▶ sub owning_cardset {
Subroutine owning_cardset: not called CC=2 SCAR=17.9
1179 0 my $self = shift;
1180 0 my $set_name = $self->{"owner"};
1181 0 my $game = &Games::Cards::Game::current_game;
1182 0 my $set = $game->get_cardset_by_name($set_name);
1183 # TODO use carp!
1184 0 ▶ warn $self->print("long"), " doesn't belong to any CardSets!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $set 0 0 0
1185 unless defined $set;
1186 0 return $set;
1187 }
1188 720 ▶ sub set_owning_cardset {
Subroutine set_owning_cardset: called CC=2 SCAR=7.2
1189 720 my ($self, $cardset) = @_;
1190 720 ▶ $self->{"owner"} =
Branch: 1 branch
BranchTrueFalseTotal
$cardset->isa('Games::Cards::CardSet') ? : 720 0 720
1191 $cardset->isa("Games::Cards::CardSet") ? $cardset->name : $cardset;
1192 } # end sub Games::Cards::Card::set_owning_cardset
1193  
1194 =back
1195  
1196 =cut
1197  
1198 } # end package Card
1199  
1200  
1201 2 1; # end package Games::Cards
1202  
1203 __END__
« blib/lib/Games/Cards/Undo.pm ↑ Summary blib/lib/Games/Cards/Tk.pm »