Class: RubyTerminalGames::Hangman::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_terminal_games/hangman/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



10
11
12
13
14
# File 'lib/ruby_terminal_games/hangman/game.rb', line 10

def initialize
  @word = Word.new
  @board = Board.new
  @wrong_guesses = []
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



7
8
9
# File 'lib/ruby_terminal_games/hangman/game.rb', line 7

def board
  @board
end

#guess_totalObject (readonly)

Returns the value of attribute guess_total.



7
8
9
# File 'lib/ruby_terminal_games/hangman/game.rb', line 7

def guess_total
  @guess_total
end

#input_indexObject (readonly)

Returns the value of attribute input_index.



7
8
9
# File 'lib/ruby_terminal_games/hangman/game.rb', line 7

def input_index
  @input_index
end

#wordObject (readonly)

Returns the value of attribute word.



7
8
9
# File 'lib/ruby_terminal_games/hangman/game.rb', line 7

def word
  @word
end

#wrong_guessesObject (readonly)

Returns the value of attribute wrong_guesses.



7
8
9
# File 'lib/ruby_terminal_games/hangman/game.rb', line 7

def wrong_guesses
  @wrong_guesses
end

Instance Method Details

#play!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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