Class: CodebreakerRuban::Game

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/app/entities/game.rb

Constant Summary collapse

LENGTH_GUESS =
4
RANGE_SECRET_CODE =
(1..6).freeze
EASY =
{ hints_total: 2, attempts_total: 15, difficulty: 'easy' }.freeze
MEDIUM =
{ hints_total: 1, attempts_total: 10, difficulty: 'medium' }.freeze
HELL =
{ hints_total: 1, attempts_total: 5, difficulty: 'hell' }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validation

#check_number, #size, #validate_range_lenght

Constructor Details

#initialize(difficulty, user) ⇒ Game

Returns a new instance of Game.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/app/entities/game.rb', line 15

def initialize(difficulty, user)
  @user = user
  @difficulty = difficulty[:difficulty]
  @secret_code = generator_secret_code
  @attempts_total = difficulty[:attempts_total]
  @hints_total = difficulty[:hints_total]
  @hint = secret_code.clone.shuffle
  @hints_used = 0
  @attempts_used = 0
  @datetime = Time.now
  @errors = []
end

Instance Attribute Details

#attempts_totalObject (readonly)

Returns the value of attribute attempts_total.



6
7
8
# File 'lib/app/entities/game.rb', line 6

def attempts_total
  @attempts_total
end

#attempts_usedObject

Returns the value of attribute attempts_used.



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

def attempts_used
  @attempts_used
end

#datetimeObject (readonly)

Returns the value of attribute datetime.



6
7
8
# File 'lib/app/entities/game.rb', line 6

def datetime
  @datetime
end

#difficultyObject (readonly)

Returns the value of attribute difficulty.



6
7
8
# File 'lib/app/entities/game.rb', line 6

def difficulty
  @difficulty
end

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

#hints_totalObject (readonly)

Returns the value of attribute hints_total.



6
7
8
# File 'lib/app/entities/game.rb', line 6

def hints_total
  @hints_total
end

#hints_usedObject

Returns the value of attribute hints_used.



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

def hints_used
  @hints_used
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



6
7
8
# File 'lib/app/entities/game.rb', line 6

def secret_code
  @secret_code
end

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'lib/app/entities/game.rb', line 6

def user
  @user
end

Instance Method Details

#check_attempsObject



34
35
36
37
# File 'lib/app/entities/game.rb', line 34

def check_attemps
  @attempts_used += 1
  @attempts_used != @attempts_total
end

#check_user_input(input) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/app/entities/game.rb', line 39

def check_user_input(input)
  input = transform_in_array(input)
  result = []
  secret_arr = secret_code.clone
  user_arr = input.clone
  check_plus(user_arr, secret_arr, result).compact!
  check_minus(user_arr, secret_arr, result)
  [result].join('')
end

#hint_useObject



49
50
51
52
53
54
55
56
57
# File 'lib/app/entities/game.rb', line 49

def hint_use
  if @hints_used < @hints_total
    @hints_used += 1
    @hint.pop
  else
    clear_errors
    errors << Message.error_guess_code
  end
end

#to_hObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/app/entities/game.rb', line 59

def to_h
  {
    name: @user.name.capitalize,
    difficulty: @difficulty,
    attempts_used: @attempts_used,
    attempts_total: @attempts_total,
    hints_total: @hints_total,
    hints_used: @hints_used
  }
end

#validate_guess_input(input) ⇒ Object



28
29
30
31
32
# File 'lib/app/entities/game.rb', line 28

def validate_guess_input(input)
  clear_errors
  return errors << Message.error_guess_code unless validate_range_lenght(input, LENGTH_GUESS)
  return errors << Message.error_guess_code unless check_number(transform_in_array(input), RANGE_SECRET_CODE)
end