PlayRockPaperScissorsGame 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/PlayRockPaperScissorsGame +117 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14e89081bf1a44f72b348861fc68a8a063a66785
|
4
|
+
data.tar.gz: b9b6873847e1c5e87302b03a80ff25d6e271de28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c95ef383ad88feecbbe4265ae30a6a9618cd1190bd111a5b0a350616b5b9d2c3fc5daca40b1efaffcd234245f8c977e6a555d85d938ad53cbd4c5567f01ed73
|
7
|
+
data.tar.gz: 1e4bf4a3b5f62bb3ad08fe2f5dddfae2beb30dc89aa5596c592dd5fbcb22b7faaa47a164dabb30631338c55eb1f8a505f26f40cd08b70c66f60d92aebfd60f00
|
@@ -0,0 +1,117 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
=begin
|
4
|
+
|=================================|
|
5
|
+
| How to Run |
|
6
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
|
7
|
+
| Mac |
|
8
|
+
|---------------------------------|
|
9
|
+
| $ ruby ~/Desktop/rps/bin/rps.rb |
|
10
|
+
|*********************************|
|
11
|
+
| Windows |
|
12
|
+
|---------------------------------|
|
13
|
+
| > CD Desktop/rps/bin |
|
14
|
+
| > rps.rb |
|
15
|
+
|_________________________________|
|
16
|
+
=end
|
17
|
+
|
18
|
+
class Master
|
19
|
+
|
20
|
+
class RPS
|
21
|
+
|
22
|
+
module Constants
|
23
|
+
NTRY_TO_SYM = {
|
24
|
+
'p' => :PAPER,
|
25
|
+
'r' => :ROCK,
|
26
|
+
's' => :SCISSORS
|
27
|
+
};
|
28
|
+
VALID_ENTRIES = NTRY_TO_SYM.keys;
|
29
|
+
COMPUTER_CHOICES = NTRY_TO_SYM.values;
|
30
|
+
WINNERS = [
|
31
|
+
[:SCISSORS, :PAPER],
|
32
|
+
[:PAPER, :ROCK],
|
33
|
+
[:ROCK, :SCISSORS]
|
34
|
+
]; # format: player choice, computer choice
|
35
|
+
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
|
36
|
+
INIT_STRINGS = [
|
37
|
+
"You are about to enter a rock-paper-scissors best of 3 match.",
|
38
|
+
"Press the return/enter key to continue...",
|
39
|
+
""
|
40
|
+
];
|
41
|
+
end;
|
42
|
+
|
43
|
+
class RockPaperScissors
|
44
|
+
class << self
|
45
|
+
def continue(str1, str2, str3)
|
46
|
+
puts str1;
|
47
|
+
print str2;
|
48
|
+
gets;
|
49
|
+
puts str3;
|
50
|
+
end;
|
51
|
+
end;
|
52
|
+
continue(Constants::INIT_STRINGS[0], Constants::INIT_STRINGS[1], Constants::INIT_STRINGS[2]);
|
53
|
+
def initialize
|
54
|
+
@player_score = @computer_score = @ties = 0;
|
55
|
+
end;
|
56
|
+
def play(winning_score)
|
57
|
+
while @player_score < winning_score && @computer_score < winning_score
|
58
|
+
puts "Player score: #{@player_score}, " +
|
59
|
+
"Computer score: #{@computer_score}, Ties: #{@ties}";
|
60
|
+
player = PrivateVars.player_choice;
|
61
|
+
computer = Constants::COMPUTER_CHOICES.sample;
|
62
|
+
puts "\nPlayer chooses #{player.to_s.downcase}";
|
63
|
+
puts "Computer chooses #{computer.to_s.downcase}";
|
64
|
+
case PrivateVars.player_outcome [player, computer]
|
65
|
+
when :WIN
|
66
|
+
puts "#{player.to_s.capitalize} beats #{computer.to_s.downcase}, player wins the round";
|
67
|
+
@player_score += 1;
|
68
|
+
when :LOSE
|
69
|
+
puts "#{computer.to_s.capitalize} beats #{player.to_s.downcase}, computer wins the round";
|
70
|
+
@computer_score += 1;
|
71
|
+
else
|
72
|
+
puts "Tie, choose again";
|
73
|
+
@ties += 1;
|
74
|
+
end;
|
75
|
+
end;
|
76
|
+
puts "\nFinal score: player: #{@player_score}, " +
|
77
|
+
"computer: #{@computer_score} (ties: #{@ties})";
|
78
|
+
case PrivateVars.final_outcome(@player_score, @computer_score)
|
79
|
+
when :WIN
|
80
|
+
puts "Player wins!";
|
81
|
+
when :LOSE
|
82
|
+
puts "Computer wins!";
|
83
|
+
else
|
84
|
+
puts "It's a tie!";
|
85
|
+
end;
|
86
|
+
puts "";
|
87
|
+
gets;
|
88
|
+
end;
|
89
|
+
private
|
90
|
+
module PrivateVars
|
91
|
+
class << self
|
92
|
+
def player_choice
|
93
|
+
loop do
|
94
|
+
print "Choose rock (r), paper (p) or scissors (s): ";
|
95
|
+
choice = gets.chomp.downcase;
|
96
|
+
return Constants::NTRY_TO_SYM[choice] if Constants::NTRY_TO_SYM.key?(choice);
|
97
|
+
puts "That entry is invalid. Please re-enter";
|
98
|
+
end;
|
99
|
+
end;
|
100
|
+
def player_outcome(plays)
|
101
|
+
return :WIN if Constants::WINNERS.include?(plays);
|
102
|
+
return :LOSE if Constants::LOSERS.include?(plays);
|
103
|
+
return :TIE if (!:WIN || !:LOSE);
|
104
|
+
end;
|
105
|
+
def final_outcome(pl, co)
|
106
|
+
return :WIN if pl > co;
|
107
|
+
return :LOSE if pl < co;
|
108
|
+
return :TIE if pl = co;
|
109
|
+
end;
|
110
|
+
end;
|
111
|
+
end;
|
112
|
+
end;
|
113
|
+
end;
|
114
|
+
end;
|
115
|
+
|
116
|
+
Master::RPS::RockPaperScissors.new.play(3); # best of 3
|
117
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: PlayRockPaperScissorsGame
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- bag3318
|
@@ -42,11 +42,13 @@ description: A Ruby-programmed rock paper scissors game
|
|
42
42
|
email: ''
|
43
43
|
executables:
|
44
44
|
- rps
|
45
|
+
- PlayRockPaperScissorsGame
|
45
46
|
extensions: []
|
46
47
|
extra_rdoc_files:
|
47
48
|
- README.md
|
48
49
|
files:
|
49
50
|
- README.md
|
51
|
+
- bin/PlayRockPaperScissorsGame
|
50
52
|
- bin/rps
|
51
53
|
homepage: https://rubygems.org/gems/PlayRockPaperScissorsGame/
|
52
54
|
licenses:
|