PlayRockPaperScissorsGame 2.8.2 → 2.8.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/docs/Code.md +2 -2
- data/docs/Comparing_Code.md +7 -7
- data/docs/_config.yml +17 -1
- data/docs/_sass/normalize.scss +1 -1
- data/docs/_sass/pace.sass +2 -1
- data/docs/_site/about.html +1 -1
- data/docs/_site/code.html +3 -3
- data/docs/_site/comparing_code.html +8 -8
- data/docs/_site/how_to_build.html +1 -1
- data/docs/_site/how_to_run.html +1 -1
- data/docs/_site/index.html +1 -1
- data/docs/_site/testing.html +1 -1
- data/docs/other_langs.md +615 -0
- data/lib/rps/version.rb +1 -1
- data/rps.gemspec +2 -2
- metadata +3 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 763a5c51b1bb097612408a1e4a81d678082c1ab2
|
4
|
+
data.tar.gz: 3b1012f1a080d12cd3614ff1cca89d68a0b76560
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9255d2a90ab9f12469c319d042ad8f7bdc57cfcc1bf6c9c524bd9e7ef1688e27be912da0133d334492890a32f39c7a174244dd2c6cc8c8ae30d7e7c68306d5d1
|
7
|
+
data.tar.gz: fe4a82a3cd4d97e3009a1dfa3822a3e2f164ad468cccbdca3e108e3d24227e576829db03c23e82c3ad7d4535d22b9a46f66e3e4fd4764b1889d1a05f4f4292aa
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/docs/Code.md
CHANGED
@@ -35,7 +35,7 @@ Master Code
|
|
35
35
|
class PlayRockPaperScissorsGame
|
36
36
|
|
37
37
|
module RockPaperScissors
|
38
|
-
VERSION = "2.8.
|
38
|
+
VERSION = "2.8.3"
|
39
39
|
end
|
40
40
|
|
41
41
|
# import the colorize gem
|
@@ -141,7 +141,7 @@ class PlayRockPaperScissorsGame
|
|
141
141
|
def final_outcome(pl,co)
|
142
142
|
return :WIN if pl > co
|
143
143
|
return :LOSE if pl < co
|
144
|
-
# there will never be a tie due to the code in the play() method
|
144
|
+
# there will never be a tie fore the final outcome due to the code in the play() method
|
145
145
|
end
|
146
146
|
end
|
147
147
|
end
|
data/docs/Comparing_Code.md
CHANGED
@@ -127,7 +127,7 @@ Subtlety is usually painted all over advanced ruby code. While this does make th
|
|
127
127
|
class PlayRockPaperScissorsGame
|
128
128
|
|
129
129
|
module RockPaperScissors
|
130
|
-
VERSION = "2.8.
|
130
|
+
VERSION = "2.8.3"
|
131
131
|
end
|
132
132
|
|
133
133
|
# import colorize gem
|
@@ -212,11 +212,11 @@ class PlayRockPaperScissorsGame
|
|
212
212
|
def player_choice
|
213
213
|
loop do
|
214
214
|
print ColorizedString["Choose rock (r), paper (p) or scissors (s): "].colorize(:green)
|
215
|
-
choice = gets.chomp.downcase
|
216
|
-
if Constants::NTRY_TO_SYM.key?(choice)
|
217
|
-
return Constants::NTRY_TO_SYM[choice]
|
218
|
-
elsif choice != Constants::VALID_ENTRIES
|
219
|
-
puts ColorizedString["That entry is invalid. Please re-enter."].colorize(:red)
|
215
|
+
choice = gets.chomp.downcase
|
216
|
+
if Constants::NTRY_TO_SYM.key?(choice)
|
217
|
+
return Constants::NTRY_TO_SYM[choice]
|
218
|
+
elsif choice != Constants::VALID_ENTRIES
|
219
|
+
puts ColorizedString["That entry is invalid. Please re-enter."].colorize(:red) entry message
|
220
220
|
else
|
221
221
|
return nil
|
222
222
|
end
|
@@ -231,7 +231,7 @@ class PlayRockPaperScissorsGame
|
|
231
231
|
def final_outcome(pl,co)
|
232
232
|
return :WIN if pl > co
|
233
233
|
return :LOSE if pl < co
|
234
|
-
# there will never be a tie because of the play() method
|
234
|
+
# there will never be a tie for the final outcome because of the play() method
|
235
235
|
end
|
236
236
|
end
|
237
237
|
end
|
data/docs/_config.yml
CHANGED
@@ -18,9 +18,25 @@ exclude:
|
|
18
18
|
- thumbnail.png
|
19
19
|
- LICENSE
|
20
20
|
- script
|
21
|
+
- other_langs.md
|
21
22
|
gems:
|
22
23
|
- github-pages
|
23
24
|
# Sass/SCSS
|
24
25
|
sass:
|
25
26
|
sass_dir: _sass
|
26
|
-
style: compressed # http://sass-lang.com/documentation/file.SASS_REFERENCE.html#output_style
|
27
|
+
style: compressed # http://sass-lang.com/documentation/file.SASS_REFERENCE.html#output_style
|
28
|
+
liquid:
|
29
|
+
error_mode: warn
|
30
|
+
keep_files:
|
31
|
+
- ".git"
|
32
|
+
- ".svn"
|
33
|
+
include:
|
34
|
+
- ".htaccess"
|
35
|
+
destination: ./_site
|
36
|
+
layouts_dir: _layouts
|
37
|
+
includes_dir: _includes
|
38
|
+
defaults:
|
39
|
+
-
|
40
|
+
values:
|
41
|
+
layout: "default"
|
42
|
+
author: "bag3318"
|
data/docs/_sass/normalize.scss
CHANGED
data/docs/_sass/pace.sass
CHANGED
data/docs/_site/about.html
CHANGED
@@ -7,7 +7,7 @@
|
|
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=
|
10
|
+
<link rel="stylesheet" href="/assets/css/style.css?v=24681c868d0c9f04c3284da474a7c294ed887eef"/>
|
11
11
|
<script src="assets/js/pace.min.js"></script>
|
12
12
|
|
13
13
|
<link rel="apple-touch-icon" sizes="180x180" href="assets/favicons/apple-touch-icon.png"/>
|
data/docs/_site/code.html
CHANGED
@@ -7,7 +7,7 @@
|
|
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=
|
10
|
+
<link rel="stylesheet" href="/assets/css/style.css?v=24681c868d0c9f04c3284da474a7c294ed887eef"/>
|
11
11
|
<script src="assets/js/pace.min.js"></script>
|
12
12
|
|
13
13
|
<link rel="apple-touch-icon" sizes="180x180" href="assets/favicons/apple-touch-icon.png"/>
|
@@ -69,7 +69,7 @@
|
|
69
69
|
<span class="k">class</span> <span class="nc">PlayRockPaperScissorsGame</span>
|
70
70
|
|
71
71
|
<span class="k">module</span> <span class="nn">RockPaperScissors</span>
|
72
|
-
<span class="no">VERSION</span> <span class="o">=</span> <span class="s2">"2.8.
|
72
|
+
<span class="no">VERSION</span> <span class="o">=</span> <span class="s2">"2.8.3"</span>
|
73
73
|
<span class="k">end</span>
|
74
74
|
|
75
75
|
<span class="c1"># import the colorize gem</span>
|
@@ -175,7 +175,7 @@
|
|
175
175
|
<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>
|
176
176
|
<span class="k">return</span> <span class="ss">:WIN</span> <span class="k">if</span> <span class="n">pl</span> <span class="o">></span> <span class="n">co</span>
|
177
177
|
<span class="k">return</span> <span class="ss">:LOSE</span> <span class="k">if</span> <span class="n">pl</span> <span class="o"><</span> <span class="n">co</span>
|
178
|
-
<span class="c1"># there will never be a tie due to the code in the play() method</span>
|
178
|
+
<span class="c1"># there will never be a tie fore the final outcome due to the code in the play() method</span>
|
179
179
|
<span class="k">end</span>
|
180
180
|
<span class="k">end</span>
|
181
181
|
<span class="k">end</span>
|
@@ -7,7 +7,7 @@
|
|
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=
|
10
|
+
<link rel="stylesheet" href="/assets/css/style.css?v=24681c868d0c9f04c3284da474a7c294ed887eef"/>
|
11
11
|
<script src="assets/js/pace.min.js"></script>
|
12
12
|
|
13
13
|
<link rel="apple-touch-icon" sizes="180x180" href="assets/favicons/apple-touch-icon.png"/>
|
@@ -179,7 +179,7 @@
|
|
179
179
|
<div class="language-ruby highlighter-rouge"><pre class="highlight"><code><span class="k">class</span> <span class="nc">PlayRockPaperScissorsGame</span>
|
180
180
|
|
181
181
|
<span class="k">module</span> <span class="nn">RockPaperScissors</span>
|
182
|
-
<span class="no">VERSION</span> <span class="o">=</span> <span class="s2">"2.8.
|
182
|
+
<span class="no">VERSION</span> <span class="o">=</span> <span class="s2">"2.8.3"</span>
|
183
183
|
<span class="k">end</span>
|
184
184
|
|
185
185
|
<span class="c1"># import colorize gem</span>
|
@@ -264,11 +264,11 @@
|
|
264
264
|
<span class="k">def</span> <span class="nf">player_choice</span>
|
265
265
|
<span class="kp">loop</span> <span class="k">do</span>
|
266
266
|
<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>
|
267
|
-
<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>
|
268
|
-
<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>
|
269
|
-
<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>
|
270
|
-
<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>
|
271
|
-
<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="
|
267
|
+
<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>
|
268
|
+
<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>
|
269
|
+
<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>
|
270
|
+
<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>
|
271
|
+
<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="n">entry</span> <span class="n">message</span>
|
272
272
|
<span class="k">else</span>
|
273
273
|
<span class="k">return</span> <span class="kp">nil</span>
|
274
274
|
<span class="k">end</span>
|
@@ -283,7 +283,7 @@
|
|
283
283
|
<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>
|
284
284
|
<span class="k">return</span> <span class="ss">:WIN</span> <span class="k">if</span> <span class="n">pl</span> <span class="o">></span> <span class="n">co</span>
|
285
285
|
<span class="k">return</span> <span class="ss">:LOSE</span> <span class="k">if</span> <span class="n">pl</span> <span class="o"><</span> <span class="n">co</span>
|
286
|
-
<span class="c1"># there will never be a tie because of the play() method</span>
|
286
|
+
<span class="c1"># there will never be a tie for the final outcome because of the play() method</span>
|
287
287
|
<span class="k">end</span>
|
288
288
|
<span class="k">end</span>
|
289
289
|
<span class="k">end</span>
|
@@ -7,7 +7,7 @@
|
|
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=
|
10
|
+
<link rel="stylesheet" href="/assets/css/style.css?v=24681c868d0c9f04c3284da474a7c294ed887eef"/>
|
11
11
|
<script src="assets/js/pace.min.js"></script>
|
12
12
|
|
13
13
|
<link rel="apple-touch-icon" sizes="180x180" href="assets/favicons/apple-touch-icon.png"/>
|
data/docs/_site/how_to_run.html
CHANGED
@@ -7,7 +7,7 @@
|
|
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=
|
10
|
+
<link rel="stylesheet" href="/assets/css/style.css?v=24681c868d0c9f04c3284da474a7c294ed887eef"/>
|
11
11
|
<script src="assets/js/pace.min.js"></script>
|
12
12
|
|
13
13
|
<link rel="apple-touch-icon" sizes="180x180" href="assets/favicons/apple-touch-icon.png"/>
|
data/docs/_site/index.html
CHANGED
@@ -7,7 +7,7 @@
|
|
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=
|
10
|
+
<link rel="stylesheet" href="/assets/css/style.css?v=24681c868d0c9f04c3284da474a7c294ed887eef"/>
|
11
11
|
<script src="assets/js/pace.min.js"></script>
|
12
12
|
|
13
13
|
<link rel="apple-touch-icon" sizes="180x180" href="assets/favicons/apple-touch-icon.png"/>
|
data/docs/_site/testing.html
CHANGED
@@ -7,7 +7,7 @@
|
|
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=
|
10
|
+
<link rel="stylesheet" href="/assets/css/style.css?v=24681c868d0c9f04c3284da474a7c294ed887eef"/>
|
11
11
|
<script src="assets/js/pace.min.js"></script>
|
12
12
|
|
13
13
|
<link rel="apple-touch-icon" sizes="180x180" href="assets/favicons/apple-touch-icon.png"/>
|
data/docs/other_langs.md
ADDED
@@ -0,0 +1,615 @@
|
|
1
|
+
---
|
2
|
+
layout: default
|
3
|
+
title: Other Langs - Play Rock Paper Scissors Game
|
4
|
+
permalink: /different_code.html
|
5
|
+
---
|
6
|
+
|
7
|
+
# Other Langs
|
8
|
+
|
9
|
+
## Python
|
10
|
+
|
11
|
+
```python
|
12
|
+
import random
|
13
|
+
import sys
|
14
|
+
|
15
|
+
def makeYourChoice():
|
16
|
+
print "Press R for Rock"
|
17
|
+
print "Press P for Paper"
|
18
|
+
print "Press S for Scissors"
|
19
|
+
print "Press Q to quit!"
|
20
|
+
|
21
|
+
userChoice = raw_input("What do you want to choose?").lower()
|
22
|
+
|
23
|
+
if userChoice == "r":
|
24
|
+
return "Rock"
|
25
|
+
if userChoice == "p":
|
26
|
+
return "Paper"
|
27
|
+
if userChoice == "s":
|
28
|
+
return "Scissors"
|
29
|
+
if userChoice == "q":
|
30
|
+
sys.exit(0)
|
31
|
+
else:
|
32
|
+
makeYourChoice()
|
33
|
+
|
34
|
+
def computerRandom():
|
35
|
+
options = ["Rock","Paper","Scissors"]
|
36
|
+
randomChoice = random.randint(0,2)
|
37
|
+
return options[randomChoice]
|
38
|
+
|
39
|
+
def comparison(humanChoice, computerChoice):
|
40
|
+
if humanChoice == computerChoice:
|
41
|
+
return "Draw"
|
42
|
+
if humanChoice == "Rock" and computerChoice == "Paper":
|
43
|
+
return "Computer Wins"
|
44
|
+
if humanChoice == "Paper" and computerChoice == "Scissors":
|
45
|
+
return "Computer Wins"
|
46
|
+
if humanChoice == "Scissors" and computerChoice == "Rock":
|
47
|
+
return "Computer Wins"
|
48
|
+
else: return "Human Wins"
|
49
|
+
|
50
|
+
#print makeYourChoice()
|
51
|
+
|
52
|
+
while True:
|
53
|
+
humanChoice = makeYourChoice()
|
54
|
+
computerChoice = computerRandom()
|
55
|
+
|
56
|
+
print "You chose", humanChoice
|
57
|
+
print "The computer chose", computerChoice
|
58
|
+
|
59
|
+
result = comparison (humanChoice, computerChoice)
|
60
|
+
|
61
|
+
if result == "Draw":
|
62
|
+
print "Its a draw"
|
63
|
+
elif result == "Computer Wins":
|
64
|
+
print "Unlucky you lost!"
|
65
|
+
else: print "Well done you won!"
|
66
|
+
|
67
|
+
print " "
|
68
|
+
```
|
69
|
+
|
70
|
+
## Perl
|
71
|
+
|
72
|
+
```perl
|
73
|
+
use 5.012;
|
74
|
+
use warnings;
|
75
|
+
use utf8;
|
76
|
+
use open qw(:encoding(utf-8) :std);
|
77
|
+
use Getopt::Long;
|
78
|
+
|
79
|
+
=pod
|
80
|
+
RPS programmed in Perl
|
81
|
+
=cut
|
82
|
+
|
83
|
+
package Game {
|
84
|
+
use List::Util qw(shuffle first);
|
85
|
+
|
86
|
+
my $turns = 0;
|
87
|
+
my %human_choice = ( rock => 0, paper => 0, scissors => 0, );
|
88
|
+
my %comp_choice = ( rock => 0, paper => 0, scissors => 0, );
|
89
|
+
my %what_beats =
|
90
|
+
( rock => 'paper', paper => 'scissors', scissors => 'rock', );
|
91
|
+
my $comp_wins = 0;
|
92
|
+
my $human_wins = 0;
|
93
|
+
my $draws = 0;
|
94
|
+
|
95
|
+
sub save_human_choice {
|
96
|
+
my $ch = lc pop;
|
97
|
+
if ( exists $human_choice{ $ch } ) {
|
98
|
+
++$human_choice{ $ch };
|
99
|
+
}
|
100
|
+
else {
|
101
|
+
die __PACKAGE__ . ":: wrong choice: '$ch'";
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
sub get_comp_choice {
|
106
|
+
my @keys = shuffle keys %human_choice;
|
107
|
+
my $ch;
|
108
|
+
my ( $prob, $rand ) = ( 0, rand );
|
109
|
+
$ch = ( first { $rand <= ( $prob += ( $human_choice{ $_ } / $turns ) ) } @keys )
|
110
|
+
if $turns > 0;
|
111
|
+
$ch //= $keys[0];
|
112
|
+
$ch = $what_beats{ $ch };
|
113
|
+
++$comp_choice{ $ch };
|
114
|
+
return $ch;
|
115
|
+
}
|
116
|
+
|
117
|
+
sub make_turn {
|
118
|
+
my ( $comp_ch, $human_ch ) = ( pop(), pop() );
|
119
|
+
++$turns;
|
120
|
+
if ( $what_beats{ $human_ch } eq $comp_ch ) {
|
121
|
+
++$comp_wins;
|
122
|
+
return 'I win!';
|
123
|
+
}
|
124
|
+
elsif ( $what_beats{ $comp_ch } eq $human_ch ) {
|
125
|
+
++$human_wins;
|
126
|
+
return 'You win!';
|
127
|
+
}
|
128
|
+
else {
|
129
|
+
++$draws;
|
130
|
+
return 'Draw!';
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
sub get_final_report {
|
135
|
+
my $report =
|
136
|
+
"You chose:\n"
|
137
|
+
. " rock = $human_choice{rock} times,\n"
|
138
|
+
. " paper = $human_choice{paper} times,\n"
|
139
|
+
. " scissors = $human_choice{scissors} times,\n"
|
140
|
+
. "I chose:\n"
|
141
|
+
. " rock = $comp_choice{rock} times,\n"
|
142
|
+
. " paper = $comp_choice{paper} times,\n"
|
143
|
+
. " scissors = $comp_choice{scissors} times,\n"
|
144
|
+
. "Turns: $turns\n"
|
145
|
+
. "I won: $comp_wins, you won: $human_wins, draws: $draws\n";
|
146
|
+
return $report;
|
147
|
+
}
|
148
|
+
}
|
149
|
+
|
150
|
+
sub main {
|
151
|
+
GetOptions( 'quiet' => \my $quiet );
|
152
|
+
greet() if !$quiet;
|
153
|
+
while (1) {
|
154
|
+
print_next_line() if !$quiet;
|
155
|
+
my $input = get_input();
|
156
|
+
last unless $input;
|
157
|
+
if ( $input eq 'error' ) {
|
158
|
+
print "I don't understand!\n" if !$quiet;
|
159
|
+
redo;
|
160
|
+
}
|
161
|
+
my $comp_choice = Game::get_comp_choice();
|
162
|
+
Game::save_human_choice($input);
|
163
|
+
my $result = Game::make_turn( $input, $comp_choice );
|
164
|
+
describe_turn_result( $input, $comp_choice, $result )
|
165
|
+
if !$quiet;
|
166
|
+
}
|
167
|
+
print Game::get_final_report();
|
168
|
+
}
|
169
|
+
|
170
|
+
sub greet {
|
171
|
+
print "Welcome to the Rock-Paper-Scissors game!\n"
|
172
|
+
. "Choose 'rock', 'paper' or 'scissors'\n"
|
173
|
+
. "Enter empty line or 'quit' to quit\n";
|
174
|
+
}
|
175
|
+
|
176
|
+
sub print_next_line {
|
177
|
+
print 'Your choice: ';
|
178
|
+
}
|
179
|
+
|
180
|
+
sub get_input {
|
181
|
+
my $input = <>;
|
182
|
+
print "\n" and return if !$input; # EOF
|
183
|
+
chomp $input;
|
184
|
+
return if !$input or $input =~ m/\A \s* q/xi;
|
185
|
+
return
|
186
|
+
( $input =~ m/\A \s* r/xi ) ? 'rock'
|
187
|
+
: ( $input =~ m/\A \s* p/xi ) ? 'paper'
|
188
|
+
: ( $input =~ m/\A \s* s/xi ) ? 'scissors'
|
189
|
+
: 'error';
|
190
|
+
}
|
191
|
+
|
192
|
+
sub describe_turn_result {
|
193
|
+
my ( $human_ch, $comp_ch, $result ) = @_;
|
194
|
+
print "You chose \u$human_ch, I chose \u$comp_ch. $result\n";
|
195
|
+
}
|
196
|
+
|
197
|
+
main();
|
198
|
+
```
|
199
|
+
|
200
|
+
## C\#
|
201
|
+
|
202
|
+
```csharp
|
203
|
+
using System;
|
204
|
+
using System.Collections.Generic;
|
205
|
+
using System.Linq;
|
206
|
+
|
207
|
+
namespace RockPaperScissors
|
208
|
+
{
|
209
|
+
class Program
|
210
|
+
{
|
211
|
+
static void Main(string[] args)
|
212
|
+
{
|
213
|
+
// There is no limit on the amount of weapons supported by RPSGame. Matchups are calculated depending on the order.
|
214
|
+
var rps = new RPSGame("scissors", "paper", "rock", "lizard", "spock");
|
215
|
+
|
216
|
+
int wins = 0, losses = 0, draws = 0;
|
217
|
+
|
218
|
+
while (true)
|
219
|
+
{
|
220
|
+
Console.WriteLine("Make your move: " + string.Join(", ", rps.Weapons) + ", quit");
|
221
|
+
|
222
|
+
string weapon = Console.ReadLine().Trim().ToLower();
|
223
|
+
|
224
|
+
if (weapon == "quit")
|
225
|
+
break;
|
226
|
+
|
227
|
+
if (!rps.Weapons.Contains(weapon))
|
228
|
+
{
|
229
|
+
Console.WriteLine("Invalid weapon!");
|
230
|
+
continue;
|
231
|
+
}
|
232
|
+
|
233
|
+
int result = rps.Next(weapon);
|
234
|
+
|
235
|
+
Console.WriteLine("You chose {0} and your opponent chose {1}!", weapon, rps.LastAIWeapon);
|
236
|
+
|
237
|
+
switch (result)
|
238
|
+
{
|
239
|
+
case 1: Console.WriteLine("{0} pwns {1}. You're a winner!", weapon, rps.LastAIWeapon);
|
240
|
+
wins++;
|
241
|
+
break;
|
242
|
+
case 0: Console.WriteLine("Draw!");
|
243
|
+
draws++;
|
244
|
+
break;
|
245
|
+
case -1: Console.WriteLine("{0} pwns {1}. You're a loser!", rps.LastAIWeapon, weapon);
|
246
|
+
losses++;
|
247
|
+
break;
|
248
|
+
}
|
249
|
+
|
250
|
+
Console.WriteLine();
|
251
|
+
}
|
252
|
+
|
253
|
+
Console.WriteLine("\nPlayer Statistics\nWins: {0}\nLosses: {1}\nDraws: {2}", wins, losses, draws);
|
254
|
+
}
|
255
|
+
|
256
|
+
class RPSGame
|
257
|
+
{
|
258
|
+
public RPSGame(params string[] weapons)
|
259
|
+
{
|
260
|
+
Weapons = weapons;
|
261
|
+
|
262
|
+
// Creates a new AI opponent, and gives it the list of weapons.
|
263
|
+
_rpsAI = new RPSAI(weapons);
|
264
|
+
}
|
265
|
+
|
266
|
+
// Play next turn.
|
267
|
+
public int Next(string weapon)
|
268
|
+
{
|
269
|
+
string aiWeapon = _rpsAI.NextMove(); // Gets the AI opponent's next move.
|
270
|
+
LastAIWeapon = aiWeapon; // Saves the AI opponent's move in a property so the player can see it.
|
271
|
+
|
272
|
+
_rpsAI.AddPlayerMove(weapon); // Let the AI know which weapon the player chose, for future predictions.
|
273
|
+
return GetWinner(Weapons, weapon, aiWeapon); // Returns -1 if AI win, 0 if draw, and 1 if player win.
|
274
|
+
}
|
275
|
+
|
276
|
+
// Returns matchup winner.
|
277
|
+
public static int GetWinner(string[] weapons, string weapon1, string weapon2)
|
278
|
+
{
|
279
|
+
if (weapon1 == weapon2)
|
280
|
+
return 0; // If weapons are the same, return 0 for draw.
|
281
|
+
|
282
|
+
if (GetVictories(weapons, weapon1).Contains(weapon2))
|
283
|
+
return 1; // Returns 1 for weapon1 win.
|
284
|
+
else if (GetVictories(weapons, weapon2).Contains(weapon1))
|
285
|
+
return -1; // Returns -1 for weapon2 win.
|
286
|
+
|
287
|
+
throw new Exception("No winner found.");
|
288
|
+
}
|
289
|
+
|
290
|
+
/*
|
291
|
+
* Return weapons that the provided weapon beats.
|
292
|
+
* The are calculated in the following way:
|
293
|
+
* If the index of the weapon is even, then all even indices less than it,
|
294
|
+
* and all odd indices greater than it, are victories.
|
295
|
+
* One exception is if it is an odd index, and also the last index in the set,
|
296
|
+
* then the first index in the set is a victory.
|
297
|
+
*/
|
298
|
+
public static IEnumerable<string> GetVictories(string[] weapons, string weapon)
|
299
|
+
{
|
300
|
+
// Gets index of weapon.
|
301
|
+
int index = Array.IndexOf(weapons, weapon);
|
302
|
+
|
303
|
+
// If weapon is odd and the final index in the set, then return the first item in the set as a victory.
|
304
|
+
if (index % 2 != 0 && index == weapons.Length - 1)
|
305
|
+
yield return weapons[0];
|
306
|
+
|
307
|
+
for (int i = index - 2; i >= 0; i -= 2)
|
308
|
+
yield return weapons[i];
|
309
|
+
|
310
|
+
for (int i = index + 1; i < weapons.Length; i += 2)
|
311
|
+
yield return weapons[i];
|
312
|
+
}
|
313
|
+
|
314
|
+
public string LastAIWeapon
|
315
|
+
{
|
316
|
+
private set;
|
317
|
+
get;
|
318
|
+
}
|
319
|
+
|
320
|
+
public readonly string[] Weapons;
|
321
|
+
private RPSAI _rpsAI;
|
322
|
+
|
323
|
+
class RPSAI
|
324
|
+
{
|
325
|
+
public RPSAI(params string[] weapons)
|
326
|
+
{
|
327
|
+
_weapons = weapons;
|
328
|
+
_weaponProbability = new Dictionary<string, int>();
|
329
|
+
|
330
|
+
// The AI sets the probability for each weapon to be chosen as 1.
|
331
|
+
foreach (string weapon in weapons)
|
332
|
+
_weaponProbability.Add(weapon, 1);
|
333
|
+
|
334
|
+
_random = new Random();
|
335
|
+
}
|
336
|
+
|
337
|
+
// Increases probability of selecting each weapon that beats the provided move.
|
338
|
+
public void AddPlayerMove(string weapon)
|
339
|
+
{
|
340
|
+
int index = Array.IndexOf(_weapons, weapon);
|
341
|
+
|
342
|
+
foreach (string winWeapon in _weapons.Except(GetVictories(_weapons, weapon)))
|
343
|
+
if (winWeapon != weapon)
|
344
|
+
_weaponProbability[winWeapon]++;
|
345
|
+
}
|
346
|
+
|
347
|
+
// Gets the AI's next move.
|
348
|
+
public string NextMove()
|
349
|
+
{
|
350
|
+
double r = _random.NextDouble();
|
351
|
+
|
352
|
+
double divisor = _weaponProbability.Values.Sum();
|
353
|
+
|
354
|
+
var weightedWeaponRanges = new Dictionary<double, string>();
|
355
|
+
|
356
|
+
double currentPos = 0.0;
|
357
|
+
|
358
|
+
// Maps probabilities to ranges between 0.0 and 1.0. Returns weighted random weapon.
|
359
|
+
foreach (var weapon in _weaponProbability)
|
360
|
+
{
|
361
|
+
double weightedRange = weapon.Value / divisor;
|
362
|
+
if (r <= currentPos + (weapon.Value / divisor))
|
363
|
+
return weapon.Key;
|
364
|
+
currentPos += weightedRange;
|
365
|
+
}
|
366
|
+
|
367
|
+
throw new Exception("Error calculating move.");
|
368
|
+
}
|
369
|
+
|
370
|
+
Random _random;
|
371
|
+
private readonly string[] _weapons;
|
372
|
+
private Dictionary<string, int> _weaponProbability;
|
373
|
+
}
|
374
|
+
}
|
375
|
+
}
|
376
|
+
}
|
377
|
+
```
|
378
|
+
|
379
|
+
## C++
|
380
|
+
|
381
|
+
```cpp
|
382
|
+
#include <windows.h>
|
383
|
+
#include <iostream>
|
384
|
+
#include <string>
|
385
|
+
|
386
|
+
//-------------------------------------------------------------------------------
|
387
|
+
using namespace std;
|
388
|
+
|
389
|
+
//-------------------------------------------------------------------------------
|
390
|
+
enum choices { ROCK, SPOCK, PAPER, LIZARD, SCISSORS, MX_C };
|
391
|
+
enum indexes { PLAYER, COMPUTER, DRAW };
|
392
|
+
|
393
|
+
//-------------------------------------------------------------------------------
|
394
|
+
class stats
|
395
|
+
{
|
396
|
+
public:
|
397
|
+
stats() : _draw( 0 )
|
398
|
+
{
|
399
|
+
ZeroMemory( _moves, sizeof( _moves ) );
|
400
|
+
ZeroMemory( _win, sizeof( _win ) );
|
401
|
+
}
|
402
|
+
void draw() { _draw++; }
|
403
|
+
void win( int p ) { _win[p]++; }
|
404
|
+
void move( int p, int m ) { _moves[p][m]++; }
|
405
|
+
int getMove( int p, int m ) { return _moves[p][m]; }
|
406
|
+
string format( int a )
|
407
|
+
{
|
408
|
+
char t[32];
|
409
|
+
wsprintf( t, "%.3d", a );
|
410
|
+
string d( t );
|
411
|
+
return d;
|
412
|
+
}
|
413
|
+
|
414
|
+
void print()
|
415
|
+
{
|
416
|
+
string d = format( _draw ),
|
417
|
+
pw = format( _win[PLAYER] ), cw = format( _win[COMPUTER] ),
|
418
|
+
pr = format( _moves[PLAYER][ROCK] ), cr = format( _moves[COMPUTER][ROCK] ),
|
419
|
+
pp = format( _moves[PLAYER][PAPER] ), cp = format( _moves[COMPUTER][PAPER] ),
|
420
|
+
ps = format( _moves[PLAYER][SCISSORS] ), cs = format( _moves[COMPUTER][SCISSORS] ),
|
421
|
+
pl = format( _moves[PLAYER][LIZARD] ), cl = format( _moves[COMPUTER][LIZARD] ),
|
422
|
+
pk = format( _moves[PLAYER][SPOCK] ), ck = format( _moves[COMPUTER][SPOCK] );
|
423
|
+
|
424
|
+
system( "cls" );
|
425
|
+
cout << endl;
|
426
|
+
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
|
427
|
+
cout << "| | WON | DRAW | ROCK | PAPER | SCISSORS | LIZARD | SPOCK |" << endl;
|
428
|
+
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
|
429
|
+
cout << "| PLAYER | " << pw << " | | " << pr << " | " << pp << " | " << ps << " | " << pl << " | " << pk << " |" << endl;
|
430
|
+
cout << "+----------+-------+ " << d << " +--------+---------+----------+--------+---------+" << endl;
|
431
|
+
cout << "| COMPUTER | " << cw << " | | " << cr << " | " << cp << " | " << cs << " | " << cl << " | " << ck << " |" << endl;
|
432
|
+
cout << "+----------+-------+--------+--------+---------+----------+--------+---------+" << endl;
|
433
|
+
cout << endl << endl;
|
434
|
+
|
435
|
+
system( "pause" );
|
436
|
+
|
437
|
+
}
|
438
|
+
|
439
|
+
private:
|
440
|
+
int _moves[2][MX_C], _win[2], _draw;
|
441
|
+
};
|
442
|
+
//-------------------------------------------------------------------------------
|
443
|
+
class rps
|
444
|
+
{
|
445
|
+
private:
|
446
|
+
int makeMove()
|
447
|
+
{
|
448
|
+
int total = 0, r, s;
|
449
|
+
for( int i = 0; i < MX_C; total += statistics.getMove( PLAYER, i++ ) );
|
450
|
+
r = rand() % total;
|
451
|
+
|
452
|
+
for( int i = ROCK; i < SCISSORS; i++ )
|
453
|
+
{
|
454
|
+
s = statistics.getMove( PLAYER, i );
|
455
|
+
if( r < s ) return ( i + 1 );
|
456
|
+
r -= s;
|
457
|
+
}
|
458
|
+
|
459
|
+
return ROCK;
|
460
|
+
}
|
461
|
+
|
462
|
+
void printMove( int p, int m )
|
463
|
+
{
|
464
|
+
if( p == COMPUTER ) cout << "My move: ";
|
465
|
+
else cout << "Your move: ";
|
466
|
+
|
467
|
+
switch( m )
|
468
|
+
{
|
469
|
+
case ROCK: cout << "ROCK\n"; break;
|
470
|
+
case PAPER: cout << "PAPER\n"; break;
|
471
|
+
case SCISSORS: cout << "SCISSORS\n"; break;
|
472
|
+
case LIZARD: cout << "LIZARD\n"; break;
|
473
|
+
case SPOCK: cout << "SPOCK\n";
|
474
|
+
}
|
475
|
+
}
|
476
|
+
|
477
|
+
public:
|
478
|
+
rps()
|
479
|
+
{
|
480
|
+
checker[ROCK][ROCK] = 2; checker[ROCK][PAPER] = 1; checker[ROCK][SCISSORS] = 0; checker[ROCK][LIZARD] = 0; checker[ROCK][SPOCK] = 1;
|
481
|
+
checker[PAPER][ROCK] = 0; checker[PAPER][PAPER] = 2; checker[PAPER][SCISSORS] = 1; checker[PAPER][LIZARD] = 1; checker[PAPER][SPOCK] = 0;
|
482
|
+
checker[SCISSORS][ROCK] = 1; checker[SCISSORS][PAPER] = 0; checker[SCISSORS][SCISSORS] = 2; checker[SCISSORS][LIZARD] = 0; checker[SCISSORS][SPOCK] = 1;
|
483
|
+
checker[LIZARD][ROCK] = 1; checker[LIZARD][PAPER] = 0; checker[LIZARD][SCISSORS] = 1; checker[LIZARD][LIZARD] = 2; checker[LIZARD][SPOCK] = 0;
|
484
|
+
checker[SPOCK][ROCK] = 0; checker[SPOCK][PAPER] = 1; checker[SPOCK][SCISSORS] = 0; checker[SPOCK][LIZARD] = 1; checker[SPOCK][SPOCK] = 2;
|
485
|
+
}
|
486
|
+
void play()
|
487
|
+
{
|
488
|
+
int p, r, m;
|
489
|
+
while( true )
|
490
|
+
{
|
491
|
+
cout << "What is your move (1)ROCK (2)SPOCK (3)PAPER (4)LIZARD (5)SCISSORS (0)Quit ? ";
|
492
|
+
cin >> p;
|
493
|
+
if( !p || p < 0 ) break;
|
494
|
+
if( p > 0 && p < 6 )
|
495
|
+
{
|
496
|
+
p--;
|
497
|
+
cout << endl;
|
498
|
+
printMove( PLAYER, p );
|
499
|
+
statistics.move( PLAYER, p );
|
500
|
+
|
501
|
+
m = makeMove();
|
502
|
+
statistics.move( COMPUTER, m );
|
503
|
+
printMove( COMPUTER, m );
|
504
|
+
|
505
|
+
r = checker[p][m];
|
506
|
+
switch( r )
|
507
|
+
{
|
508
|
+
case DRAW:
|
509
|
+
cout << endl << "DRAW!" << endl << endl;
|
510
|
+
statistics.draw();
|
511
|
+
break;
|
512
|
+
case COMPUTER:
|
513
|
+
cout << endl << "I WIN!" << endl << endl;
|
514
|
+
statistics.win( COMPUTER );
|
515
|
+
break;
|
516
|
+
case PLAYER:
|
517
|
+
cout << endl << "YOU WIN!" << endl << endl;
|
518
|
+
statistics.win( PLAYER );
|
519
|
+
|
520
|
+
}
|
521
|
+
system( "pause" );
|
522
|
+
}
|
523
|
+
system( "cls" );
|
524
|
+
}
|
525
|
+
statistics.print();
|
526
|
+
}
|
527
|
+
|
528
|
+
private:
|
529
|
+
stats statistics;
|
530
|
+
int checker[MX_C][MX_C];
|
531
|
+
};
|
532
|
+
//-------------------------------------------------------------------------------
|
533
|
+
int main( int argc, char* argv[] )
|
534
|
+
{
|
535
|
+
srand( GetTickCount() );
|
536
|
+
rps game;
|
537
|
+
game.play();
|
538
|
+
return 0;
|
539
|
+
}
|
540
|
+
//-------------------------------------------------------------------------------
|
541
|
+
```
|
542
|
+
|
543
|
+
## Go
|
544
|
+
|
545
|
+
```go
|
546
|
+
|
547
|
+
package main
|
548
|
+
import (
|
549
|
+
"fmt"
|
550
|
+
"math/rand"
|
551
|
+
"strings"
|
552
|
+
"time"
|
553
|
+
)
|
554
|
+
|
555
|
+
const rps = "rps"
|
556
|
+
|
557
|
+
var msg = []string{
|
558
|
+
"Rock breaks scissors",
|
559
|
+
"Paper covers rock",
|
560
|
+
"Scissors cut paper",
|
561
|
+
}
|
562
|
+
|
563
|
+
func main() {
|
564
|
+
rand.Seed(time.Now().UnixNano())
|
565
|
+
fmt.Println("Rock Paper Scissors")
|
566
|
+
fmt.Println("Enter r, p, or s as your play. Anything else ends the game.")
|
567
|
+
fmt.Println("Running score shown as <your wins>:<my wins>")
|
568
|
+
var pi string // player input
|
569
|
+
var aScore, pScore int
|
570
|
+
sl := 3 // for output alignment
|
571
|
+
pcf := make([]int, 3) // pcf = player choice frequency
|
572
|
+
var plays int
|
573
|
+
aChoice := rand.Intn(3) // ai choice for first play is completely random
|
574
|
+
for {
|
575
|
+
// get player choice
|
576
|
+
fmt.Print("Play: ")
|
577
|
+
_, err := fmt.Scanln(&pi) // lazy
|
578
|
+
if err != nil || len(pi) != 1 {
|
579
|
+
break
|
580
|
+
}
|
581
|
+
pChoice := strings.Index(rps, pi)
|
582
|
+
if pChoice < 0 {
|
583
|
+
break
|
584
|
+
}
|
585
|
+
pcf[pChoice]++
|
586
|
+
plays++
|
587
|
+
|
588
|
+
// show result of play
|
589
|
+
fmt.Printf("My play:%s%c. ", strings.Repeat(" ", sl-2), rps[aChoice])
|
590
|
+
switch (aChoice - pChoice + 3) % 3 {
|
591
|
+
case 0:
|
592
|
+
fmt.Println("Tie.")
|
593
|
+
case 1:
|
594
|
+
fmt.Printf("%s. My point.\n", msg[aChoice])
|
595
|
+
aScore++
|
596
|
+
case 2:
|
597
|
+
fmt.Printf("%s. Your point.\n", msg[pChoice])
|
598
|
+
pScore++
|
599
|
+
}
|
600
|
+
|
601
|
+
// show score
|
602
|
+
sl, _ = fmt.Printf("%d:%d ", pScore, aScore)
|
603
|
+
|
604
|
+
// compute ai choice for next play
|
605
|
+
switch rn := rand.Intn(plays); {
|
606
|
+
case rn < pcf[0]:
|
607
|
+
aChoice = 1
|
608
|
+
case rn < pcf[0]+pcf[1]:
|
609
|
+
aChoice = 2
|
610
|
+
default:
|
611
|
+
aChoice = 0
|
612
|
+
}
|
613
|
+
}
|
614
|
+
}
|
615
|
+
```
|
data/lib/rps/version.rb
CHANGED
data/rps.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "PlayRockPaperScissorsGame"
|
3
|
-
spec.version = "2.8.
|
4
|
-
spec.date = "2017-05-
|
3
|
+
spec.version = "2.8.3"
|
4
|
+
spec.date = "2017-05-15"
|
5
5
|
spec.summary = "A Rock Paper Scissors Ruby Gem"
|
6
6
|
spec.description = <<-EOF
|
7
7
|
A Ruby-programmed rock paper scissors game.
|
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: 2.8.
|
4
|
+
version: 2.8.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- bag3318
|
@@ -30,7 +30,7 @@ cert_chain:
|
|
30
30
|
ft+uhriUZSVDBJRLSlrQEH2f0866a9dA4oUmvvFU46Mh6pozDjOcLJIp/tCnbVOc
|
31
31
|
HSdXPrjfOoDbhBPH/4wUd5P0rDoNKN1hxH4SzA==
|
32
32
|
-----END CERTIFICATE-----
|
33
|
-
date: 2017-05-
|
33
|
+
date: 2017-05-15 00:00:00.000000000 Z
|
34
34
|
dependencies:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: bundler
|
@@ -171,6 +171,7 @@ files:
|
|
171
171
|
- docs/assets/js/pace.min.js
|
172
172
|
- docs/index.md
|
173
173
|
- docs/jekyll-theme-cayman.gemspec
|
174
|
+
- docs/other_langs.md
|
174
175
|
- docs/script/bootstrap
|
175
176
|
- docs/script/cibuild
|
176
177
|
- docs/script/release
|
metadata.gz.sig
CHANGED
Binary file
|