-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.pm
More file actions
69 lines (53 loc) · 1.04 KB
/
deck.pm
File metadata and controls
69 lines (53 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package deck;
use strict;
use card;
## Contents
#
# new
# load
# shuffle
# count_cards
# add_card
# get_next_card
my @suits = qw (h d c s);
my @values = qw (2 3 4 5 6 7 8 9 T J Q K A);
sub new {
my $self = { cards => [], };
bless $self;
$self->load();
return $self;
}
sub load {
my $self = shift;
foreach my $value (@values) {
foreach my $suit (@suits) {
$self->add_card( $value, $suit );
}
}
}
sub shuffle {
my $self = shift;
my $i;
for ( $i = $self->count_cards ; $i-- ; ) {
my $j = int rand( $i + 1 );
next if $i == $j;
@{ $self->{cards} }[ $i, $j ] = @{ $self->{cards} }[ $j, $i ];
}
}
sub count_cards {
my $self = shift;
return @{ $self->{cards} };
}
sub add_card {
my $self = shift;
my $suit = shift;
my $value = shift;
my $new_card = card->new( $value, $suit );
push( @{ $self->{cards} }, $new_card );
}
sub get_next_card {
my $self = shift;
my $card = shift @{ $self->{cards} };
return $card;
}
return 1;