File Coverage

blib/lib/Games/Cards/Bridge/Chicago.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Games::Cards::Bridge::Chicago;
2              
3 1     1   40877 use strict;
  1         3  
  1         55  
4 1     1   6 use warnings;
  1         2  
  1         36  
5              
6 1     1   6 use base qw(Class::Accessor);
  1         2  
  1         2218  
7             use Games::Cards::Bridge::Contract;
8             use Carp;
9              
10             our $VERSION = '0.01';
11              
12             __PACKAGE__->mk_accessors(
13             'contracts', # array ref
14             # these are all auto-calculated
15             # scores:
16             'NS_score',
17             'EW_score',
18             'NS_vul', # bool
19             'EW_vul', # bool
20             'complete', # bool
21             'dealer', # N E S W
22             );
23              
24             sub both_vul {
25             my $self = shift;
26             return $self->NS_vul && $self->EW_vul;
27             }
28              
29             sub __hand_setup {
30             my $self = shift;
31             my $hands_played = scalar @{$self->contracts};
32             my %states = (
33             # num_played => [ dealer, NS_vul, EW_vul ]
34             0 => [ 'N', 0, 0 ],
35             1 => [ 'E', 1, 0 ],
36             2 => [ 'S', 0, 1 ],
37             3 => [ 'W', 1, 1 ],
38             );
39             my $state = $states{$hands_played} or do {
40             $self->set('complete', 1);
41             return 0;
42             };
43             $self->set('dealer', $state->[0]);
44             $self->set('NS_vul', $state->[1]);
45             $self->set('EW_vul', $state->[2]);
46             return 1;
47             }
48              
49             sub new {
50             my $self = shift;
51             my $class = ref($self) || $self;
52             $self = bless {}, $class;
53             $self->set($_, 0) for qw/NS_score EW_score NS_vul EW_vul complete/;
54             $self->set('contracts', []);
55             $self->set('dealer', 'N');
56             $self->__hand_setup();
57             return $self;
58             }
59              
60             sub contract {
61             my $self = shift;
62             my $p = {@_};
63             my $NS = $p->{declarer} =~ /^[NS]$/;
64             my $EW = !$NS;
65             my $contract = Games::Cards::Bridge::Contract->new(
66             declarer=> $p->{declarer},
67             trump => $p->{trump},
68             bid => $p->{bid},
69             made => $p->{made},
70             down => $p->{down},
71             vul => ($NS ? $self->NS_vul : $self->EW_vul),
72             penalty => $p->{dbl},
73             );
74             push @{$self->contracts}, $contract;
75             my $score = $contract->duplicate_score;
76             my $scoreProperty =
77             ($NS && $score>0) || ($EW && $score<0)
78             ? 'NS_score'
79             : 'EW_score'
80             ;
81             $self->set($scoreProperty, $self->$scoreProperty + abs $score);
82             $self->__hand_setup();
83             return $contract;
84             }
85              
86             1;
87              
88              
89             =pod
90              
91             =head1 NAME
92              
93             Games::Cards::Bridge::Chicago - Object for Bridge (card game) Chicago scoring
94              
95             =head1 VERSION
96              
97             Version 0.01
98              
99             =head1 SYNOPSIS
100              
101             This module provides a class for creating Bridge objects for a Chicago game (aka 'Four-Deal Bridge'), including the results and scoring and current state of the game.
102              
103             use Games::Cards::Bridge::Chicago;
104             sub show_score {
105             my $chi = shift;
106             printf "NS = %d \t EW = %d\n", $chi->NS_score, $chi->EW_score;
107             printf " Vul: NS = %d \t EW = %d\n", $chi->NS_vul, $chi->EW_vul;
108             printf "==COMPLETE==\n" if $chi->complete;
109             }
110             my $chi = Games::Cards::Bridge::Chicago->new;
111              
112             show_score($chi);
113             foreach my $opts (
114             { declarer => 'N', trump => 'H', bid => '4', made => '4' },
115             { declarer => 'S', trump => 'C', bid => '3', down => '2', dbl => 1 },
116             { declarer => 'E', trump => 'N', bid => '3', made => '3' },
117             { declarer => 'W', trump => 'D', bid => '5', down => '3' },
118             ){
119             $chi->contract( %$opts );
120             show_score($chi);
121             }
122              
123              
124             =head1 METHODS
125              
126             =head2 new
127              
128             No parameters needed.
129              
130             =head2 contract
131              
132             Add a contract to the game. This needs the same arguments as L's constructor. This method is also responsible for internally updating the attributes. See also L
133              
134             =head2 both_vul
135              
136             Alias to returns true iff ->we_vul() and ->they_vul().
137              
138             =head1 ATTRIBUTES
139              
140             These are all auto-calculated/maintained; their current values are available from the accessor method provided by L.
141              
142             =head2 contracts
143              
144             Array ref holding all the contracts added by the contract() method.
145              
146             =head2 NS_score
147              
148             Gives the current total North-South score.
149              
150             =head2 they_score
151              
152             Gives the current total East-West score.
153              
154             =head2 we_vul
155              
156             Returns true if the North-South side is vulnerable.
157              
158             =head2 they_vul
159              
160             Returns true if the East-West side is vulnerable.
161              
162             =head2 complete
163              
164             Returns true if the game has concluded (played 4 hands).
165              
166             =head2 dealer
167              
168             Returns N E S or W representing the current dealer.
169              
170             =head1 PREREQUISITES
171              
172             =over 4
173              
174             =item *
175              
176             L
177              
178             =item *
179              
180             L
181              
182             =item *
183              
184             L
185              
186             =back
187              
188             =head1 AUTHOR
189              
190             David Westbrook, C<< >>
191              
192             =head1 BUGS & SUPPORT
193              
194             See L
195              
196             =head1 COPYRIGHT & LICENSE
197              
198             Copyright 2006 David Westbrook, all rights reserved.
199              
200             This program is free software; you can redistribute it and/or modify it
201             under the same terms as Perl itself.
202              
203             =cut
204              
205