Class: AndriiCodebreaker::Game
- Inherits:
-
Object
- Object
- AndriiCodebreaker::Game
show all
- Includes:
- Constant, Statistic
- Defined in:
- lib/andrii_codebreaker/game.rb
Constant Summary
Constants included
from Constant
Constant::CODE_LENGTH, Constant::CODE_LENGTH_COUNT, Constant::CODE_START_LENGTH, Constant::DIFFICULTY, Constant::DIFFICULTY_SORT, Constant::LALA, Constant::MINUS, Constant::NAME_MAX_LENGTH, Constant::NAME_MIN_LENGTH, Constant::PLUS, Constant::RANGE_SECRET_CODE, Constant::WIN
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Statistic
#statistics
Constructor Details
#initialize(player, difficulties) ⇒ Game
Returns a new instance of Game.
11
12
13
14
15
16
|
# File 'lib/andrii_codebreaker/game.rb', line 11
def initialize(player, difficulties)
@player_name = player
@difficulties = difficulties
@used_attempts = 0
@used_hints = 0
end
|
Instance Attribute Details
#available_hints ⇒ Object
Returns the value of attribute available_hints.
9
10
11
|
# File 'lib/andrii_codebreaker/game.rb', line 9
def available_hints
@available_hints
end
|
#difficulties ⇒ Object
Returns the value of attribute difficulties.
8
9
10
|
# File 'lib/andrii_codebreaker/game.rb', line 8
def difficulties
@difficulties
end
|
#player_name ⇒ Object
Returns the value of attribute player_name.
8
9
10
|
# File 'lib/andrii_codebreaker/game.rb', line 8
def player_name
@player_name
end
|
#secret_code ⇒ Object
Returns the value of attribute secret_code.
8
9
10
|
# File 'lib/andrii_codebreaker/game.rb', line 8
def secret_code
@secret_code
end
|
#used_attempts ⇒ Object
Returns the value of attribute used_attempts.
9
10
11
|
# File 'lib/andrii_codebreaker/game.rb', line 9
def used_attempts
@used_attempts
end
|
#used_hints ⇒ Object
Returns the value of attribute used_hints.
9
10
11
|
# File 'lib/andrii_codebreaker/game.rb', line 9
def used_hints
@used_hints
end
|
Instance Method Details
#check_result(result, guess) ⇒ Object
58
59
60
61
62
63
|
# File 'lib/andrii_codebreaker/game.rb', line 58
def check_result(result, guess)
guess.each.with_index do |code, index|
result.sub!(MINUS, PLUS) if code == @secret_code[index]
end
result
end
|
#code_valid?(code) ⇒ Boolean
69
70
71
|
# File 'lib/andrii_codebreaker/game.rb', line 69
def code_valid?(code)
code.to_s.match(/^[1-6]{4}$/)
end
|
#compare_codes(guess) ⇒ Object
48
49
50
51
52
53
54
55
56
|
# File 'lib/andrii_codebreaker/game.rb', line 48
def compare_codes(guess)
@used_attempts += 1
guess = guess.chars.map(&:to_i)
minuses = (@secret_code & guess).map { |element| [@secret_code.count(element), guess.count(element)].min }.sum
result = MINUS * minuses
check_result(result, guess)
end
|
#hint ⇒ Object
23
24
25
26
27
28
29
30
|
# File 'lib/andrii_codebreaker/game.rb', line 23
def hint
return unless left_hint?
hint = @available_hints.chars.sample
@available_hints.sub!(hint, '')
@used_hints += 1
hint
end
|
#left_hint? ⇒ Boolean
32
33
34
|
# File 'lib/andrii_codebreaker/game.rb', line 32
def left_hint?
@used_hints < @difficulties.hints
end
|
#lose ⇒ Object
42
43
44
45
46
|
# File 'lib/andrii_codebreaker/game.rb', line 42
def lose
return false if @used_attempts < @difficulties.attempts
true
end
|
#start ⇒ Object
18
19
20
21
|
# File 'lib/andrii_codebreaker/game.rb', line 18
def start
@secret_code = generate_code
@available_hints = @secret_code.join.dup
end
|
#win?(guees) ⇒ Boolean
36
37
38
39
40
|
# File 'lib/andrii_codebreaker/game.rb', line 36
def win?(guees)
return true if @secret_code.join == guees
false
end
|