hangman-game 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5a907fa8db46ea202a1e0b8bcb3830035579b940
4
+ data.tar.gz: 88e11f52932a137d6b863c41779b82bc00c4b5af
5
+ SHA512:
6
+ metadata.gz: 82fcce2312f4b45bd73474b9a8dbbf494f4259380d551211f9493d2653d1ee496ac1fa66b859372b9e06704848be0d0d3fdfb7d8e8207c858e8c701689c7fbc5
7
+ data.tar.gz: b7a1c6517175ba0f4740badd51a4a37efb63511b8313c6fe8eeae261cd23cc9a850a673f782248d4c37ce04dcd471a0a957c3dd5580e2324033059c22e3ea6ed
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hangman.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Austin Schneider
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.md ADDED
@@ -0,0 +1,29 @@
1
+ # Hangman
2
+
3
+ Hangman engine
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'hangman'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install hangman
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/hangman.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hangman/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hangman-game"
8
+ spec.version = Hangman::VERSION
9
+ spec.authors = ["Austin Schneider"]
10
+ spec.email = ["austinthecoder@gmail.com"]
11
+ spec.description = "Hangman engine"
12
+ spec.summary = "Hangman engine"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", '~> 2.14'
24
+ end
@@ -0,0 +1,43 @@
1
+ require 'hangman'
2
+
3
+ module Hangman
4
+ class Game
5
+ def initialize(word)
6
+ @word = word
7
+ @guesses = Set.new
8
+ end
9
+
10
+ attr_reader :guesses, :word
11
+
12
+ def guess(letter)
13
+ raise GameOverError, 'Cannot guess: game is over' if status != :in_progress
14
+ guesses << letter
15
+ end
16
+
17
+ def incorrect_guesses
18
+ guesses.select { |l| !word_letters.include? l }
19
+ end
20
+
21
+ def status
22
+ if incorrect_guesses.size < 6
23
+ if word_letters.all? { |l| guesses.include? l }
24
+ :won
25
+ else
26
+ :in_progress
27
+ end
28
+ else
29
+ :lost
30
+ end
31
+ end
32
+
33
+ def ==(other)
34
+ other.is_a?(self.class) && guesses == other.guesses && word == other.word
35
+ end
36
+
37
+ private
38
+
39
+ def word_letters
40
+ @word_letters ||= word.split('').tap(&:uniq!)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Hangman
2
+ VERSION = "0.0.1"
3
+ end
data/lib/hangman.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "hangman/version"
2
+ require 'hangman/game'
3
+
4
+ module Hangman
5
+ class Error < StandardError
6
+ end
7
+
8
+ class GameOverError < Error
9
+ end
10
+
11
+ class << self
12
+ attr_accessor :word_list
13
+
14
+ def new_game
15
+ Game.new word_list.shuffle[0]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/hangman/game'
3
+
4
+ module Hangman
5
+ describe Game do
6
+ it 'marshals' do
7
+ game = Game.new 'foo'
8
+ game.guess 'a'
9
+
10
+ unmarshaled_game = Marshal.load(Marshal.dump(game))
11
+
12
+ expect(game).to eq(unmarshaled_game)
13
+ end
14
+
15
+ describe '#word' do
16
+ it 'is the given word' do
17
+ Game.new('foo').word.should == 'foo'
18
+ end
19
+ end
20
+
21
+ describe '#guesses' do
22
+ it { expect(Game.new('foo').guesses).to be_empty }
23
+ end
24
+
25
+ describe '#guess' do
26
+ let(:game) { Game.new 'foo' }
27
+
28
+ context 'when the game is in progress' do
29
+ context 'when the letter was already guessed' do
30
+ it 'does not change the list of guesses' do
31
+ game.guess 'a'
32
+ expect { game.guess 'a' }.to_not change { game.guesses }
33
+ end
34
+ end
35
+
36
+ context 'when the letter was not already guessed' do
37
+ it 'adds the letter to the list of guesses' do
38
+ game = Game.new('foo')
39
+ game.guess 'a'
40
+ expect(game.guesses).to include('a')
41
+ end
42
+ end
43
+ end
44
+
45
+ context 'when the game is not in progress' do
46
+ it 'raises an error' do
47
+ %w[f o].each { |l| game.guess l }
48
+ expect {
49
+ game.guess 'a'
50
+ }.to raise_error(Hangman::GameOverError, "Cannot guess: game is over")
51
+ end
52
+ end
53
+ end
54
+
55
+ describe '#incorrect_guesses' do
56
+ it 'a list of incorrectly guessed letters' do
57
+ game = Game.new('hangman')
58
+ game.guess 'a'
59
+ game.guess 'x'
60
+ expect(game.incorrect_guesses).to_not include('a')
61
+ expect(game.incorrect_guesses).to include('x')
62
+ end
63
+ end
64
+
65
+ describe '#status' do
66
+ let(:game) { Game.new 'hangman' }
67
+
68
+ context 'when there are 6 incorrect guesses' do
69
+ it 'is :lost' do
70
+ %w[x y z q f h a n g l].each { |l| game.guess l }
71
+ expect(game.status).to eq :lost
72
+ end
73
+ end
74
+
75
+ context 'when there is less than 6 incorrect guesses' do
76
+ context 'when all letters were guessed' do
77
+ it 'is :won' do
78
+ %w[x y z q f h a n g m].each { |l| game.guess l }
79
+ expect(game.status).to eq :won
80
+ end
81
+ end
82
+
83
+ context 'when all letters were not guessed' do
84
+ it 'is :in_progress' do
85
+ %w[x y z q f h a n g].each { |l| game.guess l }
86
+ expect(game.status).to eq :in_progress
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ describe '#==' do
93
+ it 'is true given another Hangman game with the same word & guesses' do
94
+ game = Game.new('foo')
95
+ game.guess 'a'
96
+
97
+ other_game = Game.new('foo')
98
+ other_game.guess 'a'
99
+
100
+ expect(game).to eq(other_game)
101
+ end
102
+
103
+ it 'is false given another Hangman game with different guesses' do
104
+ game = Game.new('foo')
105
+ game.guess 'a'
106
+
107
+ other_game = Game.new('foo')
108
+ other_game.guess 'b'
109
+
110
+ expect(game).to_not eq(other_game)
111
+ end
112
+
113
+ it 'is false given another Hangman game with a different word' do
114
+ game = Game.new('foo')
115
+ game.guess 'a'
116
+
117
+ other_game = Game.new('bar')
118
+ other_game.guess 'a'
119
+
120
+ expect(game).to_not eq(other_game)
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require_relative '../lib/hangman'
3
+
4
+ describe Hangman do
5
+ describe Hangman::Error do
6
+ it { expect(Hangman::Error.new).to be_a StandardError }
7
+ end
8
+
9
+ describe Hangman::GameOverError do
10
+ it { expect(Hangman::GameOverError.new).to be_a Hangman::Error }
11
+ end
12
+
13
+ describe '.new_game' do
14
+ it 'returns a new Hangman::Game with a random word from the word list' do
15
+ Hangman.word_list = ['foo']
16
+ expect(Hangman.new_game).to eq(Hangman::Game.new('foo'))
17
+ end
18
+ end
19
+ end
@@ -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"` 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
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hangman-game
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Austin Schneider
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-21 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ description: Hangman engine
56
+ email:
57
+ - austinthecoder@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .ruby-version
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - hangman.gemspec
69
+ - lib/hangman.rb
70
+ - lib/hangman/game.rb
71
+ - lib/hangman/version.rb
72
+ - spec/hangman/game_spec.rb
73
+ - spec/hangman_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.0.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Hangman engine
99
+ test_files:
100
+ - spec/hangman/game_spec.rb
101
+ - spec/hangman_spec.rb
102
+ - spec/spec_helper.rb