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
DIFFICULTY =
{
  easy: { hints_total: 2, attempts_total: 15, difficulty: 'easy' },
  medium: { hints_total: 1, attempts_total: 10, difficulty: 'medium' },
  hell: { hints_total: 1, attempts_total: 5, difficulty: 'hell' }
}

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.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/app/entities/game.rb', line 17

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
  @hints_showed = []
  @datetime = Time.now
  @guess_code = nil
  @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

#guess_codeObject

Returns the value of attribute guess_code.



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

def guess_code
  @guess_code
end

#hints_showedObject (readonly)

Returns the value of attribute hints_showed.



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

def hints_showed
  @hints_showed
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



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

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

#check_user_input(input) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/app/entities/game.rb', line 43

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



53
54
55
56
57
58
59
60
61
# File 'lib/app/entities/game.rb', line 53

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

#to_hObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/app/entities/game.rb', line 63

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



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

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