hangman_game 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d747df08c08c1a384697112a7e8166ab80f93152
4
+ data.tar.gz: a511858141a669d328e87cf5e5521372bfe099e0
5
+ SHA512:
6
+ metadata.gz: b47ba0c622c8656fddb46f8940a3aa4ed10f5fd5367ffb26d6b8e6c6a411653b2aeab5d46388b0381668ff21d4b1ebe9e5ee5d29011250e0a18c293c838188ca
7
+ data.tar.gz: c176cdd0deef25e771b2174c6f1fb4d701a6588e19f9bba3a02cfa17c7498710b3a44f01bb892276ba88878e16a433cf0bfbedb9215b8b53e26e87e402df9af6
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 John Nguyen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # Hangman Game
2
+
3
+ A ruby hangman game for the commandline.
4
+
5
+
6
+ ## Installation
7
+ $ gem install hangman_game
8
+
9
+ ## Usage
10
+
11
+ `hangman_game` to start playing.
data/bin/hangman_game ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/hangman/hangman'
4
+
5
+ game = HangmanGame::Hangman.new
6
+
7
+ puts "This word has #{game.revealed_letters.size} letters."
8
+ puts game
9
+
10
+ while game.guessed_letters.uniq.size < game.available_letters.size
11
+
12
+ puts "========================================"
13
+
14
+ unless game.guessed_letters.empty?
15
+ puts game.reveal_guessed_letters_into_word
16
+ puts game.guess_status
17
+ end
18
+
19
+ break if game.finished?
20
+
21
+ puts "What is your guess?"
22
+
23
+ user_input = gets.chomp
24
+
25
+ puts "Your guess, #{user_input}, was invalid." unless game.valid?(user_input)
26
+ puts 'You need to provide a guess.' if user_input.empty?
27
+ puts 'No need for spaces' if user_input == ' '
28
+ puts 'Your guess needs to be a letter.' unless user_input.match(/[[:alpha:]]/)
29
+
30
+ game.normalize_guessed_letters(user_input)
31
+ end
32
+
33
+ puts game
@@ -0,0 +1,62 @@
1
+ require 'random-word'
2
+
3
+ module HangmanGame
4
+ class Hangman
5
+
6
+ attr_writer :guessed_letters
7
+ attr_reader :guessed_letters, :available_letters, :revealed_letters
8
+
9
+ def initialize
10
+ RandomWord.exclude_list << /_/
11
+
12
+ @word = get_word
13
+ @revealed_letters = @word.gsub(/\w/, '_')
14
+ @available_letters = ('a'..'z').to_a
15
+ @guessed_letters = []
16
+ end
17
+
18
+ def get_word
19
+ RandomWord.nouns.next
20
+ end
21
+
22
+ def guess_status
23
+ "Your guesses so far: #{@guessed_letters}"
24
+ end
25
+
26
+ def normalize_guessed_letters(str)
27
+ @guessed_letters << str.split('')
28
+ @guessed_letters.flatten!
29
+ @guessed_letters.reject!{ |i| i.nil? || i == '' || i == ' ' || i =~ /\d/ }
30
+ @guessed_letters.uniq!
31
+ @guessed_letters
32
+ end
33
+
34
+ def valid?(str)
35
+ if str.match(/^[A-z]+$/)
36
+ return true
37
+ end
38
+ false
39
+ end
40
+
41
+ def finished?
42
+ @word == @revealed_letters
43
+ end
44
+
45
+ def to_s
46
+ @revealed_letters
47
+ end
48
+
49
+ def reveal_guessed_letters_into_word
50
+ @guessed_letters.each do |letter|
51
+ if @word.include?(letter)
52
+ @word.split('').each_with_index do |l, i|
53
+ if l == letter
54
+ @revealed_letters[i] = letter
55
+ end
56
+ end
57
+ end
58
+ end
59
+ self
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,26 @@
1
+ require 'hangman/hangman'
2
+
3
+ module HangmanGame
4
+ describe Hangman do
5
+ before do
6
+ @game = HangmanGame::Hangman.new
7
+ end
8
+
9
+ it 'has a random word' do
10
+ expect(@game.get_word).to match(/\w/)
11
+ end
12
+
13
+ it 'does not have duplicate guessed letters' do
14
+ guessed_letters = ('a'..'m').to_a << 'a'
15
+ expect(@game.normalize_guessed_letters(guessed_letters.join(''))).to_not eq(guessed_letters)
16
+ end
17
+
18
+ it 'accepts only letters from user input' do
19
+ expect(@game.valid?('8;s')).to_not be(true)
20
+ end
21
+
22
+ it 'does not reveal the WORD as default' do
23
+ expect(@game.to_s).to eq(@game.revealed_letters.gsub(/\w/, '_'))
24
+ end
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hangman_game
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - John Nguyen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: random-word
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
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
+ description: |-
42
+ # Hangman Game
43
+
44
+ A ruby hangman game for the commandline.
45
+
46
+
47
+ ## Installation
48
+ $ gem install hangman_game
49
+
50
+ ## Usage
51
+
52
+ `hangman_game` to start playing.
53
+ email:
54
+ - hello@jnguyen.me
55
+ executables:
56
+ - hangman_game
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - LICENSE
61
+ - README.md
62
+ - bin/hangman_game
63
+ - lib/hangman/hangman.rb
64
+ - spec/hangman/hangman_spec.rb
65
+ homepage: https://github.com/nguyenj/hangman
66
+ licenses: []
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '1.9'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.6
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: A commandline hangman game.
88
+ test_files:
89
+ - spec/hangman/hangman_spec.rb