File Coverage

blib/lib/Games/Roshambo.pm
Criterion Covered Total %
statement 6 49 12.2
branch 0 30 0.0
condition 0 24 0.0
subroutine 2 8 25.0
pod 3 6 50.0
total 11 117 9.4


line stmt bran cond sub pod time code
1             ##############################################################################
2             # Games::Roshambo - Object Oriented Rock/Paper/Scissors
3             # v1.01
4             # Copyright (c) 2008 Chris Thompson
5             ##############################################################################
6             package Games::Roshambo;
7             $VERSION ="1.01";
8 1     1   27529 use strict;
  1         3  
  1         36  
9 1     1   4614 use Games::Roshambo::Thrownames;
  1         5  
  1         769  
10              
11             sub new {
12 0     0 1   my $class = shift;
13 0           my %conf = @_;
14            
15 0 0         $conf{numthrows} = 3 unless defined $conf{numthrows};
16 0 0         $conf{sortable} = 0 unless defined $conf{sortable};
17              
18 0 0         if (! ($conf{numthrows} % 2)) {
19 0           warn "Cannot use an even number of throws: " . $conf{numthrows};
20 0           return undef;
21             }
22              
23              
24 0           $conf{thrownames} = Games::Roshambo::Thrownames::getnames($conf{numthrows});
25 0           return bless {%conf}, $class;
26             }
27              
28             sub gen_throw {
29 0     0 0   my $self = shift;
30 0           return int(rand($self->{numthrows})) + 1;
31             }
32              
33             sub judge {
34 0     0 1   my $self = shift;
35              
36             #######################################################################
37             ### MADNESS!
38             ### throws are passed to this function as $x->judge($player1, $player2)
39             ### but I'm reversing them when I grab them here. Why? well, this whole
40             ### set of logic was based on a "Higher Throw Wins" set of math. Since
41             ### I'm crazy enough to want this to play RPS-101 with throw names,
42             ### and since I didn't realize that the whole RPS-101 is based on "Lower
43             ### Throw Wins", I was resigned to rewriting the logic itself.
44             ### Chris Prather helped me realize, however, that simply swapping the
45             ### values was the same thing. This is why he gets to work at a cool
46             ### job doing cool perl things, while I have to edit badly formed HTML.
47             ########################################################################
48              
49 0   0       my $player2 = shift || $self->gen_throw;
50 0   0       my $player1 = shift || $self->gen_throw;
51            
52 0 0         $player1 = $self->name_to_num($player1) unless ($player1 !~ m/\D+/);
53 0 0         $player2 = $self->name_to_num($player2) unless ($player2 !~ m/\D+/);
54              
55 0 0 0       return undef unless ($player1 && $player2);
56            
57 0           my $numthrows = $self->{numthrows};
58 0           my $winrange = ($numthrows - 1) / 2;
59              
60 0 0         return 0 if ($player1 == $player2);
61              
62 0 0         my $onewins = $self->{sortable} ? -1 : 1;
63 0 0         my $twowins = $self->{sortable} ? 1 : 2;
64            
65 0 0 0       return $onewins if (($player1 >= $winrange) &&
      0        
66             (($player1 > $player2) && ($player1 - $winrange <= $player2))
67             );
68            
69 0 0 0       return $onewins if (($player1 <= $winrange) &&
      0        
70             (($player1 > $player2) || ($numthrows - ($winrange - $player1) <= $player2))
71             );
72            
73 0           return $twowins;
74            
75             }
76              
77             sub num_to_name {
78 0     0 0   my $self = shift;
79 0           my $thrownum = shift;
80            
81 0           return $self->{thrownames}->{_names}->[$thrownum];
82            
83             }
84              
85             sub name_to_num {
86 0     0 0   my $self = shift;
87 0           my $throwname = uc(shift);
88            
89 0           my $thrownum = $self->{thrownames}->{$throwname};
90 0 0         warn "Invalid Throw Name $throwname, returning undef" unless defined $thrownum;
91 0           return $thrownum;
92             }
93              
94             sub getaction {
95 0     0 1   my $self = shift;
96 0           my $a = shift;
97 0           my $b = shift;
98              
99 0 0         if ($a =~ m/\D/) {
100 0           $a = $self->name_to_num($a);
101             }
102              
103 0 0         if ($b =~ m/\D/) {
104 0           $b = $self->name_to_num($a);
105             }
106              
107 0 0         return "ties" if $a == $b;
108              
109 0   0       my $ret = $self->{thrownames}->{_actions}->{$a}->{$b} ||
110             $self->{thrownames}->{_actions}->{$b}->{$a};
111              
112 0           return $ret;
113             }
114              
115              
116             1;
117             __END__