Class: GamesAndRpgParadise::Hangman

Inherits:
Base
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/games/hangman/hangman.rb,
lib/games_and_rpg_paradise/games/hangman/ascii_art.rb

Overview

GamesAndRpgParadise::Hangman

Constant Summary collapse

ARRAY_ANIMALS_WORDS =
#

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 =
#

RIP_ASCII_PICTURE

#
"

         -|-
          |
      .-'~~~`-.
    .'         `.
    |  R  I  P  |
    |           |
    |           |
  \\|           |//

"
ARRAY_ASCII_HANGMAN_PROGRESSION =
#

ARRAY_ASCII_HANGMAN_PROGRESSION

#
[

"  +---+
|   |
    |
    |
    |
    |
=========",
 
"  +---+
|   |
O   |
    |
    |
    |
=========",
 
"  +---+
|   |
O   |
|   |
    |
    |
=========",
 
"  +---+
|   |
O   |
 /|   |
    |
    |
=========",
 
"  +---+
|   |
O   |
 /|\\  |
    |
    |
=========",
 
"  +---+
|   |
O   |
 /|\\  |
 /    |
    |
=========",
 
"  +---+
|   |
O   |
 /|\\  |
 / \\  |
    |
========="
]

Constants included from Base::Extensions::Colours

Base::Extensions::Colours::ARRAY_AVAILABLE_KONSOLE_COLOURS

Constants included from CommonExtensions

CommonExtensions::CONTROL_C_CODE, CommonExtensions::N

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base::Extensions::Colours

ecomment, #efancy, #eparse, #forestgreen, #gold, #grey, #lightblue, #mediumseagreen, #mediumslateblue, #peru, #rev, sdir, sfancy, #sfile, simp, #teal, #yellow

Methods included from Base::Extensions::CommandlineArguments

#commandline_arguments?, #filter_away_commandline_arguments, #first_argument?, #first_non_hyphened_argument?, #set_commandline_arguments

Methods included from CommonExtensions

#cat, #cd, #cliner, #copy_file, #delete, #dirname_but_retains_the_trailing_slash, #disable_colours, #ensure_that_the_log_directory_exists, #esystem, #get_user_input, #infer_the_namespace, #is_on_roebe?, #log_dir?, #mkdir, #mkdir_then_cd_into_it, #mv, #namespace?, #opne, #opnn, #project_base_directory?, #project_image_directory?, #project_yaml_directory?, #rds, #register_sigint, #remove_this_directory, #rename_file, #reset_the_internal_hash, #return_pwd, #return_today, #touch_file, #wrap, #write_what_into

Constructor Details

#initialize(commandline_arguments = ARGV, run_already = true) ⇒ Hangman

#

initialize

#


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 57

def initialize(
    commandline_arguments = ARGV,
    run_already           = true
  )
  register_sigint
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  case first_argument?
  # ======================================================================= #
  # === :do_not_run_yet
  # ======================================================================= #
  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

#

GamesAndRpgParadise::Hangman[]

#


519
520
521
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 519

def self.[](i = '')
  new(i)
end

.ascii?Boolean

#

GamesAndRpgParadise::Hangman.ascii?

#

Returns:

  • (Boolean)


32
33
34
# File 'lib/games_and_rpg_paradise/games/hangman/ascii_art.rb', line 32

def self.ascii?
  ARRAY_ASCII_HANGMAN_PROGRESSION
end

.frame_number_one?Boolean

#

GamesAndRpgParadise::Hangman.frame_number_one?

#

Returns:

  • (Boolean)


50
51
52
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 50

def self.frame_number_one?
  ARRAY_ASCII_HANGMAN_PROGRESSION[0]
end

Instance Method Details

#array_guessed_letters_so_far?Boolean Also known as: guessed_characters?

#

array_guessed_letters_so_far?

#

Returns:

  • (Boolean)


198
199
200
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 198

def array_guessed_letters_so_far? 
  @array_guessed_letters_so_far
end

#ask_the_user_for_the_letterObject

#

ask_the_user_for_the_letter

This is the method that will ask the user for the letter to guess.

#


181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 181

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 # Get only one char.
  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_stateObject

#

check_for_game_over_state

#


302
303
304
305
306
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 302

def check_for_game_over_state
  if @progression_index > THE_USER_MAY_TRY_TO_GUESS_N_TIMES
    game_over # Jump to the game-over state here.
  end
end

#decrement_the_frame_counter_by_plus_oneObject

#

decrement_the_frame_counter_by_plus_one

#


318
319
320
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 318

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.

#


407
408
409
410
411
412
413
414
415
416
417
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 407

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_lineObject

#

display_underscored_line

#


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 144

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
  # ======================================================================= #
  # Next we build up the exit-condition. We can exit when every
  # character in the @guess_this_word.chars is included in
  # the @array_guessed_letters_so_far.uniq array.
  # ======================================================================= #
  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_wonObject

#

do_end_the_game_as_the_player_has_won

#


169
170
171
172
173
174
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 169

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_interfaceObject

#

do_start_the_graphical_user_interface

#


395
396
397
398
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 395

def do_start_the_graphical_user_interface
  require 'games_and_rpg_paradise/gui/gtk3/hangman/hangman.rb'
  ::GamesAndRpgParadise::GUI::Gtk::Hangman.run
end

#frame?Boolean

#

frame?

#

Returns:

  • (Boolean)


205
206
207
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 205

def frame?
  @frame
end

#game_over(may_we_exit = @may_we_exit) ⇒ Object

#

game_over

#


