codebreaker_kirill 1.0.2 → 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: 28e0e679b239448c2ecb1a85533cd95911ab43b0ae2e6e43335e729ec9afbaf7
4
- data.tar.gz: 3a525a2ef7ca7695e50616d65465daf74a1397b96a80f58d6fc71a40b06705a0
3
+ metadata.gz: afc87eab226b7b01059d904163528772616e89ad35530d975ab498adf20b740d
4
+ data.tar.gz: 2d2414828d7e4e146b96814b30b4629f52152ddcdd7feca3ba2bdc3f4a7dad70
5
5
  SHA512:
6
- metadata.gz: df757d08d82c326d37dd964d7e769aab908b4208ff9e887d865bfdcc4cd81a3764b49885bf5cfa100bf1bbfbf1a978ce5e9fe0166c157486ca53da046918559e
7
- data.tar.gz: a8e97a8eb267fe20f7d6452c478fbcf12cc9b53bdca50479e66729ca17182b750ffab02ac4fc26f4ee1f61eb0a872ce30ed4f38fe1fdcba48f40d08c2f51fb99
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,13 +1,11 @@
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
9
7
 
10
- attr_reader :secret_code, :user
8
+ attr_reader :user
11
9
 
12
10
  def initialize(user)
13
11
  @secret_code = CodeGenerator.call
@@ -15,9 +13,23 @@ class Game
15
13
  end
16
14
 
17
15
  def give_a_hint
18
- return 0 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[@user.hints[:used]]
18
+ @user.hints_used += 1
19
+ @secret_code.shuffle[@user.hints_used]
20
+ end
21
+
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
+ { status: status, response: resolved_code }
27
+ end
28
+
29
+ def status(input, code)
30
+ return 'loss' if @user.attempts_used >= @user.level[:attempts]
31
+ return 'win' if input.each_char.map(&:to_i) == code
32
+
33
+ 'in_game'
22
34
  end
23
35
  end
@@ -1,53 +1,36 @@
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)
8
- validation(input)
9
- return game.give_a_hint if input == 'hint'
10
-
6
+ def initialize(input, code)
7
+ Validations.validate_guess(input)
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
15
-
16
- assess_guess(@code_clone, @numbers)
17
- attempts_handler(game)
18
11
  end
19
12
 
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
26
-
27
- @result = code.each_with_index do |el, index|
28
- if @numbers.include?(el)
29
- code[index] = '-'
30
- input.slice!(@numbers.index(el)) end
31
- end
13
+ def call
14
+ check_same_indexes
15
+ check_different_indexes
16
+ @result
32
17
  end
33
18
 
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
19
+ def check_same_indexes
20
+ @input.each_index do |index|
21
+ next unless @input[index] == @code[index]
22
+
23
+ @result << Settings::POSITIVE_RESPONSE
24
+ @input[index], @code[index] = nil
42
25
  end
43
26
  end
44
27
 
45
- private
28
+ def check_different_indexes
29
+ @input.each_with_index do |value, _index|
30
+ next unless !value.nil? && @code.include?(value)
46
31
 
47
- def validation(input)
48
- Validations.validate_guess(input)
49
- rescue StandardError => e
50
- puts e.message
51
- nil
32
+ @result << Settings::NEGATIVE_RESPONSE
33
+ @input[@input.find_index(value)], @code[@code.find_index(value)] = nil
34
+ end
52
35
  end
53
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,4 +10,9 @@ class Settings
8
10
  }.freeze
9
11
  CODE_LENGTH = 4
10
12
  RANDOM_RANGE = (1..6).freeze
13
+ POSITIVE_RESPONSE = '+'
14
+ NEGATIVE_RESPONSE = '-'
15
+ NAME_MIN_LENGTH = 3
16
+ NAME_MAX_LENGTH = 20
17
+ GUESS_LENGTH = 4
11
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: #{Settings::DIFFICULTY.key({ attempts: user.attempts[:all], hints: user.hints[:all] })}",
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,24 +1,22 @@
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
+ @difficulty = difficulty
11
12
  @name = name
12
- @difficulty = Settings::DIFFICULTY[difficulty]
13
- @attempts = { all: @difficulty[:attempts], used: 0 }
14
- @hints = { all: @difficulty[:hints], used: 0 }
13
+ @level = Settings::DIFFICULTY[@difficulty]
14
+ @attempts_used = 0
15
+ @hints_used = 0
15
16
  end
16
17
 
17
18
  def validation(name, difficulty)
18
19
  Validations.validate_name(name)
19
20
  Validations.validate_difficulty(difficulty)
20
- rescue StandardError => e
21
- puts e.message
22
- nil
23
21
  end
24
22
  end
@@ -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
- return if difficulty.match?(/easy|medium|hell/)
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.0.2'
6
+ VERSION = '1.2.1'
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.2
4
+ version: 1.2.1
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-23 00:00:00.000000000 Z
11
+ date: 2022-08-25 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: []