angryrock 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README +13 -0
- data/README.md +34 -0
- data/Rakefile +2 -0
- data/angryrock.gemspec +19 -0
- data/lib/angryrock.rb +6 -0
- data/lib/angryrock/play.rb +44 -0
- data/lib/angryrock/version.rb +3 -0
- data/spec/angryrock/play_spec.rb +43 -0
- data/spec/spec_helper.rb +17 -0
- metadata +76 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Bala Paranj
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# Angryrock
|
2
|
+
|
3
|
+
Rock Paper Scissors Game Engine. Has two solutions:
|
4
|
+
|
5
|
+
1. Well Grounded Rubyist based solution refactored to a better design.
|
6
|
+
2. Sinatra Up and Running based concise solution.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'angryrock'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install angryrock
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
See the specs under spec directory on how to use the gem. If you have checked out the source code of this gem, you can run the specs for this gem by:
|
25
|
+
|
26
|
+
rspec spec/angryrock/play_spec.rb --color
|
27
|
+
|
28
|
+
## Contributing
|
29
|
+
|
30
|
+
1. Fork it
|
31
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
32
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
33
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
34
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/angryrock.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/angryrock/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Bala Paranj"]
|
6
|
+
gem.email = ["bparanj@gmail.com"]
|
7
|
+
gem.description = %q{Rock Paper Scissors Game}
|
8
|
+
gem.summary = %q{Rock Paper Scissors Game Engine}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "angryrock"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Angryrock::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec"
|
19
|
+
end
|
data/lib/angryrock.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module AngryRock
|
2
|
+
class Play
|
3
|
+
def initialize(first_choice, second_choice)
|
4
|
+
@choice_1 = Internal::AngryRock.new(first_choice)
|
5
|
+
@choice_2 = Internal::AngryRock.new(second_choice)
|
6
|
+
|
7
|
+
@winner = @choice_1.winner(@choice_2)
|
8
|
+
end
|
9
|
+
|
10
|
+
def has_winner?
|
11
|
+
@choice_1.has_winner?(@choice_2)
|
12
|
+
end
|
13
|
+
|
14
|
+
def winning_move
|
15
|
+
@winner.move
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module Internal # no-rdoc
|
20
|
+
# This is implementation details. Not for client use. Don't touch me.
|
21
|
+
class AngryRock
|
22
|
+
WINS = {rock: :scissors, scissors: :paper, paper: :rock}
|
23
|
+
|
24
|
+
attr_accessor :move
|
25
|
+
|
26
|
+
def initialize(move)
|
27
|
+
@move = move
|
28
|
+
end
|
29
|
+
|
30
|
+
def has_winner?(opponent)
|
31
|
+
self.move != opponent.move
|
32
|
+
end
|
33
|
+
# fetch will raise exception when the key is not one of the allowed choice
|
34
|
+
def winner(opponent)
|
35
|
+
if WINS.fetch(self.move)
|
36
|
+
self
|
37
|
+
else
|
38
|
+
opponent
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'angryrock/play'
|
3
|
+
|
4
|
+
module AngryRock
|
5
|
+
describe Play do
|
6
|
+
|
7
|
+
it "should pick paper as the winner over rock" do
|
8
|
+
play = Play.new(:paper, :rock)
|
9
|
+
|
10
|
+
play.should have_winner
|
11
|
+
play.winning_move.should == :paper
|
12
|
+
end
|
13
|
+
|
14
|
+
it "picks scissors as the winner over paper" do
|
15
|
+
play = Play.new(:scissors, :paper)
|
16
|
+
|
17
|
+
play.should have_winner
|
18
|
+
play.winning_move.should == :scissors
|
19
|
+
end
|
20
|
+
|
21
|
+
it "picks rock as the winner over scissors " do
|
22
|
+
play = Play.new(:rock, :scissors)
|
23
|
+
|
24
|
+
play.should have_winner
|
25
|
+
play.winning_move.should == :rock
|
26
|
+
end
|
27
|
+
|
28
|
+
it "results in a tie when the same choice is made by both players : rock, paper or scissors" do
|
29
|
+
data_driven_spec([:rock, :paper, :scissors]) do |choice|
|
30
|
+
play = Play.new(choice, choice)
|
31
|
+
|
32
|
+
play.should_not have_winner
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should raise exception when illegal input is provided" do
|
37
|
+
expect do
|
38
|
+
play = Play.new(:junk, :hunk)
|
39
|
+
end.to raise_error
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
end
|
12
|
+
|
13
|
+
def data_driven_spec(container)
|
14
|
+
container.each do |element|
|
15
|
+
yield element
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: angryrock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bala Paranj
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-05-19 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
description: Rock Paper Scissors Game
|
27
|
+
email:
|
28
|
+
- bparanj@gmail.com
|
29
|
+
executables: []
|
30
|
+
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files: []
|
34
|
+
|
35
|
+
files:
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE
|
38
|
+
- README
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- angryrock.gemspec
|
42
|
+
- lib/angryrock.rb
|
43
|
+
- lib/angryrock/play.rb
|
44
|
+
- lib/angryrock/version.rb
|
45
|
+
- spec/angryrock/play_spec.rb
|
46
|
+
- spec/spec_helper.rb
|
47
|
+
homepage: ""
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.8.15
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Rock Paper Scissors Game Engine
|
74
|
+
test_files:
|
75
|
+
- spec/angryrock/play_spec.rb
|
76
|
+
- spec/spec_helper.rb
|