class RubyTerminalGames::Hangman::Game

Attributes

board[R]
guess_total[R]
input_index[R]
word[R]
wrong_guesses[R]

Public Class Methods

new() click to toggle source
# File lib/ruby_terminal_games/hangman/game.rb, line 10
def initialize
  @word = Word.new
  @board = Board.new
  @wrong_guesses = []
end

Public Instance Methods

play!() click to toggle source
# File lib/ruby_terminal_games/hangman/game.rb, line 16
def play!
  @playing = true

  Keyboard.capture(detect_direction: true) do |key|
    begin
      @playing = false if key =~ /Q/
      next unless allowed_key?(key)
      guess(key)
    rescue
      @playing = false
      Keyboard.stop_capture!
    end
  end

  while @playing
    board.print_world!(self)
    game_interval!
  end
end

Private Instance Methods

allowed_key?(key) click to toggle source
# File lib/ruby_terminal_games/hangman/game.rb, line 38
def allowed_key?(key)
  ('a'..'z').to_a.include?(key.downcase)
rescue
  false
end
game_interval!() click to toggle source
# File lib/ruby_terminal_games/hangman/game.rb, line 50
def game_interval!
  sleep(0.1)
end
guess(key) click to toggle source
# File lib/ruby_terminal_games/hangman/game.rb, line 44
def guess(key)
  return if word.guess!(key.strip.downcase)
  @wrong_guesses << key.upcase
  @wrong_guesses.uniq!
end