Class: AndriiCodebreaker::Game

Inherits:
Object
  • Object
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_hintsObject

Returns the value of attribute available_hints.



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

def available_hints
  @available_hints
end

#difficultiesObject (readonly)

Returns the value of attribute difficulties.



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

def difficulties
  @difficulties
end

#player_nameObject (readonly)

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_codeObject (readonly)

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_attemptsObject

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_hintsObject

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

Returns:

  • (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

#generate_codeObject



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

def generate_code
  Array.new(CODE_LENGTH_COUNT) { rand(RANGE_SECRET_CODE) }
end

#hintObject



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

Returns:

  • (Boolean)


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

def left_hint?
  @used_hints < @difficulties.hints
end

#loseObject



42
43
44
45
46
# File 'lib/andrii_codebreaker/game.rb', line 42

def lose
  return false if @used_attempts < @difficulties.attempts

  true
end

#startObject



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

Returns:

  • (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