hangman_engine 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.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/hangman_engine.rb +109 -0
  3. metadata +50 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YWY5OTE2Zjk3ODRiMDA5NGM0NTU1ODFiNzM5YTM5NGEzZGJmNjNiNg==
5
+ data.tar.gz: !binary |-
6
+ ODZjYWIzOGJiMGVlODMyMWZkZTdiMGIwOThhYjdkZTQzZDUzZmM1OA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YmYwMGIxNTkzYzYyY2UwZDU0OGQwOTU2MzQ1YzY5YTcxZDIzMjE0ZTkyNjU3
10
+ NzBiMzZkZjkxMWRmOTkzYTE4NWExY2MyMTY3NDk2NzFlYjhkMGRmZTljY2Fm
11
+ MWI1YmUzNWI2NmE2MjEwN2VkOGYxMWE3YjgzM2YxODQ1YjhkZWQ=
12
+ data.tar.gz: !binary |-
13
+ YzAyZGNjZDgwMWRjZmIzMGFmZjg5OWQwMDkzNzM2OWI2MThiNjk3OWI2NTM4
14
+ MDA5NTdlMjVjZWMyYjc1MTY0YmFmZjU3YzZmMzVjMTZlYzE0ZDhlYmRmMTRj
15
+ ZTE5MDkyZWMzZTc3Y2MzNTExMzg5NjNjNWVmNGU3OTZiMzQ5MmM=
@@ -0,0 +1,109 @@
1
+ module HangmanEngine
2
+
3
+ class GameError < StandardError
4
+ end
5
+
6
+ class Game
7
+
8
+ attr_reader :word, :clue, :guesses, :attempts, :allowed_attempts
9
+
10
+ # Builder
11
+ def initialize(word, allowed_attempts, clue)
12
+ self.word = word.downcase
13
+ self.allowed_attempts = allowed_attempts.to_i
14
+ @clue = clue
15
+ @attempts = 0
16
+ @guesses = []
17
+ end
18
+
19
+ # Methods
20
+ def word=(word)
21
+ raise GameError, "word can't be blank" unless word.length > 0
22
+ raise GameError, "word to guess can't have whitespaces. Use only one word." if word.match(/\s/)
23
+ @word = word
24
+ end
25
+
26
+ def allowed_attempts=(n)
27
+ raise GameError, "allowed attempts must be greater or equal to the word length (>= #{word.length})." unless n >= word.length
28
+ @allowed_attempts = n
29
+ end
30
+
31
+ def guess(ltr)
32
+ if guessed? ltr
33
+ @guesses.push(ltr) unless @guesses.include?(ltr)
34
+ else
35
+ @attempts += 1
36
+ end
37
+ end
38
+
39
+ def guessed?(ltr)
40
+ @word.include?(ltr)
41
+ end
42
+
43
+ def remaining_attempts
44
+ @allowed_attempts - @attempts
45
+ end
46
+
47
+ def solved?
48
+ @word.split('').uniq.length == @guesses.length
49
+ end
50
+
51
+ def lost?
52
+ remaining_attempts == 0
53
+ end
54
+
55
+ def finished?
56
+ solved? || lost?
57
+ end
58
+ end
59
+
60
+ # Helper class to draw a Hangman::Game on the console
61
+ class Drawer
62
+
63
+ # Dinamically generate the game 'puppet' based on the length of the word to guess and the failed guesses count
64
+ #
65
+ # O
66
+ # /|\
67
+ # / \
68
+ #
69
+ def self.draw_puppet(hangman_game)
70
+ allowat = hangman_game.allowed_attempts
71
+ rattempts = hangman_game.remaining_attempts
72
+ puppet = burn_or_body(" O ", allowat, allowat, rattempts) + "\n"
73
+ 1.upto(allowat) do |i|
74
+ puppet << case allowat - i
75
+ when 5
76
+ burn_or_body('/', 5, allowat, rattempts)
77
+ when 4
78
+ burn_or_body('|', 4, allowat, rattempts)
79
+ when 3
80
+ burn_or_body("\\", 3, allowat, rattempts) + "\n"
81
+ when 2
82
+ burn_or_body('/', 2, allowat, rattempts) + ' '
83
+ when 1
84
+ burn_or_body("\\", 1, allowat, rattempts) + "\n"
85
+ when 0
86
+ ''
87
+ else
88
+ burn_or_body(" | ", allowat - i, allowat, rattempts) + "\n"
89
+ end
90
+ end
91
+ puppet
92
+ end
93
+
94
+ # Draw game board based on the word to guess and the gessues
95
+ #
96
+ # a _ f o n _ _
97
+ #
98
+ def self.draw_board(hangman_game)
99
+ board = ''
100
+ hangman_game.word.each_char { |ltr| board << (hangman_game.guesses.include?(ltr) ? "#{ltr} " : '_ ') }
101
+ board
102
+ end
103
+
104
+ private
105
+ def self.burn_or_body(part, part_number, allowed_attempts, remaining_attempts)
106
+ (allowed_attempts - remaining_attempts) >= part_number ? ' ' : part
107
+ end
108
+ end
109
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hangman_engine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alfonso Mancilla
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ! 'Use this library to build your own HANGMAN game.
14
+
15
+ HangmanEngine::Game have all you need to control the game flow.
16
+
17
+ HangmanEngine::Drawer will help you to build a console based interface for your
18
+ HangmanEngine::Game'
19
+ email:
20
+ - almancill@gmail.com
21
+ executables: []
22
+ extensions: []
23
+ extra_rdoc_files: []
24
+ files:
25
+ - lib/hangman_engine.rb
26
+ homepage: https://github.com/ammancilla/hangman_engine
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.4.5
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Build your own HANGMAN game!
50
+ test_files: []