codebreaker_kirill 1.1.0 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea8d9499d3f3afaf3a663f2f5d2d393843be5bb29eef1cd4cc769fdbd08e5d4c
4
- data.tar.gz: b465bcb3d9eefe5fc004a2c98cbc174979985b1d363d0150f445ce5a1343065c
3
+ metadata.gz: 13184f386f8e6c28fce445b1bd7849afe194a85129b8ade51d8a72c58da0faae
4
+ data.tar.gz: a5c9fdd868c7f244304685f542fbc38cd9a238efb42e842b84043934762b3b32
5
5
  SHA512:
6
- metadata.gz: c440ac19bd13a5678d0cdfa24e94b04d278cf770fae412e64d77e718004f17f6031b114cca8764439435015386b0a6a0337cd331c1abf0810ca64e03a1ed601b
7
- data.tar.gz: '05239e74c1dacb9277e2e3a2ceb879938624e482c52757148f74346b59a9dab9f638d10aa3221a7a6e44ae8a6d3b236f5da7fd832a56bbc9292bfe2290a24087'
6
+ metadata.gz: 38cc6d0005882e01699e6997749d451f82f99c37c06f41bfb5f4a48a7159a38605b60d8c7e7fe9617147148e1ce544e9cfd74ad24643267a602fbd99920e1657
7
+ data.tar.gz: '09e7d0b555b122db1e004623082a695d1b62ff519b136eba6e35ac52073317bf788e8c30c76569b399eea4d24814ac00492f88c6a9389e33934f64c2fe65b029'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- codebreaker_kirill (0.3.2)
4
+ codebreaker_kirill (1.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'yaml'
4
- require_relative 'validations'
5
- require_relative 'guess_handler'
6
4
  require_relative 'settings'
7
5
  require_relative 'code_generator'
8
- require_relative 'stats'
6
+ require_relative 'validations'
7
+ require_relative 'guess_handler'
9
8
  require_relative 'user'
9
+ require_relative 'game'
10
+ require_relative 'stats'
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'autoload'
4
- require_relative 'code_generator'
5
- require_relative 'user'
6
4
 
7
5
  class Game
8
6
  include CodeGenerator
@@ -15,25 +13,23 @@ class Game
15
13
  end
16
14
 
17
15
  def give_a_hint
18
- return nil if @user.hints[:used] >= @user.hints[:all]
16
+ return nil if @user.hints_used >= @user.level[:hints]
19
17
 
20
- @user.hints[:used] += 1
21
- @secret_code.shuffle[@user.hints[:used]]
18
+ @user.hints_used += 1
19
+ @secret_code.shuffle[@user.hints_used]
22
20
  end
23
21
 
24
- def respond_to_guess(input, code)
25
- @user.attempts[:used] += 1
26
- status = status(input, code)
27
- resolved_code = GuessHandler.new(input, code).call
28
- { status: status, response: resolved_code }
29
- end
22
+ def respond_to_guess(input)
23
+ @user.attempts_used += 1
24
+ status = status(input, @secret_code)
25
+ resolved_code = GuessHandler.new(input, @secret_code).call
26
+ return { status: status, secret_code: @secret_code } if status == 'loss'
30
27
 
31
- def attempts_handler
32
- @user.attempts[:used] += 1
28
+ { status: status, response: resolved_code, secret_code: code }
33
29
  end
34
30
 
35
31
  def status(input, code)
36
- return 'loss' if @user.attempts[:used] >= @user.attempts[:all]
32
+ return 'loss' if @user.attempts_used >= @user.level[:attempts]
37
33
  return 'win' if input.each_char.map(&:to_i) == code
38
34
 
39
35
  'in_game'
@@ -3,47 +3,36 @@
3
3
  require_relative 'autoload'
4
4
 
5
5
  class GuessHandler
6
+ include Settings
7
+
6
8
  def initialize(input, code)
7
- validation(input)
9
+ Validations.validate_guess(input)
8
10
  @input = input.each_char.map(&:to_i)
9
11
  @code = code.clone
10
12
  @result = []
11
13
  end
12
14
 
13
15
  def call
14
- add_pluses(@input, @code)
15
- add_minuses(@input, @code)
16
+ check_same_indexes
17
+ check_different_indexes
16
18
  @result
17
19
  end
18
20
 
19
- def add_pluses(input, code)
20
- input.each_with_index do |element, index|
21
- next unless element == code[index]
21
+ def check_same_indexes
22
+ @input.each_index do |index|
23
+ next unless @input[index] == @code[index]
22
24
 
23
- @result << Settings::POSITIVE
24
- input[input.find_index(element)] = ''
25
- code[index] = ''
25
+ @result << Settings::POSITIVE_RESPONSE
26
+ @input[index], @code[index] = nil
26
27
  end
27
- input.delete('')
28
- code.delete('')
29
28
  end
30
29
 
31
- def add_minuses(input, code)
32
- input.each_with_index do |element, _index|
33
- next unless code.include?(element)
30
+ def check_different_indexes
31
+ @input.each_with_index do |value, _index|
32
+ next unless !value.nil? && @code.include?(value)
34
33
 
35
- @result << Settings::NEGATIVE
36
- input[input.find_index(element)] = ''
37
- code[code.find_index(element)] = ''
34
+ @result << Settings::NEGATIVE_RESPONSE
35
+ @input[@input.find_index(value)], @code[@code.find_index(value)] = nil
38
36
  end
39
37
  end
40
-
41
- private
42
-
43
- def validation(input)
44
- Validations.validate_guess(input)
45
- rescue StandardError => e
46
- puts e.message
47
- nil
48
- end
49
38
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Settings
3
+ require_relative 'autoload'
4
+
5
+ module Settings
4
6
  DIFFICULTY = {
5
7
  'easy' => { attempts: 15, hints: 2 },
6
8
  'medium' => { attempts: 10, hints: 1 },
@@ -8,6 +10,9 @@ class Settings
8
10
  }.freeze
9
11
  CODE_LENGTH = 4
10
12
  RANDOM_RANGE = (1..6).freeze
11
- POSITIVE = '+'
12
- NEGATIVE = '-'
13
+ POSITIVE_RESPONSE = '+'
14
+ NEGATIVE_RESPONSE = '-'
15
+ NAME_MIN_LENGTH = 3
16
+ NAME_MAX_LENGTH = 20
17
+ GUESS_LENGTH = 4
13
18
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'yaml'
4
- require_relative 'user'
3
+ require_relative 'autoload'
5
4
 
6
5
  class Stats
7
6
  def self.save_game(user)
@@ -13,20 +12,6 @@ class Stats
13
12
 
14
13
  def self.show_stats
15
14
  @data = File.exist?('stats.yml') ? YAML.load_file('stats.yml') : []
16
- @data.sort_by! { |user| [user.attempts[:all], user.attempts[:used], user.hints[:used]] }
17
- @data.each_with_index do |user, index|
18
- stats_format(user, index)
19
- end
20
- end
21
-
22
- def self.stats_format(user, index)
23
- index += 1
24
- puts "Rating: #{index}", "Name: #{user.name}",
25
- "Difficulty: #{user.difficulty}",
26
- "Available Attempts: #{user.attempts[:all]}",
27
- "Used Attempts: #{user.attempts[:used]}",
28
- "Available Hints: #{user.hints[:all]}",
29
- "Used Hints: #{user.hints[:used]}",
30
- '-----------------------'
15
+ @data.sort_by! { |user| [user.level[:attempts], user.attempts_used, user.hints_used] }
31
16
  end
32
17
  end
@@ -1,18 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'autoload'
4
- require_relative 'settings'
5
4
 
6
5
  class User
7
- attr_reader :name, :difficulty, :attempts, :hints
6
+ include Settings
7
+
8
+ attr_accessor :attempts_used, :hints_used
9
+ attr_reader :difficulty, :name, :level
8
10
 
9
11
  def initialize(name, difficulty)
10
12
  validation(name, difficulty)
11
13
  @difficulty = difficulty
12
14
  @name = name
13
15
  @level = Settings::DIFFICULTY[@difficulty]
14
- @attempts = { all: @level[:attempts], used: 0 }
15
- @hints = { all: @level[:hints], used: 0 }
16
+ @attempts_used = 0
17
+ @hints_used = 0
16
18
  end
17
19
 
18
20
  def validation(name, difficulty)
@@ -1,23 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'autoload'
4
+
3
5
  class Validations
6
+ include Settings
7
+
4
8
  def self.validate_name(name)
5
9
  raise "Name shouldn't be empty" if name.empty?
6
10
  raise 'Name should be string' if name.class != String
7
- raise 'Name should be at least 3 characters long' if name.length < 3
8
- raise "Name shouldn't be more than 20 characters long" if name.length > 20
11
+ raise 'Name should be at least 3 characters long' if name.length < Settings::NAME_MIN_LENGTH
12
+ raise "Name shouldn't be more than 20 characters long" if name.length > Settings::NAME_MAX_LENGTH
9
13
  end
10
14
 
11
15
  def self.validate_guess(guess)
12
16
  raise "Guess shouldn't be empty" if guess.empty?
13
- raise 'Guess should be at least 3 characters long' if guess.length < 4
14
- raise 'Guess shouldn\'t be more than 4 characters long' if guess.length > 4
17
+ raise "Guess should be 4 characters. Got #{guess}" if guess.length < Settings::GUESS_LENGTH
18
+ raise 'Guess shouldn\'t be more than 4 characters long' if guess.length > Settings::GUESS_LENGTH
15
19
  end
16
20
 
17
21
  def self.validate_difficulty(difficulty)
18
22
  raise "Input shouldn't be empty" if difficulty.empty?
19
23
  return if Settings::DIFFICULTY.keys.include?(difficulty)
20
24
 
21
- raise 'You should enter one of the following options: easy, medium, hell'
25
+ raise "You should enter one of the following options: #{Settings::DIFFICULTY.keys.join(', ')}"
22
26
  end
23
27
  end
@@ -3,5 +3,5 @@
3
3
  require_relative 'game'
4
4
 
5
5
  module CodebreakerKirill
6
- VERSION = '1.1.0'
6
+ VERSION = '1.2.3'
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebreaker_kirill
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Dudchenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-24 00:00:00.000000000 Z
11
+ date: 2022-08-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: