tarzan 0.0.2 → 0.0.3

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
  SHA1:
3
- metadata.gz: 8d1f8f241b3d98808dd495d75bc8ccfcb8967e39
4
- data.tar.gz: 6f297af38489451bb3876e22b888bcfb84edf0eb
3
+ metadata.gz: f31aa1899f9fbf7f76718daf62b6cd37ceda35b1
4
+ data.tar.gz: dce2d1f47a6cdaa2205c8816a261afb84d8374ad
5
5
  SHA512:
6
- metadata.gz: c63d52815b50dbf9ad537a39fa5ed99f50bfbf3c85037c2d115977b7778943b342c7eb387b8dcab616fcad1a31baf0b766054387de52ee2c9a71273f643b13f3
7
- data.tar.gz: 28bcfd412957d457c90a81275d1a5ee124303a2d0fffd7b5a5a67e6ac57298798c54928b7cad81e0cb2a125bdf431ce7496abbe765ac5d85c6c315cf56b74b2a
6
+ metadata.gz: f5b39f5e860d52b9b4390ae3e322720f307a56c40eace5a0e42178c37e928588563915d8e5b3355e3ff55b2b23fcefc67644dd524b67692c9a480c0365733564
7
+ data.tar.gz: c26ad5145f86c11cf6de78d836afbd570fb915c88d31ed01c9c51aeb2e75a9777983a6e0a74d59532327be69bfbb4e274e043bed4a522067fd4e105215566fc9
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'debugger'
4
+
3
5
  # Specify your gem's dependencies in tarzan.gemspec
4
6
  gemspec
data/HISTORY.md ADDED
@@ -0,0 +1,9 @@
1
+ v0.0.3 - 2014/02/05
2
+
3
+ * Add Odd Or Evens game
4
+ * Separate interface, game and moves into modules
5
+ * Integrate Campfire with Nico gem
6
+
7
+ v0.0.2 - 2014/02/05
8
+
9
+ * Shell-only Rock Paper Scissors game
data/README.md CHANGED
@@ -22,6 +22,8 @@ From the command line run:
22
22
 
23
23
  $ tarzan
24
24
 
25
+ To use Campfire as an interface, define the following environment variables: `CAMPFIRE_SUBDOMAIN`, `CAMPFIRE_ROOM_ID` and `CAMPFIRE_TOKEN`.
26
+
25
27
  ## Contributing
26
28
 
