Class: Codebraker::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



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

def initialize
  @secret_code = ''
  @user_suggested_code = ''
  @attempts = 5
  @hints = 2
  @score = 0
  generate_code
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



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

def attempts
  @attempts
end

#hintsObject (readonly)

Returns the value of attribute hints.



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

def hints
  @hints
end

#result_of_comparingObject (readonly)

Returns the value of attribute result_of_comparing.



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

def result_of_comparing
  @result_of_comparing
end

Instance Method Details

#compare_codesObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/codebracker/game.rb', line 43

def compare_codes
  @secret_code_for_comparing = @secret_code.dup
  4.times { |i|
    if @secret_code_for_comparing[i] == @user_suggested_code[i]
      @secret_code_for_comparing[i] = '$'
      @user_suggested_code[i] = '+'
    end
  }
  4.times { |i|
    if @secret_code_for_comparing.include?(@user_suggested_code[i])
      @secret_code_for_comparing.sub!(@user_suggested_code[i], '$')
      @user_suggested_code[i] = '-'
    end
  }
  4.times { |i|
    if /[\d]/ =~ @user_suggested_code[i]
      @user_suggested_code[i] = ' '
    end
  }
  @result_of_comparing = @user_suggested_code.dup
end

#count_scoreObject



65
66
67
# File 'lib/codebracker/game.rb', line 65

def count_score
  @score = @attempts * 10 + @hints * 15
end

#decrease_attemptsObject



69
70
71
# File 'lib/codebracker/game.rb', line 69

def decrease_attempts
  @attempts -= 1
end

#generate_codeObject



34
35
36
# File 'lib/codebracker/game.rb', line 34

def generate_code
  @secret_code = (1..4).map { rand(1..6) }.join
end

#loose?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/codebracker/game.rb', line 30

def loose?
  @attempts.zero?
end

#show_hintObject



79
80
81
# File 'lib/codebracker/game.rb', line 79

def show_hint
  puts @secret_code[rand(0..3)]
end

#show_result_of_comparingObject



83
84
85
# File 'lib/codebracker/game.rb', line 83

def show_result_of_comparing
  puts @user_suggested_code
end

#start(user_suggested_code) ⇒ Object



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

def start(user_suggested_code)
  @user_suggested_code = user_suggested_code
  decrease_attempts
  validate_input(user_suggested_code)
  compare_codes
  show_result_of_comparing
end

#use_hintObject



73
74
75
76
77
# File 'lib/codebracker/game.rb', line 73

def use_hint
  return ABSENT_HITNS_MESSAGE if @hints.zero?
  @hints -= 1
  show_hint
end

#validate_input(input) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
# File 'lib/codebracker/game.rb', line 38

def validate_input(input)
  raise ArgumentError, 'Type exactly 4 integer' unless input.length == 4
  raise ArgumentError, 'Type only integers from 1 to 6' if input =~ /[^1-6]/
end

#win?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/codebracker/game.rb', line 26

def win?
  @result_of_comparing == '++++'
end