ro_sham_bo 0.1.0 → 0.1.2

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
  SHA256:
3
- metadata.gz: ce77bcab61a9ae0943e2c524ce9db8549f2f655fe0c94b232459af086332416e
4
- data.tar.gz: '0692f563d97e79eedbf2e52d9d06c5b26cafbe7cb4c8f1472b2325ffe2066f69'
3
+ metadata.gz: 18c5694e6c976138f96301fba33a604a8bdea019c83a0a9877aac0dde5d4930a
4
+ data.tar.gz: 00b2827afb8f3e2809b70b884292a2edae559f8518cea936c291b585ab374e31
5
5
  SHA512:
6
- metadata.gz: d777b31737a483305adc350b97a5fd3983b15f1ba64905c667bbe3862c60ef2f72e458b3cccea258b36e904dccbd956fbecbbe1b3f5720fcdb30e1329a837d7f
7
- data.tar.gz: 3598e5ce1fbbbea18391fa235994b7e364eeccdd7780465c0f27386baa60e0953e471b605fe6fa608ccf01477fca70254698fb819e7735584541347e283c29b8
6
+ metadata.gz: 9fbc808f35951ea2d74fcdc896266cecf27d1083a5710119f94a7818779d3f615a0e31a262cdfc1ad682bfaacf1e0f9f4b0fab035ddc33d5df6d0e898d2c74fc
7
+ data.tar.gz: 0e494de6e32c446478d73567e0eb48c5ab7fb99e366727276175645e1cf0371a5864f9d68033bc27d22ec3e3137dde1dfb46e83f2a9f64643cd03510bb5bab40
@@ -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
- - uses: actions/checkout@v2
16
- - name: Set up Ruby 2.6
17
- uses: actions/setup-ruby@v1
18
- with:
19
- ruby-version: 2.6.x
20
- - name: Build and test with Rake
21
- run: |
22
- gem install bundler
23
- bundle install
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ro_sham_bo (0.1.0)
4
+ ro_sham_bo (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -19,6 +19,7 @@ GEM
19
19
  power_assert
20
20
 
21
21
  PLATFORMS
22
+ arm64-darwin-21
22
23
  x86_64-darwin-20
23
24
 
24
25
  DEPENDENCIES
data/README.md CHANGED
@@ -18,6 +18,7 @@ rake install
18
18
  ```
19
19
 
20
20
  ## Usage
21
+ ### Command Line
21
22
  Just call the `ro_sham_bo` executable, which should now be in your `$PATH`.
22
23
  The default number of rounds is best-of `3`. This can be altered with the
23
24
  `-r [NUMBER OF ROUNDS]` option. The number passed must be an odd number. To see
@@ -32,11 +33,77 @@ Once you hit `[return]`, your choice will be compared to the computer's choice,
32
33
  and a winner for that round will be chosen based off the classic rules of the
33
34
  game.
34
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
+
35
62
  Cheating is possible, both for the user and the computer. Pass `-c` (or `-c
36
63
  user`) for the user to always win. Pass `-c computer` to make the computer win.
37
- It will still look real; cheating only actually happens when the non-cheater has
38
- one less point than is needed to win. Even then, it can still return a draw for
39
- that round.
64
+ It will still look real; cheating only actually happens when the non-cheater
65
+ only needs one more point to win. Even then, it can still return a draw for that
66
+ round.
67
+
68
+ ### Console
69
+ You can play via an IRB/Pry console via the gem's API.
70
+
71
+ ```ruby
72
+ require 'ro_sham_bo'
73
+ # => true
74
+
75
+ game = RoShamBo.new
76
+ # => #<RoShamBo:0x00007f7ffa99a5a8
77
+ # @cheater=nil,
78
+ # @draws=0,
79
+ # @points_to_win=2,
80
+ # @rounds=3,
81
+ # @score={:user=>0, :computer=>0}>
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
+
94
+ game.play(%i[rock paper scissors].sample) until game.over?
95
+ game
96
+ # => #<RoShamBo:0x00007f7ffa99a5a8
97
+ # @cheater=nil,
98
+ # @computer_choice=:paper,
99
+ # @draws=3,
100
+ # @points_to_win=2,
101
+ # @round_winner=:user,
102
+ # @rounds=3,
103
+ # @score={:user=>2, :computer=>0},
104
+ # @user_choice=:scissors,
105
+ # @winner=:user>
106
+ ```
40
107
 
41
108
  ### Rules
42
109
  The rules are standard rock, paper, scissors rules.
data/Rakefile CHANGED
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'lib/ro_sham_bo'
4
- require 'bundler/gem_tasks'
5
- require 'rake/testtask'
6
- # require 'rdoc/task'
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 = ['lib']
9
+ t.libs = ["lib"]
10
10
  t.warning = true
11
11
  t.verbose = true
12
- t.test_files = FileList['test/**/*_test.rb']
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('-c', '--cheater [USER]', 'Choose who wins', 'user/computer') do |f|
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
@@ -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
- PATCH = 0
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
- def self.to_s
24
- "#{MAJOR}.#{MINOR}.#{PATCH}"
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,15 +1,16 @@
1
- require_relative 'ro_sham_bo/version'
1
+ require_relative "ro_sham_bo/version"
2
2
 
3
3
  class RoShamBo
4
4
  RULES = {
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,
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
11
11
  attr_reader :rounds
12
12
  attr_reader :score
13
+ attr_reader :draws
13
14
  attr_reader :points_to_win
14
15
  attr_reader :user_choice
15
16
  attr_reader :computer_choice
@@ -18,19 +19,20 @@ class RoShamBo
18
19
 
19
20
  def initialize(cheater: nil, rounds: 3)
20
21
  if rounds.even?
21
- raise ArgumentError, 'rounds must be odd'
22
+ raise ArgumentError, "rounds must be odd"
22
23
  elsif ![nil, :user, :computer].include?(cheater)
23
- raise ArgumentError, 'cheater must be :computer or :user'
24
+ raise ArgumentError, "cheater must be :computer or :user"
24
25
  end
25
26
 
26
27
  @cheater = cheater
27
28
  @rounds = rounds
28
29
  @score = {user: 0, computer: 0}
30
+ @draws = 0
29
31
  @points_to_win = (rounds.to_f / 2).ceil
30
32
  end
31
33
 
32
34
  def play(input)
33
- raise RuntimeError, 'Game Over!' if over?
35
+ raise "Game Over!" if over?
34
36
 
35
37
  @user_choice = sanitized_choice(input)
36
38
  @computer_choice = computer_turn(user_choice)
@@ -47,22 +49,19 @@ class RoShamBo
47
49
  private
48
50
 
49
51
  def determine_round_winner(u_choice, c_choice)
50
- winner =
51
- case RULES[u_choice][c_choice]
52
- when :win then :user
53
- when :lose then :computer
54
- else :draw
55
- end
56
- score[winner] += 1 unless winner == :draw
57
- 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 }
58
57
  end
59
58
 
60
59
  def sanitized_choice(input)
61
60
  case input
62
- when 'r', 'rock', :r, :rock then :rock
63
- when 'p', 'paper', :p, :paper then :paper
64
- when 's', 'scissors', :s, :scissors then :scissors
65
- else raise ArgumentError, 'Invalid choice [rpsx]'
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]"
66
65
  end
67
66
  end
68
67
 
@@ -77,7 +76,7 @@ class RoShamBo
77
76
  end
78
77
 
79
78
  def cheat(input, win_or_lose)
80
- 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)
81
80
  end
82
81
 
83
82
  def about_to_win?(user)
data/ro_sham_bo.gemspec CHANGED
@@ -1,34 +1,33 @@
1
- require_relative 'lib/ro_sham_bo/version'
1
+ require_relative "lib/ro_sham_bo/version"
2
2
 
3
3
  Gem::Specification.new do |spec|
4
- spec.name = 'ro_sham_bo'
5
- spec.version = RoShamBo::Version.to_s
6
- spec.authors = ['Evan Gray']
7
- spec.email = 'evanthegrayt@vivaldi.net'
8
- spec.license = 'MIT'
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 = %q{Rock Paper Scissors in Ruby}
12
- spec.description = %q{Play rock paper scissors from the command line}
13
- spec.homepage = 'https://evanthegrayt.github.io/ro_sham_bo/'
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 'RubyGems 2.0 or newer is required to protect against ' \
17
- 'public gem pushes.'
15
+ raise "RubyGems 2.0 or newer is required to protect against " \
16
+ "public gem pushes."
18
17
  end
19
18
 
20
- spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
- spec.metadata['homepage_uri'] = spec.homepage
22
- spec.metadata['source_code_uri'] =
23
- 'https://github.com/evanthegrayt/ro_sham_bo'
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 = 'bin'
29
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
30
- spec.require_paths = ['lib']
31
- spec.add_development_dependency 'rake', '~> 13.0', '>= 13.0.1'
32
- spec.add_development_dependency 'test-unit', '~> 3.3', '>= 3.3.5'
33
- spec.add_development_dependency 'simplecov'
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.0
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: 2021-05-04 00:00:00.000000000 Z
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.2.3
107
+ rubygems_version: 3.3.7
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: Rock Paper Scissors in Ruby