codebreaker_kirill 1.2.1 → 1.3.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: afc87eab226b7b01059d904163528772616e89ad35530d975ab498adf20b740d
4
- data.tar.gz: 2d2414828d7e4e146b96814b30b4629f52152ddcdd7feca3ba2bdc3f4a7dad70
3
+ metadata.gz: 062f979cfbfa6fae7dafffd0050694e70e9437246fd70ff52105311a553f2c88
4
+ data.tar.gz: 74b6e47c51c165ba8d8e282e65e5f0fd78a87478087866e2a42a395e4c24a832
5
5
  SHA512:
6
- metadata.gz: 4d2454ef0c9edb8ca8db36258a867fa1a0a133303524bd1b5cba537a7bb30c218d711b00be84b5a76b13f498b5b27c55ac2157b6484e441abdccd29922756d51
7
- data.tar.gz: 9c30dd02139d489491c1285d11e4981cccffdb0b819fd71ae09e2c8331557e724b43447d9cf00a7d7e96184a7b96ce4c96483294512953b8f4fcf186d896d133
6
+ metadata.gz: f9f476f9734a0aa99d1aebe9ff5205b647ae976939bf3b11322b56a638dfbb041d01f4ff5dccc4d47afdc4be272bc05debba1e83c57b4f2bd5fc4bff8b33fc78
7
+ data.tar.gz: 66ab34e34020fa4b49951f71b95928a26a62ba339c5a6a008a0369c30fe159e66e7cfcfceb1d85e96ade537e39bfb7b984e43953a0589740e556e30731304a31
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'yaml'
4
- require_relative 'settings'
5
- require_relative 'code_generator'
6
- require_relative 'validations'
7
- require_relative 'guess_handler'
8
- require_relative 'user'
9
- require_relative 'game'
10
- require_relative 'stats'
3
+ module CodebreakerKirill
4
+ require 'yaml'
5
+ require_relative 'settings'
6
+ require_relative 'code_generator'
7
+ require_relative 'validations'
8
+ require_relative 'guess_handler'
9
+ require_relative 'user'
10
+ require_relative 'game'
11
+ require_relative 'stats'
12
+ end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module CodeGenerator
4
- def self.call
5
- Array.new(Settings::CODE_LENGTH) { rand(Settings::RANDOM_RANGE) }
3
+ module CodebreakerKirill
4
+ module CodeGenerator
5
+ def self.call
6
+ Array.new(Settings::CODE_LENGTH) { rand(Settings::RANDOM_RANGE) }
7
+ end
6
8
  end
7
9
  end
@@ -2,34 +2,38 @@
2
2
 
3
3
  require_relative 'autoload'
4
4
 
5
- class Game
6
- include CodeGenerator
5
+ module CodebreakerKirill
6
+ class Game
7
+ include CodeGenerator
7
8
 
8
- attr_reader :user
9
+ attr_reader :user
9
10
 
10
- def initialize(user)
11
- @secret_code = CodeGenerator.call
12
- @user = user
13
- end
11
+ def initialize(user)
12
+ @secret_code = CodeGenerator.call
13
+ @user = user
14
+ end
14
15
 
15
- def give_a_hint
16
- return nil if @user.hints_used >= @user.level[:hints]
16
+ def give_a_hint
17
+ return nil if @user.hints_used >= @user.level[:hints]
17
18
 
18
- @user.hints_used += 1
19
- @secret_code.shuffle[@user.hints_used]
20
- end
19
+ @user.hints_used += 1
20
+ @secret_code.shuffle[@user.hints_used]
21
+ end
21
22
 
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
23
+ def respond_to_guess(input)
24
+ @user.attempts_used += 1
25
+ status = status(input, @secret_code)
26
+ resolved_code = GuessHandler.new(input, @secret_code).call
27
+ return { status: status, secret_code: @secret_code } if status == 'loss'
28
+
29
+ { status: status, response: resolved_code, secret_code: @secret_code }
30
+ end
28
31
 
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
+ def status(input, code)
33
+ return 'loss' if @user.attempts_used >= @user.level[:attempts]
34
+ return 'win' if input.each_char.map(&:to_i) == code
32
35
 
33
- 'in_game'
36
+ 'in_game'
37
+ end
34
38
  end
35
39
  end
@@ -2,35 +2,39 @@
2
2
 
3
3
  require_relative 'autoload'
4
4
 
