PlayRockPaperScissorsGame 2.4.3 → 2.4.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73fd45bc0d5ac753551c7a450a0430a5a1c48b0b
4
- data.tar.gz: a766b7a3a93320729bb7abab7ac9c9249f5cb92d
3
+ metadata.gz: 3ed27c6319b475fc6208597675960f3c8d2e4d43
4
+ data.tar.gz: 2013bc0146fed6a2fc9ba070b2d229e22b616b77
5
5
  SHA512:
6
- metadata.gz: 1a9f4564eed49b6b1e54855ef152fabd41f4af6a5605118ecc622bbfacbb5a62c68a6736937e72eaf4a0527e0f76e107f9a13df73bd6ba870828ffaabe7f773e
7
- data.tar.gz: 1dcbd00613af9ea5f23091b332a342ef6378020659d2613463847e873e0c49b0b7cebe05d2aad35d2e29deb3ced4feda5f9d3722913c5245086d06734c518774
6
+ metadata.gz: 07a36525c4378a69d4fa4bf682fa018660fda33588b7659baaf110debab57a9a6f4cea59918bcd4356294159091917cbb0eb8d993df0fde2df02c377613904e4
7
+ data.tar.gz: e41b3d3e3bda1e5bb46dfea08df420903eeb458461e1cc12de89139b3dddab9ff77597075e201cfd326b42d1b285f0e7c52aa3d6627b3b805d8258d337c821af
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/.gitignore CHANGED
@@ -5,6 +5,7 @@
5
5
  *.sublime-workspace
6
6
  *.jekyll-metadata
7
7
  *master
8
+ *beginner.rb
8
9
  # _site
9
10
  # .sass-cache
10
11
 
