Class: GamesParadise::Hangman

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

Overview

GamesParadise::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 inherited from Base

Base::CONTROL_C_CODE, Base::N

Class Method Summary collapse

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

#

initialize

#


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

#

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?

#

Returns:

  • (Boolean)


30
31
32
# File 'lib/games_paradise/hangman/ascii_art.rb', line 30

def self.ascii?
  ARRAY_ASCII_HANGMAN_PROGRESSION
end

.frame_number_one?Boolean

#

GamesParadise::Hangman.frame_number_one?

#

Returns:

  • (Boolean)


49
50
51
# File 'lib/games_paradise/hangman/hangman.rb', line 49

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)


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_letterObject

#

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

#


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 # Jump to the game-over state here.
  end
end

#decrement_the_frame_counter_by_plus_oneObject

#

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_lineObject

#

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

#


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_interfaceObject

#

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

#

frame?

#

Returns:

  • (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

#

game_over

#


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?

#

game_over?

#

Returns:

  • (Boolean)


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

#

guess_this_character

#


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?
    # 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)


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?

#

Returns:

  • (Boolean)


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_oneObject 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?

#

Returns:

  • (Boolean)


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

#

may_we_exit?

#

Returns:

  • (Boolean)


501
502
503
# File 'lib/games_paradise/hangman/hangman.rb', line 501

def may_we_exit?
  @may_we_exit
end
#

menu (menu tag)

#


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 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)


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?

#

progression_index?

#

Returns:

  • (Boolean)


324
325
326
# File 'lib/games_paradise/hangman/hangman.rb', line 324

def progression_index?
  @progression_index
end

#resetObject Also known as: restart

#

reset (reset tag)

#


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


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_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.

#


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_wordObject

#

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

#runObject

#

run (run tag)

#


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

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

#


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_helpObject

#

show_help

#


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_farObject

#

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_projectObject 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_exitObject

#

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

#

won?

#

Returns:

  • (Boolean)


249
250
251
# File 'lib/games_paradise/hangman/hangman.rb', line 249

def won?
  game_over? and return_can_we_exit? 
end