Class: GamesParadise::Hangman
- Inherits:
-
Base
- Object
- Base
- GamesParadise::Hangman
show all
- Defined in:
- lib/games_paradise/hangman/hangman.rb,
lib/games_paradise/hangman/ascii_art.rb
Overview
Constant Summary
collapse
- ARRAY_ANIMALS_WORDS =
%w(
ant baboon badger bat bear beaver camel cat clam cobra cougar
coyote crow deer dog donkey duck eagle ferret fox frog goat
goose hawk lion lizard llama mole monkey moose mouse mule newt
otter owl panda parrot pigeon python rabbit ram rat raven
rhino salmon seal shark sheep skunk sloth snake spider
stork swan tiger toad trout turkey turtle weasel whale
wolf wombat zebra
)
- THE_USER_MAY_TRY_TO_GUESS_N_TIMES =
#
THE_USER_MAY_TRY_TO_GUESS_N_TIMES
How many tries the user may do.
#
6
- RIP_ASCII_PICTURE =
"
-|-
|
.-'~~~`-.
.' `.
| R I P |
| |
| |
\\| |//
"
- ARRAY_ASCII_HANGMAN_PROGRESSION =
#
ARRAY_ASCII_HANGMAN_PROGRESSION
#
[
" +---+
| |
|
|
|
|
=========",
" +---+
| |
O |
|
|
|
=========",
" +---+
| |
O |
| |
|
|
=========",
" +---+
| |
O |
/| |
|
|
=========",
" +---+
| |
O |
/|\\ |
|
|
=========",
" +---+
| |
O |
/|\\ |
/ |
|
=========",
" +---+
| |
O |
/|\\ |
/ \\ |
|
========="
]
Constants inherited
from Base
Base::CONTROL_C_CODE, Base::N
Class Method Summary
collapse
-
.[](i = '') ⇒ Object
# === GamesParadise::Hangman[] ========================================================================= #.
-
.ascii? ⇒ Boolean
# === GamesParadise::Hangman.ascii? ========================================================================= #.
-
.frame_number_one? ⇒ Boolean
# === GamesParadise::Hangman.frame_number_one? ========================================================================= #.
Instance Method Summary
collapse
Methods inherited from Base
#cat, #commandline_arguments?, #efancy, #eparse, #first_argument?, #forestgreen, #gold, #lightblue, #lightgreen, #mediumorchid, #mediumslateblue, #opnn, #peru, #register_sigint, #rev, #royalblue, #set_commandline_arguments, #sfile, #steelblue, #teal, #tomato, #yellow
Methods included from BaseModule
#cliner, #commandline_arguments?, #first_argument?, #infer_the_namespace, #namespace?, #rename_file, #reset_the_internal_hash, #return_pwd, #set_commandline_arguments
Constructor Details
#initialize(commandline_arguments = ARGV, run_already = true) ⇒ Hangman
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/games_paradise/hangman/hangman.rb', line 56
def initialize(
commandline_arguments = ARGV,
run_already = true
)
register_sigint
reset
set_commandline_arguments(
commandline_arguments
)
case first_argument?
when :do_not_run_yet
run_already = false
end
if block_given?
if yield == :do_not_run_yet
run_already = false
end
end
run if run_already
end
|
Class Method Details
.[](i = '') ⇒ Object
#
GamesParadise::Hangman[]
#
518
519
520
|
# File 'lib/games_paradise/hangman/hangman.rb', line 518
def self.[](i = '')
new(i)
end
|
.ascii? ⇒ Boolean
#
GamesParadise::Hangman.ascii?
#
.frame_number_one? ⇒ Boolean
#
GamesParadise::Hangman.frame_number_one?
#
Instance Method Details
#array_guessed_letters_so_far? ⇒ Boolean
Also known as:
guessed_characters?
#
array_guessed_letters_so_far?
#
197
198
199
|
# File 'lib/games_paradise/hangman/hangman.rb', line 197
def array_guessed_letters_so_far?
@array_guessed_letters_so_far
end
|
#ask_the_user_for_the_letter ⇒ Object
#
ask_the_user_for_the_letter
This is the method that will ask the user for the letter to guess.
#
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/games_paradise/hangman/hangman.rb', line 180
def ask_the_user_for_the_letter
display_underscored_line
e 'Next, '+tomato('input')+' a character to guess, or input '\
'nothing to quit:'
e
@user_input = STDIN.getch.strip if @array_guessed_letters_so_far.include? @user_input
e mediumorchid('You already did input this letter; it '\
'is included in the word that is to be guessed.')
else
guess_this_character(@user_input)
end
end
|
#check_for_game_over_state ⇒ Object
#
check_for_game_over_state
#
301
302
303
304
305
|
# File 'lib/games_paradise/hangman/hangman.rb', line 301
def check_for_game_over_state
if @progression_index > THE_USER_MAY_TRY_TO_GUESS_N_TIMES
game_over end
end
|
#decrement_the_frame_counter_by_plus_one ⇒ Object
#
decrement_the_frame_counter_by_plus_one
#
317
318
319
|
# File 'lib/games_paradise/hangman/hangman.rb', line 317
def decrement_the_frame_counter_by_plus_one
@progression_index -= 1
end
|
#display_the_current_frame(frame = @frame, n_left_padding_to_use = 4) ⇒ Object
#
display_the_current_frame
This method will display the current frame, via the commandline.
Note that the method will also apply some padding usually.
#
406
407
408
409
410
411
412
413
414
415
416
|
# File 'lib/games_paradise/hangman/hangman.rb', line 406
def display_the_current_frame(
frame = @frame,
n_left_padding_to_use = 4
)
if n_left_padding_to_use and (n_left_padding_to_use > 0)
frame = frame.split(N).map {|entry|
(' ' * n_left_padding_to_use)+entry
}.join(N)
end
e teal(frame)
end
|
#display_underscored_line ⇒ Object
#
display_underscored_line
#
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/games_paradise/hangman/hangman.rb', line 143
def display_underscored_line
ee 'The '+steelblue('word')+' that is to be guessed is: '
@guess_this_word.each_char {|this_char|
if @array_guessed_letters_so_far.include? this_char
ee lightblue(this_char)
else
ee lightblue('_')
end
}
e
can_we_exit = return_can_we_exit?
if can_we_exit
do_end_the_game_as_the_player_has_won
end
e
end
|
#do_end_the_game_as_the_player_has_won ⇒ Object
#
do_end_the_game_as_the_player_has_won
#
168
169
170
171
172
173
|
# File 'lib/games_paradise/hangman/hangman.rb', line 168
def do_end_the_game_as_the_player_has_won
e mediumslateblue('All characters have been guessed correctly. You won! \\o/')
e 'The ASCII person on the gallows escaped today - excellent '\
'job!'
exit
end
|
#do_start_the_graphical_user_interface ⇒ Object
#
do_start_the_graphical_user_interface
#
394
395
396
397
|
# File 'lib/games_paradise/hangman/hangman.rb', line 394
def do_start_the_graphical_user_interface
require 'games_paradise/gui/gtk3/hangman/hangman.rb'
::GamesParadise::GUI::Gtk::Hangman.run
end
|
#frame? ⇒ Boolean
204
205
206
|
# File 'lib/games_paradise/hangman/hangman.rb', line 204
def frame?
@frame
end
|
#game_over(may_we_exit = @may_we_exit) ⇒ Object
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
# File 'lib/games_paradise/hangman/hangman.rb', line 483
def game_over(
may_we_exit = @may_we_exit
)
e
e tomato('Oh noes! You did not guess correctly. ')+
steelblue(UNHAPPY_SMILEY)
e
e 'The game is over ... the ASCII "person" did not live '\
'to see another day ...'
e
reveal_the_guessed_word
@the_game_is_over = true
exit if @may_we_exit
end
|
#game_over? ⇒ Boolean
Also known as:
is_the_game_over?
428
429
430
|
# File 'lib/games_paradise/hangman/hangman.rb', line 428
def game_over?
@the_game_is_over
end
|
#guess_this_character(i = @user_input) ⇒ Object
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
# File 'lib/games_paradise/hangman/hangman.rb', line 267
def guess_this_character(i = @user_input)
if i.empty?
elsif @array_guessed_letters_so_far.include? i
e 'The character '+i+' has already been guessed before.'
else
@array_guessed_letters_so_far << i
if @guess_this_word.include?(i) and !i.empty?
@message = forestgreen(
"Yes this character (")+
steelblue(i)+
forestgreen(") is included! Good job!\nMore of "\
"the word that is to be guessed, will be revealed next."
)
e @message
else
e rev+
'No, the character '+i.to_s+' is '+tomato('NOT included')+
'. The hangman-build-up proceeds ...'
e
@frame = ARRAY_ASCII_HANGMAN_PROGRESSION[@progression_index]
display_the_current_frame
@progression_index += 1
check_for_game_over_state
end
end
end
|
#guess_this_word? ⇒ Boolean
136
137
138
|
# File 'lib/games_paradise/hangman/hangman.rb', line 136
def guess_this_word?
@guess_this_word
end
|
#has_this_character_been_guessed_already?(i) ⇒ Boolean
#
has_this_character_been_guessed_already?
#
129
130
131
|
# File 'lib/games_paradise/hangman/hangman.rb', line 129
def has_this_character_been_guessed_already?(i)
@array_guessed_letters_so_far.include?(i)
end
|
#increase_the_frame_counter_by_plus_one ⇒ Object
Also known as:
increment_the_frame_counter_by_plus_one
#
increase_the_frame_counter_by_plus_one
#
310
311
312
|
# File 'lib/games_paradise/hangman/hangman.rb', line 310
def increase_the_frame_counter_by_plus_one
@progression_index += 1
end
|
#is_this_character_included?(i) ⇒ Boolean
#
is_this_character_included?
#
331
332
333
|
# File 'lib/games_paradise/hangman/hangman.rb', line 331
def is_this_character_included?(i)
@guess_this_word.include?(i)
end
|
#may_we_exit? ⇒ Boolean
501
502
503
|
# File 'lib/games_paradise/hangman/hangman.rb', line 501
def may_we_exit?
@may_we_exit
end
|
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
# File 'lib/games_paradise/hangman/hangman.rb', line 348
def (
i = commandline_arguments?
)
if i.is_a? Array
i.each {|entry| (entry) }
else
case i
when /^-?-?help$/i
show_help
exit
when /^-?-?gui$/i,
/^-?-?gtk$/i
do_start_the_graphical_user_interface
when /dictionary/
use_a_random_word_from_the_dictionary_project
else
if i and File.exist?(i)
set_guess_this_word(
File.readlines(i).reject {|line|
line.to_s.empty?
}.sample
)
end
end
end
end
|
#n_characters_to_guess? ⇒ Boolean
#
n_characters_to_guess?
#
122
123
124
|
# File 'lib/games_paradise/hangman/hangman.rb', line 122
def n_characters_to_guess?
@guess_this_word.size
end
|
#progression_index? ⇒ Boolean
Also known as:
frame_counter?
324
325
326
|
# File 'lib/games_paradise/hangman/hangman.rb', line 324
def progression_index?
@progression_index
end
|
#reset ⇒ Object
Also known as:
restart
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/games_paradise/hangman/hangman.rb', line 83
def reset
@guess_this_word = ARRAY_ANIMALS_WORDS.sample @may_we_exit = true
@array_guessed_letters_so_far = []
@user_input = nil
@progression_index = 0
@the_game_is_over = false
@frame = ARRAY_ASCII_HANGMAN_PROGRESSION[@progression_index]
end
|
#return_can_we_exit? ⇒ Boolean
256
257
258
259
260
261
262
|
# File 'lib/games_paradise/hangman/hangman.rb', line 256
def return_can_we_exit?
(
@guess_this_word.chars.uniq.all? {|this_char|
@array_guessed_letters_so_far.uniq.sort.include? this_char
}
)
end
|
#return_the_word_that_has_been_guessed_so_far ⇒ Object
#
return_the_word_that_has_been_guessed_so_far
This method has been created specifically for the ruby-gtk3 bindings. It will return the current word that the user has guessed so far, or ‘_’ for every character that was not yet guessed correctly.
#
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/games_paradise/hangman/hangman.rb', line 222
def return_the_word_that_has_been_guessed_so_far
_ = ''.dup
array = @guess_this_word.chars
array.each {|this_char|
if @array_guessed_letters_so_far.include? this_char
_ << this_char
else
_ << '_'
end
}
return _
end
|
#reveal_the_guessed_word ⇒ Object
#
reveal_the_guessed_word
#
211
212
213
|
# File 'lib/games_paradise/hangman/hangman.rb', line 211
def reveal_the_guessed_word
e 'The guessed word would have been `'+gold(guess_this_word?)+'`.'
end
|
#run ⇒ Object
468
469
470
471
472
473
474
475
476
477
478
|
# File 'lib/games_paradise/hangman/hangman.rb', line 468
def run
loop {
show_which_letters_have_been_guessed_so_far
ask_the_user_for_the_letter
case @user_input
when nil, '', CONTROL_C_CODE break
end
}
end
|
#set_guess_this_word(i) ⇒ Object
238
239
240
241
242
243
244
|
# File 'lib/games_paradise/hangman/hangman.rb', line 238
def set_guess_this_word(i)
if i.is_a? Array
i = i.first
end
i = i.to_s.strip
@guess_this_word = i
end
|
#set_progression_index(i) ⇒ Object
Also known as:
frame_counter=
#
set_progression_index
#
421
422
423
|
# File 'lib/games_paradise/hangman/hangman.rb', line 421
def set_progression_index(i)
@progression_index = i
end
|
#show_help ⇒ Object
338
339
340
341
342
343
|
# File 'lib/games_paradise/hangman/hangman.rb', line 338
def show_help
e
e ' --gtk # start the ruby-gtk3 bindings of hangman'
e ' --dictionary # make use of a random word from the dictionary project'
e
end
|
#show_which_letters_have_been_guessed_so_far ⇒ Object
#
show_which_letters_have_been_guessed_so_far
#
435
436
437
438
439
440
441
442
443
444
445
446
447
|
# File 'lib/games_paradise/hangman/hangman.rb', line 435
def show_which_letters_have_been_guessed_so_far
e
if @array_guessed_letters_so_far.empty?
e rev+'So far no letters have been guessed.'
else
e rev+
'The following letters have been guessed so far: '+
steelblue(
@array_guessed_letters_so_far.join(', ').strip
)
end
e
end
|
#use_a_random_word_from_the_dictionary_project ⇒ Object
Also known as:
make_use_of_the_dictionaries_gem
#
use_a_random_word_from_the_dictionary_project
#
452
453
454
455
456
457
458
459
460
461
462
463
|
# File 'lib/games_paradise/hangman/hangman.rb', line 452
def use_a_random_word_from_the_dictionary_project
require 'yaml'
require 'dictionaries/ask_english_word.rb'
file = Dictionaries.path_to_the_english_file?
if File.exist? file
dataset = YAML.load_file(file)
use_this_word = dataset.keys.sample
set_guess_this_word(use_this_word)
else
e 'No file exists at '+sfile(file)+'.'
end
end
|
#we_may_not_exit ⇒ Object
#
we_may_not_exit
This method was specifically used to allow the ruby-gtk3 binding to continue.
#
511
512
513
|
# File 'lib/games_paradise/hangman/hangman.rb', line 511
def we_may_not_exit
@may_we_exit = false
end
|
#won? ⇒ Boolean
249
250
251
|
# File 'lib/games_paradise/hangman/hangman.rb', line 249
def won?
game_over? and return_can_we_exit?
end
|