-
Notifications
You must be signed in to change notification settings - Fork 314
/
ch-2.pl
executable file
·82 lines (61 loc) · 1.29 KB
/
ch-2.pl
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/perl
use warnings;
use strict;
use 5.010;
my @choices = ( \&chose_paper, \&chose_scissors, \&chose_rock );
my @outcomes = (
[ \&draw, \&scissors_loss, \&paper_win, ],
[ \&scissors_win, \&draw, \&rock_loss, ],
[ \&paper_loss, \&rock_win, \&draw, ]
);
my @winners = ( \&winner_A, \&winner_B );
sub chose_paper {
my ($chooser) = @_;
say "$chooser chose Paper.";
}
sub chose_scissors {
my ($chooser) = @_;
say "$chooser chose Scissors.";
}
sub chose_rock {
my ($chooser) = @_;
say "$chooser chose Rock.";
}
sub paper_win {
say "Paper covers Rock. ";
&{$winners[0]};
}
sub paper_loss {
say "Paper covers Rock. ";
&{$winners[1]};
}
sub scissors_win {
say "Scissors cuts Paper. ";
&{$winners[0]};
}
sub scissors_loss {
say "Scissors cuts Paper. ";
&{$winners[1]};
}
sub rock_win {
say "Rock smashes Scissors. ";
&{$winners[0]};
}
sub rock_loss {
say "Rock smashes Scissors. ";
&{$winners[1]};
}
sub draw {
say "It's a draw.";
}
sub winner_A {
say "A wins.";
}
sub winner_B {
say "B wins.";
}
my $achoice = int rand 3;
my $bchoice = int rand 3;
$choices[$achoice]('A');
$choices[$bchoice]('B');
$outcomes[$achoice][$bchoice]();