27
29
  1. Fork it ( http://github.com/claudiob/tarzan/fork )
data/bin/tarzan CHANGED
@@ -7,4 +7,10 @@ rescue LoadError
7
7
  require 'tarzan'
8
8
  end
9
9
 
10
- Tarzan.play!
10
+ puts %{Pick [S]hell or [C]ampfire: }
11
+ interface = case gets.strip
12
+ when 'S' then Tarzan::Interfaces::Shell::Interface.new
13
+ when 'C' then Tarzan::Interfaces::Campfire::Interface.new subdomain: ENV['CAMPFIRE_SUBDOMAIN'], room_id: ENV['CAMPFIRE_ROOM_ID'], token: ENV['CAMPFIRE_TOKEN']
14
+ end
15
+
16
+ interface.play
data/lib/tarzan.rb CHANGED
@@ -1,22 +1,2 @@
1
- module Tarzan
2
- def self.play!
3
- puts %{Welcome to the Game Hall}
4
-
5
- puts %{Let’s play Rock Paper Scissors}
6
-
7
- puts %{Pick [R]ock, [P]aper, or [S]cissors: }
8
-
9
- move_p1 = gets.strip
10
- move_p2 = ['R', 'P', 'S'].sample
11
-
12
- puts %{You played #{move_p1} - I played #{move_p2}}
13
-
14
- case "#{move_p1}#{move_p2}"
15
- when 'RS', 'SP', 'PR' then puts %{You win!}
16
- when 'RP', 'SR', 'PS' then puts %{You lose!}
17
- when 'RR', 'SS', 'PP' then puts %{It’s a tie!}
18
- end
19
-
20
- puts %{Goodbye, and come back to the Game Hall}
21
- end
22
- end
1
+ require 'tarzan/interfaces/shell/interface'
2
+ require 'tarzan/interfaces/campfire/interface'
@@ -0,0 +1,42 @@
1
+ module Tarzan
2
+ module Games
3
+ module Base
4
+ class Game
5
+ def initialize(options = {})
6
+ @interface = options[:interface]
7
+ end
8
+
9
+ def play
10
+ @interface.say %{Rules: #{rules}}
11
+
12
+ move_p1 = prompt_move
13
+ move_p2 = random_move
14
+
15
+ @interface.say %{You played #{move_p1} - I played #{move_p2}}
16
+
17
+ outcome = case move_p1 <=> move_p2
18
+ when 1 then %{You win!}
19
+ when -1 then %{You lose!}
20
+ when 0 then %{It’s a tie!}
21
+ end
22
+
23
+ @interface.say outcome
24
+ end
25
+
26
+ private
27
+
28
+ def rules
29
+ # Subclasses are expected to define rules
30
+ end
31
+
32
+ def prompt_move
33
+ # Subclasses are expected to define how to prompt the user for a move
34
+ end
35
+
36
+ def random_move
37
+ # Subclasses are expected to define how to get a random move
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,27 @@
1
+ require 'tarzan/games/base/game'
2
+ require 'tarzan/games/odds_and_evens/move'
3
+
4
+ module Tarzan
5
+ module Games
6
+ module OddsAndEvens
7
+ class Game < Base::Game
8
+
9
+ private
10
+
11
+ def rules
12
+ 'Odd sum wins, even sum loses'
13
+ end
14
+
15
+ def prompt_move
16
+ choice = @interface.prompt(Move.valid).to_i
17
+ Move.new choice: choice, wins_on_odds: true
18
+ end
19
+
20
+ def random_move
21
+ choice = Move.valid.sample
22
+ Move.new choice: choice, wins_on_odds: false
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ module Tarzan
2
+ module Games
3
+ module OddsAndEvens
4
+ class Move
5
+ attr_reader :choice
6
+
7
+ def self.valid
8
+ ('1'..'5').to_a
9
+ end
10
+
11
+ def initialize(options = {})
12
+ @wins_on_odds = options[:wins_on_odds]
13
+ @choice = options[:choice]
14
+ end
15
+
16
+ def <=>(another)
17
+ case choice.to_i + another.choice.to_i + finger_offset
18
+ when ->(sum) { sum.odd? } then 1
19
+ when ->(sum) { sum.even? } then -1
20
+ else 0 # impossible :)
21
+ end
22
+ end
23
+
24
+ def to_s
25
+ "#{@choice}"
26
+ end
27
+
28
+ private
29
+
30
+ def finger_offset
31
+ @wins_on_odds ? 0 : 1
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ require 'tarzan/games/base/game'
2
+ require 'tarzan/games/rock_paper_scissors/move'
3
+
4
+ module Tarzan
5
+ module Games
6
+ module RockPaperScissors
7
+ class Game < Base::Game
8
+
9
+ private
10
+
11
+ def rules
12
+ 'Rock beats Scissors beats Paper beats Rock'
13
+ end
14
+
15
+ def prompt_move
16
+ choice = @interface.prompt Move.valid
17
+ Move.new choice: choice
18
+ end
19
+
20
+ def random_move
21
+ choice = Move.valid.sample
22
+ Move.new choice: choice
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ module Tarzan
2
+ module Games
3
+ module RockPaperScissors
4
+ class Move
5
+ attr_reader :choice
6
+
7
+ def self.valid
8
+ ['R', 'P', 'S']
9
+ end
10
+
11
+ def initialize(options = {})
12
+ @choice = options[:choice]
13
+ end
14
+
15
+ def <=>(another)
16
+ case "#{choice}#{another.choice}"
17
+ when 'RS', 'SP', 'PR' then 1
18
+ when 'RP', 'SR', 'PS' then -1
19
+ else 0
20
+ end
21
+ end
22
+
23
+ def to_s
24
+ "#{@choice}"
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+ require 'tarzan/games/rock_paper_scissors/game'
2
+ require 'tarzan/games/odds_and_evens/game'
3
+
4
+ module Tarzan
5
+ module Interfaces
6
+ module Base
7
+ class Interface
8
+ def play
9
+ say %{Welcome to the Game Hall}
10
+
11
+ say %{Pick [R]ockPaperScissors or [O]ddsAndEvens: }
12
+
13
+ game = case prompt(['R', 'O'])
14
+ when 'R' then Tarzan::Games::RockPaperScissors::Game.new interface: self
15
+ when 'O' then Tarzan::Games::OddsAndEvens::Game.new interface: self
16
+ end
17
+
18
+ game.play
19
+
20
+ say %{Goodbye, and come back to the Game Hall}
21
+ end
22
+
23
+ def say(message)
24
+ # Subclasses are expected to define how to say messages
25
+ end
26
+
27
+ def prompt(valid_choices = [])
28
+ # Subclasses are expected to define how to prompt messages
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ require 'tarzan/interfaces/base/interface'
2
+ require 'nico'
3
+
4
+ module Tarzan
5
+ module Interfaces
6
+ module Campfire
7
+ class Interface < Base::Interface
8
+
9
+ def initialize(options = {})
10
+ @room = Nico::Room.new options
11
+ end
12
+
13
+ def say(message)
14
+ @room.say message
15
+ end
16
+
17
+ def prompt(valid_choices = [])
18
+ say %{Pick one: #{valid_choices.join ' - '} }
19
+ @room.each_message do |message|
20
+ return message if valid_choices.include?(message)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ require 'tarzan/interfaces/base/interface'
2
+
3
+ module Tarzan
4
+ module Interfaces
5
+ module Shell
6
+ class Interface < Base::Interface
7
+
8
+ def say(message)
9
+ puts message
10
+ end
11
+
12
+ def prompt(valid_choices = [])
13
+ say %{Pick one: #{valid_choices.join ' - '} }
14
+ choice = gets.strip
15
+ valid_choices.include?(choice) ? choice : prompt(valid_choices)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Tarzan
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,25 @@
1
+ require 'tarzan/games/odds_and_evens/move'
2
+
3
+ module Tarzan
4
+ module Games
5
+ module OddsAndEvens
6
+ describe Move do
7
+ describe '<=>' do
8
+ context 'given that victory is on odds and a hand of 3 fingers' do
9
+ let (:move_p1) { Move.new wins_on_odds: true, fingers: 3 }
10
+
11
+ context 'versus a hand of 4 fingers' do
12
+ let (:move_p2) { Move.new wins_on_odds: false, fingers: 4 }
13
+ it { expect(move_p1 <=> move_p2).to eq 1}
14
+ end
15
+
16
+ context 'versus a hand of 5 fingers' do
17
+ let (:move_p2) { Move.new wins_on_odds: false, fingers: 5 }
18
+ it { expect(move_p1 <=> move_p2).to eq -1}
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ require 'tarzan/games/rock_paper_scissors/move'
2
+
3
+ module Tarzan
4
+ module Games
5
+ module RockPaperScissors
6
+ describe Move do
7
+ describe '<=>' do
8
+ context 'given a hand of Rock' do
9
+ let (:move_p1) { Move.new hand: 'R' }
10
+
11
+ context 'versus a hand of Scissors' do
12
+ let (:move_p2) { Move.new hand: 'S'}
13
+ it { expect(move_p1 <=> move_p2).to eq 1}
14
+ end
15
+
16
+ context 'versus a hand of Paper' do
17
+ let (:move_p2) { Move.new hand: 'P'}
18
+ it { expect(move_p1 <=> move_p2).to eq -1}
19
+ end
20
+
21
+ context 'versus a hand of Rock' do
22
+ let (:move_p2) { Move.new hand: 'R'}
23
+ it { expect(move_p1 <=> move_p2).to eq 0}
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
data/tarzan.gemspec CHANGED
@@ -4,20 +4,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'tarzan/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "tarzan"
7
+ spec.name = 'tarzan'
8
8
  spec.version = Tarzan::VERSION
9
- spec.authors = ["claudiob"]
10
- spec.email = ["claudiob@gmail.com"]
9
+ spec.authors = ['claudiob']
10
+ spec.email = ['claudiob@gmail.com']
11
11
  spec.summary = %q{King of the Game Jungle.}
12
12
  spec.description = %q{Step-by-step tutorial to modular games in Ruby}
13
- spec.homepage = "https://github.com/claudiob/tarzan"
14
- spec.license = "MIT"
13
+ spec.homepage = 'https://github.com/claudiob/tarzan'
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.5"
22
- spec.add_development_dependency "rake", "~> 0"
21
+ spec.add_dependency 'nico', '>= 0.1.0' # Campfire integration
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.5'
24
+ spec.add_development_dependency 'rake', '~> 0'
25
+ spec.add_development_dependency 'rspec'
23
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tarzan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - claudiob
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nico
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,20 @@ dependencies:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  description: Step-by-step tutorial to modular games in Ruby
42
70
  email:
43
71
  - claudiob@gmail.com
@@ -47,13 +75,25 @@ extensions: []
47
75
  extra_rdoc_files: []
48
76
  files:
49
77
  - ".gitignore"
78
+ - ".rspec"
50
79
  - Gemfile
80
+ - HISTORY.md
51
81
  - LICENSE.txt
52
82
  - README.md
53
83
  - Rakefile
54
84
  - bin/tarzan
55
85
  - lib/tarzan.rb
86
+ - lib/tarzan/games/base/game.rb
87
+ - lib/tarzan/games/odds_and_evens/game.rb
88
+ - lib/tarzan/games/odds_and_evens/move.rb
89
+ - lib/tarzan/games/rock_paper_scissors/game.rb
90
+ - lib/tarzan/games/rock_paper_scissors/move.rb
91
+ - lib/tarzan/interfaces/base/interface.rb
92
+ - lib/tarzan/interfaces/campfire/interface.rb
93
+ - lib/tarzan/interfaces/shell/interface.rb
56
94
  - lib/tarzan/version.rb
95
+ - spec/tarzan/games/odds_and_evens/move_spec.rb
96
+ - spec/tarzan/games/rock_paper_scissors/move_spec.rb
57
97
  - tarzan.gemspec
58
98
  homepage: https://github.com/claudiob/tarzan
59
99
  licenses:
@@ -79,4 +119,6 @@ rubygems_version: 2.2.0
79
119
  signing_key:
80
120
  specification_version: 4
81
121
  summary: King of the Game Jungle.
82
- test_files: []
122
+ test_files:
123
+ - spec/tarzan/games/odds_and_evens/move_spec.rb
124
+ - spec/tarzan/games/rock_paper_scissors/move_spec.rb