codebreaker_kirill 1.0.0 → 1.1.0

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: 03bddb3432e90811fc0b774863524cc77b31d8d6ef333f40294a823558d7f952
4
- data.tar.gz: 5e5c9fe328e24f3418ae00f06a1a533f95ce103557f302e291dfbc4782a107fb
3
+ metadata.gz: ea8d9499d3f3afaf3a663f2f5d2d393843be5bb29eef1cd4cc769fdbd08e5d4c
4
+ data.tar.gz: b465bcb3d9eefe5fc004a2c98cbc174979985b1d363d0150f445ce5a1343065c
5
5
  SHA512:
6
- metadata.gz: e70b37a7b95ceca8e465524bda082bb6def70b9292ff7caced08acc62e91449abf52ea97e60101ac100121a01a5c68d505c0a1ac26ca1dda035287bd2405b820
7
- data.tar.gz: 1649b7b0235f9a90e860ba341d0db3eb0f6b5fe35a7d98ad9352a49b0e2fb68c246e7b61623bc56408bca7e16d015b10ba0444898b52e1bffd3a17ceac43acb3
6
+ metadata.gz: c440ac19bd13a5678d0cdfa24e94b04d278cf770fae412e64d77e718004f17f6031b114cca8764439435015386b0a6a0337cd331c1abf0810ca64e03a1ed601b
7
+ data.tar.gz: '05239e74c1dacb9277e2e3a2ceb879938624e482c52757148f74346b59a9dab9f638d10aa3221a7a6e44ae8a6d3b236f5da7fd832a56bbc9292bfe2290a24087'
@@ -7,7 +7,7 @@ require_relative 'user'
7
7
  class Game
8
8
  include CodeGenerator
9
9
 
10
- attr_reader :secret_code, :user
10
+ attr_reader :user
11
11
 
12
12
  def initialize(user)
13
13
  @secret_code = CodeGenerator.call
@@ -15,9 +15,27 @@ class Game
15
15
  end
16
16
 
17
17
  def give_a_hint
18
- return 0 if @user.hints[:used] >= @user.hints[:all]
18
+ return nil if @user.hints[:used] >= @user.hints[:all]
19
19
 
20
20
  @user.hints[:used] += 1
21
- @secret_code[@user.hints[:used]]
21
+ @secret_code.shuffle[@user.hints[:used]]
22
+ end
23
+
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
30
+
31
+ def attempts_handler
32
+ @user.attempts[:used] += 1
33
+ end
34
+
35
+ def status(input, code)
36
+ return 'loss' if @user.attempts[:used] >= @user.attempts[:all]
37
+ return 'win' if input.each_char.map(&:to_i) == code
38
+
39
+ 'in_game'
22
40
  end
23
41
  end
@@ -1,44 +1,40 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'autoload'
4
- require_relative 'game'
5
4
 
6
5
  class GuessHandler
7
- def respond_to_guess(game, input, code)
6
+ def initialize(input, code)
8
7
  validation(input)
9
- return game.give_a_hint if input == 'hint'
10
-
8
+ @input = input.each_char.map(&:to_i)
9
+ @code = code.clone
11
10
  @result = []
12
- @code_clone = code.clone
13
- @numbers = input.split('').map(&:to_i)
14
- return 'win' if @numbers == code
11
+ end
15
12
 
16
- assess_guess(@code_clone, @numbers)
17
- attempts_handler(game)
13
+ def call
14
+ add_pluses(@input, @code)
15
+ add_minuses(@input, @code)
16
+ @result
18
17
  end
19
18
 
20
- def assess_guess(code, input)
21
- @numbers.each_with_index do |element, index|
22
- if element == code[index]
23
- code[index] = '+'
24
- input[@numbers.index(element)] = '' end
25
- end
19
+ def add_pluses(input, code)
20
+ input.each_with_index do |element, index|
21
+ next unless element == code[index]
26
22
 
27
- @result = code.each_with_index do |el, index|
28
- if @numbers.include?(el)
29
- code[index] = '-'
30
- input.slice!(@numbers.index(el)) end
23
+ @result << Settings::POSITIVE
24
+ input[input.find_index(element)] = ''
25
+ code[index] = ''
31
26
  end
27
+ input.delete('')
28
+ code.delete('')
32
29
  end
33
30
 
