PlayRockPaperScissorsGame 1.7.3 → 1.7.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/PlayRockPaperScissorsGame +69 -76
- data/bin/rps +72 -77
- data/lib/rps.rb +48 -53
- data/rps.gemspec +1 -1
- data/test/test_rps.rb +70 -75
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fce8c420198c19a1b5cc1bb1326ab45e88064f1
|
4
|
+
data.tar.gz: 17cd148ee059571d6aae118dc1e76c6e61ba26fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f41bf9cf1afafa4c482a68c4343eb8074a04a29b50570454f735cea9c2fc215a616d90bcef458822253410fdf2fe37a422bd70258caa1de19e7d38b7858fea3a
|
7
|
+
data.tar.gz: 7e6e7e3ad9712f1d32c18ca3144c80f17cec9dda3c60444f227086f22c386f8d9e41ccc5eb97c73b51b5c84e0645ff1726bedb9bd2732710c44c14b279da8428
|
@@ -15,89 +15,82 @@ class PlayRockPaperScissorsGame
|
|
15
15
|
ColorizedString.colors;
|
16
16
|
ColorizedString.modes;
|
17
17
|
|
18
|
-
|
18
|
+
module Constants
|
19
|
+
NTRY_TO_SYM = { 'p' => :PAPER, 'r' => :ROCK, 's' => :SCISSORS };
|
20
|
+
VALID_ENTRIES = NTRY_TO_SYM.keys;
|
21
|
+
COMPUTER_CHOICES = NTRY_TO_SYM.values;
|
22
|
+
WINNERS = [[:SCISSORS, :PAPER], [:PAPER, :ROCK], [:ROCK, :SCISSORS]]; # format: player choice, computer choice
|
23
|
+
LOSERS = WINNERS.map { |i,j| [j,i] }; # this will take the original WINNERS array and flip the symbols, thus returning a loss for the user/player
|
24
|
+
INIT_STRINGS = ["You are about to enter a rock-paper-scissors best of 3 match.", "Press the return/enter key to continue...", ""];
|
25
|
+
end;
|
19
26
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
27
|
+
class << self
|
28
|
+
def continue(str1, str2, str3)
|
29
|
+
puts ColorizedString[str1].colorize(:color => :green);
|
30
|
+
print ColorizedString[str2].colorize(:color => :green);
|
31
|
+
gets;
|
32
|
+
puts ColorizedString[str3].colorize(:color => :green);
|
33
|
+
end;
|
34
|
+
end;
|
35
|
+
continue(Constants::INIT_STRINGS[0], Constants::INIT_STRINGS[1], Constants::INIT_STRINGS[2]);
|
36
|
+
def initialize
|
37
|
+
@player_score = @computer_score = @ties = 0;
|
38
|
+
end;
|
39
|
+
def play(winning_score)
|
40
|
+
while @player_score < winning_score && @computer_score < winning_score
|
41
|
+
puts ColorizedString["Player score: #{@player_score}, "].colorize(:blue) +
|
42
|
+
ColorizedString["Computer score: #{@computer_score}, Ties: #{@ties}"].colorize(:blue);
|
43
|
+
player = PrivateMethods.player_choice;
|
44
|
+
computer = Constants::COMPUTER_CHOICES.sample;
|
45
|
+
puts ColorizedString["\nPlayer chooses #{player.to_s.downcase}"].colorize(:blue);
|
46
|
+
puts ColorizedString["Computer chooses #{computer.to_s.downcase}"].colorize(:blue);
|
47
|
+
case PrivateMethods.player_outcome [player, computer]
|
48
|
+
when :WIN
|
49
|
+
puts ColorizedString["#{player.to_s.capitalize} beats #{computer.to_s.downcase}, player wins the round"].colorize(:red);
|
50
|
+
@player_score += 1;
|
51
|
+
when :LOSE
|
52
|
+
puts ColorizedString["#{computer.to_s.capitalize} beats #{player.to_s.downcase}, computer wins the round"].colorize(:red);
|
53
|
+
@computer_score += 1;
|
54
|
+
else
|
55
|
+
puts ColorizedString["Tie, choose again"].colorize(:red);
|
56
|
+
@ties += 1;
|
57
|
+
end;
|
28
58
|
end;
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
59
|
+
puts ColorizedString["\nFinal score: player: #{@player_score}, "].colorize(:blue) +
|
60
|
+
ColorizedString["computer: #{@computer_score} (ties: #{@ties})"].colorize(:blue);
|
61
|
+
case PrivateMethods.final_outcome(@player_score, @computer_score)
|
62
|
+
when :WIN
|
63
|
+
puts ColorizedString["Player wins!"].colorize(:red);
|
64
|
+
when :LOSE
|
65
|
+
puts ColorizedString["Computer wins!"].colorize(:red);
|
66
|
+
else
|
67
|
+
puts ColorizedString["It's a tie!"].colorize(:red);
|
68
|
+
end;
|
69
|
+
gets;
|
70
|
+
end;
|
71
|
+
module PrivateMethods
|
72
|
+
class << self
|
73
|
+
def player_choice
|
74
|
+
loop do
|
75
|
+
print ColorizedString["Choose rock (r), paper (p) or scissors (s): "].colorize(:green);
|
76
|
+
choice = gets.chomp.downcase;
|
77
|
+
return Constants::NTRY_TO_SYM[choice] if Constants::NTRY_TO_SYM.key?(choice);
|
78
|
+
puts ColorizedString["That entry is invalid. Please re-enter."].colorize(:green);
|
37
79
|
end;
|
38
80
|
end;
|
39
|
-
|
40
|
-
|
41
|
-
|
81
|
+
def player_outcome(plays)
|
82
|
+
return :WIN if Constants::WINNERS.include?(plays);
|
83
|
+
return :LOSE if Constants::LOSERS.include?(plays);
|
84
|
+
return :TIE if (!:WIN || !:LOSE);
|
42
85
|
end;
|
43
|
-
def
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
player = PrivateMethods.player_choice;
|
48
|
-
computer = Constants::COMPUTER_CHOICES.sample;
|
49
|
-
puts ColorizedString["\nPlayer chooses #{player.to_s.downcase}"].colorize(:blue);
|
50
|
-
puts ColorizedString["Computer chooses #{computer.to_s.downcase}"].colorize(:blue);
|
51
|
-
case PrivateMethods.player_outcome [player, computer]
|
52
|
-
when :WIN
|
53
|
-
puts ColorizedString["#{player.to_s.capitalize} beats #{computer.to_s.downcase}, player wins the round"].colorize(:red);
|
54
|
-
@player_score += 1;
|
55
|
-
when :LOSE
|
56
|
-
puts ColorizedString["#{computer.to_s.capitalize} beats #{player.to_s.downcase}, computer wins the round"].colorize(:red);
|
57
|
-
@computer_score += 1;
|
58
|
-
else
|
59
|
-
puts ColorizedString["Tie, choose again"].colorize(:red);
|
60
|
-
@ties += 1;
|
61
|
-
end;
|
62
|
-
end;
|
63
|
-
puts ColorizedString["\nFinal score: player: #{@player_score}, "].colorize(:blue) +
|
64
|
-
ColorizedString["computer: #{@computer_score} (ties: #{@ties})"].colorize(:blue);
|
65
|
-
case PrivateMethods.final_outcome(@player_score, @computer_score)
|
66
|
-
when :WIN
|
67
|
-
puts ColorizedString["Player wins!"].colorize(:red);
|
68
|
-
when :LOSE
|
69
|
-
puts ColorizedString["Computer wins!"].colorize(:red);
|
70
|
-
else
|
71
|
-
puts ColorizedString["It's a tie!"].colorize(:red);
|
72
|
-
end;
|
73
|
-
gets;
|
86
|
+
def final_outcome(pl, co)
|
87
|
+
return :WIN if pl > co;
|
88
|
+
return :LOSE if pl < co;
|
89
|
+
return :TIE if pl = co;
|
74
90
|
end;
|
75
|
-
|
76
|
-
module PrivateMethods
|
77
|
-
class << self
|
78
|
-
def player_choice
|
79
|
-
loop do
|
80
|
-
print ColorizedString["Choose rock (r), paper (p) or scissors (s): "].colorize(:green);
|
81
|
-
choice = gets.chomp.downcase;
|
82
|
-
return Constants::NTRY_TO_SYM[choice] if Constants::NTRY_TO_SYM.key?(choice);
|
83
|
-
puts ColorizedString["That entry is invalid. Please re-enter."].colorize(:green);
|
84
|
-
end;
|
85
|
-
end;
|
86
|
-
def player_outcome(plays)
|
87
|
-
return :WIN if Constants::WINNERS.include?(plays);
|
88
|
-
return :LOSE if Constants::LOSERS.include?(plays);
|
89
|
-
return :TIE if (!:WIN || !:LOSE);
|
90
|
-
end;
|
91
|
-
def final_outcome(pl, co)
|
92
|
-
return :WIN if pl > co;
|
93
|
-
return :LOSE if pl < co;
|
94
|
-
return :TIE if pl = co;
|
95
|
-
end;
|
96
|
-
end;
|
97
|
-
end;
|
98
|
-
end;
|
91
|
+
end;
|
99
92
|
end;
|
100
93
|
end;
|
101
94
|
|
102
|
-
PlayRockPaperScissorsGame
|
95
|
+
PlayRockPaperScissorsGame.new.play(2); # best of 3
|
103
96
|
|
data/bin/rps
CHANGED
@@ -8,94 +8,89 @@
|
|
8
8
|
|====================================|
|
9
9
|
=end
|
10
10
|
|
11
|
+
|
11
12
|
class PlayRockPaperScissorsGame
|
12
13
|
|
13
14
|
require "colorized_string";
|
14
|
-
ColorizedString.colors;
|
15
|
-
ColorizedString.modes;
|
15
|
+
ColorizedString.colors;
|
16
|
+
ColorizedString.modes;
|
16
17
|
|
17
|
-
|
18
|
+
module Constants
|
19
|
+
NTRY_TO_SYM = { 'p' => :PAPER, 'r' => :ROCK, 's' => :SCISSORS };
|
20
|
+
VALID_ENTRIES = NTRY_TO_SYM.keys;
|
21
|
+
COMPUTER_CHOICES = NTRY_TO_SYM.values;
|
22
|
+
WINNERS = [[:SCISSORS, :PAPER], [:PAPER, :ROCK], [:ROCK, :SCISSORS]]; # format: player choice, computer choice
|
23
|
+
LOSERS = WINNERS.map { |i,j| [j,i] }; # this will take the original WINNERS array and flip the symbols, thus returning a loss for the user/player
|
24
|
+
INIT_STRINGS = ["You are about to enter a rock-paper-scissors best of 3 match.", "Press the return/enter key to continue...", ""];
|
25
|
+
end;
|
18
26
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
+
class << self
|
28
|
+
def continue(str1, str2, str3)
|
29
|
+
puts ColorizedString[str1].colorize(:color => :green);
|
30
|
+
print ColorizedString[str2].colorize(:color => :green);
|
31
|
+
gets;
|
32
|
+
puts ColorizedString[str3].colorize(:color => :green);
|
33
|
+
end;
|
34
|
+
end;
|
35
|
+
continue(Constants::INIT_STRINGS[0], Constants::INIT_STRINGS[1], Constants::INIT_STRINGS[2]);
|
36
|
+
def initialize
|
37
|
+
@player_score = @computer_score = @ties = 0;
|
38
|
+
end;
|
39
|
+
def play(winning_score)
|
40
|
+
while @player_score < winning_score && @computer_score < winning_score
|
41
|
+
puts ColorizedString["Player score: #{@player_score}, "].colorize(:blue) +
|
42
|
+
ColorizedString["Computer score: #{@computer_score}, Ties: #{@ties}"].colorize(:blue);
|
43
|
+
player = PrivateMethods.player_choice;
|
44
|
+
computer = Constants::COMPUTER_CHOICES.sample;
|
45
|
+
puts ColorizedString["\nPlayer chooses #{player.to_s.downcase}"].colorize(:blue);
|
46
|
+
puts ColorizedString["Computer chooses #{computer.to_s.downcase}"].colorize(:blue);
|
47
|
+
case PrivateMethods.player_outcome [player, computer]
|
48
|
+
when :WIN
|
49
|
+
puts ColorizedString["#{player.to_s.capitalize} beats #{computer.to_s.downcase}, player wins the round"].colorize(:red);
|
50
|
+
@player_score += 1;
|
51
|
+
when :LOSE
|
52
|
+
puts ColorizedString["#{computer.to_s.capitalize} beats #{player.to_s.downcase}, computer wins the round"].colorize(:red);
|
53
|
+
@computer_score += 1;
|
54
|
+
else
|
55
|
+
puts ColorizedString["Tie, choose again"].colorize(:red);
|
56
|
+
@ties += 1;
|
57
|
+
end;
|
27
58
|
end;
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
59
|
+
puts ColorizedString["\nFinal score: player: #{@player_score}, "].colorize(:blue) +
|
60
|
+
ColorizedString["computer: #{@computer_score} (ties: #{@ties})"].colorize(:blue);
|
61
|
+
case PrivateMethods.final_outcome(@player_score, @computer_score)
|
62
|
+
when :WIN
|
63
|
+
puts ColorizedString["Player wins!"].colorize(:red);
|
64
|
+
when :LOSE
|
65
|
+
puts ColorizedString["Computer wins!"].colorize(:red);
|
66
|
+
else
|
67
|
+
puts ColorizedString["It's a tie!"].colorize(:red);
|
68
|
+
end;
|
69
|
+
gets;
|
70
|
+
end;
|
71
|
+
module PrivateMethods
|
72
|
+
class << self
|
73
|
+
def player_choice
|
74
|
+
loop do
|
75
|
+
print ColorizedString["Choose rock (r), paper (p) or scissors (s): "].colorize(:green);
|
76
|
+
choice = gets.chomp.downcase;
|
77
|
+
return Constants::NTRY_TO_SYM[choice] if Constants::NTRY_TO_SYM.key?(choice);
|
78
|
+
puts ColorizedString["That entry is invalid. Please re-enter."].colorize(:green);
|
36
79
|
end;
|
37
80
|
end;
|
38
|
-
|
39
|
-
|
40
|
-
|
81
|
+
def player_outcome(plays)
|
82
|
+
return :WIN if Constants::WINNERS.include?(plays);
|
83
|
+
return :LOSE if Constants::LOSERS.include?(plays);
|
84
|
+
return :TIE if (!:WIN || !:LOSE);
|
41
85
|
end;
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
player = PrivateMethods.player_choice;
|
47
|
-
computer = Constants::COMPUTER_CHOICES.sample;
|
48
|
-
puts ColorizedString["\nPlayer chooses #{player.to_s.downcase}"].colorize(:blue);
|
49
|
-
puts ColorizedString["Computer chooses #{computer.to_s.downcase}"].colorize(:blue);
|
50
|
-
case PrivateMethods.player_outcome [player, computer]
|
51
|
-
when :WIN
|
52
|
-
puts ColorizedString["#{player.to_s.capitalize} beats #{computer.to_s.downcase}, player wins the round"].colorize(:red);
|
53
|
-
@player_score += 1;
|
54
|
-
when :LOSE
|
55
|
-
puts ColorizedString["#{computer.to_s.capitalize} beats #{player.to_s.downcase}, computer wins the round"].colorize(:red);
|
56
|
-
@computer_score += 1;
|
57
|
-
else
|
58
|
-
puts ColorizedString["Tie, choose again"].colorize(:red);
|
59
|
-
@ties += 1;
|
60
|
-
end;
|
61
|
-
end;
|
62
|
-
puts ColorizedString["\nFinal score: player: #{@player_score}, "].colorize(:blue) +
|
63
|
-
ColorizedString["computer: #{@computer_score} (ties: #{@ties})"].colorize(:blue);
|
64
|
-
case PrivateMethods.final_outcome(@player_score, @computer_score)
|
65
|
-
when :WIN
|
66
|
-
puts ColorizedString["Player wins!"].colorize(:red);
|
67
|
-
when :LOSE
|
68
|
-
puts ColorizedString["Computer wins!"].colorize(:red);
|
69
|
-
else
|
70
|
-
puts ColorizedString["It's a tie!"].colorize(:red);
|
71
|
-
end;
|
72
|
-
gets;
|
86
|
+
def final_outcome(pl, co)
|
87
|
+
return :WIN if pl > co;
|
88
|
+
return :LOSE if pl < co;
|
89
|
+
return :TIE if pl = co;
|
73
90
|
end;
|
74
|
-
private;
|
75
|
-
module PrivateMethods
|
76
|
-
class << self
|
77
|
-
def player_choice
|
78
|
-
loop do
|
79
|
-
print ColorizedString["Choose rock (r), paper (p) or scissors (s): "].colorize(:green);
|
80
|
-
choice = gets.chomp.downcase;
|
81
|
-
return Constants::NTRY_TO_SYM[choice] if Constants::NTRY_TO_SYM.key?(choice);
|
82
|
-
puts ColorizedString["That entry is invalid. Please re-enter"].colorize(:green);
|
83
|
-
end;
|
84
|
-
end;
|
85
|
-
def player_outcome(plays)
|
86
|
-
return :WIN if Constants::WINNERS.include?(plays);
|
87
|
-
return :LOSE if Constants::LOSERS.include?(plays);
|
88
|
-
return :TIE if (!:WIN || !:LOSE);
|
89
|
-
end;
|
90
|
-
def final_outcome(pl, co)
|
91
|
-
return :WIN if pl > co;
|
92
|
-
return :LOSE if pl < co;
|
93
|
-
return :TIE if pl = co;
|
94
|
-
end;
|
95
|
-
end;
|
96
|
-
end;
|
97
91
|
end;
|
98
92
|
end;
|
99
93
|
end;
|
100
94
|
|
101
|
-
PlayRockPaperScissorsGame
|
95
|
+
PlayRockPaperScissorsGame.new.play(2); # best of 3
|
96
|
+
|
data/lib/rps.rb
CHANGED
@@ -12,60 +12,55 @@ class PlayRockPaperScissorsGame
|
|
12
12
|
ColorizedString.colors;
|
13
13
|
ColorizedString.modes;
|
14
14
|
|
15
|
-
|
16
|
-
protected;
|
17
|
-
require "ref/Constants.rb";
|
15
|
+
require "ref/Constants.rb";
|
18
16
|
|
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
|
-
end;
|
51
|
-
end;
|
52
|
-
puts ColorizedString["\nFinal score: player: #{@player_score}, "].colorize(:blue) +
|
53
|
-
ColorizedString["computer: #{@computer_score} (ties: #{@ties})"].colorize(:blue);
|
54
|
-
case PrivateMethods.final_outcome(@player_score, @computer_score)
|
55
|
-
when :WIN
|
56
|
-
puts ColorizedString["Player wins!"].colorize(:red);
|
57
|
-
when :LOSE
|
58
|
-
puts ColorizedString["Computer wins!"].colorize(:red);
|
59
|
-
else
|
60
|
-
puts ColorizedString["It's a tie!"].colorize(:red);
|
61
|
-
end;
|
62
|
-
gets;
|
63
|
-
end;
|
64
|
-
private;
|
65
|
-
require "ref/PrivateMethods.rb";
|
17
|
+
class << self
|
18
|
+
def continue(str1, str2, str3)
|
19
|
+
puts ColorizedString[str1].colorize(:color => :green);
|
20
|
+
print ColorizedString[str2].colorize(:color => :green);
|
21
|
+
gets;
|
22
|
+
puts ColorizedString[str3].colorize(:color => :green);
|
23
|
+
end;
|
24
|
+
end;
|
25
|
+
continue(Constants::INIT_STRINGS[0], Constants::INIT_STRINGS[1], Constants::INIT_STRINGS[2]);
|
26
|
+
def initialize
|
27
|
+
@player_score = @computer_score = @ties = 0;
|
28
|
+
end;
|
29
|
+
def play(winning_score)
|
30
|
+
while @player_score < winning_score && @computer_score < winning_score
|
31
|
+
puts ColorizedString["Player score: #{@player_score}, "].colorize(:blue) +
|
32
|
+
ColorizedString["Computer score: #{@computer_score}, Ties: #{@ties}"].colorize(:blue);
|
33
|
+
player = PrivateMethods.player_choice;
|
34
|
+
computer = Constants::COMPUTER_CHOICES.sample;
|
35
|
+
puts ColorizedString["\nPlayer chooses #{player.to_s.downcase}"].colorize(:blue);
|
36
|
+
puts ColorizedString["Computer chooses #{computer.to_s.downcase}"].colorize(:blue);
|
37
|
+
case PrivateMethods.player_outcome [player, computer]
|
38
|
+
when :WIN
|
39
|
+
puts ColorizedString["#{player.to_s.capitalize} beats #{computer.to_s.downcase}, player wins the round"].colorize(:red);
|
40
|
+
@player_score += 1;
|
41
|
+
when :LOSE
|
42
|
+
puts ColorizedString["#{computer.to_s.capitalize} beats #{player.to_s.downcase}, computer wins the round"].colorize(:red);
|
43
|
+
@computer_score += 1;
|
44
|
+
else
|
45
|
+
puts ColorizedString["Tie, choose again"].colorize(:red);
|
46
|
+
@ties += 1;
|
47
|
+
end;
|
66
48
|
end;
|
67
|
-
|
68
|
-
|
49
|
+
puts ColorizedString["\nFinal score: player: #{@player_score}, "].colorize(:blue) +
|
50
|
+
ColorizedString["computer: #{@computer_score} (ties: #{@ties})"].colorize(:blue);
|
51
|
+
case PrivateMethods.final_outcome(@player_score, @computer_score)
|
52
|
+
when :WIN
|
53
|
+
puts ColorizedString["Player wins!"].colorize(:red);
|
54
|
+
when :LOSE
|
55
|
+
puts ColorizedString["Computer wins!"].colorize(:red);
|
56
|
+
else
|
57
|
+
puts ColorizedString["It's a tie!"].colorize(:red);
|
58
|
+
end;
|
59
|
+
gets;
|
60
|
+
end;
|
61
|
+
require "ref/PrivateMethods.rb";
|
62
|
+
end;
|
69
63
|
|
70
|
-
|
64
|
+
|
65
|
+
PlayRockPaperScissorsGame.new.play(2); # best of 3
|
71
66
|
|
data/rps.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "PlayRockPaperScissorsGame"
|
3
|
-
spec.version = "1.7.
|
3
|
+
spec.version = "1.7.4"
|
4
4
|
spec.date = "2017-04-01"
|
5
5
|
spec.summary = "A Rock Paper Scissors Ruby Gem"
|
6
6
|
spec.description = "A Ruby-programmed rock paper scissors game. To install: gem install PlayRockPaperScissorsGame; To run: rps; or: PlayRockPaperScissorsGame; For issues: https://github.com/bag3318/RockPaperScissors/issues"
|
data/test/test_rps.rb
CHANGED
@@ -6,93 +6,88 @@
|
|
6
6
|
|====================================|
|
7
7
|
=end
|
8
8
|
|
9
|
-
class
|
9
|
+
class RakeTest
|
10
10
|
|
11
11
|
require "colorized_string";
|
12
12
|
ColorizedString.colors;
|
13
13
|
ColorizedString.modes;
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
15
|
+
module Constants
|
16
|
+
NTRY_TO_SYM = { 'p' => :PAPER, 'r' => :ROCK, 's' => :SCISSORS };
|
17
|
+
VALID_ENTRIES = NTRY_TO_SYM.keys;
|
18
|
+
COMPUTER_CHOICES = NTRY_TO_SYM.values;
|
19
|
+
WINNERS = [[:SCISSORS, :PAPER], [:PAPER, :ROCK], [:ROCK, :SCISSORS]]; # format: player choice, computer choice
|
20
|
+
LOSERS = WINNERS.map { |i,j| [j,i] }; # this will take the original WINNERS array and flip the symbols, thus returning a loss for the user/player
|
21
|
+
INIT_STRINGS = ["You are about to enter a rock-paper-scissors best of 3 match.", "Press the return/enter key to continue...", ""];
|
22
|
+
end;
|
23
|
+
|
24
|
+
class << self
|
25
|
+
def continue(str1, str2, str3)
|
26
|
+
puts ColorizedString[str1].colorize(:color => :green);
|
27
|
+
print ColorizedString[str2].colorize(:color => :green);
|
28
|
+
gets;
|
29
|
+
puts ColorizedString[str3].colorize(:color => :green);
|
30
|
+
end;
|
31
|
+
end;
|
32
|
+
continue(Constants::INIT_STRINGS[0], Constants::INIT_STRINGS[1], Constants::INIT_STRINGS[2]);
|
33
|
+
def initialize
|
34
|
+
@player_score = @computer_score = @ties = 0;
|
35
|
+
end;
|
36
|
+
def testPlay(winning_score)
|
37
|
+
while @player_score < winning_score && @computer_score < winning_score
|
38
|
+
puts ColorizedString["Player score: #{@player_score}, "].colorize(:blue) +
|
39
|
+
ColorizedString["Computer score: #{@computer_score}, Ties: #{@ties}"].colorize(:blue);
|
40
|
+
player = PrivateMethods.player_choice;
|
41
|
+
computer = Constants::COMPUTER_CHOICES.sample;
|
42
|
+
puts ColorizedString["\nPlayer chooses #{player.to_s.downcase}"].colorize(:blue);
|
43
|
+
puts ColorizedString["Computer chooses #{computer.to_s.downcase}"].colorize(:blue);
|
44
|
+
case PrivateMethods.player_outcome [player, computer]
|
45
|
+
when :WIN
|
46
|
+
puts ColorizedString["#{player.to_s.capitalize} beats #{computer.to_s.downcase}, player wins the round"].colorize(:red);
|
47
|
+
@player_score += 1;
|
48
|
+
when :LOSE
|
49
|
+
puts ColorizedString["#{computer.to_s.capitalize} beats #{player.to_s.downcase}, computer wins the round"].colorize(:red);
|
50
|
+
@computer_score += 1;
|
51
|
+
else
|
52
|
+
puts ColorizedString["Tie, choose again"].colorize(:red);
|
53
|
+
@ties += 1;
|
54
|
+
end;
|
24
55
|
end;
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
56
|
+
puts ColorizedString["\nFinal score: player: #{@player_score}, "].colorize(:blue) +
|
57
|
+
ColorizedString["computer: #{@computer_score} (ties: #{@ties})"].colorize(:blue);
|
58
|
+
case PrivateMethods.final_outcome(@player_score, @computer_score)
|
59
|
+
when :WIN
|
60
|
+
puts ColorizedString["Player wins!"].colorize(:red);
|
61
|
+
when :LOSE
|
62
|
+
puts ColorizedString["Computer wins!"].colorize(:red);
|
63
|
+
else
|
64
|
+
puts ColorizedString["It's a tie!"].colorize(:red);
|
65
|
+
end;
|
66
|
+
gets;
|
67
|
+
end;
|
68
|
+
module PrivateMethods
|
69
|
+
class << self
|
70
|
+
def player_choice
|
71
|
+
loop do
|
72
|
+
print ColorizedString["Choose rock (r), paper (p) or scissors (s): "].colorize(:green);
|
73
|
+
choice = gets.chomp.downcase;
|
74
|
+
return Constants::NTRY_TO_SYM[choice] if Constants::NTRY_TO_SYM.key?(choice);
|
75
|
+
puts ColorizedString["That entry is invalid. Please re-enter"].colorize(:green);
|
32
76
|
end;
|
33
77
|
end;
|
34
|
-
|
35
|
-
|
36
|
-
|
78
|
+
def player_outcome(plays)
|
79
|
+
return :WIN if Constants::WINNERS.include?(plays);
|
80
|
+
return :LOSE if Constants::LOSERS.include?(plays);
|
81
|
+
return :TIE if !:WIN | !:LOSE;
|
37
82
|
end;
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
player = PrivateMethods.player_choice;
|
43
|
-
computer = Constants::COMPUTER_CHOICES.sample;
|
44
|
-
puts ColorizedString["\nPlayer chooses #{player.to_s.downcase}"].colorize(:blue);
|
45
|
-
puts ColorizedString["Computer chooses #{computer.to_s.downcase}"].colorize(:blue);
|
46
|
-
case PrivateMethods.player_outcome [player, computer]
|
47
|
-
when :WIN
|
48
|
-
puts ColorizedString["#{player.to_s.capitalize} beats #{computer.to_s.downcase}, player wins the round"].colorize(:red);
|
49
|
-
@player_score += 1;
|
50
|
-
when :LOSE
|
51
|
-
puts ColorizedString["#{computer.to_s.capitalize} beats #{player.to_s.downcase}, computer wins the round"].colorize(:red);
|
52
|
-
@computer_score += 1;
|
53
|
-
else
|
54
|
-
puts ColorizedString["Tie, choose again"].colorize(:red);
|
55
|
-
@ties += 1;
|
56
|
-
end;
|
57
|
-
end;
|
58
|
-
puts ColorizedString["\nFinal score: player: #{@player_score}, "].colorize(:blue) +
|
59
|
-
ColorizedString["computer: #{@computer_score} (ties: #{@ties})"].colorize(:blue);
|
60
|
-
case PrivateMethods.final_outcome(@player_score, @computer_score)
|
61
|
-
when :WIN
|
62
|
-
puts ColorizedString["Player wins!"].colorize(:red);
|
63
|
-
when :LOSE
|
64
|
-
puts ColorizedString["Computer wins!"].colorize(:red);
|
65
|
-
else
|
66
|
-
puts ColorizedString["It's a tie!"].colorize(:red);
|
67
|
-
end;
|
68
|
-
gets;
|
83
|
+
def final_outcome(pl, co)
|
84
|
+
return :WIN if pl > co;
|
85
|
+
return :LOSE if pl < co;
|
86
|
+
return :TIE if pl = co;
|
69
87
|
end;
|
70
|
-
private;
|
71
|
-
module PrivateMethods
|
72
|
-
class << self
|
73
|
-
def player_choice
|
74
|
-
loop do
|
75
|
-
print ColorizedString["Choose rock (r), paper (p) or scissors (s): "].colorize(:green);
|
76
|
-
choice = gets.chomp.downcase;
|
77
|
-
return Constants::NTRY_TO_SYM[choice] if Constants::NTRY_TO_SYM.key?(choice);
|
78
|
-
puts ColorizedString["That entry is invalid. Please re-enter"].colorize(:green);
|
79
|
-
end;
|
80
|
-
end;
|
81
|
-
def player_outcome(plays)
|
82
|
-
return :WIN if Constants::WINNERS.include?(plays);
|
83
|
-
return :LOSE if Constants::LOSERS.include?(plays);
|
84
|
-
return :TIE if !:WIN | !:LOSE;
|
85
|
-
end;
|
86
|
-
def final_outcome(pl, co)
|
87
|
-
return :WIN if pl > co;
|
88
|
-
return :LOSE if pl < co;
|
89
|
-
return :TIE if pl = co;
|
90
|
-
end;
|
91
|
-
end;
|
92
|
-
end;
|
93
88
|
end;
|
94
89
|
end;
|
95
90
|
end;
|
96
91
|
|
97
|
-
|
92
|
+
RakeTest.new.testPlay(2); # best of 3
|
98
93
|
|