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