alex_codebreaker 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f18cfa67f231f11172b1ba9a7d7a7b0cb1422388f19035108d2aa89f82c01efa
4
- data.tar.gz: 37623313c45504b3946bef46091f0075fb22041f693ea95990d058081225a455
3
+ metadata.gz: 26769c84cb57b5f1cd0c183b113399ed6ed2d552bcefc28d1ecf2af8dbb4de6a
4
+ data.tar.gz: be13615fb5241f768943b66d5e78f8ae8fb0a2211e26aad4916ee0d22281dabc
5
5
  SHA512:
6
- metadata.gz: 5839469508281a0062cd4c716627a0c735056e3fd288d9ddb7a28be4947fc630346a903da1315b90a43764b5d1b52ec4fa8d123121ffcc6ac8fd7fc808706ce8
7
- data.tar.gz: b14f877f74a5e2a2f9598c04a2a7d487082a5480570b6a5012ef9d366600ea010495005127554f5adb6109804f34c9eef4d98be1475a6481f8d26a3ea32f1f5b
6
+ metadata.gz: 89f4e1efaa071d08e2e7ad1f12f2dc8bcdefea304b3efd68d2268b6398104e16e51579c800c0a6254a233c623b09742180624246943f17060f3d6b58e21128d0
7
+ data.tar.gz: b291bb4605a14c54bc45a99b23dd3b63ac64e26c9e378d6a331f81df09d923ed1d6c89059d15bc26b3ea3e859f7ecaf923e43161cb1fcf913e98394cf87bf3c6
@@ -1,17 +1,10 @@
1
1
  require_relative 'config/gem.rb'
2
2
  require_relative 'alex_codebreaker/version'
3
3
  require_relative 'alex_codebreaker/modules/files'
4
- require_relative 'alex_codebreaker/modules/game_messages'
4
+ require_relative 'alex_codebreaker/modules/settings'
5
5
  require_relative 'alex_codebreaker/modules/difficulty_levels'
6
6
  require_relative 'alex_codebreaker/modules/validators/validators'
7
7
  require_relative 'alex_codebreaker/modules/validators/arguments_validation'
8
- require_relative 'alex_codebreaker/exceptions/file_does_not_exist'
9
- require_relative 'alex_codebreaker/exceptions/invalid_digit'
10
- require_relative 'alex_codebreaker/exceptions/invalid_length'
11
- require_relative 'alex_codebreaker/exceptions/no_attempts_left'
12
- require_relative 'alex_codebreaker/exceptions/no_hints_left'
13
- require_relative 'alex_codebreaker/exceptions/wrong_class'
14
- require_relative 'alex_codebreaker/exceptions/wrong_difficulty'
15
8
  require_relative 'alex_codebreaker/game'
16
9
  require_relative 'alex_codebreaker/players_rating'
17
10
  require_relative 'alex_codebreaker/session'
@@ -4,13 +4,13 @@ class Game
4
4
  attr_reader :session
5
5
 
6
6
  def start
7
- @secret_code = Array.new(4) { rand(1..6) }
7
+ @secret_code = Array.new(Settings::CODE_LENGTH) { rand(Settings::CODE_MIN_DIGIT..Settings::CODE_MAX_DIGIT) }
8
8
  @session = Session.new
9
9
  @win_status = false
10
10
  end
11
11
 
12
12
  def add_player_name(given_name)
13
- @session.player_name = name_validation(given_name)
13
+ name_validation(given_name) ? @session.player_name = given_name : false
14
14
  end
15
15
 
16
16
  def choose_difficulty(chosen_difficulty)
@@ -18,7 +18,7 @@ class Game
18
18
  end
19
19
 
20
20
  def hint
21
- raise NoHintsLeft if @session.hints_used >= @session.hints_total
21
+ return false if @session.hints_used >= @session.hints_total
22
22
 
23
23
  @session.hints_used += 1
24
24
  @secret_code_for_hint ||= @secret_code.clone.uniq
@@ -26,12 +26,13 @@ class Game
26
26
  end
27
27
 
28
28
  def guess(user_input)
29
- guess_validation(user_input)
30
- raise NoAttemptsLeft if @session.attempts_used >= @session.attempts_total
29
+ return false unless guess_validation(user_input)
30
+
31
+ return I18n.t(:no_attempts_left_message) if @session.attempts_used >= @session.attempts_total
31
32
 
32
33
  @session.attempts_used += 1
33
- @win_status = true if comparator(user_input.to_s) == GameMessages::WIN_CONDITION
34
- comparator(user_input.to_s)
34
+ @win_status = true if user_input.to_i == @secret_code.join.to_i
35
+ comparator(user_input)
35
36
  end
36
37
 
37
38
  def save_game
@@ -48,17 +49,19 @@ class Game
48
49
  user_input = user_input.chars.map!(&:to_i)
49
50
  @secret_code_for_comparison = @secret_code.clone
