play_hangman 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/bin/play_hangman +3 -0
- data/lib/play_hangman.rb +112 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MjQ5NTg2YWQxYTJhZTlhZGMwODRlMDdkZDA0YjRlY2Y5ODQ1MTlhNQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NWNmODdkNjA5ZmEzZmJhMGM4MTcyODhlNTY0NGM0NzM3ZDA4OGU3Zg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NTJkN2ZjMmRiMDUyNjlkZDJkNDBkZTM0NDYzY2U4NDljNjhhOTkwYTMzZmQ5
|
10
|
+
MmM5MzUyNmMwOWVmMjc2NmJjYmU2MGJjNzJhM2Y3YTVkYTIzZDVmOTZlMGZj
|
11
|
+
YTdkMjEyZTgxZmQ4NWYwZjY0YzA1NDVlMGFmMDYwZjUyZjAzMDE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NWRjZWQzNmQ0MzY4ZWUyZjQzYTc3M2M2ODcxNjlmZWUyYjU5MjljYWJhOTk2
|
14
|
+
NGU5Nzc2N2M5NWVhZTZjNTE4ZmRjZjRmMzYwM2FmZTIzZDJhNmY4M2E3MDIz
|
15
|
+
YjAwMjZjZTUyYTg0MTM5NmJjYjNjNWU4NjVjZGM2NmQwYmI5ZmU=
|
data/bin/play_hangman
ADDED
data/lib/play_hangman.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# Encoding: utf-8
|
2
|
+
# ----------------------------- PLAY HANGMAN -------------------------------
|
3
|
+
#
|
4
|
+
# Ready to play console version of HANGMAN built using 'hangman_engine' gem
|
5
|
+
#
|
6
|
+
# --------------------------------------------------------------------------
|
7
|
+
require 'rbconfig'
|
8
|
+
require 'win32console' if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
|
9
|
+
require 'hangman_engine'
|
10
|
+
require 'console_view_helper'
|
11
|
+
include ConsoleViewHelper
|
12
|
+
|
13
|
+
# --- Helper Methods
|
14
|
+
def display_notice(notice)
|
15
|
+
putsi(colorize(notice[:msg], notice[:status]) + nl(2)) if notice[:msg]
|
16
|
+
end
|
17
|
+
|
18
|
+
def display_game_banner(base_width, base_idt)
|
19
|
+
puts nl(2) + banner('HANGMAN GAME', subtitle: 'by Alfonso Mancilla', width: base_width, indent: base_idt) + nl(2)
|
20
|
+
end
|
21
|
+
|
22
|
+
def display_game_board(hangman_game, base_width, base_idt, notice = {})
|
23
|
+
cls
|
24
|
+
display_game_banner(base_width, base_idt)
|
25
|
+
display_notice(notice)
|
26
|
+
putsi "Clue: '#{hangman_game.clue}'" + nl(3) if hangman_game.clue.length > 0
|
27
|
+
putsi "Remaining attemps: #{hangman_game.remaining_attempts}" + nl(3)
|
28
|
+
puts HangmanEngine::Drawer.draw_puppet(hangman_game).split("\n").map { |part| align(part, base_width + 15, :center) }.join("\n") + nl(3)
|
29
|
+
game_board = HangmanEngine::Drawer.draw_board(hangman_game)
|
30
|
+
putsi align(game_board, base_width, :center) + nl(3)
|
31
|
+
end
|
32
|
+
|
33
|
+
def display_won(base_width)
|
34
|
+
putsi align('* * * * * * * *', base_width, :center)
|
35
|
+
putsi align('* * * * * * *', base_width, :center)
|
36
|
+
putsi align('* ** * * * * * *', base_width, :center)
|
37
|
+
putsi align(' * * * * * * * * *' , base_width, :center)
|
38
|
+
putsi align(' ** ** * * * * * **', base_width, :center)
|
39
|
+
end
|
40
|
+
|
41
|
+
def display_lost(base_width)
|
42
|
+
putsi align('* * * * * * * * * * * * * *', base_width, :center)
|
43
|
+
putsi align('* * * * * * * * ', base_width, :center)
|
44
|
+
putsi align('* * * * * * * * * * * * * * * ', base_width, :center)
|
45
|
+
putsi align('* * * * * * * * ', base_width, :center)
|
46
|
+
putsi align('* * * * * * * * * * * *', base_width, :center)
|
47
|
+
puts nl
|
48
|
+
putsi align('* * * * * * * * * * * * * *' , base_width, :center)
|
49
|
+
putsi align('* * * * * * *' , base_width, :center)
|
50
|
+
putsi align('* * * * * * * * * * *' , base_width, :center)
|
51
|
+
putsi align('* * * * * * * ' , base_width, :center)
|
52
|
+
putsi align('* * * * ** * * * * * *', base_width, :center)
|
53
|
+
end
|
54
|
+
|
55
|
+
# --- Main
|
56
|
+
base_idt, base_width, keep_playing = 1, 50, 'y'
|
57
|
+
|
58
|
+
while keep_playing == 'y'
|
59
|
+
notice = {}
|
60
|
+
begin
|
61
|
+
# Read data
|
62
|
+
data_error = false
|
63
|
+
begin
|
64
|
+
cls
|
65
|
+
display_game_banner(base_width, base_idt)
|
66
|
+
display_notice(notice)
|
67
|
+
word = hidden_input('Word to guess (will be hidden):', base_idt)
|
68
|
+
puts nl(2)
|
69
|
+
allowed_attempts = input("Allowed attempts (>= #{word.length}):", base_idt)
|
70
|
+
puts nl
|
71
|
+
clue = input("Clue:", base_idt)
|
72
|
+
hangman_game = HangmanEngine::Game.new(word, allowed_attempts, clue)
|
73
|
+
rescue HangmanEngine::GameError => e
|
74
|
+
notice = { msg: e.message, status: :error }
|
75
|
+
data_error = true
|
76
|
+
end
|
77
|
+
end while data_error
|
78
|
+
|
79
|
+
# Gameplay
|
80
|
+
notice = {}
|
81
|
+
until hangman_game.finished?
|
82
|
+
begin
|
83
|
+
display_game_board(hangman_game, base_width, base_idt, notice)
|
84
|
+
ltr = input('Your guess:', 1)
|
85
|
+
raise HangmanEngine::GameError, "Type a letter!" unless ltr.length > 0
|
86
|
+
hangman_game.guess(ltr.length > 1 ? ltr[0] : ltr)
|
87
|
+
notice = if hangman_game.guessed?(ltr)
|
88
|
+
{ msg: "'#{ltr}' guessed!", status: :success }
|
89
|
+
else
|
90
|
+
{ msg: "Ouuuch! burned :/", status: :error }
|
91
|
+
end
|
92
|
+
rescue HangmanEngine::GameError => e
|
93
|
+
notice = { msg: e.message, status: :error }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Result
|
98
|
+
display_game_board(hangman_game, base_width, base_idt)
|
99
|
+
putsi underscore * base_width + nl(3)
|
100
|
+
if hangman_game.solved?
|
101
|
+
display_won(base_width)
|
102
|
+
puts nl
|
103
|
+
else
|
104
|
+
display_lost(base_width)
|
105
|
+
puts nl(3)
|
106
|
+
putsi align("Sol.: #{hangman_game.word}", base_width, :center) + nl(2)
|
107
|
+
end
|
108
|
+
putsi underscore * base_width + nl(3)
|
109
|
+
|
110
|
+
# Play again?
|
111
|
+
keep_playing = input('New game? (y/n):', 1)[0].downcase
|
112
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: play_hangman
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
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
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: console_view_helper
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.0.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hangman_engine
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.0.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.0.1
|
41
|
+
description: ! 'Play HANGMAN on your console! Install, run the command ''play_hangman''
|
42
|
+
and enjoy.
|
43
|
+
|
44
|
+
This game was built using the ''hangman_engine'' gem. Check it out to build your
|
45
|
+
own HANGMAN version.'
|
46
|
+
email:
|
47
|
+
- almancill@gmail.com
|
48
|
+
executables:
|
49
|
+
- play_hangman
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- bin/play_hangman
|
54
|
+
- lib/play_hangman.rb
|
55
|
+
homepage: https://github.com/ammancilla/play_hangman
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata: {}
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 2.4.5
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Play HANGMAN on your console! Install, run the command 'play_hangman' and
|
79
|
+
enjoy.
|
80
|
+
test_files: []
|