5
- class GuessHandler
6
- def initialize(input, code)
7
- Validations.validate_guess(input)
8
- @input = input.each_char.map(&:to_i)
9
- @code = code.clone
10
- @result = []
11
- end
5
+ module CodebreakerKirill
6
+ class GuessHandler
7
+ include Settings
12
8
 
13
- def call
14
- check_same_indexes
15
- check_different_indexes
16
- @result
17
- end
9
+ def initialize(input, code)
10
+ Validations.validate_guess(input)
11
+ @input = input.each_char.map(&:to_i)
12
+ @code = code.clone
13
+ @result = []
14
+ end
18
15
 
19
- def check_same_indexes
20
- @input.each_index do |index|
21
- next unless @input[index] == @code[index]
16
+ def call
17
+ check_same_indexes
18
+ check_different_indexes
19
+ @result
20
+ end
21
+
22
+ def check_same_indexes
23
+ @input.each_index do |index|
24
+ next unless @input[index] == @code[index]
22
25
 
23
- @result << Settings::POSITIVE_RESPONSE
24
- @input[index], @code[index] = nil
26
+ @result << Settings::POSITIVE_RESPONSE
27
+ @input[index], @code[index] = nil
28
+ end
25
29
  end
26
- end
27
30
 
28
- def check_different_indexes
29
- @input.each_with_index do |value, _index|
30
- next unless !value.nil? && @code.include?(value)
31
+ def check_different_indexes
32
+ @input.each_with_index do |value, _index|
33
+ next unless !value.nil? && @code.include?(value)
31
34
 
32
- @result << Settings::NEGATIVE_RESPONSE
33
- @input[@input.find_index(value)], @code[@code.find_index(value)] = nil
35
+ @result << Settings::NEGATIVE_RESPONSE
36
+ @input[@input.find_index(value)], @code[@code.find_index(value)] = nil
37
+ end
34
38
  end
35
39
  end
36
40
  end
@@ -2,17 +2,19 @@
2
2
 
3
3
  require_relative 'autoload'
4
4
 
5
- class Settings
6
- DIFFICULTY = {
7
- 'easy' => { attempts: 15, hints: 2 },
8
- 'medium' => { attempts: 10, hints: 1 },
9
- 'hell' => { attempts: 5, hints: 1 }
10
- }.freeze
11
- CODE_LENGTH = 4
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
5
+ module CodebreakerKirill
6
+ module Settings
7
+ DIFFICULTY = {
8
+ 'easy' => { attempts: 15, hints: 2 },
9
+ 'medium' => { attempts: 10, hints: 1 },
10
+ 'hell' => { attempts: 5, hints: 1 }
11
+ }.freeze
12
+ CODE_LENGTH = 4
13
+ RANDOM_RANGE = (1..6).freeze
14
+ POSITIVE_RESPONSE = '+'
15
+ NEGATIVE_RESPONSE = '-'
16
+ NAME_MIN_LENGTH = 3
17
+ NAME_MAX_LENGTH = 20
18
+ GUESS_LENGTH = 4
19
+ end
18
20
  end
@@ -2,16 +2,18 @@
2
2
 
3
3
  require_relative 'autoload'
4
4
 
5
- class Stats
6
- def self.save_game(user)
7
- @data = File.exist?('stats.yml') ? YAML.load_file('stats.yml') : []
8
- @data << user
9
- File.new('stats.yml', 'w') unless File.exist?('stats.yml')
10
- File.open('stats.yml', 'w') { |file| file.write(@data.to_yaml) }
11
- end
5
+ module CodebreakerKirill
6
+ class Stats
7
+ def self.save_game(user)
8
+ @data = File.exist?('stats.yml') ? YAML.load_file('stats.yml') : []
9
+ @data << user
10
+ File.new('stats.yml', 'w') unless File.exist?('stats.yml')
11
+ File.open('stats.yml', 'w') { |file| file.write(@data.to_yaml) }
12
+ end
12
13
 
13
- def self.show_stats
14
- @data = File.exist?('stats.yml') ? YAML.load_file('stats.yml') : []
15
- @data.sort_by! { |user| [user.level[:attempts], user.attempts_used, user.hints_used] }
14
+ def self.show_stats
15
+ @data = File.exist?('stats.yml') ? YAML.load_file('stats.yml') : []
16
+ @data.sort_by! { |user| [user.level[:attempts], user.attempts_used, user.hints_used] }
17
+ end
16
18
  end