50
51
  user_input = matching_numbers_and_indexes(user_input)
52
+
51
53
  user_input = matching_only_numbers(user_input).sort
52
- user_input.rotate(user_input.count(' ')).join
54
+
55
+ user_input.rotate(user_input.count(Settings::MATCHING[:absent])).join
53
56
  end
54
57
 
55
58
  def matching_numbers_and_indexes(user_input)
56
59
  user_input.map.with_index do |value, index|
57
60
  if value == @secret_code_for_comparison[index]
58
61
  @secret_code_for_comparison[index] = nil
59
- GameMessages::MATCHING[:number_and_index]
62
+ Settings::MATCHING[:place]
60
63
  elsif !@secret_code_for_comparison.include?(value)
61
- GameMessages::MATCHING[:no_matching]
64
+ Settings::MATCHING[:absent]
62
65
  else
63
66
  value
64
67
  end
@@ -68,12 +71,12 @@ class Game
68
71
  def matching_only_numbers(user_input)
69
72
  user_input.map do |value|
70
73
  if @secret_code_for_comparison.include?(value)
71
- @secret_code_for_comparison.delete(value)
72
- GameMessages::MATCHING[:only_number]
73
- elsif [GameMessages::MATCHING[:number_and_index], GameMessages::MATCHING[:no_matching]].include?(value)
74
+ @secret_code_for_comparison.delete_at(@secret_code_for_comparison.index(value))
75
+ Settings::MATCHING[:presence]
76
+ elsif [Settings::MATCHING[:place], Settings::MATCHING[:absent]].include?(value)
74
77
  value
75
78
  else
76
- GameMessages::MATCHING[:no_matching]
79
+ Settings::MATCHING[:absent]
77
80
  end
78
81
  end
79
82
  end
@@ -1,7 +1,7 @@
1
1
  module DifficultyLevels
