ro_sham_bo 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a6ebceb13a58c7ff84e66be32d08b422a57f41b37d361d591c69d88fbfbc9898
4
- data.tar.gz: b574f3923921bce52e828b177b950277c8983ec3eb12b2d83f2948e8010963c4
3
+ metadata.gz: b6d7272df4c4bb07d3cb42f6f8f8c53cab4df3c90de0845eb114f33324d6776f
4
+ data.tar.gz: a2d1afeea2ea0dc7dcc5a59081a2bd1a68d68055b759a06bd37b5ee89654deb8
5
5
  SHA512:
6
- metadata.gz: '084251e4644082549a1526be1a9d827b0aca60f7fa739b1db62258f5d31a4f4c259f4baea97f421ef1313ebbc6e8a1ca309764dbac3231c2e7ca86f1708d30e8'
7
- data.tar.gz: 264a793620438584bbe4ef70f7641a6aa287d43cd571baaaade783175a17f31ec35bcd07f654497289afeb1236184c9b2a330d5586c8c10870b7be90f8ffbf16
6
+ metadata.gz: 4a3c15a28d03029167282ce16bb04b57da8507ec7e957dc88c95c5a70dec2fbb69dc9a75d531d722f3a0043e386a82c37907e249fa243a24135aab11afc197d2
7
+ data.tar.gz: 48737233a2a9ed372df508dda9b4086d83eebaa9ee2a94ac4d77b3671bc0c1975b502a9c1a8b04e6b4c02fc76eba05f084e8560264c2d0a9c64ee5a7bb30e829
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
6
+
7
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,19 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ro_sham_bo (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (13.0.3)
10
+
11
+ PLATFORMS
12
+ x86_64-darwin-20
13
+
14
+ DEPENDENCIES
15
+ rake (~> 13.0, >= 13.0.1)
16
+ ro_sham_bo!
17
+
18
+ BUNDLED WITH
19
+ 2.2.16
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
- options = {rounds: 3}
5
+ clear = false
6
+ options = {}
6
7
  OptionParser.new do |o|
7
- o.on('-c', '--[no-]clear', "Clear screen after every turn") do |f|
8
- options[:clear] = f
8
+ o.on('-c', '--[no-]clear', 'Clear screen after every turn') do |f|
9
+ clear = f
9
10
  end
10
- o.on('-r', '--rounds=N', Integer, "Best of N rounds", "Default is 3") do |f|
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
- game = RoShamBo.new(options)
27
+ system('clear') if clear
17
28
 
18
- system('clear') if options[:clear]
29
+ game = RoShamBo.new(**options)
19
30
 
20
31
  puts ">> FIRST TO #{game.points_to_win} WINS! <<"
21
32
 
22
- until game.game_winner?
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
- if u_choice == :exit
29
- break
30
- elsif !u_choice
31
- puts "Please enter a valid choice!"
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: u_choice, computer: c_choice}.each { |k,v| puts "> #{k} chose #{v}" }
36
- system('clear') if options[:clear]
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(u_choice, c_choice)}"
51
+ puts "Round winner: #{game.round_winner}"
39
52
  end
40
53
 
41
- puts game.game_winner ? "\nGame over! #{game.game_winner} won!" :
42
- "\nGame was quit. Score was #{game.score.inspect}"
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 :score, :points_to_win
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(options = {rounds: 3})
11
- @options = options
17
+ def initialize(cheater: nil, rounds: 3)
18
+ @cheater = cheater
19
+ @rounds = rounds
12
20
  @score = {user: 0, computer: 0}
13
- @points_to_win = (options[:rounds].to_f / 2).ceil
21
+ @points_to_win = (rounds.to_f / 2).ceil
14
22
  end
15
23
 
16
- def user_choice(user_input)
17
- case user_input
18
- when 'r', 'rock' then :rock
19
- when 'p', 'paper' then :paper
20
- when 's', 'scissors' then :scissors
21
- when 'x', 'exit', 'q', 'quit' then :exit
22
- end
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 computer_choice
26
- RULES.keys.sample
35
+ def over?
36
+ !winner.nil?
27
37
  end
28
38
 
29
- def round_winner(u_choice, c_choice)
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 game_winner
41
- score.key(points_to_win).capitalize if score.value?(points_to_win)
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 game_winner?
45
- !game_winner.nil?
75
+ def about_to_win?(user)
76
+ score[user] == points_to_win - 1
46
77
  end
47
78
  end
@@ -16,7 +16,7 @@ class RoShamBo
16
16
 
17
17
  ##
18
18
  # Patch version.
19
- PATCH = 1
19
+ PATCH = 2
20
20
 
21
21
  ##
22
22
  # Version as +MAJOR.MINOR.PATCH+
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.1
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-30 00:00:00.000000000 Z
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