484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 484

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?

#

game_over?

#

Returns:

  • (Boolean)


429
430
431
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 429

def game_over?
  @the_game_is_over
end

#guess_this_character(i = @user_input) ⇒ Object

#

guess_this_character

#


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
297
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 268

def guess_this_character(i = @user_input)
  if i.empty?
    # This means "quit".
  elsif @array_guessed_letters_so_far.include? i
    e 'The character '+i+' has already been guessed before.'
  else
    # ===================================================================== #
    # First register it anyway.
    # ===================================================================== #
    @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

#

guess_this_word?

#

Returns:

  • (Boolean)


137
138
139
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 137

def guess_this_word?
  @guess_this_word
end

#has_this_character_been_guessed_already?(i) ⇒ Boolean

#

has_this_character_been_guessed_already?

#

Returns:

  • (Boolean)


130
131
132
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 130

def has_this_character_been_guessed_already?(i)
  @array_guessed_letters_so_far.include?(i)
end

#increase_the_frame_counter_by_plus_oneObject Also known as: increment_the_frame_counter_by_plus_one

#

increase_the_frame_counter_by_plus_one

#


311
312
313
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 311

def increase_the_frame_counter_by_plus_one
  @progression_index += 1
end

#is_this_character_included?(i) ⇒ Boolean

#

is_this_character_included?

#

Returns:

  • (Boolean)


332
333
334
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 332

def is_this_character_included?(i)
  @guess_this_word.include?(i)
end

#may_we_exit?Boolean

#

may_we_exit?

#

Returns:

  • (Boolean)


502
503
504
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 502

def may_we_exit?
  @may_we_exit
end
#

menu (menu tag)

#


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
390
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 349

def menu(
    i = commandline_arguments?
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i
    # ===================================================================== #
    # === hangman --help
    #
    # This entry point will display the help-interface.
    # ===================================================================== #
    when /^-?-?help$/i
      show_help
      exit
    # ===================================================================== #
    # === hangman --gui
    #
    # This entry point will start the ruby-gtk3 binding for hangman.
    # ===================================================================== #
    when /^-?-?gui$/i,
         /^-?-?gtk$/i
      do_start_the_graphical_user_interface
    # ===================================================================== #
    # === hangman --dictionary
    #
    # This entry point will load up a word from the english dictionary,
    # if the "dictionaries" gem has been installed.
    # ===================================================================== #
    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?

#

Returns:

  • (Boolean)


123
124
125
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 123

def n_characters_to_guess?
  @guess_this_word.size
end

#progression_index?Boolean Also known as: frame_counter?

#

progression_index?

#

Returns:

  • (Boolean)


325
326
327
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 325

def progression_index?
  @progression_index
end

#resetObject Also known as: restart

#

reset (reset tag)

#


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
118
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 84

def reset
  # ======================================================================= #
  # === @guess_this_word
  #
  # This is the word that has to be guessed.
  # ======================================================================= #
  @guess_this_word = ARRAY_ANIMALS_WORDS.sample # 'hangman'
  # ======================================================================= #
  # === @may_we_exit
  # ======================================================================= #
  @may_we_exit = true
  # ======================================================================= #
  # === @array_guessed_letters_so_far
  # ======================================================================= #
  @array_guessed_letters_so_far = []
  # ======================================================================= #
  # === @user_input
  # ======================================================================= #
  @user_input = nil
  # ======================================================================= #
  # === @progression_index
  # ======================================================================= #
  @progression_index = 0
  # ======================================================================= #
  # === @the_game_is_over
  # ======================================================================= #
  @the_game_is_over = false
  # ======================================================================= #
  # === @frame
  #
  # The @frame variable contains the current frame, based on the
  # ASCII hangman dataset.
  # ======================================================================= #
  @frame = ARRAY_ASCII_HANGMAN_PROGRESSION[@progression_index]
end

#return_can_we_exit?Boolean

#

return_can_we_exit?

#

Returns:

  • (Boolean)


257
258
259
260
261
262
263
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 257

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_farObject

#

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.

#


223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 223

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_wordObject

#

reveal_the_guessed_word

#


212
213
214
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 212

def reveal_the_guessed_word
  e 'The guessed word would have been `'+gold(guess_this_word?)+'`.'
end

#runObject

#

run (run tag)

#


469
470
471
472
473
474
475
476
477
478
479
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 469

def run
  menu
  loop {
    show_which_letters_have_been_guessed_so_far
    ask_the_user_for_the_letter
    case @user_input
    when nil, '', CONTROL_C_CODE # Valid ways to exit.
      break
    end
  }
end

#set_guess_this_word(i) ⇒ Object

#

set_guess_this_word

#


239
240
241
242
243
244
245
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 239

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

#


422
423
424
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 422

def set_progression_index(i)
  @progression_index = i
end

#show_helpObject

#

show_help

#


339
340
341
342
343
344
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 339

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_farObject

#

show_which_letters_have_been_guessed_so_far

#


436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 436

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_projectObject Also known as: make_use_of_the_dictionaries_gem

#

use_a_random_word_from_the_dictionary_project

#


453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 453

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_exitObject

#

we_may_not_exit

This method was specifically used to allow the ruby-gtk3 binding to continue.

#


512
513
514
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 512

def we_may_not_exit
  @may_we_exit = false
end

#won?Boolean

#

won?

#

Returns:

  • (Boolean)


250
251
252
# File 'lib/games_and_rpg_paradise/games/hangman/hangman.rb', line 250

def won?
  game_over? and return_can_we_exit? 
end