Perl: Game - Rock Scissor Paper - by Eun Bae Kim (08/26/2018)
 

 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
83
84
85
86
  
use strict;

my $iScore = 0;
my $jScore = 0;

while(1) {
	print "Rock(1) Scissor(2) Paper(3) - Which one?\n";
	print "Your code?";
	my $i = int(rand(3) + 1);
	my $j = <STDIN>;

	my $iCode = funcTransform($i);
	my $jCode = funcTransform($j);

	print "Computer: ".funcTransform2($iCode)."\n";
	print "You:      ".funcTransform2($jCode)."\n";



	if ($iCode eq "S" and $jCode eq "S") {
		print "Same Code!\n";
	} elsif ($iCode eq "S" and $jCode eq "R") {
		print "You win!\n";
		$jScore++;
	} elsif ($iCode eq "S" and $jCode eq "P") {
		$iScore++;
		print "You lose!\n";
	} elsif ($iCode eq "R" and $jCode eq "S") {
		$iScore++;
		print "You lose!\n";
	} elsif ($iCode eq "R" and $jCode eq "R") {
		print "Same Code!\n";
	} elsif ($iCode eq "R" and $jCode eq "P") {
		$jScore++;
		print "You win!\n";
	} elsif ($iCode eq "P" and $jCode eq "S") {
		$jScore++;
		print "You win!\n";
	} elsif ($iCode eq "P" and $jCode eq "R") {
		$iScore++;$iScore++;
		print "You lose!\n";
	} elsif ($iCode eq "P" and $jCode eq "P") {
		print "Same Code!\n";
	}

	print "Computer: ".$iScore."\n";
	print "You:      ".$jScore."\n";

	print "===============================\n";
}

sub funcTransform {
	my $s = shift;

	if ($s == 2) {
		$s = "S";
	} elsif ($s == 1) {
		$s = "R";
	} elsif ($s == 3) {
		$s = "P";
	}

	return $s;
}


sub funcTransform2 {
	my $s = shift;

	if ($s eq "S") {
		$s = "Scissor";
	} elsif ($s eq "R") {
		$s = "Rock";
	} elsif ($s eq "P") {
		$s = "Paper";
	}

	return $s;
}




# 1 - Scissor - S
# 2 - Rock    - R
# 3 - Paper   - P