line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Game::RockPaperScissor; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
70544
|
use 5.006; |
|
1
|
|
|
|
|
4
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
5
|
1
|
|
|
1
|
|
16
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
6
|
1
|
|
|
1
|
|
8
|
use Carp qw/croak/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
631
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Game::RockPaperScissor - object oriented Game::RockPaperScissor! |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Version 0.01 |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=cut |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
#see pod |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new { |
23
|
1
|
|
|
1
|
1
|
624
|
my ($class, $args) = (@_); |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
50
|
|
|
10
|
return (bless $args || {}, $class); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
#see pod |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub get_result { |
31
|
3
|
|
|
3
|
1
|
1210
|
my $self = shift; |
32
|
3
|
|
|
|
|
4
|
my $game = shift; |
33
|
|
|
|
|
|
|
|
34
|
3
|
|
|
|
|
9
|
$self->_validate($game); #validate |
35
|
|
|
|
|
|
|
|
36
|
3
|
|
|
|
|
4
|
my $result = 0; |
37
|
|
|
|
|
|
|
|
38
|
3
|
|
|
|
|
17
|
my $win_matrix = { |
39
|
|
|
|
|
|
|
'r' => { 'p' => '-1', 's' => '1' }, |
40
|
|
|
|
|
|
|
's' => { 'r' => '-1', 'p' => '1' }, |
41
|
|
|
|
|
|
|
'p' => { 'r' => '1', 's' => '-1' } |
42
|
|
|
|
|
|
|
}; |
43
|
|
|
|
|
|
|
|
44
|
3
|
100
|
|
|
|
14
|
return $result if ($game->{p1} eq $game->{p2}); #return for tie |
45
|
|
|
|
|
|
|
|
46
|
2
|
50
|
33
|
|
|
9
|
if (exists $game->{p1} && exists $game->{p2}) { |
47
|
2
|
|
|
|
|
6
|
my ($p1_choice, $p2_choice) = ($game->{p1}, $game->{p2}); #for readability |
48
|
2
|
|
|
|
|
3
|
$result = $win_matrix->{$p1_choice}->{$p2_choice}; |
49
|
|
|
|
|
|
|
} |
50
|
2
|
|
|
|
|
11
|
return $result; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
#see pod |
53
|
|
|
|
|
|
|
sub get_result_modulus { |
54
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
55
|
0
|
|
|
|
|
0
|
my $game = shift; |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
0
|
$self->_validate($game); #validate |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
0
|
my $win_matrix = { |
60
|
|
|
|
|
|
|
's' => 0, |
61
|
|
|
|
|
|
|
'p' => 1, |
62
|
|
|
|
|
|
|
'r' => 2 |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
0
|
my $result_map = { |
66
|
|
|
|
|
|
|
'1' => '-1', #p2 win |
67
|
|
|
|
|
|
|
'2' => '1', #p1 win |
68
|
|
|
|
|
|
|
'0' => '0' #tie |
69
|
|
|
|
|
|
|
}; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
#use map |
72
|
0
|
|
|
|
|
0
|
my $value = ($win_matrix->{$game->{p1}} - $win_matrix->{$game->{p2}}) % 3 ; |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
0
|
$value += 3 if($value < 0); #handle negative |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
0
|
return $result_map->{$value}; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
#see pod |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _validate { |
82
|
3
|
|
|
3
|
|
4
|
my $self = shift; |
83
|
3
|
|
|
|
|
7
|
my $game = shift; |
84
|
|
|
|
|
|
|
|
85
|
3
|
50
|
|
|
|
4
|
croak "Error Game hash needed eg. {'p1' =>'s' ,'p2' => 'r'}" unless (keys %{$game}); |
|
3
|
|
|
|
|
11
|
|
86
|
|
|
|
|
|
|
|
87
|
3
|
|
|
|
|
7
|
foreach my $key (qw/p1 p2/) { |
88
|
6
|
50
|
33
|
|
|
26
|
if (!(defined $game->{$key} && exists $game->{$key})) { |
89
|
0
|
|
|
|
|
0
|
croak "Error Required key '$key' missing from hash params eg. {'p1' =>'s' ,'p2' => 'r'}"; |
90
|
|
|
|
|
|
|
} else { |
91
|
6
|
|
|
|
|
14
|
$game->{$key} = lc(substr($game->{$key}, 0, 1)); #convert into subroutine specific format if not provided already |
92
|
6
|
50
|
|
|
|
29
|
if ($game->{$key} !~ /^(p|s|r)$/x) { |
93
|
0
|
|
|
|
|
0
|
croak 'Error Invalid symbol passed use "rock" or "paper" or "scissor"'; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
3
|
|
|
|
|
6
|
return; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 Game::RockPaperScissor |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Game::RockPaperScissor package to output result for Rock - Paper - Scissor game. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 SYNOPSIS |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
use Game::RockPaperScissor; |
110
|
|
|
|
|
|
|
my $rps = Game::RockPaperScissor->new(); |
111
|
|
|
|
|
|
|
my $game = { |
112
|
|
|
|
|
|
|
p1 => 'rock', |
113
|
|
|
|
|
|
|
p2 => 'scissor', |
114
|
|
|
|
|
|
|
}; |
115
|
|
|
|
|
|
|
print $rps->get_result($game); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 INTRODUCTION |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Game::RockPaperScissor package ouputs the result of Rock - Paper - Scissor game for given choice by player 1 and player 2 |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 METHODS |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head2 new |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
use to create the instace of Game::RockPaperScissor class. Optional args can be passed |
126
|
|
|
|
|
|
|
Input : - |
127
|
|
|
|
|
|
|
Caller method |
128
|
|
|
|
|
|
|
Ouput :- |
129
|
|
|
|
|
|
|
Game::RockPaperScissor class instance |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 get_result |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
used to return the result/ outcome of the game for player 1 only. |
134
|
|
|
|
|
|
|
Result belongs to the player 1. It validates the input before calculating outcome. |
135
|
|
|
|
|
|
|
Sub will die on invalid input stating the valid option to use. |
136
|
|
|
|
|
|
|
call _validate method see pod for more info |
137
|
|
|
|
|
|
|
Input :- |
138
|
|
|
|
|
|
|
1) instance of class |
139
|
|
|
|
|
|
|
2) hash ref of game with keys p1 and p2 with values as their respective choices. |
140
|
|
|
|
|
|
|
Mandatory input :- |
141
|
|
|
|
|
|
|
1) instance of class |
142
|
|
|
|
|
|
|
2) Keys p1 and p2 |
143
|
|
|
|
|
|
|
{ |
144
|
|
|
|
|
|
|
p1 => 'rock', #valid choices ('rock|r' | 'paper|p' | 'scissor|s') |
145
|
|
|
|
|
|
|
p2 => 'paper' #valid choices ('rock|r' | 'paper|p' | 'scissor|s') |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
Output :- |
148
|
|
|
|
|
|
|
return integer values :- 0 or 1 or -1 |
149
|
|
|
|
|
|
|
0 => Tie |
150
|
|
|
|
|
|
|
1 => Win |
151
|
|
|
|
|
|
|
-1 => Loose |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head2 get_result_modulus |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
#believe in TIMTOWTDI |
156
|
|
|
|
|
|
|
Yet another method using different algorithm that uses modulus |
157
|
|
|
|
|
|
|
used to return the result/ outcome of the game for player 1 only. |
158
|
|
|
|
|
|
|
Result belongs to the player 1. It validates the input before calculating outcome. |
159
|
|
|
|
|
|
|
Sub will die on invalid input stating the valid option to use. |
160
|
|
|
|
|
|
|
call _validate method see pod for more info |
161
|
|
|
|
|
|
|
Input :- |
162
|
|
|
|
|
|
|
1) instance of class |
163
|
|
|
|
|
|
|
2) hash ref of game with keys p1 and p2 with values as their respective choices. |
164
|
|
|
|
|
|
|
Mandatory input :- |
165
|
|
|
|
|
|
|
1) instance of class |
166
|
|
|
|
|
|
|
2) Keys p1 and p2 |
167
|
|
|
|
|
|
|
{ |
168
|
|
|
|
|
|
|
p1 => 'rock', #valid choices ('rock|r' | 'paper|p' | 'scissor|s') |
169
|
|
|
|
|
|
|
p2 => 'paper' #valid choices ('rock|r' | 'paper|p' | 'scissor|s') |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
Output :- |
172
|
|
|
|
|
|
|
return integer values :- 0 or 1 or -1 |
173
|
|
|
|
|
|
|
0 => Tie |
174
|
|
|
|
|
|
|
1 => Win |
175
|
|
|
|
|
|
|
-1 => Loose |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head2 _validate |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
internal private method not to be called outside, |
180
|
|
|
|
|
|
|
use to validate the input provided to get_result method |
181
|
|
|
|
|
|
|
Dies on invalid input |
182
|
|
|
|
|
|
|
Input :- hash ref of game with keys p1 and p2 with values as their respective choices. |
183
|
|
|
|
|
|
|
Game hash needed eg. {'p1' =>'s' ,'p2' => 'r'} |
184
|
|
|
|
|
|
|
or |
185
|
|
|
|
|
|
|
Game hash needed eg. {'p1' =>'scissor' ,'p2' => 'Rock'} |
186
|
|
|
|
|
|
|
output :- |
187
|
|
|
|
|
|
|
nothing |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=cut |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 AUTHOR |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Sushrut Pajai, C<< >> |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 BUGS |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
199
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
200
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head1 SUPPORT |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
perldoc Game::RockPaperScissor |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
You can also look for information at: |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=over 4 |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
L |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
L |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=item * CPAN Ratings |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
L |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=item * Search CPAN |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
L |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=back |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
This software is Copyright (c) 2020 by Sushrut Pajai. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
This is free software, licensed under: |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=cut |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
1; # End of Game::RockPaperScissor |