Class: CodebreakerVk::Game

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

Constant Summary collapse

INCLUDE_IN_GAME_NUMBERS =
(1..6).freeze
CODE_SIZE =
4
GUESS_PLACE =
'+'
GUESS_PRESENCE =
'-'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(difficulty, user) ⇒ Game

Returns a new instance of Game.



13
14
15
16
17
18
19
20
21
# File 'lib/codebreaker_vk/game.rb', line 13

def initialize(difficulty, user)
  @breaker_numbers = generate_random_code
  @breaker_numbers_copy = @breaker_numbers.clone.shuffle
  @hints = difficulty.level[:hints]
  @attempts = difficulty.level[:attempts]
  @difficulty = difficulty.level
  @player_name = user.name
  @showed_hints = []
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



5
6
7
# File 'lib/codebreaker_vk/game.rb', line 5

def attempts
  @attempts
end

#breaker_numbersObject (readonly)

Returns the value of attribute breaker_numbers.



5
6
7
# File 'lib/codebreaker_vk/game.rb', line 5

def breaker_numbers
  @breaker_numbers
end

#difficultyObject (readonly)

Returns the value of attribute difficulty.



5
6
7
# File 'lib/codebreaker_vk/game.rb', line 5

def difficulty
  @difficulty
end

#hintsObject (readonly)

Returns the value of attribute hints.



5
6
7
# File 'lib/codebreaker_vk/game.rb', line 5

def hints
  @hints
end

#player_nameObject (readonly)

Returns the value of attribute player_name.



5
6
7
# File 'lib/codebreaker_vk/game.rb', line 5

def player_name
  @player_name
end

#showed_hintsObject (readonly)

Returns the value of attribute showed_hints.



5
6
7
# File 'lib/codebreaker_vk/game.rb', line 5

def showed_hints
  @showed_hints
end

Instance Method Details

#hintObject



29
30
31
32
33
34
35
# File 'lib/codebreaker_vk/game.rb', line 29

def hint
  return if @hints.zero?

  @hints -= 1
  @showed_hints << @breaker_numbers_copy.pop
  @showed_hints.last
end

#lose?(result) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/codebreaker_vk/game.rb', line 41

def lose?(result)
  @attempts == 1 && @breaker_numbers != result
end

#start_round(user_input) ⇒ Object



23
24
25
26
27
# File 'lib/codebreaker_vk/game.rb', line 23

def start_round(user_input)
  @attempts -= 1
  @game_numbers = { code: @breaker_numbers.clone, input: user_input }
  collect_place_guess + collect_presence_guess
end

#to_hObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/codebreaker_vk/game.rb', line 45

def to_h
  {
    player_name: @player_name,
    level: @difficulty[:level],
    all_hints: @difficulty[:hints],
    all_attempts: @difficulty[:attempts],
    left_hints: @hints,
    left_attempts: @attempts,
    date: Time.now
  }
end

#win?(result) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/codebreaker_vk/game.rb', line 37

def win?(result)
  @breaker_numbers == result
end