codebreaker_kirill 1.2.3 → 1.3.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: 13184f386f8e6c28fce445b1bd7849afe194a85129b8ade51d8a72c58da0faae
4
- data.tar.gz: a5c9fdd868c7f244304685f542fbc38cd9a238efb42e842b84043934762b3b32
3
+ metadata.gz: a03a907c9b2e62374b5b2dc875cd3ffe3932ecf08f6ba8d42d638ad3ba95dd6c
4
+ data.tar.gz: a06fc23a1f15807000df1fc4200ad91875639d4bd8c126b34ebab4dbe0c060cf
5
5
  SHA512:
6
- metadata.gz: 38cc6d0005882e01699e6997749d451f82f99c37c06f41bfb5f4a48a7159a38605b60d8c7e7fe9617147148e1ce544e9cfd74ad24643267a602fbd99920e1657
7
- data.tar.gz: '09e7d0b555b122db1e004623082a695d1b62ff519b136eba6e35ac52073317bf788e8c30c76569b399eea4d24814ac00492f88c6a9389e33934f64c2fe65b029'
6
+ metadata.gz: a5f9f30855b9376c97645b0313b57be92ab8bf49b5c4514c8052173a4f7956811516b4bd940f2d6e55a4c93677fdc7db4fe5d2fab3665280296d66d25d205181
7
+ data.tar.gz: e45e126160305a4e25c4227b2637439854fabb40a09db1295eb25672e010fc1c4ad6914254304e82986be9d3d8f5515bffa931394a0f6f8aeef5508dfdc7bbcc
@@ -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,36 +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
- return { status: status, secret_code: @secret_code } if status == 'loss'
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'
27
28
 
28
- { status: status, response: resolved_code, secret_code: code }
29
- end
29
+ { status: status, response: resolved_code, secret_code: @secret_code }
30
+ end
30
31
 
31
- def status(input, code)
32
- return 'loss' if @user.attempts_used >= @user.level[:attempts]
33
- 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
34
35
 
35
- 'in_game'
36
+ 'in_game'
37
+ end
36
38
  end
37
39
  end
@@ -2,37 +2,39 @@
2
2
 
3
3
  require_relative 'autoload'
4
4
 
5
- class GuessHandler
6
- include Settings
7
-
8
- def initialize(input, code)
9
- Validations.validate_guess(input)
10
- @input = input.each_char.map(&:to_i)
11
- @code = code.clone
12
- @result = []
13
- end
5
+ module CodebreakerKirill
6
+ class GuessHandler
7
+ include Settings
8
+
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
14
15
 
15
- def call
16
- check_same_indexes
17
- check_different_indexes
18
- @result
19
- end
16
+ def call
17
+ check_same_indexes
18
+ check_different_indexes
19
+ @result
20
+ end
20
21
 
21
- def check_same_indexes
22
- @input.each_index do |index|
23
- next unless @input[index] == @code[index]
22
+ def check_same_indexes
23
+ @input.each_index do |index|
24
+ next unless @input[index] == @code[index]
24
25
 
25
- @result << Settings::POSITIVE_RESPONSE
26
- @input[index], @code[index] = nil
26
+ @result << Settings::POSITIVE_RESPONSE
27
+ @input[index], @code[index] = nil
28
+ end
27
29
  end
28
- end
29
30
 
30
- def check_different_indexes
31
- @input.each_with_index do |value, _index|
32
- 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)
33
34
 
34
- @result << Settings::NEGATIVE_RESPONSE
35
- @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
36
38
  end
37
39
  end
38
40
  end
@@ -2,17 +2,19 @@
2
2
 
3
3
  require_relative 'autoload'
4
4
 
5
- module 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,23 +2,25 @@
2
2
 
3
3
  require_relative 'autoload'
4
4
 
5
- class User
6
- include Settings
5
+ module CodebreakerKirill
6
+ class User
7
+ include Settings
7
8
 
8
- attr_accessor :attempts_used, :hints_used
9
- attr_reader :difficulty, :name, :level
9
+ attr_accessor :attempts_used, :hints_used
10
+ attr_reader :difficulty, :name, :level
10
11
 
11
- def initialize(name, difficulty)
12
- validation(name, difficulty)
13
- @difficulty = difficulty
14
- @name = name
15
- @level = Settings::DIFFICULTY[@difficulty]
16
- @attempts_used = 0
17
- @hints_used = 0
18
- end
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
19
20
 
20
- def validation(name, difficulty)
21
- Validations.validate_name(name)
22
- Validations.validate_difficulty(difficulty)
21
+ def validation(name, difficulty)
22
+ Validations.validate_name(name)
23
+ Validations.validate_difficulty(difficulty)
24
+ end
23
25
  end
24
26
  end
@@ -2,26 +2,28 @@
2
2
 
3
3
  require_relative 'autoload'
4
4
 
5
- class Validations
6
- include Settings
5
+ module CodebreakerKirill
6
+ class Validations
7
+ include Settings
7
8
 
8
- def self.validate_name(name)
9
- raise "Name shouldn't be empty" if name.empty?
10
- raise 'Name should be string' if name.class != String
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
13
- 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
14
15
 
15
- def self.validate_guess(guess)
16
- raise "Guess shouldn't be empty" if guess.empty?
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
19
- end
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
20
21
 
21
- def self.validate_difficulty(difficulty)
22
- raise "Input shouldn't be empty" if difficulty.empty?
23
- 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)
24
25
 
25
- 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
26
28
  end
27
29
  end
@@ -3,5 +3,5 @@
3
3
  require_relative 'game'
4
4
 
5
5
  module CodebreakerKirill
6
- VERSION = '1.2.3'
6
+ VERSION = '1.3.1'
7
7
  end
@@ -1,8 +1,8 @@
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/validations'
5
+ require_relative 'codebreaker_kirill/guess_handler'
6
+ require_relative 'codebreaker_kirill/user'
7
+ require_relative 'codebreaker_kirill/game'
8
8
  require_relative 'codebreaker_kirill/stats'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebreaker_kirill
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Dudchenko