codebreaker_kirill 1.1.1 → 1.2.1

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: f3ae216b2fbb6ce449e709441baf3ea61bc5ba82b8f6a84f401366c493f4d901
4
- data.tar.gz: f5938755f2f1e776a9344f47b07dd248ef93469e9d88635568bdc237070fa3c8
3
+ metadata.gz: afc87eab226b7b01059d904163528772616e89ad35530d975ab498adf20b740d
4
+ data.tar.gz: 2d2414828d7e4e146b96814b30b4629f52152ddcdd7feca3ba2bdc3f4a7dad70
5
5
  SHA512:
6
- metadata.gz: f2d2d57bbb54dff8a28de034744994be16619ba93ee6e072d99b7fc985ccafc564f5b36ab6df4d8b1ef2f9b8ecea5c7941709229da06a59bf747469abde56ec2
7
- data.tar.gz: cb2a0e47bb4fb4654d6ae37b6fd16a36c20b0ef43af9cb23c2d042d4a318d27891537f28f4ec0736b43c1aabaa6f0381b5ffc07e220835d72830566415a42bd3
6
+ metadata.gz: 4d2454ef0c9edb8ca8db36258a867fa1a0a133303524bd1b5cba537a7bb30c218d711b00be84b5a76b13f498b5b27c55ac2157b6484e441abdccd29922756d51
7
+ data.tar.gz: 9c30dd02139d489491c1285d11e4981cccffdb0b819fd71ae09e2c8331557e724b43447d9cf00a7d7e96184a7b96ce4c96483294512953b8f4fcf186d896d133
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,21 @@ 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
22
  def respond_to_guess(input)
25
- @user.attempts[:used] += 1
23
+ @user.attempts_used += 1
26
24
  status = status(input, @secret_code)
27
25
  resolved_code = GuessHandler.new(input, @secret_code).call
28
26
  { status: status, response: resolved_code }
29
27
  end
30
28
 
31
- def attempts_handler
32
- @user.attempts[:used] += 1
33
- end
34
-
35
29
  def status(input, code)
36
- return 'loss' if @user.attempts[:used] >= @user.attempts[:all]
30
+ return 'loss' if @user.attempts_used >= @user.level[:attempts]
37
31
  return 'win' if input.each_char.map(&:to_i) == code
38
32
 
39
33
  'in_game'
@@ -4,46 +4,33 @@ require_relative 'autoload'
4
4
 
5
5
  class GuessHandler
6
6
  def initialize(input, code)
7
- validation(input)
7
+ Validations.validate_guess(input)
8
8
  @input = input.each_char.map(&:to_i)
9
9
  @code = code.clone
10
10
  @result = []
11
11
  end
12
12
 
13
13
  def call
14
- add_pluses(@input, @code)
15
- add_minuses(@input, @code)
14
+ check_same_indexes
15
+ check_different_indexes
16
16
  @result
17
17
  end
18
18
 
19
- def add_pluses(input, code)
20
- input.each_with_index do |element, index|
21
- next unless element == code[index]
19
+ def check_same_indexes
20
+ @input.each_index do |index|
21
+ next unless @input[index] == @code[index]
22
22
 
23
- @result << Settings::POSITIVE
24
- input[input.find_index(element)] = ''
25
- code[index] = ''
23
+ @result << Settings::POSITIVE_RESPONSE
24
+ @input[index], @code[index] = nil
26
25
  end
27
- input.delete('')
28
- code.delete('')
29
26
  end
30
27
 
31
- def add_minuses(input, code)
32
- input.each_with_index do |element, _index|
33
- next unless code.include?(element)
28
+ def check_different_indexes
29
+ @input.each_with_index do |value, _index|
30
+ next unless !value.nil? && @code.include?(value)
34
31
 
35
- @result << Settings::NEGATIVE
36
- input[input.find_index(element)] = ''
37
- code[code.find_index(element)] = ''
32
+ @result << Settings::NEGATIVE_RESPONSE
33
+ @input[@input.find_index(value)], @code[@code.find_index(value)] = nil
38
34
  end
39
35
  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
36
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'autoload'
4
+
3
5
  class Settings
4
6
  DIFFICULTY = {
5
7
  'easy' => { attempts: 15, hints: 2 },
@@ -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,18 @@
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
+ attr_accessor :attempts_used, :hints_used
7
+ attr_reader :difficulty, :name, :level
8
8
 
9
9
  def initialize(name, difficulty)
10
10
  validation(name, difficulty)
11
11
  @difficulty = difficulty
12
12
  @name = name
13
13
  @level = Settings::DIFFICULTY[@difficulty]
14
- @attempts = { all: @level[:attempts], used: 0 }
15
- @hints = { all: @level[:hints], used: 0 }
14
+ @attempts_used = 0
15
+ @hints_used = 0
16
16
  end
17
17
 
18
18
  def validation(name, difficulty)
@@ -1,23 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'autoload'
4
+
3
5
  class Validations
4
6
  def self.validate_name(name)
5
7
  raise "Name shouldn't be empty" if name.empty?
6
8
  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
9
+ raise 'Name should be at least 3 characters long' if name.length < Settings::NAME_MIN_LENGTH
10
+ raise "Name shouldn't be more than 20 characters long" if name.length > Settings::NAME_MAX_LENGTH
9
11
  end
10
12
 
11
13
  def self.validate_guess(guess)
12
14
  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
15
+ raise "Guess should be 4 characters. Got #{guess}" if guess.length < Settings::GUESS_LENGTH
16
+ raise 'Guess shouldn\'t be more than 4 characters long' if guess.length > Settings::GUESS_LENGTH
15
17
  end
16
18
 
17
19
  def self.validate_difficulty(difficulty)
18
20
  raise "Input shouldn't be empty" if difficulty.empty?
19
21
  return if Settings::DIFFICULTY.keys.include?(difficulty)
20
22
 
21
- raise 'You should enter one of the following options: easy, medium, hell'
23
+ raise "You should enter one of the following options: #{Settings::DIFFICULTY.keys.join(', ')}"
22
24
  end
23
25
  end
@@ -3,5 +3,5 @@
3
3
  require_relative 'game'
4
4
 
5
5
  module CodebreakerKirill
6
- VERSION = '1.1.1'
6
+ VERSION = '1.2.1'
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.1
4
+ version: 1.2.1
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-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: