Class: Githug::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



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

def initialize
  @profile = Profile.load
end

Instance Attribute Details

#profileObject

Returns the value of attribute profile.



4
5
6
# File 'lib/githug/game.rb', line 4

def profile
  @profile
end

Instance Method Details

#level_bumpObject



48
49
50
51
52
53
54
# File 'lib/githug/game.rb', line 48

def level_bump
  profile.level_bump
  if level = Level.load(profile.level)
    UI.puts(level.full_description)
    level.setup_level
  end
end

#play_levelObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/githug/game.rb', line 10

def play_level
  solve = true
  if profile.level.nil?
    UI.puts("Welcome to Githug!")
    solve = false
    level_bump
  else
    level = Level.load(profile.level)
    if solve && level
      if level.solve
        UI.success "Congratulations, you have solved the level!"
        level_bump
      else
        UI.error "Sorry, this solution is not quite right!"
        profile.current_attempts += 1
        profile.save

        if (profile.current_attempts > 2 && profile.current_attempts % 3 == 0)
          UI.error "Don't forget you can type `githug hint` for a hint and `githug reset` to reset the current level."
        end

        UI.puts level.full_description
      end
    end
  end
end

#test_level(level, errors = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/githug/game.rb', line 37

def test_level(level, errors = nil)
  UI.puts level.full_description
  method = :solve
  method = :test if errors
  if level.send(method)
    UI.success "Valid solution"
  else
    UI.error "Invalid solution"
  end
end