@@ -82,4 +82,4 @@ available at [http://contributor-covenant.org/version/1/4][version]
82
82
 
83
83
  ## Site Nav
84
84
 
85
- [Home](./) | [How to Build](How_to_Build) | [Master Code](Code) | [Contributing](CONTRIBUTING) | [How to Test](Testing)
85
+ [Home](./) | [Comparing Code](Comparing_Code) | [How to Build](How_to_Build) | [Master Code](Code) | [Contributing](CONTRIBUTING) | [How to Test](Testing)
data/docs/CONTRIBUTING.md CHANGED
@@ -41,4 +41,4 @@ Here are a few things you can do that will increase the likelihood of your pull
41
41
 
42
42
  ## Site Nav
43
43
 
44
- [Home](./) | [How to Build](How_to_Build) | [Master Code](Code) | [Code of Conduct](CODE_OF_CONDUCT) | [How to Test](Testing.md)
44
+ [Home](./) | [Comparing Code](Comparing_Code) | [How to Build](How_to_Build) | [Master Code](Code) | [Code of Conduct](CODE_OF_CONDUCT) | [How to Test](Testing.md)
data/docs/Code.md CHANGED
@@ -25,11 +25,10 @@ __`% rps`__
25
25
  |====================================|
26
26
  =end
27
27
 
28
-
29
28
  class PlayRockPaperScissorsGame # define master class
30
29
 
31
30
  module RockPaperScissors
32
- VERSION = "2.4.3" # define version constant
31
+ VERSION = "2.4.4" # define version constant
33
32
  end
34
33
 
35
34
  # intiate the colorize gem
@@ -70,7 +69,7 @@ class PlayRockPaperScissorsGame # define master class
70
69
  end
71
70
  end
72
71
 
73
- continue(Constants::INIT_STRINGS[0], Constants::INIT_STRINGS[1], Constants::INIT_STRINGS[2]) # call continue class
72
+ continue(Constants::INIT_STRINGS[0], Constants::INIT_STRINGS[1], Constants::INIT_STRINGS[2]) # call continue method
74
73
 
75
74
  def initialize # initialize variables
76
75
  @player_score = @computer_score = @ties = 0
@@ -98,7 +97,7 @@ class PlayRockPaperScissorsGame # define master class
98
97
  end
99
98
  puts ColorizedString["\nFinal score: player: #{@player_score}, "].colorize(:blue) +
100
99
  ColorizedString["computer: #{@computer_score} (ties: #{@ties})"].colorize(:blue)
101
- # define a case for the final outcomes
100
+ # define a case for the final outcomes
102
101
  case PrivateMethods.final_outcome(@player_score, @computer_score)
103
102
  when :WIN
104
103
  puts ColorizedString["Player wins!"].colorize(:red)
@@ -134,7 +133,7 @@ class PlayRockPaperScissorsGame # define master class
134
133
  def player_outcome(plays) # define method for the player's outcome while passing in a parameter of type array
135
134
  return :WIN if Constants::WINNERS.include?(plays) # return a win if the one of the sub-arrays in the winners array is called
136
135
  return :LOSE if Constants::LOSERS.include?(plays) # return a loss if any of the mapped sub-arrays in the losers constant is present
137
- return :TIE if !:WIN | !:LOSE # return a tie if not (!x) win or if not loose
136
+ return :TIE if !:WIN | !:LOSE # return a tie if not (!) win or if not loose
138
137
  end
139
138
  def final_outcome(pl,co) # define final outcome method
140
139
  return :WIN if pl > co
@@ -155,6 +154,6 @@ PlayRockPaperScissorsGame.new.play(2) # call the play method and pass in 3 (0, 1
155
154
 
156
155
  ## Site Nav
157
156
 
158
- [Home](./) | [How to Test](Testing) | [How to Build](How_to_Build) | [Contributing](CONTRIBUTING) | [Code of Conduct](CODE_OF_CONDUCT)
157
+ [Home](./) | [Comparing Code](Comparing_Code) | [How to Test](Testing) | [How to Build](How_to_Build) | [Contributing](CONTRIBUTING) | [Code of Conduct](CODE_OF_CONDUCT)
159
158
 
160
159
  [//]: # (end markdown)
@@ -0,0 +1,228 @@
1
+ ---
2
+ layout: default
3
+ title: Comparing Code - Play Rock Paper Scissors Game
4
+ ---
5
+
6
+ [//]: # (start markdown)
7
+
8
+ # Comparing Beginner's RPS Code to Advanced RPS Code
9
+
10
+ **Table of Contents**
11
+
12
+ - [Comparing Beginner's RPS Code to Advanced RPS Code](#comparing-beginners-rps-code-to-advanced-rps-code)
13
+ - [Beginner's Code](#beginners-code)
14
+ - [Beginner's Code Pros](#beginners-code-pros)
15
+ - [Beginner's Code Cons](#beginners-code-cons)
16
+ - [What Beginner's Code Looks :ike](#what-beginners-code-looks-ike)
17
+ - [Advanced Code](#advanced-code)
18
+ - [Advanced Code Pros](#advanced-code-pros)
19
+ - [Advanced Code Cons](#advanced-code-cons)
20
+ - [What Advanced Code Looks Like](#what-advanced-code-looks-like)
21
+ - [Site Nav](#site-nav)
22
+
23
+ ## Beginner's Code
24
+
25
+ ### Beginner's Code Pros
26
+ + Simple
27
+ - Easy to understand
28
+ * Short
29
+
30
+ ### Beginner's Code Cons
31
+ + Unorganized
32
+ - Lacks modularization
33
+ * Many `if`s and `else`s
34
+
35
+ ### What Beginner's Code Looks :ike
36
+
37
+ __`% ruby beginner.rb`__
38
+
39
+ [//]: # (end markdown)
40
+
41
+ [//]: # (start ruby)
42
+
43
+ ```ruby
44
+ options = ["rock", "paper", "scissors"]
45
+ computer_choice = options[rand(options.length)]
46
+ # the above line chooses a random pick in the options array, and the `options.length` means to choose from however many options there are in the `options` array
47
+ puts "What's your choice?"
48
+ puts "rock, paper, or scissors"
49
+ user_input = gets.chomp.downcase # `gets.chomp` reads user input, .downcase is like `.toLowerCase()` in javascript
50
+ loop do
51
+ if (user_input == "rock" || user_input == "paper" || user_input == "scissors")
52
+ if (user_input == computer_choice)
53
+ puts "We got the same, let's keep playing!"
54
+ elsif (user_input == "rock" && computer_choice == "scissors")
55
+ puts "computer choice is: " + computer_choice + " , you win! :)"
56
+ elsif (user_input == "rock" && computer_choice == "paper")
57
+ puts "computer choice is: " + computer_choice + " ,computer wins :("
58
+ elsif (user_input == "paper" && computer_choice == "scissors")
59
+ puts "computer choice is: " + computer_choice + " ,computer wins :("
60
+ elsif (user_input == "paper" && computer_choice == "rock")
61
+ puts "computer choice is: " + computer_choice + " , you win! :)"
62
+ elsif (user_input == "scissors" && computer_choice == "rock")
63
+ puts "computer choice is: " + computer_choice + " ,computer wins :("
64
+ elsif (user_input == "scissors" && computer_choice == "paper")
65
+ puts "computer choice is: " + computer_choice + " , you win! :)"
66
+ end
67
+ else
68
+ puts "Invalid choice, enter Rock, Paper, or Scissors"
69
+ puts "What's your choice?"
70
+ user_input = gets.chomp.downcase # have the user choose again
71
+ end
72
+ break # break the loop after all the above has been completed
73
+ end
74
+ ```
75
+ [//]: # (end ruby)
76
+ [//]: # (start markdown)
77
+ ## Advanced Code
78
+
79
+ ### Advanced Code Pros
80
+
81
+ + Organized
82
+ - Modularized
83
+ * Structured
84
+
85
+ ### Advanced Code Cons
86
+
87
+ + Long
88
+ - Hard to understand
89
+
90
+ ### What Advanced Code Looks Like
91
+
92
+ __`% ruby advanced.rb`__
93
+
94
+ [//]: # (end markdown)
95
+
96
+ [//]: # (start ruby)
97
+ ```ruby
98
+ class PlayRockPaperScissorsGame # define master class
99
+
100
+ # intiate the colorize gem
101
+ require "colorized_string"
102
+ ColorizedString.colors # import colors; ex: red, green, blue
103
+ ColorizedString.modes # import modes; ex: bold, italic, underline
104
+
105
+ module Constants
106
+ NTRY_TO_SYM = { # define constants from an entry to a symbol class
107
+ 'p' => :PAPER,
108
+ 'r' => :ROCK,
109
+ 's' => :SCISSORS
110
+ }
111
+ VALID_ENTRIES = NTRY_TO_SYM.keys # create valid entries
112
+ COMPUTER_CHOICES = NTRY_TO_SYM.values # define computer choices
113
+ WINNERS = [ # define winners
114
+ # format: player choice, computer choice
115
+ [:SCISSORS, :PAPER],
116
+ [:PAPER , :ROCK],
117
+ [:ROCK , :SCISSORS]
118
+ ]
119
+ LOSERS = WINNERS.map { |pc,cc| [cc,pc] } # this will take the original WINNERS array and flip the symbols, thus returning a loss for the user/player
120
+ INIT_STRINGS = [
121
+ ColorizedString["You are about to enter a rock-paper-scissors best of 3 match."].colorize(:green),
122
+ ColorizedString["Press the return/enter key to continue..."].colorize(:green),
123
+ ""
124
+ ]
125
+ end
126
+
127
+ protected_methods :Constants # make the constants module protected
128
+
129
+ class << self # define a self calling method within the parent class
130
+ def continue(str1,str2,str3) # pass in 3 parameters
131
+ puts ColorizedString[str1].colorize(:color => :green)
132
+ print ColorizedString[str2].colorize(:color => :green)
133
+ gets # press enter or return to continue
134
+ puts str3
135
+ end
136
+ end
137
+
138
+ continue(Constants::INIT_STRINGS[0], Constants::INIT_STRINGS[1], Constants::INIT_STRINGS[2]) # call continue method
139
+
140
+ def initialize # initialize variables
141
+ @player_score = @computer_score = @ties = 0
142
+ end
143
+ def play(winning_score)
144
+ while @player_score < winning_score && @computer_score < winning_score # both the computer's score and the player's score have to be less than the value passed in for the winning score at the end
145
+ puts ColorizedString["Player score: #{@player_score}, "].colorize(:blue) +
146
+ ColorizedString["Computer score: #{@computer_score}, Ties: #{@ties}"].colorize(:blue)
147
+ player = PrivateMethods.player_choice
148
+ computer = Constants::COMPUTER_CHOICES.sample # chooses a "random" option
149
+ puts ColorizedString["\nPlayer chooses #{player.to_s.downcase}"].colorize(:blue)
150
+ puts ColorizedString["Computer chooses #{computer.to_s.downcase}"].colorize(:blue)
151
+ case PrivateMethods.player_outcome [player, computer] # define a reference call for player and computer for the arrays called in the player_outcome method
152
+ when :WIN
153
+ puts ColorizedString["#{player.to_s.capitalize} beats #{computer.to_s.downcase}, player wins the round"].colorize(:red)
154
+ @player_score += 1 # @player_score = @player_score + 1
155
+ when :LOSE
156
+ puts ColorizedString["#{computer.to_s.capitalize} beats #{player.to_s.downcase}, computer wins the round"].colorize(:red)
157
+ @computer_score += 1
158
+ else # since there is only 1 option left, there is no need to define when :TIE
159
+ puts ColorizedString["Tie, choose again"].colorize(:red)
160
+ @ties += 1
161
+ # since tie is not in the original pass-in argument for the while loop, it will not be affected by the winning score
162
+ end
163
+ end
164
+ puts ColorizedString["\nFinal score: player: #{@player_score}, "].colorize(:blue) +
165
+ ColorizedString["computer: #{@computer_score} (ties: #{@ties})"].colorize(:blue)
166
+ # define a case for the final outcomes
167
+ case PrivateMethods.final_outcome(@player_score, @computer_score)
168
+ when :WIN
169
+ puts ColorizedString["Player wins!"].colorize(:red)
170
+ when :LOSE
171
+ puts ColorizedString["Computer wins!"].colorize(:red)
172
+ else
173
+ puts ColorizedString["It's a tie!"].colorize(:red)
174
+ end
175
+ gets
176
+ end
177
+
178
+ module PrivateMethods
179
+ class << self # make all methods below self calling methods of this class (PlayRockPaperScissorsGame) so that the other methods will not to call their parents of these methods
180
+ def player_choice
181
+ loop do # for loop with no arguments passed in
182
+ print ColorizedString["Choose rock (r), paper (p) or scissors (s): "].colorize(:green)
183
+ choice = gets.chomp.downcase # read user input and convert all to lower case
184
+ # define valid and invalid entries by using an if else-if statement(s)
185
+ if Constants::NTRY_TO_SYM.key?(choice) # if the NTRY_TO_SYM array's key is one of the keys defined in the original array
186
+ return Constants::NTRY_TO_SYM[choice] # return the users choice
187
+ elsif choice != Constants::VALID_ENTRIES # else if it is not one of the valid entries...
188
+ puts ColorizedString["That entry is invalid. Please re-enter."].colorize(:red) # return an error message
189
+ end
190
+ # # one may also do this (albeit more useless):
191
+ # case
192
+ # when Constants::NTRY_TO_SYM.key?(choice)
193
+ # return Constants::NTRY_TO_SYM[choice]
194
+ # when choice != Constants::VALID_ENTRIES
195
+ # puts ColorizedString["That entry is invalid. Please re-enter."].colorize(:green)
196
+ # end
197
+ end
198
+ end
199
+ def player_outcome(plays) # define method for the player's outcome while passing in a parameter of type array
200
+ return :WIN if Constants::WINNERS.include?(plays) # return a win if the one of the sub-arrays in the winners array is called
201
+ return :LOSE if Constants::LOSERS.include?(plays) # return a loss if any of the mapped sub-arrays in the losers constant is present
202
+ return :TIE if !:WIN | !:LOSE # return a tie if not (!) win or if not loose
203
+ end
204
+ def final_outcome(pl,co) # define final outcome method
205
+ return :WIN if pl > co
206
+ return :LOSE if pl < co
207
+ return :TIE if pl = co # this can never happen due to the code in the play method, but it is worth noting
208
+ end
209
+ end
210
+ end
211
+
212
+ private_methods :PrivateMethods # make the PrivateMethods module private
213
+
214
+ end
215
+
216
+ PlayRockPaperScissorsGame.new.play(2) # call the play method and pass in 3 (0, 1, 2) for the winning score
217
+ ```
218
+ [//]: # (end ruby)
219
+
220
+ [//]: # (start markdown)
221
+
222
+ ------------
223
+
224
+ ## Site Nav
225
+
226
+ [Home](./) | [Master Code](Code) | [How to Test](Testing) | [How to Build](How_to_Build) | [Contributing](CONTRIBUTING) | [Code of Conduct](CODE_OF_CONDUCT)
227
+
228
+ [//]: # (end markdown)
data/docs/How_to_Build.md CHANGED
@@ -25,4 +25,4 @@ title: How to Build - Play Rock Paper Scissors Game
25
25
 
26
26
  ## Site Nav
27
27
 
28
- [Home](./) | [How to Test](Testing) | [Master Code](Code) | [Contributing](CONTRIBUTING) | [Code of Conduct](CODE_OF_CONDUCT)
28
+ [Home](./) | [Comparing Code](Comparing_Code) | [How to Test](Testing) | [Master Code](Code) | [Contributing](CONTRIBUTING) | [Code of Conduct](CODE_OF_CONDUCT)
data/docs/Testing.md CHANGED
@@ -22,4 +22,4 @@ What you will need
22
22
 
23
23
  ## Site Nav
24
24
 
25
- [Home](./) | [How to Build](How_to_Build) | [Master Code](Code) | [Contributing](CONTRIBUTING) | [Code of Conduct](CODE_OF_CONDUCT)
25
+ [Home](./) | [Comparing Code](Comparing_Code) | [How to Build](How_to_Build) | [Master Code](Code) | [Contributing](CONTRIBUTING) | [Code of Conduct](CODE_OF_CONDUCT)
@@ -8,6 +8,8 @@
8
8
  <meta name="theme-color" content="#157878">
9
9
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
10
10
  <link rel="stylesheet" href="{{ '/assets/css/style.css?v=' | append: site.github.build_revision | relative_url }}">
11
+ <link rel="stylesheet" href="assets/css/pace.css" />
12
+ <script src="assets/js/pace.min.js"></script>
11
13
  </head>
12
14
  <body>
13
15
  <section class="page-header">
@@ -7,7 +7,9 @@
7
7
  <meta name="viewport" content="width=device-width, initial-scale=1">
8
8
  <meta name="theme-color" content="#157878">
9
9
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
10
- <link rel="stylesheet" href="/assets/css/style.css?v=b9b8e81243f85ef7e306bbe53b23572f793119fb">
10
+ <link rel="stylesheet" href="/assets/css/style.css?v=ddb92b73967ef3a920af7c36e2a3cb938bfc1f06">
11
+ <link rel="stylesheet" href="assets/css/pace.css" />
12
+ <script src="assets/js/pace.min.js"></script>
11
13
  </head>
12
14
  <body>
13
15
  <section class="page-header">
@@ -115,6 +117,7 @@ available at <a href="http://contributor-covenant.org/version/1/4/">http://contr
115
117
  <tbody>
116
118
  <tr>
117
119
  <td><a href="./">Home</a></td>
120
+ <td><a href="Comparing_Code">Comparing Code</a></td>
118
121
  <td><a href="How_to_Build">How to Build</a></td>
119
122
  <td><a href="Code">Master Code</a></td>
120
123
  <td><a href="CONTRIBUTING">Contributing</a></td>
@@ -7,7 +7,9 @@
7
7
  <meta name="viewport" content="width=device-width, initial-scale=1">
8
8
  <meta name="theme-color" content="#157878">
9
9
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
10
- <link rel="stylesheet" href="/assets/css/style.css?v=b9b8e81243f85ef7e306bbe53b23572f793119fb">
10
+ <link rel="stylesheet" href="/assets/css/style.css?v=ddb92b73967ef3a920af7c36e2a3cb938bfc1f06">
11
+ <link rel="stylesheet" href="assets/css/pace.css" />
12
+ <script src="assets/js/pace.min.js"></script>
11
13
  </head>
12
14
  <body>
13
15
  <section class="page-header">
@@ -74,6 +76,7 @@
74
76
  <tbody>
75
77
  <tr>
76
78
  <td><a href="./">Home</a></td>
79
+ <td><a href="Comparing_Code">Comparing Code</a></td>
77
80
  <td><a href="How_to_Build">How to Build</a></td>
78
81
  <td><a href="Code">Master Code</a></td>
79
82
  <td><a href="CODE_OF_CONDUCT">Code of Conduct</a></td>
data/docs/_site/Code.html CHANGED
@@ -7,7 +7,9 @@
7
7
  <meta name="viewport" content="width=device-width, initial-scale=1">
8
8
  <meta name="theme-color" content="#157878">
9
9
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
10
- <link rel="stylesheet" href="/assets/css/style.css?v=b9b8e81243f85ef7e306bbe53b23572f793119fb">
10
+ <link rel="stylesheet" href="/assets/css/style.css?v=ddb92b73967ef3a920af7c36e2a3cb938bfc1f06">
11
+ <link rel="stylesheet" href="assets/css/pace.css" />
12
+ <script src="assets/js/pace.min.js"></script>
11
13
  </head>
12
14
  <body>
13
15
  <section class="page-header">
@@ -48,11 +50,10 @@
48
50
  |====================================|
49
51
  =end</span>
50
52
 
51
-
52
53
  <span class="k">class</span> <span class="nc">PlayRockPaperScissorsGame</span> <span class="c1"># define master class</span>
53
54
 
54
55
  <span class="k">module</span> <span class="nn">RockPaperScissors</span>
55
- <span class="no">VERSION</span> <span class="o">=</span> <span class="s2">"2.4.3"</span> <span class="c1"># define version constant</span>
56
+ <span class="no">VERSION</span> <span class="o">=</span> <span class="s2">"2.4.4"</span> <span class="c1"># define version constant</span>
56
57
  <span class="k">end</span>
57
58
 
58
59
  <span class="c1"># intiate the colorize gem</span>
@@ -93,7 +94,7 @@
93
94
  <span class="k">end</span>
94
95
  <span class="k">end</span>
95
96
 
96
- <span class="n">continue</span><span class="p">(</span><span class="no">Constants</span><span class="o">::</span><span class="no">INIT_STRINGS</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="no">Constants</span><span class="o">::</span><span class="no">INIT_STRINGS</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="no">Constants</span><span class="o">::</span><span class="no">INIT_STRINGS</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="c1"># call continue class</span>
97
+ <span class="n">continue</span><span class="p">(</span><span class="no">Constants</span><span class="o">::</span><span class="no">INIT_STRINGS</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="no">Constants</span><span class="o">::</span><span class="no">INIT_STRINGS</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="no">Constants</span><span class="o">::</span><span class="no">INIT_STRINGS</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="c1"># call continue method</span>
97
98
 
98
99
  <span class="k">def</span> <span class="nf">initialize</span> <span class="c1"># initialize variables</span>
99
100
  <span class="vi">@player_score</span> <span class="o">=</span> <span class="vi">@computer_score</span> <span class="o">=</span> <span class="vi">@ties</span> <span class="o">=</span> <span class="mi">0</span>
@@ -121,7 +122,7 @@
121
122
  <span class="k">end</span>
122
123
  <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"</span><span class="se">\n</span><span class="s2">Final score: player: </span><span class="si">#{</span><span class="vi">@player_score</span><span class="si">}</span><span class="s2">, "</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:blue</span><span class="p">)</span> <span class="o">+</span>
123
124
  <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"computer: </span><span class="si">#{</span><span class="vi">@computer_score</span><span class="si">}</span><span class="s2"> (ties: </span><span class="si">#{</span><span class="vi">@ties</span><span class="si">}</span><span class="s2">)"</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:blue</span><span class="p">)</span>
124
- <span class="c1"># define a case for the final outcomes </span>
125
+ <span class="c1"># define a case for the final outcomes </span>
125
126
  <span class="k">case</span> <span class="no">PrivateMethods</span><span class="p">.</span><span class="nf">final_outcome</span><span class="p">(</span><span class="vi">@player_score</span><span class="p">,</span> <span class="vi">@computer_score</span><span class="p">)</span>
126
127
  <span class="k">when</span> <span class="ss">:WIN</span>
127
128
  <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"Player wins!"</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:red</span><span class="p">)</span>
@@ -157,7 +158,7 @@
157
158
  <span class="k">def</span> <span class="nf">player_outcome</span><span class="p">(</span><span class="n">plays</span><span class="p">)</span> <span class="c1"># define method for the player's outcome while passing in a parameter of type array</span>
158
159
  <span class="k">return</span> <span class="ss">:WIN</span> <span class="k">if</span> <span class="no">Constants</span><span class="o">::</span><span class="no">WINNERS</span><span class="p">.</span><span class="nf">include?</span><span class="p">(</span><span class="n">plays</span><span class="p">)</span> <span class="c1"># return a win if the one of the sub-arrays in the winners array is called</span>
159
160
  <span class="k">return</span> <span class="ss">:LOSE</span> <span class="k">if</span> <span class="no">Constants</span><span class="o">::</span><span class="no">LOSERS</span><span class="p">.</span><span class="nf">include?</span><span class="p">(</span><span class="n">plays</span><span class="p">)</span> <span class="c1"># return a loss if any of the mapped sub-arrays in the losers constant is present</span>
160
- <span class="k">return</span> <span class="ss">:TIE</span> <span class="k">if</span> <span class="o">!</span><span class="ss">:WIN</span> <span class="o">|</span> <span class="o">!</span><span class="ss">:LOSE</span> <span class="c1"># return a tie if not (!x) win or if not loose</span>
161
+ <span class="k">return</span> <span class="ss">:TIE</span> <span class="k">if</span> <span class="o">!</span><span class="ss">:WIN</span> <span class="o">|</span> <span class="o">!</span><span class="ss">:LOSE</span> <span class="c1"># return a tie if not (!) win or if not loose</span>
161
162
  <span class="k">end</span>
162
163
  <span class="k">def</span> <span class="nf">final_outcome</span><span class="p">(</span><span class="n">pl</span><span class="p">,</span><span class="n">co</span><span class="p">)</span> <span class="c1"># define final outcome method</span>
163
164
  <span class="k">return</span> <span class="ss">:WIN</span> <span class="k">if</span> <span class="n">pl</span> <span class="o">&gt;</span> <span class="n">co</span>
@@ -183,6 +184,7 @@
183
184
  <tbody>
184
185
  <tr>
185
186
  <td><a href="./">Home</a></td>
187
+ <td><a href="Comparing_Code">Comparing Code</a></td>
186
188
  <td><a href="Testing">How to Test</a></td>
187
189
  <td><a href="How_to_Build">How to Build</a></td>
188
190
  <td><a href="CONTRIBUTING">Contributing</a></td>
@@ -0,0 +1,296 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en-us">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Comparing Code - Play Rock Paper Scissors Game</title>
6
+ <meta name="description" content="A Ruby Programmed Rock Paper Scissors Game"/>
7
+ <meta name="viewport" content="width=device-width, initial-scale=1">
8
+ <meta name="theme-color" content="#157878">
9
+ <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
10
+ <link rel="stylesheet" href="/assets/css/style.css?v=ddb92b73967ef3a920af7c36e2a3cb938bfc1f06">
11
+ <link rel="stylesheet" href="assets/css/pace.css" />
12
+ <script src="assets/js/pace.min.js"></script>
13
+ </head>
14
+ <body>
15
+ <section class="page-header">
16
+ <h1 class="project-name">Play Rock Paper Scissors Game</h1>
17
+ <h2 class="project-tagline">A Ruby Programmed Rock Paper Scissors Game</h2>
18
+
19
+ <a href="http://github.com/bag3318/RockPaperScissors" class="btn">View on GitHub</a>
20
+ <a href="http://github.com/bag3318/RockPaperScissors/issues" class="btn">Report a Bug</a>
21
+
22
+
23
+ <a href="http://github.com/bag3318/RockPaperScissors/zipball/gh-pages" class="btn">Download .zip</a>
24
+ <a href="http://github.com/bag3318/RockPaperScissors/tarball/gh-pages" class="btn">Download .tar.gz</a>
25
+
26
+ <br/>
27
+
28
+ <a href="#site-nav" class="btn">Site Nav</a>
29
+
30
+
31
+ <a href="./" class="btn">Home</a>
32
+
33
+ </section>
34
+
35
+ <section class="main-content">
36
+
37
+ <h1 id="comparing-beginners-rps-code-to-advanced-rps-code">Comparing Beginner’s RPS Code to Advanced RPS Code</h1>
38
+
39
+ <p><strong>Table of Contents</strong></p>
40
+
41
+ <ul>
42
+ <li><a href="#comparing-beginners-rps-code-to-advanced-rps-code">Comparing Beginner’s RPS Code to Advanced RPS Code</a>
43
+ <ul>
44
+ <li><a href="#beginners-code">Beginner’s Code</a>
45
+ <ul>
46
+ <li><a href="#beginners-code-pros">Beginner’s Code Pros</a></li>
47
+ <li><a href="#beginners-code-cons">Beginner’s Code Cons</a></li>
48
+ <li><a href="#what-beginners-code-looks-ike">What Beginner’s Code Looks :ike</a></li>
49
+ </ul>
50
+ </li>
51
+ <li><a href="#advanced-code">Advanced Code</a>
52
+ <ul>
53
+ <li><a href="#advanced-code-pros">Advanced Code Pros</a></li>
54
+ <li><a href="#advanced-code-cons">Advanced Code Cons</a></li>
55
+ <li><a href="#what-advanced-code-looks-like">What Advanced Code Looks Like</a></li>
56
+ </ul>
57
+ </li>
58
+ <li><a href="#site-nav">Site Nav</a></li>
59
+ </ul>
60
+ </li>
61
+ </ul>
62
+
63
+ <h2 id="beginners-code">Beginner’s Code</h2>
64
+
65
+ <h3 id="beginners-code-pros">Beginner’s Code Pros</h3>
66
+ <ul>
67
+ <li>Simple</li>
68
+ <li>Easy to understand</li>
69
+ <li>Short</li>
70
+ </ul>
71
+
72
+ <h3 id="beginners-code-cons">Beginner’s Code Cons</h3>
73
+ <ul>
74
+ <li>Unorganized</li>
75
+ <li>Lacks modularization</li>
76
+ <li>Many <code class="highlighter-rouge">if</code>s and <code class="highlighter-rouge">else</code>s</li>
77
+ </ul>
78
+
79
+ <h3 id="what-beginners-code-looks-ike">What Beginner’s Code Looks :ike</h3>
80
+
81
+ <p><strong><code class="highlighter-rouge">% ruby beginner.rb</code></strong></p>
82
+
83
+ <div class="language-ruby highlighter-rouge"><pre class="highlight"><code><span class="n">options</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"rock"</span><span class="p">,</span> <span class="s2">"paper"</span><span class="p">,</span> <span class="s2">"scissors"</span><span class="p">]</span>
84
+ <span class="n">computer_choice</span> <span class="o">=</span> <span class="n">options</span><span class="p">[</span><span class="nb">rand</span><span class="p">(</span><span class="n">options</span><span class="p">.</span><span class="nf">length</span><span class="p">)]</span>
85
+ <span class="c1"># the above line chooses a random pick in the options array, and the `options.length` means to choose from however many options there are in the `options` array</span>
86
+ <span class="nb">puts</span> <span class="s2">"What's your choice?"</span>
87
+ <span class="nb">puts</span> <span class="s2">"rock, paper, or scissors"</span>
88
+ <span class="n">user_input</span> <span class="o">=</span> <span class="nb">gets</span><span class="p">.</span><span class="nf">chomp</span><span class="p">.</span><span class="nf">downcase</span> <span class="c1"># `gets.chomp` reads user input, .downcase is like `.toLowerCase()` in javascript</span>
89
+ <span class="kp">loop</span> <span class="k">do</span>
90
+ <span class="k">if</span> <span class="p">(</span><span class="n">user_input</span> <span class="o">==</span> <span class="s2">"rock"</span> <span class="o">||</span> <span class="n">user_input</span> <span class="o">==</span> <span class="s2">"paper"</span> <span class="o">||</span> <span class="n">user_input</span> <span class="o">==</span> <span class="s2">"scissors"</span><span class="p">)</span>
91
+ <span class="k">if</span> <span class="p">(</span><span class="n">user_input</span> <span class="o">==</span> <span class="n">computer_choice</span><span class="p">)</span>
92
+ <span class="nb">puts</span> <span class="s2">"We got the same, let's keep playing!"</span>
93
+ <span class="k">elsif</span> <span class="p">(</span><span class="n">user_input</span> <span class="o">==</span> <span class="s2">"rock"</span> <span class="o">&amp;&amp;</span> <span class="n">computer_choice</span> <span class="o">==</span> <span class="s2">"scissors"</span><span class="p">)</span>
94
+ <span class="nb">puts</span> <span class="s2">"computer choice is: "</span> <span class="o">+</span> <span class="n">computer_choice</span> <span class="o">+</span> <span class="s2">" , you win! :)"</span>
95
+ <span class="k">elsif</span> <span class="p">(</span><span class="n">user_input</span> <span class="o">==</span> <span class="s2">"rock"</span> <span class="o">&amp;&amp;</span> <span class="n">computer_choice</span> <span class="o">==</span> <span class="s2">"paper"</span><span class="p">)</span>
96
+ <span class="nb">puts</span> <span class="s2">"computer choice is: "</span> <span class="o">+</span> <span class="n">computer_choice</span> <span class="o">+</span> <span class="s2">" ,computer wins :("</span>
97
+ <span class="k">elsif</span> <span class="p">(</span><span class="n">user_input</span> <span class="o">==</span> <span class="s2">"paper"</span> <span class="o">&amp;&amp;</span> <span class="n">computer_choice</span> <span class="o">==</span> <span class="s2">"scissors"</span><span class="p">)</span>
98
+ <span class="nb">puts</span> <span class="s2">"computer choice is: "</span> <span class="o">+</span> <span class="n">computer_choice</span> <span class="o">+</span> <span class="s2">" ,computer wins :("</span>
99
+ <span class="k">elsif</span> <span class="p">(</span><span class="n">user_input</span> <span class="o">==</span> <span class="s2">"paper"</span> <span class="o">&amp;&amp;</span> <span class="n">computer_choice</span> <span class="o">==</span> <span class="s2">"rock"</span><span class="p">)</span>
100
+ <span class="nb">puts</span> <span class="s2">"computer choice is: "</span> <span class="o">+</span> <span class="n">computer_choice</span> <span class="o">+</span> <span class="s2">" , you win! :)"</span>
101
+ <span class="k">elsif</span> <span class="p">(</span><span class="n">user_input</span> <span class="o">==</span> <span class="s2">"scissors"</span> <span class="o">&amp;&amp;</span> <span class="n">computer_choice</span> <span class="o">==</span> <span class="s2">"rock"</span><span class="p">)</span>
102
+ <span class="nb">puts</span> <span class="s2">"computer choice is: "</span> <span class="o">+</span> <span class="n">computer_choice</span> <span class="o">+</span> <span class="s2">" ,computer wins :("</span>
103
+ <span class="k">elsif</span> <span class="p">(</span><span class="n">user_input</span> <span class="o">==</span> <span class="s2">"scissors"</span> <span class="o">&amp;&amp;</span> <span class="n">computer_choice</span> <span class="o">==</span> <span class="s2">"paper"</span><span class="p">)</span>
104
+ <span class="nb">puts</span> <span class="s2">"computer choice is: "</span> <span class="o">+</span> <span class="n">computer_choice</span> <span class="o">+</span> <span class="s2">" , you win! :)"</span>
105
+ <span class="k">end</span>
106
+ <span class="k">else</span>
107
+ <span class="nb">puts</span> <span class="s2">"Invalid choice, enter Rock, Paper, or Scissors"</span>
108
+ <span class="nb">puts</span> <span class="s2">"What's your choice?"</span>
109
+ <span class="n">user_input</span> <span class="o">=</span> <span class="nb">gets</span><span class="p">.</span><span class="nf">chomp</span><span class="p">.</span><span class="nf">downcase</span> <span class="c1"># have the user choose again</span>
110
+ <span class="k">end</span>
111
+ <span class="k">break</span> <span class="c1"># break the loop after all the above has been completed</span>
112
+ <span class="k">end</span>
113
+ </code></pre>
114
+ </div>
115
+ <h2 id="advanced-code">Advanced Code</h2>
116
+
117
+ <h3 id="advanced-code-pros">Advanced Code Pros</h3>
118
+
119
+ <ul>
120
+ <li>Organized</li>
121
+ <li>Modularized</li>
122
+ <li>Structured</li>
123
+ </ul>
124
+
125
+ <h3 id="advanced-code-cons">Advanced Code Cons</h3>
126
+
127
+ <ul>
128
+ <li>Long</li>
129
+ <li>Hard to understand</li>
130
+ </ul>
131
+
132
+ <h3 id="what-advanced-code-looks-like">What Advanced Code Looks Like</h3>
133
+
134
+ <p><strong><code class="highlighter-rouge">% ruby advanced.rb</code></strong></p>
135
+
136
+ <div class="language-ruby highlighter-rouge"><pre class="highlight"><code><span class="k">class</span> <span class="nc">PlayRockPaperScissorsGame</span> <span class="c1"># define master class</span>
137
+
138
+ <span class="c1"># intiate the colorize gem</span>
139
+ <span class="nb">require</span> <span class="s2">"colorized_string"</span>
140
+ <span class="no">ColorizedString</span><span class="p">.</span><span class="nf">colors</span> <span class="c1"># import colors; ex: red, green, blue</span>
141
+ <span class="no">ColorizedString</span><span class="p">.</span><span class="nf">modes</span> <span class="c1"># import modes; ex: bold, italic, underline</span>
142
+
143
+ <span class="k">module</span> <span class="nn">Constants</span>
144
+ <span class="no">NTRY_TO_SYM</span> <span class="o">=</span> <span class="p">{</span> <span class="c1"># define constants from an entry to a symbol class</span>
145
+ <span class="s1">'p'</span> <span class="o">=&gt;</span> <span class="ss">:PAPER</span><span class="p">,</span>
146
+ <span class="s1">'r'</span> <span class="o">=&gt;</span> <span class="ss">:ROCK</span><span class="p">,</span>
147
+ <span class="s1">'s'</span> <span class="o">=&gt;</span> <span class="ss">:SCISSORS</span>
148
+ <span class="p">}</span>
149
+ <span class="no">VALID_ENTRIES</span> <span class="o">=</span> <span class="no">NTRY_TO_SYM</span><span class="p">.</span><span class="nf">keys</span> <span class="c1"># create valid entries</span>
150
+ <span class="no">COMPUTER_CHOICES</span> <span class="o">=</span> <span class="no">NTRY_TO_SYM</span><span class="p">.</span><span class="nf">values</span> <span class="c1"># define computer choices</span>
151
+ <span class="no">WINNERS</span> <span class="o">=</span> <span class="p">[</span> <span class="c1"># define winners</span>
152
+ <span class="c1"># format: player choice, computer choice</span>
153
+ <span class="p">[</span><span class="ss">:SCISSORS</span><span class="p">,</span> <span class="ss">:PAPER</span><span class="p">],</span>
154
+ <span class="p">[</span><span class="ss">:PAPER</span> <span class="p">,</span> <span class="ss">:ROCK</span><span class="p">],</span>
155
+ <span class="p">[</span><span class="ss">:ROCK</span> <span class="p">,</span> <span class="ss">:SCISSORS</span><span class="p">]</span>
156
+ <span class="p">]</span>
157
+ <span class="no">LOSERS</span> <span class="o">=</span> <span class="no">WINNERS</span><span class="p">.</span><span class="nf">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">pc</span><span class="p">,</span><span class="n">cc</span><span class="o">|</span> <span class="p">[</span><span class="n">cc</span><span class="p">,</span><span class="n">pc</span><span class="p">]</span> <span class="p">}</span> <span class="c1"># this will take the original WINNERS array and flip the symbols, thus returning a loss for the user/player</span>
158
+ <span class="no">INIT_STRINGS</span> <span class="o">=</span> <span class="p">[</span>
159
+ <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"You are about to enter a rock-paper-scissors best of 3 match."</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:green</span><span class="p">),</span>
160
+ <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"Press the return/enter key to continue..."</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:green</span><span class="p">),</span>
161
+ <span class="s2">""</span>
162
+ <span class="p">]</span>
163
+ <span class="k">end</span>
164
+
165
+ <span class="nb">protected_methods</span> <span class="ss">:Constants</span> <span class="c1"># make the constants module protected</span>
166
+
167
+ <span class="k">class</span> <span class="o">&lt;&lt;</span> <span class="nb">self</span> <span class="c1"># define a self calling method within the parent class</span>
168
+ <span class="k">def</span> <span class="nf">continue</span><span class="p">(</span><span class="n">str1</span><span class="p">,</span><span class="n">str2</span><span class="p">,</span><span class="n">str3</span><span class="p">)</span> <span class="c1"># pass in 3 parameters</span>
169
+ <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="n">str1</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:color</span> <span class="o">=&gt;</span> <span class="ss">:green</span><span class="p">)</span>
170
+ <span class="nb">print</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="n">str2</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:color</span> <span class="o">=&gt;</span> <span class="ss">:green</span><span class="p">)</span>
171
+ <span class="nb">gets</span> <span class="c1"># press enter or return to continue</span>
172
+ <span class="nb">puts</span> <span class="n">str3</span>
173
+ <span class="k">end</span>
174
+ <span class="k">end</span>
175
+
176
+ <span class="n">continue</span><span class="p">(</span><span class="no">Constants</span><span class="o">::</span><span class="no">INIT_STRINGS</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="no">Constants</span><span class="o">::</span><span class="no">INIT_STRINGS</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="no">Constants</span><span class="o">::</span><span class="no">INIT_STRINGS</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="c1"># call continue method</span>
177
+
178
+ <span class="k">def</span> <span class="nf">initialize</span> <span class="c1"># initialize variables</span>
179
+ <span class="vi">@player_score</span> <span class="o">=</span> <span class="vi">@computer_score</span> <span class="o">=</span> <span class="vi">@ties</span> <span class="o">=</span> <span class="mi">0</span>
180
+ <span class="k">end</span>
181
+ <span class="k">def</span> <span class="nf">play</span><span class="p">(</span><span class="n">winning_score</span><span class="p">)</span>
182
+ <span class="k">while</span> <span class="vi">@player_score</span> <span class="o">&lt;</span> <span class="n">winning_score</span> <span class="o">&amp;&amp;</span> <span class="vi">@computer_score</span> <span class="o">&lt;</span> <span class="n">winning_score</span> <span class="c1"># both the computer's score and the player's score have to be less than the value passed in for the winning score at the end</span>
183
+ <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"Player score: </span><span class="si">#{</span><span class="vi">@player_score</span><span class="si">}</span><span class="s2">, "</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:blue</span><span class="p">)</span> <span class="o">+</span>
184
+ <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"Computer score: </span><span class="si">#{</span><span class="vi">@computer_score</span><span class="si">}</span><span class="s2">, Ties: </span><span class="si">#{</span><span class="vi">@ties</span><span class="si">}</span><span class="s2">"</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:blue</span><span class="p">)</span>
185
+ <span class="n">player</span> <span class="o">=</span> <span class="no">PrivateMethods</span><span class="p">.</span><span class="nf">player_choice</span>
186
+ <span class="n">computer</span> <span class="o">=</span> <span class="no">Constants</span><span class="o">::</span><span class="no">COMPUTER_CHOICES</span><span class="p">.</span><span class="nf">sample</span> <span class="c1"># chooses a "random" option</span>
187
+ <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"</span><span class="se">\n</span><span class="s2">Player chooses </span><span class="si">#{</span><span class="n">player</span><span class="p">.</span><span class="nf">to_s</span><span class="p">.</span><span class="nf">downcase</span><span class="si">}</span><span class="s2">"</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:blue</span><span class="p">)</span>
188
+ <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"Computer chooses </span><span class="si">#{</span><span class="n">computer</span><span class="p">.</span><span class="nf">to_s</span><span class="p">.</span><span class="nf">downcase</span><span class="si">}</span><span class="s2">"</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:blue</span><span class="p">)</span>
189
+ <span class="k">case</span> <span class="no">PrivateMethods</span><span class="p">.</span><span class="nf">player_outcome</span> <span class="p">[</span><span class="n">player</span><span class="p">,</span> <span class="n">computer</span><span class="p">]</span> <span class="c1"># define a reference call for player and computer for the arrays called in the player_outcome method</span>
190
+ <span class="k">when</span> <span class="ss">:WIN</span>
191
+ <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"</span><span class="si">#{</span><span class="n">player</span><span class="p">.</span><span class="nf">to_s</span><span class="p">.</span><span class="nf">capitalize</span><span class="si">}</span><span class="s2"> beats </span><span class="si">#{</span><span class="n">computer</span><span class="p">.</span><span class="nf">to_s</span><span class="p">.</span><span class="nf">downcase</span><span class="si">}</span><span class="s2">, player wins the round"</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:red</span><span class="p">)</span>
192
+ <span class="vi">@player_score</span> <span class="o">+=</span> <span class="mi">1</span> <span class="c1"># @player_score = @player_score + 1</span>
193
+ <span class="k">when</span> <span class="ss">:LOSE</span>
194
+ <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"</span><span class="si">#{</span><span class="n">computer</span><span class="p">.</span><span class="nf">to_s</span><span class="p">.</span><span class="nf">capitalize</span><span class="si">}</span><span class="s2"> beats </span><span class="si">#{</span><span class="n">player</span><span class="p">.</span><span class="nf">to_s</span><span class="p">.</span><span class="nf">downcase</span><span class="si">}</span><span class="s2">, computer wins the round"</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:red</span><span class="p">)</span>
195
+ <span class="vi">@computer_score</span> <span class="o">+=</span> <span class="mi">1</span>
196
+ <span class="k">else</span> <span class="c1"># since there is only 1 option left, there is no need to define when :TIE</span>
197
+ <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"Tie, choose again"</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:red</span><span class="p">)</span>
198
+ <span class="vi">@ties</span> <span class="o">+=</span> <span class="mi">1</span>
199
+ <span class="c1"># since tie is not in the original pass-in argument for the while loop, it will not be affected by the winning score</span>
200
+ <span class="k">end</span>
201
+ <span class="k">end</span>
202
+ <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"</span><span class="se">\n</span><span class="s2">Final score: player: </span><span class="si">#{</span><span class="vi">@player_score</span><span class="si">}</span><span class="s2">, "</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:blue</span><span class="p">)</span> <span class="o">+</span>
203
+ <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"computer: </span><span class="si">#{</span><span class="vi">@computer_score</span><span class="si">}</span><span class="s2"> (ties: </span><span class="si">#{</span><span class="vi">@ties</span><span class="si">}</span><span class="s2">)"</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:blue</span><span class="p">)</span>
204
+ <span class="c1"># define a case for the final outcomes </span>
205
+ <span class="k">case</span> <span class="no">PrivateMethods</span><span class="p">.</span><span class="nf">final_outcome</span><span class="p">(</span><span class="vi">@player_score</span><span class="p">,</span> <span class="vi">@computer_score</span><span class="p">)</span>
206
+ <span class="k">when</span> <span class="ss">:WIN</span>
207
+ <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"Player wins!"</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:red</span><span class="p">)</span>
208
+ <span class="k">when</span> <span class="ss">:LOSE</span>
209
+ <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"Computer wins!"</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:red</span><span class="p">)</span>
210
+ <span class="k">else</span>
211
+ <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"It's a tie!"</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:red</span><span class="p">)</span>
212
+ <span class="k">end</span>
213
+ <span class="nb">gets</span>
214
+ <span class="k">end</span>
215
+
216
+ <span class="k">module</span> <span class="nn">PrivateMethods</span>
217
+ <span class="k">class</span> <span class="o">&lt;&lt;</span> <span class="nb">self</span> <span class="c1"># make all methods below self calling methods of this class (PlayRockPaperScissorsGame) so that the other methods will not to call their parents of these methods</span>
218
+ <span class="k">def</span> <span class="nf">player_choice</span>
219
+ <span class="kp">loop</span> <span class="k">do</span> <span class="c1"># for loop with no arguments passed in</span>
220
+ <span class="nb">print</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"Choose rock (r), paper (p) or scissors (s): "</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:green</span><span class="p">)</span>
221
+ <span class="n">choice</span> <span class="o">=</span> <span class="nb">gets</span><span class="p">.</span><span class="nf">chomp</span><span class="p">.</span><span class="nf">downcase</span> <span class="c1"># read user input and convert all to lower case</span>
222
+ <span class="c1"># define valid and invalid entries by using an if else-if statement(s)</span>
223
+ <span class="k">if</span> <span class="no">Constants</span><span class="o">::</span><span class="no">NTRY_TO_SYM</span><span class="p">.</span><span class="nf">key?</span><span class="p">(</span><span class="n">choice</span><span class="p">)</span> <span class="c1"># if the NTRY_TO_SYM array's key is one of the keys defined in the original array</span>
224
+ <span class="k">return</span> <span class="no">Constants</span><span class="o">::</span><span class="no">NTRY_TO_SYM</span><span class="p">[</span><span class="n">choice</span><span class="p">]</span> <span class="c1"># return the users choice</span>
225
+ <span class="k">elsif</span> <span class="n">choice</span> <span class="o">!=</span> <span class="no">Constants</span><span class="o">::</span><span class="no">VALID_ENTRIES</span> <span class="c1"># else if it is not one of the valid entries...</span>
226
+ <span class="nb">puts</span> <span class="no">ColorizedString</span><span class="p">[</span><span class="s2">"That entry is invalid. Please re-enter."</span><span class="p">].</span><span class="nf">colorize</span><span class="p">(</span><span class="ss">:red</span><span class="p">)</span> <span class="c1"># return an error message</span>
227
+ <span class="k">end</span>
228
+ <span class="c1"># # one may also do this (albeit more useless):</span>
229
+ <span class="c1"># case</span>
230
+ <span class="c1"># when Constants::NTRY_TO_SYM.key?(choice)</span>
231
+ <span class="c1"># return Constants::NTRY_TO_SYM[choice]</span>
232
+ <span class="c1"># when choice != Constants::VALID_ENTRIES</span>
233
+ <span class="c1"># puts ColorizedString["That entry is invalid. Please re-enter."].colorize(:green) </span>
234
+ <span class="c1"># end</span>
235
+ <span class="k">end</span>
236
+ <span class="k">end</span>
237
+ <span class="k">def</span> <span class="nf">player_outcome</span><span class="p">(</span><span class="n">plays</span><span class="p">)</span> <span class="c1"># define method for the player's outcome while passing in a parameter of type array</span>
238
+ <span class="k">return</span> <span class="ss">:WIN</span> <span class="k">if</span> <span class="no">Constants</span><span class="o">::</span><span class="no">WINNERS</span><span class="p">.</span><span class="nf">include?</span><span class="p">(</span><span class="n">plays</span><span class="p">)</span> <span class="c1"># return a win if the one of the sub-arrays in the winners array is called</span>
239
+ <span class="k">return</span> <span class="ss">:LOSE</span> <span class="k">if</span> <span class="no">Constants</span><span class="o">::</span><span class="no">LOSERS</span><span class="p">.</span><span class="nf">include?</span><span class="p">(</span><span class="n">plays</span><span class="p">)</span> <span class="c1"># return a loss if any of the mapped sub-arrays in the losers constant is present</span>
240
+ <span class="k">return</span> <span class="ss">:TIE</span> <span class="k">if</span> <span class="o">!</span><span class="ss">:WIN</span> <span class="o">|</span> <span class="o">!</span><span class="ss">:LOSE</span> <span class="c1"># return a tie if not (!) win or if not loose</span>
241
+ <span class="k">end</span>
242
+ <span class="k">def</span> <span class="nf">final_outcome</span><span class="p">(</span><span class="n">pl</span><span class="p">,</span><span class="n">co</span><span class="p">)</span> <span class="c1"># define final outcome method</span>
243
+ <span class="k">return</span> <span class="ss">:WIN</span> <span class="k">if</span> <span class="n">pl</span> <span class="o">&gt;</span> <span class="n">co</span>
244
+ <span class="k">return</span> <span class="ss">:LOSE</span> <span class="k">if</span> <span class="n">pl</span> <span class="o">&lt;</span> <span class="n">co</span>
245
+ <span class="k">return</span> <span class="ss">:TIE</span> <span class="k">if</span> <span class="n">pl</span> <span class="o">=</span> <span class="n">co</span> <span class="c1"># this can never happen due to the code in the play method, but it is worth noting</span>
246
+ <span class="k">end</span>
247
+ <span class="k">end</span>
248
+ <span class="k">end</span>
249
+
250
+ <span class="nb">private_methods</span> <span class="ss">:PrivateMethods</span> <span class="c1"># make the PrivateMethods module private</span>
251
+
252
+ <span class="k">end</span>
253
+
254
+ <span class="no">PlayRockPaperScissorsGame</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">play</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># call the play method and pass in 3 (0, 1, 2) for the winning score</span>
255
+ </code></pre>
256
+ </div>
257
+
258
+ <hr />
259
+
260
+ <h2 id="site-nav">Site Nav</h2>
261
+
262
+ <table>
263
+ <tbody>
264
+ <tr>
265
+ <td><a href="./">Home</a></td>
266
+ <td><a href="Code">Master Code</a></td>
267
+ <td><a href="Testing">How to Test</a></td>
268
+ <td><a href="How_to_Build">How to Build</a></td>
269
+ <td><a href="CONTRIBUTING">Contributing</a></td>
270
+ <td><a href="CODE_OF_CONDUCT">Code of Conduct</a></td>
271
+ </tr>
272
+ </tbody>
273
+ </table>
274
+
275
+
276
+
277
+ <footer class="site-footer">
278
+
279
+ <span class="site-footer-owner"><a href="http://github.com/bag3318/RockPaperScissors">RockPaperScissors</a> is maintained by <a href="http://github.com/bag3318">bag3318</a>.</span>
280
+
281
+ </footer>
282
+ </section>
283
+
284
+
285
+ <script type="text/javascript">
286
+ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
287
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
288
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
289
+ })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
290
+
291
+ ga('create', 'true', 'auto');
292
+ ga('send', 'pageview');
293
+ </script>
294
+
295
+ </body>
296
+ </html>