2
2
  DIFFICULTY_LEVELS = {
3
3
  Easy: {
4
- name: :easy,
4
+ name: :Easy,
5
5
  level: 0,
6
6
  attempts_total: 15,
7
7
  hints_total: 2
@@ -0,0 +1,8 @@
1
+ module Settings
2
+ MATCHING = { place: '+', presence: '-', absent: ' ' }.freeze
3
+ CODE_MIN_DIGIT = 1
4
+ CODE_MAX_DIGIT = 6
5
+ CODE_LENGTH = 4
6
+ MIN_PLAYER_NAME_LENGTH = 3
7
+ MAX_PLAYER_NAME_LENGTH = 20
8
+ end
@@ -2,15 +2,12 @@ module ArgumentsValidation
2
2
  include Validators
3
3
 
4
4
  def name_validation(name)
5
- argument_instance_check(name, String)
6
- argument_length_check(name, max: 20, min: 3)
7
- name
5
+ argument_min_length_check(name, Settings::MIN_PLAYER_NAME_LENGTH) && \
6
+ argument_max_length_check(name, Settings::MAX_PLAYER_NAME_LENGTH)
8
7
  end
9
8
 
10
9
  def guess_validation(guess)
11
- argument_instance_check(guess, Integer)
12
- argument_fixed_length_check(guess, 4)
13
- digits_check(guess)
14
- guess
10
+ argument_fixed_length_check(guess, Settings::CODE_LENGTH) && \
11
+ digits_check(guess, min_digit: Settings::CODE_MIN_DIGIT, max_digit: Settings::CODE_MAX_DIGIT)
15
12
  end
16
13
  end
@@ -1,19 +1,17 @@
1
1
  module Validators
2
- def argument_instance_check(arg, checking_class)
3
- raise WrongClass, (I18n.t(:value_instance_of) + checking_class.to_s) unless arg.instance_of?(checking_class)
2
+ def argument_fixed_length_check(arg, fixed_length)
3
+ arg.to_s.length == fixed_length
4
4
  end
5
5
 
6
- def argument_fixed_length_check(arg, fix_length)
7
- message = I18n.t(:invalid_argument_length) + fix_length.to_s + I18n.t(:given) + arg.to_s.length.to_s
8
- raise InvalidLength, message unless arg.to_s.length == fix_length
6
+ def argument_max_length_check(arg, max)
7
+ arg.length <= max
9
8
  end
10
9
 
11
- def argument_length_check(arg, max: nil, min: nil)
12
- raise InvalidLength, (I18n.t(:argument_length_long) + max.to_s) if max && arg.length > max
13
- raise InvalidLength, (I18n.t(:argument_length_short) + min.to_s) if min && arg.length < min
10
+ def argument_min_length_check(arg, min)
11
+ arg.length >= min
14
12
  end
15
13
 
16
- def digits_check(arg)
17
- raise InvalidDigit unless arg.to_s.split('').map { |value| value unless value.to_i.between?(1, 6) }.compact.empty?
14
+ def digits_check(arg, min_digit: 1, max_digit: 6)
15
+ arg.to_s.split('').map { |value| value unless value.to_i.between?(min_digit, max_digit) }.compact.empty?
18
16
  end
19
17
  end
@@ -8,7 +8,7 @@ class PlayersRating
8
8
  private
9
9
 
10
10
  def stats_loader
11
- raise FileDoesNotExist unless File.exist?(Files::STATS_FILE)
11
+ return @stats = false unless File.exist?(Files::STATS_FILE)
12
12
 
13
13
  load_stats
14
14
  sort_players_rating
@@ -1,18 +1,21 @@
1
1
  class Session
2
+ INITIAL_ATTEMPTS_USED = 0
3
+ INITIAL_HINTS_USED = 0
4
+
2
5
  attr_reader :difficulty_name, :difficulty_level, :attempts_total, :hints_total
3
6
 
4
7
  attr_accessor :player_name, :attempts_used, :hints_used
5
8
 
6
9
  def initialize
7
- @attempts_used = 0
8
- @hints_used = 0
10
+ @attempts_used = INITIAL_ATTEMPTS_USED
11
+ @hints_used = INITIAL_HINTS_USED
9
12
  end
10
13
 
11
14
  def difficulty_manager(difficulty)
12
15
  if DifficultyLevels::DIFFICULTY_LEVELS.include?(difficulty)
13
16
  add_difficulty(DifficultyLevels::DIFFICULTY_LEVELS[difficulty])
14
17
  end
15
- raise WrongDifficulty unless DifficultyLevels::DIFFICULTY_LEVELS.include?(difficulty)
18
+ return false unless DifficultyLevels::DIFFICULTY_LEVELS.include?(difficulty)
16
19
  end
17
20
 
18
21
  def save_session_statistic
@@ -1,3 +1,3 @@
1
1
  module AlexCodebreaker
2
- VERSION = '0.1.4'.freeze
2
+ VERSION = '0.1.5'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alex_codebreaker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleksandr Loza
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-04 00:00:00.000000000 Z
11
+ date: 2019-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -160,17 +160,10 @@ files:
160
160
  - LICENSE.txt
161
161
  - README.md
162
162
  - lib/alex_codebreaker.rb
163
- - lib/alex_codebreaker/exceptions/file_does_not_exist.rb
164
- - lib/alex_codebreaker/exceptions/invalid_digit.rb
165
- - lib/alex_codebreaker/exceptions/invalid_length.rb
166
- - lib/alex_codebreaker/exceptions/no_attempts_left.rb
167
- - lib/alex_codebreaker/exceptions/no_hints_left.rb
168
- - lib/alex_codebreaker/exceptions/wrong_class.rb
169
- - lib/alex_codebreaker/exceptions/wrong_difficulty.rb
170
163
  - lib/alex_codebreaker/game.rb
171
164
  - lib/alex_codebreaker/modules/difficulty_levels.rb
172
165
  - lib/alex_codebreaker/modules/files.rb
173
- - lib/alex_codebreaker/modules/game_messages.rb
166
+ - lib/alex_codebreaker/modules/settings.rb
174
167
  - lib/alex_codebreaker/modules/validators/arguments_validation.rb
175
168
  - lib/alex_codebreaker/modules/validators/validators.rb
176
169
  - lib/alex_codebreaker/players_rating.rb
@@ -1,5 +0,0 @@
1
- class FileDoesNotExist < StandardError
2
- def initialize(msg = I18n.t(:file_does_not_exist_message))
3
- super(msg)
4
- end
5
- end
@@ -1,5 +0,0 @@
1
- class InvalidDigit < StandardError
2
- def initialize(msg = I18n.t(:invalid_digit_message))
3
- super(msg)
4
- end
5
- end
@@ -1,5 +0,0 @@
1
- class InvalidLength < StandardError
2
- def initialize(msg = I18n.t(:invalid_length_message))
3
- super(msg)
4
- end
5
- end
@@ -1,5 +0,0 @@
1
- class NoAttemptsLeft < StandardError
2
- def initialize(msg = I18n.t(:no_attempts_left_message))
3
- super(msg)
4
- end
5
- end
@@ -1,5 +0,0 @@
1
- class NoHintsLeft < StandardError
2
- def initialize(msg = I18n.t(:no_hints_left_message))
3
- super(msg)
4
- end
5
- end
@@ -1,5 +0,0 @@
1
- class WrongClass < StandardError
2
- def initialize(msg = I18n.t(:wrong_class_message))
3
- super(msg)
4
- end
5
- end
@@ -1,5 +0,0 @@
1
- class WrongDifficulty < StandardError
2
- def initialize(msg = I18n.t(:wrong_difficulty_message))
3
- super(msg)
4
- end
5
- end
@@ -1,4 +0,0 @@
1
- module GameMessages
2
- WIN_CONDITION = '++++'.freeze
3
- MATCHING = { number_and_index: '+', only_number: '-', no_matching: ' ' }.freeze
4
- end