34
- def attempts_handler(game)
35
- game.user.attempts[:used] += 1
36
- if game.user.attempts[:used] >= game.user.attempts[:all]
37
- 'loss'
38
- else
39
- @result.delete_if do |value|
40
- (1..6).include?(value)
41
- end.sort
31
+ def add_minuses(input, code)
32
+ input.each_with_index do |element, _index|
33
+ next unless code.include?(element)
34
+
35
+ @result << Settings::NEGATIVE
36
+ input[input.find_index(element)] = ''
37
+ code[code.find_index(element)] = ''
42
38
  end
43
39
  end
44
40
 
@@ -8,4 +8,6 @@ class Settings
8
8
  }.freeze
9
9
  CODE_LENGTH = 4
10
10
  RANDOM_RANGE = (1..6).freeze
11
+ POSITIVE = '+'
12
+ NEGATIVE = '-'
11
13
  end
@@ -22,7 +22,7 @@ class Stats
22
22
  def self.stats_format(user, index)
23
23
  index += 1
24
24
  puts "Rating: #{index}", "Name: #{user.name}",
25
- "Difficulty: #{Settings::DIFFICULTY.key({ attempts: user.attempts[:all], hints: user.hints[:all] })}",
25
+ "Difficulty: #{user.difficulty}",
26
26
  "Available Attempts: #{user.attempts[:all]}",
27
27
  "Used Attempts: #{user.attempts[:used]}",
28
28
  "Available Hints: #{user.hints[:all]}",
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'autoload'
3
4
  require_relative 'settings'
4
5
 
5
6
  class User
@@ -7,17 +8,15 @@ class User
7
8
 
8
9
  def initialize(name, difficulty)
9
10
  validation(name, difficulty)
11
+ @difficulty = difficulty
10
12
  @name = name
11
- @difficulty = Settings::DIFFICULTY[difficulty]
12
- @attempts = { all: @difficulty[:attempts], used: 0 }
13
- @hints = { all: @difficulty[:hints], used: 0 }
13
+ @level = Settings::DIFFICULTY[@difficulty]
14
+ @attempts = { all: @level[:attempts], used: 0 }
15
+ @hints = { all: @level[:hints], used: 0 }
14
16
  end
15
17
 
16
18
  def validation(name, difficulty)
17
19
  Validations.validate_name(name)
18
20
  Validations.validate_difficulty(difficulty)
19
- rescue StandardError => e
20
- puts e.message
21
- nil
22
21
  end
23
22
  end
@@ -16,7 +16,7 @@ class Validations
16
16
 
17
17
  def self.validate_difficulty(difficulty)
18
18
  raise "Input shouldn't be empty" if difficulty.empty?
19
- return if difficulty.match?(/easy|medium|hell/)
19
+ return if Settings::DIFFICULTY.keys.include?(difficulty)
20
20
 
21
21
  raise 'You should enter one of the following options: easy, medium, hell'
22
22
  end
@@ -3,5 +3,5 @@
3
3
  require_relative 'game'
4
4
 
5
5
  module CodebreakerKirill
6
- VERSION = '1.0.0'
6
+ VERSION = '1.1.0'
7
7
  end
@@ -6,8 +6,3 @@ require_relative 'codebreaker_kirill/user'
6
6
  require_relative 'codebreaker_kirill/guess_handler'
7
7
  require_relative 'codebreaker_kirill/settings'
8
8
  require_relative 'codebreaker_kirill/stats'
9
-
10
- module CodebreakerKirill
11
- class Error < StandardError; end
12
- # Your code goes here...
13
- end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebreaker_kirill
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Dudchenko
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-22 00:00:00.000000000 Z
11
+ date: 2022-08-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description:
13
+ description:
14
14
  email:
15
15
  - dudchenko.kirill.1995@gmail.com
16
16
  executables: []
@@ -36,11 +36,11 @@ files:
36
36
  - lib/codebreaker_kirill/validations.rb
37
37
  - lib/codebreaker_kirill/version.rb
38
38
  - sig/codebreaker_kirill.rbs
39
- homepage:
39
+ homepage:
40
40
  licenses:
41
41
  - MIT
42
42
  metadata: {}
43
- post_install_message:
43
+ post_install_message:
44
44
  rdoc_options: []
45
45
  require_paths:
46
46
  - lib
@@ -55,8 +55,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
57
  requirements: []
58
- rubygems_version: 3.3.20
59
- signing_key:
58
+ rubygems_version: 3.2.3
59
+ signing_key:
60
60
  specification_version: 4
61
61
  summary: Logic game
62
62
  test_files: []