17
19
  end
@@ -2,21 +2,25 @@
2
2
 
3
3
  require_relative 'autoload'
4
4
 
5
- class User
6
- attr_accessor :attempts_used, :hints_used
7
- attr_reader :difficulty, :name, :level
5
+ module CodebreakerKirill
6
+ class User
7
+ include Settings
8
8
 
9
- def initialize(name, difficulty)
10
- validation(name, difficulty)
11
- @difficulty = difficulty
12
- @name = name
13
- @level = Settings::DIFFICULTY[@difficulty]
14
- @attempts_used = 0
15
- @hints_used = 0
16
- end
9
+ attr_accessor :attempts_used, :hints_used
10
+ attr_reader :difficulty, :name, :level
11
+
12
+ def initialize(name, difficulty)
13
+ validation(name, difficulty)
14
+ @difficulty = difficulty
15
+ @name = name
16
+ @level = Settings::DIFFICULTY[@difficulty]
17
+ @attempts_used = 0
18
+ @hints_used = 0
19
+ end
17
20
 
18
- def validation(name, difficulty)
19
- Validations.validate_name(name)
20
- Validations.validate_difficulty(difficulty)
21
+ def validation(name, difficulty)
22
+ Validations.validate_name(name)
23
+ Validations.validate_difficulty(difficulty)
24
+ end
21
25
  end
22
26
  end
@@ -2,24 +2,28 @@
2
2
 
3
3
  require_relative 'autoload'
4
4
 
5
- class Validations
6
- def self.validate_name(name)
7
- raise "Name shouldn't be empty" if name.empty?
8
- raise 'Name should be string' if name.class != String
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
11
- end
5
+ module CodebreakerKirill
6
+ class Validations
7
+ include Settings
12
8
 
13
- def self.validate_guess(guess)
14
- raise "Guess shouldn't be empty" if guess.empty?
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
17
- end
9
+ def self.validate_name(name)
10
+ raise "Name shouldn't be empty" if name.empty?
11
+ raise 'Name should be string' if name.class != String
12
+ raise 'Name should be at least 3 characters long' if name.length < Settings::NAME_MIN_LENGTH
13
+ raise "Name shouldn't be more than 20 characters long" if name.length > Settings::NAME_MAX_LENGTH
14
+ end
15
+
16
+ def self.validate_guess(guess)
17
+ raise "Guess shouldn't be empty" if guess.empty?
18
+ raise "Guess should be 4 characters. Got #{guess}" if guess.length < Settings::GUESS_LENGTH
19
+ raise 'Guess shouldn\'t be more than 4 characters long' if guess.length > Settings::GUESS_LENGTH
20
+ end
18
21
 
19
- def self.validate_difficulty(difficulty)
20
- raise "Input shouldn't be empty" if difficulty.empty?
21
- return if Settings::DIFFICULTY.keys.include?(difficulty)
22
+ def self.validate_difficulty(difficulty)
23
+ raise "Input shouldn't be empty" if difficulty.empty?
24
+ return if Settings::DIFFICULTY.keys.include?(difficulty)
22
25
 
23
- raise "You should enter one of the following options: #{Settings::DIFFICULTY.keys.join(', ')}"
26
+ raise "You should enter one of the following options: #{Settings::DIFFICULTY.keys.join(', ')}"
27
+ end
24
28
  end
25
29
  end
@@ -3,5 +3,5 @@
3
3
  require_relative 'game'
4
4
 
5
5
  module CodebreakerKirill
6
- VERSION = '1.2.1'
6
+ VERSION = '1.3.0'
7
7
  end
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'codebreaker_kirill/version'
4
- require_relative 'codebreaker_kirill/game'
5
- require_relative 'codebreaker_kirill/user'
6
- require_relative 'codebreaker_kirill/guess_handler'
7
3
  require_relative 'codebreaker_kirill/settings'
4
+ require_relative 'codebreaker_kirill/code_generator'
5
+ require_relative 'codebreaker_kirill/validations'
6
+ require_relative 'codebreaker_kirill/guess_handler'
7
+ require_relative 'codebreaker_kirill/user'
8
+ require_relative 'codebreaker_kirill/game'
8
9
  require_relative 'codebreaker_kirill/stats'
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.2.1
4
+ version: 1.3.0
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-25 00:00:00.000000000 Z
11
+ date: 2022-08-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: