saku_rps 0.4.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db2e4aaeefb18f9cb67fabc1d12b2cc6a5123aaa
4
+ data.tar.gz: 5f28d19a40d5d4a7f651adffda8d0a69f80af31f
5
+ SHA512:
6
+ metadata.gz: db95a3526948c46bd7391b70e33fd0774c795f36a8b08d4b9964e034aaacb72b7128f27bd57fae4bf715480e06e4252cbcfbfa9bbe1c73c13cc2ca051cf6903d
7
+ data.tar.gz: ad23285a063ef93708f73f21aa823feb0d5f3118a5e625a32999880f8e6db4266b85b9488e104624dabf7706987886d815912bc540e6d59e791f8b73b6ec8075
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in saku_rps.gemspec
4
+ gemspec
@@ -0,0 +1,38 @@
1
+ # SakuRps
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/saku_rps`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'saku_rps'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install saku_rps
22
+
23
+ ## Usage
24
+
25
+ In order to use the gem either
26
+
27
+ 1. require 'saku_rps' in a script and enter the code line 'SakuRps.start_game'
28
+ 2. load the saku_rps rps into your ruby console (i.e. irb, pry, etc) and enter the line 'SakuRps.start_game'
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/saku_rps.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "saku_rps"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ require "saku_rps/version"
2
+ require_relative "saku_rps/arena"
3
+ require_relative "saku_rps/player"
4
+ require_relative "saku_rps/rps"
5
+
6
+ module SakuRps
7
+ def self.start_game
8
+ SakuRps::RPS.init
9
+ end
10
+ end
@@ -0,0 +1,111 @@
1
+ # This contains the code to progress/play the game.
2
+ module SakuRps
3
+ class Arena
4
+ attr_accessor :rps_game, :player_1, :player_2, :rps_game_wins
5
+
6
+ def initialize(rps_game)
7
+ @rps_game = rps_game
8
+ @rps_game_wins = rps_game.game_state[:wins]
9
+ @player_1 = rps_game.players[:player_1]
10
+ @player_2 = rps_game.players[:player_2]
11
+
12
+ @rps_game_wins = {
13
+ @player_1.name => 0,
14
+ @player_2.name => 0
15
+ }
16
+ end
17
+
18
+ def intro
19
+ print `clear`
20
+ puts 'Welcome to the high ocatane world of RPS!'
21
+ puts "Let's get ready to RUUUUUUUMBLE!!"
22
+ sleep(1)
23
+ 3.times do |i|
24
+ puts "#{i + 1}!"
25
+ sleep(0.5)
26
+ end
27
+ puts 'Go!!!'
28
+ print `clear`
29
+
30
+ play
31
+ end
32
+
33
+ def play
34
+ begin
35
+ run
36
+ rescue SystemExit, Interrupt
37
+ end
38
+
39
+ puts "\nGoodbye!"
40
+ end
41
+
42
+ def run
43
+ check_game
44
+ if rps_game.game_state[:won] == true
45
+ puts 'Thanks for playing!'
46
+ elsif rps_game.game_state[:won] == false
47
+ # make this into a choice method for the player
48
+ rps_game.players.each_value(&:choose_play)
49
+ clash
50
+ end
51
+ end
52
+
53
+ def clash
54
+ puts "\n"
55
+ puts "#{player_1.name} picked #{player_1.choice}!!!"
56
+ puts "#{player_2.name} picked #{player_2.choice}!!!"
57
+
58
+ if player_1.choice == player_2.choice
59
+ puts "The might of #{player_1.choice} is an equal match for #{player_2.choice}\n"
60
+ turn_acc('DRAW')
61
+ else
62
+ if player_2.choice == rps_game.win_tree[player_1.choice]
63
+ puts "#{player_2.name}'s #{player_2.choice} defeats #{player_1.name}'s #{player_1.choice}\n"
64
+ turn_acc(player_2.name)
65
+ elsif player_1.choice == rps_game.win_tree[player_2.choice]
66
+ puts "#{player_1.name}'s #{player_1.choice} defeats #{player_2.name}'s #{player_2.choice}\n"
67
+ turn_acc(player_1.name)
68
+ end
69
+ end
70
+ end
71
+
72
+
73
+ #
74
+
75
+ def turn_acc(last_turn_winner)
76
+ case last_turn_winner
77
+ when player_1.name
78
+ rps_game_wins[player_1.name] += 1
79
+ run
80
+ when player_2.name
81
+ rps_game_wins[player_2.name] += 1
82
+ run
83
+ else
84
+ run
85
+ end
86
+ end
87
+
88
+ def check_game
89
+ end_game(player_1.name) if rps_game_wins[player_1.name] == 2
90
+ end_game(player_2.name) if rps_game_wins[player_2.name] == 2
91
+ display_score(rps_game_wins) if rps_game.game_state[:won] == false
92
+ end
93
+
94
+ def end_game(winner)
95
+ rps_game.game_state[:winner] = winner
96
+ rps_game.game_state[:won] = true
97
+ puts "*****\n"
98
+ puts "And the winner is....#{rps_game.game_state[:winner]}!!!!"
99
+ puts 'With a final score of: '
100
+ display_score(rps_game_wins)
101
+ end
102
+
103
+ def display_score(wins)
104
+ wins.each do |player, score|
105
+ puts "#{player}: #{score}"
106
+ end
107
+
108
+ puts "\n"
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,53 @@
1
+ # This contains the code for the players and player actions.
2
+ module SakuRps
3
+ class Player
4
+ attr_accessor :name
5
+ attr_accessor :choice
6
+
7
+ def initialize(player_name = nil)
8
+ @name = if player_name
9
+ player_name
10
+ else
11
+ 'CPU'
12
+ end
13
+
14
+ @choice = ''
15
+ end
16
+ end
17
+
18
+ class HumanPlayer < Player
19
+ PLAY_OPTIONS = %w(1 2 3 R P S ROCK PAPER SCISSORS).freeze
20
+
21
+ def choose_play
22
+ begin
23
+ puts 'Please select your play!'
24
+ puts '1. Rock'
25
+ puts '2. Paper'
26
+ puts '3. Scissors'
27
+ print '> '
28
+ choice = gets.chomp
29
+ end until PLAY_OPTIONS.include?(choice)
30
+
31
+ case choice
32
+ when '1', 'R', 'ROCK'
33
+ choice = 'ROCK'
34
+ when '2', 'P', 'PAPER'
35
+ choice = 'PAPER'
36
+ when '3', 'S', 'SCISSORS'
37
+ choice = 'SCISSORS'
38
+ end
39
+
40
+ self.choice = choice
41
+ nil
42
+ end
43
+ end
44
+
45
+ class CPUPlayer < Player
46
+ CPU_PLAY_OPTIONS = %w(ROCK PAPER SCISSORS).freeze
47
+
48
+ def choose_play
49
+ self.choice = CPU_PLAY_OPTIONS.sample
50
+ nil
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,82 @@
1
+ # if you wish to play the game in IRB/PRY load this file
2
+ # This contains the code for the game state
3
+
4
+ require_relative 'arena'
5
+ require_relative 'player'
6
+
7
+ module SakuRps
8
+ class RPS
9
+ attr_reader :players, :wins, :game_state, :win_tree
10
+
11
+ def initialize(options = {})
12
+ @players = {
13
+ }
14
+ 2.times do |num|
15
+ @players["player_#{num + 1}".to_sym] = options[:players][num]
16
+ end
17
+ @game_state = {
18
+ won: false,
19
+ wins: {},
20
+ winner: ''
21
+ }
22
+
23
+ @win_tree = {
24
+ 'SCISSORS' => 'ROCK',
25
+ 'ROCK' => 'PAPER',
26
+ 'PAPER' => 'SCISSORS'
27
+ }
28
+ end
29
+
30
+ def self.init
31
+ print `clear`
32
+ puts "Welcome to the Rock! Paper! SCISSOOOOOOOOOOOOOORS!\n"
33
+ num_of_players
34
+ end
35
+
36
+ def self.start(rps_game)
37
+ Arena.new(rps_game).intro
38
+ end
39
+
40
+ def self.num_of_players
41
+ begin
42
+ print `clear`
43
+ puts 'Will this be a [One] or [Two] player game?'
44
+ puts '1. One Player'
45
+ puts '2. Two Players'
46
+ print '> '
47
+ one_or_two_of_players = gets.chomp.to_s
48
+ entry_check = %w(1 2).include?(one_or_two_of_players)
49
+ puts "Please enter '1' for a single player game or '2' for a two player game" unless entry_check
50
+ end until %w(1 2).include?(one_or_two_of_players)
51
+
52
+ if one_or_two_of_players == '1'
53
+ build_players(false)
54
+ elsif one_or_two_of_players == '2'
55
+ build_players(true)
56
+ end
57
+ end
58
+
59
+ def self.build_players(is_two_players)
60
+ if is_two_players
61
+ puts 'Please enter player 1\'s name!'
62
+ print '> '
63
+ p1_name = gets.chomp
64
+ puts 'Please enter player 2\'s name!'
65
+ print '> '
66
+ p2_name = gets.chomp
67
+ two_player = {
68
+ players: [HumanPlayer.new(p1_name), HumanPlayer.new(p2_name)],
69
+ }
70
+ self.start(RPS.new(two_player))
71
+ else
72
+ puts 'Please enter your name!'
73
+ print '> '
74
+ p1_name = gets.chomp
75
+ one_player = {
76
+ players: [HumanPlayer.new(p1_name), CPUPlayer.new],
77
+ }
78
+ self.start(RPS.new(one_player))
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ module SakuRps
2
+ VERSION = "0.4.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'saku_rps/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "saku_rps"
8
+ spec.version = SakuRps::VERSION
9
+ spec.authors = ["Stephen Akuffo"]
10
+ spec.email = ["slyven@gmail.com"]
11
+
12
+ spec.summary = "A CLI Rock Paper Scissors game"
13
+ spec.homepage = "https://github.com/saakuffo/saku_rps"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against " \
21
+ "public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.13"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: saku_rps
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Akuffo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - slyven@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/saku_rps.rb
71
+ - lib/saku_rps/arena.rb
72
+ - lib/saku_rps/player.rb
73
+ - lib/saku_rps/rps.rb
74
+ - lib/saku_rps/version.rb
75
+ - saku_rps.gemspec
76
+ homepage: https://github.com/saakuffo/saku_rps
77
+ licenses: []
78
+ metadata:
79
+ allowed_push_host: https://rubygems.org
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.5.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A CLI Rock Paper Scissors game
100
+ test_files: []