ro_sham_bo 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +7 -0
- data/Gemfile.lock +19 -0
- data/README.md +7 -0
- data/bin/ro_sham_bo +32 -18
- data/lib/ro_sham_bo.rb +49 -18
- data/lib/ro_sham_bo/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6d7272df4c4bb07d3cb42f6f8f8c53cab4df3c90de0845eb114f33324d6776f
|
4
|
+
data.tar.gz: a2d1afeea2ea0dc7dcc5a59081a2bd1a68d68055b759a06bd37b5ee89654deb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a3c15a28d03029167282ce16bb04b57da8507ec7e957dc88c95c5a70dec2fbb69dc9a75d531d722f3a0043e386a82c37907e249fa243a24135aab11afc197d2
|
7
|
+
data.tar.gz: 48737233a2a9ed372df508dda9b4086d83eebaa9ee2a94ac4d77b3671bc0c1975b502a9c1a8b04e6b4c02fc76eba05f084e8560264c2d0a9c64ee5a7bb30e829
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
CHANGED
@@ -28,11 +28,18 @@ Once you hit `[return]`, your choice will be compared to the computer's choice,
|
|
28
28
|
and a winner for that round will be chosen based off the classic rules of the
|
29
29
|
game.
|
30
30
|
|
31
|
+
Cheating is possible, both for the user and the computer. Pass `-c` (or `-c
|
32
|
+
user`) for the user to always win. Pass `-c computer` to make the computer win.
|
33
|
+
It will still look real; cheating only actually happens when the non-cheater has
|
34
|
+
one less point than is needed to win. Even then, it can still return a draw for
|
35
|
+
that round.
|
36
|
+
|
31
37
|
### Rules
|
32
38
|
The rules are standard rock, paper, scissors rules.
|
33
39
|
- Rock beats Scissors
|
34
40
|
- Scissors beats Paper
|
35
41
|
- Paper beats Rock
|
42
|
+
- Two of a kind results in a draw
|
36
43
|
|
37
44
|
## Reporting Bugs
|
38
45
|
This was just a fun project for me to work on, but if you find problems, please
|
data/bin/ro_sham_bo
CHANGED
@@ -2,41 +2,55 @@
|
|
2
2
|
require 'optparse'
|
3
3
|
require_relative '../lib/ro_sham_bo.rb'
|
4
4
|
|
5
|
-
|
5
|
+
clear = false
|
6
|
+
options = {}
|
6
7
|
OptionParser.new do |o|
|
7
|
-
o.on('-c', '--[no-]clear',
|
8
|
-
|
8
|
+
o.on('-c', '--[no-]clear', 'Clear screen after every turn') do |f|
|
9
|
+
clear = f
|
9
10
|
end
|
10
|
-
o.on('-r', '--rounds
|
11
|
+
o.on('-r', '--rounds N', Integer, 'Best of N rounds', 'Default is 3') do |f|
|
11
12
|
abort("Please use an odd number") if f.even?
|
12
13
|
options[:rounds] = f
|
13
14
|
end
|
15
|
+
o.on('-c', '--cheater [USER]', 'Choose who wins', 'user/computer') do |f|
|
16
|
+
options[:cheater] =
|
17
|
+
if f.nil?
|
18
|
+
:user
|
19
|
+
elsif !%w[user computer].include?(f)
|
20
|
+
abort('Invalid cheater [user, computer]')
|
21
|
+
else
|
22
|
+
f.intern
|
23
|
+
end
|
24
|
+
end
|
14
25
|
end.parse!(ARGV)
|
15
26
|
|
16
|
-
|
27
|
+
system('clear') if clear
|
17
28
|
|
18
|
-
|
29
|
+
game = RoShamBo.new(**options)
|
19
30
|
|
20
31
|
puts ">> FIRST TO #{game.points_to_win} WINS! <<"
|
21
32
|
|
22
|
-
until game.
|
33
|
+
until game.over?
|
23
34
|
puts "\nCurrent score: #{game.score.inspect}"
|
24
35
|
print "What is your choice? (r)ock, (p)aper, or (s)cissors?: "
|
25
|
-
u_choice = game.user_choice($stdin.gets.chomp.downcase)
|
26
|
-
c_choice = game.computer_choice
|
27
36
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
37
|
+
begin
|
38
|
+
input = $stdin.gets.chomp.downcase
|
39
|
+
break if %w[x q exit quit].include?(input)
|
40
|
+
game.play(input)
|
41
|
+
rescue ArgumentError
|
42
|
+
puts 'Invalid choice [rpsx]'
|
32
43
|
next
|
33
44
|
end
|
34
45
|
|
35
|
-
{user:
|
36
|
-
|
46
|
+
{ user: game.user_choice, computer: game.computer_choice }.each do |k, v|
|
47
|
+
puts "> #{k} chose #{v}"
|
48
|
+
end
|
49
|
+
system('clear') if clear
|
37
50
|
|
38
|
-
puts "Round winner: #{game.round_winner
|
51
|
+
puts "Round winner: #{game.round_winner}"
|
39
52
|
end
|
40
53
|
|
41
|
-
puts game.
|
42
|
-
"\nGame was quit.
|
54
|
+
puts game.over? ? "\nGame over! #{game.winner.capitalize} won!" :
|
55
|
+
"\nGame was quit."
|
56
|
+
puts "Score was #{game.score.inspect}"
|
data/lib/ro_sham_bo.rb
CHANGED
@@ -5,28 +5,40 @@ class RoShamBo
|
|
5
5
|
scissors: {rock: :lose, paper: :win, scissors: :draw}.freeze,
|
6
6
|
}.freeze
|
7
7
|
|
8
|
-
attr_reader :
|
8
|
+
attr_reader :cheater
|
9
|
+
attr_reader :rounds
|
10
|
+
attr_reader :score
|
11
|
+
attr_reader :points_to_win
|
12
|
+
attr_reader :user_choice
|
13
|
+
attr_reader :computer_choice
|
14
|
+
attr_reader :round_winner
|
15
|
+
attr_reader :winner
|
9
16
|
|
10
|
-
def initialize(
|
11
|
-
@
|
17
|
+
def initialize(cheater: nil, rounds: 3)
|
18
|
+
@cheater = cheater
|
19
|
+
@rounds = rounds
|
12
20
|
@score = {user: 0, computer: 0}
|
13
|
-
@points_to_win = (
|
21
|
+
@points_to_win = (rounds.to_f / 2).ceil
|
14
22
|
end
|
15
23
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
24
|
+
def play(input)
|
25
|
+
raise 'Game Over!' if over?
|
26
|
+
|
27
|
+
@user_choice = sanitized_choice(input)
|
28
|
+
@computer_choice = computer_turn(user_choice)
|
29
|
+
@round_winner = determine_round_winner(@user_choice, @computer_choice)
|
30
|
+
@winner = score.key(points_to_win) if score.value?(points_to_win)
|
31
|
+
|
32
|
+
self
|
23
33
|
end
|
24
34
|
|
25
|
-
def
|
26
|
-
|
35
|
+
def over?
|
36
|
+
!winner.nil?
|
27
37
|
end
|
28
38
|
|
29
|
-
|
39
|
+
private
|
40
|
+
|
41
|
+
def determine_round_winner(u_choice, c_choice)
|
30
42
|
winner =
|
31
43
|
case RULES[u_choice][c_choice]
|
32
44
|
when :win then :user
|
@@ -37,11 +49,30 @@ class RoShamBo
|
|
37
49
|
winner
|
38
50
|
end
|
39
51
|
|
40
|
-
def
|
41
|
-
|
52
|
+
def sanitized_choice(input)
|
53
|
+
case input
|
54
|
+
when 'r', 'rock', :r, :rock then :rock
|
55
|
+
when 'p', 'paper', :p, :paper then :paper
|
56
|
+
when 's', 'scissors', :s, :scissors then :scissors
|
57
|
+
else raise ArgumentError, 'Invalid choice [rpsx]'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def computer_turn(input)
|
62
|
+
if cheater == :user && about_to_win?(:computer)
|
63
|
+
cheat(input, :win)
|
64
|
+
elsif cheater == :computer && about_to_win?(:user)
|
65
|
+
cheat(input, :win)
|
66
|
+
else
|
67
|
+
RULES.keys.sample
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def cheat(input, win_or_lose)
|
72
|
+
rand >= 0.33 ? RULES[input].key(win_or_lose) : RULES[input].key(:draw)
|
42
73
|
end
|
43
74
|
|
44
|
-
def
|
45
|
-
|
75
|
+
def about_to_win?(user)
|
76
|
+
score[user] == points_to_win - 1
|
46
77
|
end
|
47
78
|
end
|
data/lib/ro_sham_bo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ro_sham_bo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan Gray
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-04
|
11
|
+
date: 2021-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -38,6 +38,8 @@ extensions: []
|
|
38
38
|
extra_rdoc_files: []
|
39
39
|
files:
|
40
40
|
- ".gitignore"
|
41
|
+
- Gemfile
|
42
|
+
- Gemfile.lock
|
41
43
|
- LICENSE
|
42
44
|
- README.md
|
43
45
|
- Rakefile
|