Class: Game

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.correctObject (readonly)

Returns the value of attribute correct.



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

def correct
  @correct
end

.deckObject (readonly)

Returns the value of attribute deck.



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

def deck
  @deck
end

Class Method Details

.check_answerObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/game.rb', line 29

def check_answer
  while !@correct
    sleep(0.5)
    View.render("Guess:")
    answer = GameLogic.user_input
    if answer == @current_card.answer
      @correct = true
      View.correct_answer
    elsif answer.downcase == "exit"
      @user_exit = true
      break
    else
      View.wrong_answer
      try_again = GameLogic.user_input
      if try_again == 'n'
        View.show_answer(@current_card)
        break
      end
    end
  end
end

.initialize(args = {}) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/game.rb', line 8

def initialize(args={})
  @current_card = nil
  @file = args[:file] || ARGV[0]
  @deck = Deck.initialize(@file)
  @user_exit = false
  @counter = 0
  @correct = false
end

.playObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/game.rb', line 17

def play
  View.game_intro
  while !@user_exit && @counter < deck.length
    sleep(0.5)
    # repeat = true
    @correct = false
    @current_card = deck[@counter]
    View.show_question(@current_card)
    check_answer
    @counter += 1
  end
end