ro_sham_bo 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +14 -11
- data/Gemfile.lock +2 -1
- data/README.md +43 -4
- data/Rakefile +6 -6
- data/bin/ro_sham_bo +1 -1
- data/lib/ro_sham_bo/version.rb +35 -4
- data/lib/ro_sham_bo.rb +17 -20
- data/ro_sham_bo.gemspec +21 -22
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18c5694e6c976138f96301fba33a604a8bdea019c83a0a9877aac0dde5d4930a
|
4
|
+
data.tar.gz: 00b2827afb8f3e2809b70b884292a2edae559f8518cea936c291b585ab374e31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fbc808f35951ea2d74fcdc896266cecf27d1083a5710119f94a7818779d3f615a0e31a262cdfc1ad682bfaacf1e0f9f4b0fab035ddc33d5df6d0e898d2c74fc
|
7
|
+
data.tar.gz: 0e494de6e32c446478d73567e0eb48c5ab7fb99e366727276175645e1cf0371a5864f9d68033bc27d22ec3e3137dde1dfb46e83f2a9f64643cd03510bb5bab40
|
data/.github/workflows/ruby.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
name: Ruby
|
1
|
+
name: Ruby CI
|
2
2
|
|
3
3
|
on:
|
4
4
|
push:
|
@@ -11,14 +11,17 @@ jobs:
|
|
11
11
|
|
12
12
|
runs-on: ubuntu-latest
|
13
13
|
|
14
|
+
strategy:
|
15
|
+
matrix:
|
16
|
+
ruby-version: ['3.2', '3.1', '3.0']
|
17
|
+
|
14
18
|
steps:
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
bundle
|
24
|
-
bundle exec rake test
|
19
|
+
- uses: actions/checkout@v3
|
20
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
21
|
+
uses: ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1
|
22
|
+
with:
|
23
|
+
ruby-version: ${{ matrix.ruby-version }}
|
24
|
+
- name: Install dependencies
|
25
|
+
run: bundle install
|
26
|
+
- name: Run tests
|
27
|
+
run: bundle exec rake
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -33,6 +33,32 @@ Once you hit `[return]`, your choice will be compared to the computer's choice,
|
|
33
33
|
and a winner for that round will be chosen based off the classic rules of the
|
34
34
|
game.
|
35
35
|
|
36
|
+
```
|
37
|
+
$ ro_sham_bo
|
38
|
+
>> FIRST TO 2 WINS! <<
|
39
|
+
|
40
|
+
Current score: {:user=>0, :computer=>0}
|
41
|
+
What is your choice? (r)ock, (p)aper, or (s)cissors?: p
|
42
|
+
> user chose paper
|
43
|
+
> computer chose paper
|
44
|
+
Round winner: draw
|
45
|
+
|
46
|
+
Current score: {:user=>0, :computer=>0}
|
47
|
+
What is your choice? (r)ock, (p)aper, or (s)cissors?: s
|
48
|
+
> user chose scissors
|
49
|
+
> computer chose rock
|
50
|
+
Round winner: computer
|
51
|
+
|
52
|
+
Current score: {:user=>0, :computer=>1}
|
53
|
+
What is your choice? (r)ock, (p)aper, or (s)cissors?: r
|
54
|
+
> user chose rock
|
55
|
+
> computer chose paper
|
56
|
+
Round winner: computer
|
57
|
+
|
58
|
+
Game over! Computer won!
|
59
|
+
Score was {:user=>0, :computer=>2}
|
60
|
+
```
|
61
|
+
|
36
62
|
Cheating is possible, both for the user and the computer. Pass `-c` (or `-c
|
37
63
|
user`) for the user to always win. Pass `-c computer` to make the computer win.
|
38
64
|
It will still look real; cheating only actually happens when the non-cheater
|
@@ -44,24 +70,37 @@ You can play via an IRB/Pry console via the gem's API.
|
|
44
70
|
|
45
71
|
```ruby
|
46
72
|
require 'ro_sham_bo'
|
73
|
+
# => true
|
47
74
|
|
48
75
|
game = RoShamBo.new
|
49
|
-
|
50
|
-
# => #<RoShamBo:0x00007fe3011a2070
|
76
|
+
# => #<RoShamBo:0x00007f7ffa99a5a8
|
51
77
|
# @cheater=nil,
|
78
|
+
# @draws=0,
|
52
79
|
# @points_to_win=2,
|
53
80
|
# @rounds=3,
|
54
81
|
# @score={:user=>0, :computer=>0}>
|
55
82
|
|
83
|
+
game.play(:rock)
|
84
|
+
#=> #<RoShamBo:0x00007f7ffa99a5a8
|
85
|
+
# @cheater=nil,
|
86
|
+
# @computer_choice=:rock,
|
87
|
+
# @draws=1,
|
88
|
+
# @points_to_win=2,
|
89
|
+
# @round_winner=:draw,
|
90
|
+
# @rounds=3,
|
91
|
+
# @score={:user=>0, :computer=>0},
|
92
|
+
# @user_choice=:rock>
|
93
|
+
|
56
94
|
game.play(%i[rock paper scissors].sample) until game.over?
|
57
95
|
game
|
58
|
-
# => #<RoShamBo:
|
96
|
+
# => #<RoShamBo:0x00007f7ffa99a5a8
|
59
97
|
# @cheater=nil,
|
60
98
|
# @computer_choice=:paper,
|
99
|
+
# @draws=3,
|
61
100
|
# @points_to_win=2,
|
62
101
|
# @round_winner=:user,
|
63
102
|
# @rounds=3,
|
64
|
-
# @score={:user=>2, :computer=>
|
103
|
+
# @score={:user=>2, :computer=>0},
|
65
104
|
# @user_choice=:scissors,
|
66
105
|
# @winner=:user>
|
67
106
|
```
|
data/Rakefile
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require
|
5
|
-
require
|
6
|
-
|
3
|
+
require_relative "lib/ro_sham_bo"
|
4
|
+
require "bundler/gem_tasks"
|
5
|
+
require "rake/testtask"
|
6
|
+
require "rdoc/task"
|
7
7
|
|
8
8
|
Rake::TestTask.new do |t|
|
9
|
-
t.libs = [
|
9
|
+
t.libs = ["lib"]
|
10
10
|
t.warning = true
|
11
11
|
t.verbose = true
|
12
|
-
t.test_files = FileList[
|
12
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
13
13
|
end
|
14
14
|
|
15
15
|
# TODO: Add documentation.
|
data/bin/ro_sham_bo
CHANGED
@@ -12,7 +12,7 @@ OptionParser.new do |o|
|
|
12
12
|
abort("Please use an odd number") if f.even?
|
13
13
|
options[:rounds] = f
|
14
14
|
end
|
15
|
-
o.on('-
|
15
|
+
o.on('-C', '--cheater [USER]', 'Choose who wins', 'user/computer') do |f|
|
16
16
|
options[:cheater] =
|
17
17
|
if f.nil?
|
18
18
|
:user
|
data/lib/ro_sham_bo/version.rb
CHANGED
@@ -5,23 +5,54 @@ class RoShamBo
|
|
5
5
|
# Module that contains all gem version information. Follows semantic
|
6
6
|
# versioning. Read: https://semver.org/
|
7
7
|
module Version
|
8
|
-
|
9
8
|
##
|
10
9
|
# Major version.
|
10
|
+
#
|
11
|
+
# @return [Integer]
|
11
12
|
MAJOR = 0
|
12
13
|
|
13
14
|
##
|
14
15
|
# Minor version.
|
16
|
+
#
|
17
|
+
# @return [Integer]
|
15
18
|
MINOR = 1
|
16
19
|
|
17
20
|
##
|
18
21
|
# Patch version.
|
19
|
-
|
22
|
+
#
|
23
|
+
# @return [Integer]
|
24
|
+
PATCH = 2
|
25
|
+
|
26
|
+
module_function
|
27
|
+
|
28
|
+
##
|
29
|
+
# Version as +[MAJOR, MINOR, PATCH]+
|
30
|
+
#
|
31
|
+
# @return [Array<Integer>]
|
32
|
+
def to_a
|
33
|
+
[MAJOR, MINOR, PATCH]
|
34
|
+
end
|
20
35
|
|
21
36
|
##
|
22
37
|
# Version as +MAJOR.MINOR.PATCH+
|
23
|
-
|
24
|
-
|
38
|
+
#
|
39
|
+
# @return [String]
|
40
|
+
def to_s
|
41
|
+
to_a.join(".")
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Version as +{major: MAJOR, minor: MINOR, patch: PATCH}+
|
46
|
+
#
|
47
|
+
# @return [Hash]
|
48
|
+
def to_h
|
49
|
+
%i[major minor patch].zip(to_a).to_h
|
25
50
|
end
|
26
51
|
end
|
52
|
+
|
53
|
+
##
|
54
|
+
# The version, as a string.
|
55
|
+
#
|
56
|
+
# @return [String]
|
57
|
+
VERSION = Version.to_s
|
27
58
|
end
|
data/lib/ro_sham_bo.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "ro_sham_bo/version"
|
2
2
|
|
3
3
|
class RoShamBo
|
4
4
|
RULES = {
|
5
|
-
rock:
|
6
|
-
paper:
|
7
|
-
scissors: {rock: :lose, paper: :win,
|
5
|
+
rock: {rock: :draw, paper: :lose, scissors: :win}.freeze,
|
6
|
+
paper: {rock: :win, paper: :draw, scissors: :lose}.freeze,
|
7
|
+
scissors: {rock: :lose, paper: :win, scissors: :draw}.freeze
|
8
8
|
}.freeze
|
9
9
|
|
10
10
|
attr_reader :cheater
|
@@ -19,9 +19,9 @@ class RoShamBo
|
|
19
19
|
|
20
20
|
def initialize(cheater: nil, rounds: 3)
|
21
21
|
if rounds.even?
|
22
|
-
raise ArgumentError,
|
22
|
+
raise ArgumentError, "rounds must be odd"
|
23
23
|
elsif ![nil, :user, :computer].include?(cheater)
|
24
|
-
raise ArgumentError,
|
24
|
+
raise ArgumentError, "cheater must be :computer or :user"
|
25
25
|
end
|
26
26
|
|
27
27
|
@cheater = cheater
|
@@ -32,7 +32,7 @@ class RoShamBo
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def play(input)
|
35
|
-
raise
|
35
|
+
raise "Game Over!" if over?
|
36
36
|
|
37
37
|
@user_choice = sanitized_choice(input)
|
38
38
|
@computer_choice = computer_turn(user_choice)
|
@@ -49,22 +49,19 @@ class RoShamBo
|
|
49
49
|
private
|
50
50
|
|
51
51
|
def determine_round_winner(u_choice, c_choice)
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
end
|
58
|
-
winner == :draw ? @draws += 1 : score[winner] += 1
|
59
|
-
winner
|
52
|
+
case RULES.dig(u_choice, c_choice)
|
53
|
+
when :win then :user
|
54
|
+
when :lose then :computer
|
55
|
+
else :draw
|
56
|
+
end.tap { |winner| (winner == :draw) ? @draws += 1 : score[winner] += 1 }
|
60
57
|
end
|
61
58
|
|
62
59
|
def sanitized_choice(input)
|
63
60
|
case input
|
64
|
-
when
|
65
|
-
when
|
66
|
-
when
|
67
|
-
else raise ArgumentError,
|
61
|
+
when "r", "rock", :r, :rock then :rock
|
62
|
+
when "p", "paper", :p, :paper then :paper
|
63
|
+
when "s", "scissors", :s, :scissors then :scissors
|
64
|
+
else raise ArgumentError, "Invalid choice [rpsx]"
|
68
65
|
end
|
69
66
|
end
|
70
67
|
|
@@ -79,7 +76,7 @@ class RoShamBo
|
|
79
76
|
end
|
80
77
|
|
81
78
|
def cheat(input, win_or_lose)
|
82
|
-
rand >= 0.33 ? RULES[input].key(win_or_lose) : RULES[input].key(:draw)
|
79
|
+
(rand >= 0.33) ? RULES[input].key(win_or_lose) : RULES[input].key(:draw)
|
83
80
|
end
|
84
81
|
|
85
82
|
def about_to_win?(user)
|
data/ro_sham_bo.gemspec
CHANGED
@@ -1,34 +1,33 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "lib/ro_sham_bo/version"
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
|
-
spec.name
|
5
|
-
spec.version
|
6
|
-
spec.authors
|
7
|
-
spec.email
|
8
|
-
spec.license
|
9
|
-
spec.date = Time.now.strftime('%Y-%m-%d')
|
4
|
+
spec.name = "ro_sham_bo"
|
5
|
+
spec.version = RoShamBo::VERSION
|
6
|
+
spec.authors = ["Evan Gray"]
|
7
|
+
spec.email = "evanthegrayt@vivaldi.net"
|
8
|
+
spec.license = "MIT"
|
10
9
|
|
11
|
-
spec.summary
|
12
|
-
spec.description
|
13
|
-
spec.homepage
|
10
|
+
spec.summary = %(Rock Paper Scissors in Ruby)
|
11
|
+
spec.description = %(Play rock paper scissors from the command line)
|
12
|
+
spec.homepage = "https://evanthegrayt.github.io/ro_sham_bo/"
|
14
13
|
|
15
14
|
unless spec.respond_to?(:metadata)
|
16
|
-
raise
|
17
|
-
|
15
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
16
|
+
"public gem pushes."
|
18
17
|
end
|
19
18
|
|
20
|
-
spec.metadata[
|
21
|
-
spec.metadata[
|
22
|
-
spec.metadata[
|
23
|
-
|
19
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
20
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
21
|
+
spec.metadata["source_code_uri"] =
|
22
|
+
"https://github.com/evanthegrayt/ro_sham_bo"
|
24
23
|
|
25
24
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
25
|
f.match(%r{^(test|spec|features)/})
|
27
26
|
end
|
28
|
-
spec.bindir
|
29
|
-
spec.executables
|
30
|
-
spec.require_paths = [
|
31
|
-
spec.add_development_dependency
|
32
|
-
spec.add_development_dependency
|
33
|
-
spec.add_development_dependency
|
27
|
+
spec.bindir = "bin"
|
28
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
spec.add_development_dependency "rake", "~> 13.0", ">= 13.0.1"
|
31
|
+
spec.add_development_dependency "test-unit", "~> 3.3", ">= 3.3.5"
|
32
|
+
spec.add_development_dependency "simplecov"
|
34
33
|
end
|
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.1.
|
4
|
+
version: 0.1.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:
|
11
|
+
date: 2023-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
104
|
- !ruby/object:Gem::Version
|
105
105
|
version: '0'
|
106
106
|
requirements: []
|
107
|
-
rubygems_version: 3.
|
107
|
+
rubygems_version: 3.3.7
|
108
108
|
signing_key:
|
109
109
|
specification_version: 4
|
110
110
|
summary: Rock Paper Scissors in Ruby
|