Class: CodebrackerSimb::Game

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

Overview

game cl

Constant Summary collapse

ERR_UNEXPECTED_COMPLEXITY =
'complexity could be easy, medium or hard'
IS_OVER =
'Game is over, you lose'
GREETING =
'Hello! Welcome to CodeBracker Game! Have fun!'
CONGRATULATIONS =
'Congratulations! You broked a code, master!'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(complexity) ⇒ Game

Returns a new instance of Game.



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

def initialize(complexity)
  @attempts = set_attempts(complexity)
end

Instance Attribute Details

#answerObject

Returns the value of attribute answer.



11
12
13
# File 'lib/codebracker_simb/game.rb', line 11

def answer
  @answer
end

#attemptsObject (readonly)

Returns the value of attribute attempts.



10
11
12
# File 'lib/codebracker_simb/game.rb', line 10

def attempts
  @attempts
end

#codeObject (readonly)

Returns the value of attribute code.



9
10
11
# File 'lib/codebracker_simb/game.rb', line 9

def code
  @code
end

Instance Method Details

#define_codeObject



36
37
38
# File 'lib/codebracker_simb/game.rb', line 36

def define_code
  @code = Array.new(4) { rand(1..6) }
end

#end_with_win?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/codebracker_simb/game.rb', line 40

def end_with_win?
  code == answer 
end

#input_answer(input) ⇒ Object



32
33
34
# File 'lib/codebracker_simb/game.rb', line 32

def input_answer(input)
  @answer = Code.new(input)
end

#playObject



22
23
24
25
26
27
28
29
30
# File 'lib/codebracker_simb/game.rb', line 22

def play
  define_code
  loop do
    input_answer
    result =  Checker.new(code, answer).compare
    refresh_attempts_quantity
    break if win? || @attempts == 0
  end
end

#refresh_attempts_quantityObject



44
45
46
47
48
49
# File 'lib/codebracker_simb/game.rb', line 44

def refresh_attempts_quantity
  if @attempts > 0
    @attempts -= 1
    message = "#{@attempts} left"
  end
end

#set_attempts(complexity) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/codebracker_simb/game.rb', line 51

def set_attempts(complexity)
  case complexity
  when 'easy'
    14
  when 'medium'
    10
  when 'hard'
    5
  else
    raise ERR_UNEXPECTED_COMPLEXITY
  end
end