Class: Game

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

Overview

Class to hold all main game elements and behaviour

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



19
20
21
22
23
# File 'lib/game.rb', line 19

def initialize
  @player = nil
  @word_to_guess = word
  @guess_logic = GuessLogic.new(word_to_guess)
end

Instance Attribute Details

#guess_logicObject

Returns the value of attribute guess_logic.



17
18
19
# File 'lib/game.rb', line 17

def guess_logic
  @guess_logic
end

#playerObject

Returns the value of attribute player.



17
18
19
# File 'lib/game.rb', line 17

def player
  @player
end

#word_to_guessObject

Returns the value of attribute word_to_guess.



17
18
19
# File 'lib/game.rb', line 17

def word_to_guess
  @word_to_guess
end

Instance Method Details

#guessObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/game.rb', line 56

def guess
  loop do
    Display.beginning_of_guess_round(guess_logic)
    guess = Guess.new($stdin.gets, word_to_guess)
    Display.validation_errors(guess.errors)
    redo unless guess.valid?
    full_word_check(guess) if guess_logic.full_word_guess?(guess)
    guess_logic.compare(guess)
    Display.end_of_guess_round(guess_logic)
    break
  end
end

#main_game_loopObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/game.rb', line 33

def main_game_loop
  loop do
    Gallows::GALLOWS[guess_logic.incorrect_guesses]
    # puts word_to_guess.word TESTING
    guess
    victory if win?
    defeat if game_over?
    GameSave.save(self)
  end
end

#player_setupObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/game.rb', line 44

def player_setup
  loop do
    Display.enter_name
    player_name = PlayerName.new($stdin.gets)
    Display.validation_errors(player_name.errors)
    redo unless player_name.valid?
    self.player = Player.new(player_name.answer)
    Display.thank_player(player)
    break
  end
end

#startObject



25
26
27
28
29
30
31
# File 'lib/game.rb', line 25

def start
  welcome
  load_game
  instructions
  player_setup
  main_game_loop
end