codebreaker_kirill 0.2.13 → 0.2.16

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: 29bb8deb5b88bc6d24768d6fd678027be29fe77dece02f153434b65aa8080aba
4
- data.tar.gz: 6d3d7aed857fd189ff9d55c4e27f37a32feffb320f57701273b0ce574a2a5948
3
+ metadata.gz: bbc9656673988586a9dddfe7280ff0b0bc514cb3ece9773769861e9ac49cff08
4
+ data.tar.gz: 61b705f0e861cff5c867de51248d4cf7fcb4245540176c3ee9fe14ed128a9382
5
5
  SHA512:
6
- metadata.gz: f8f92771e599a13813c0661d7886c71fc2f1f03085d56a29f120d73aec303c6a1acce651a50cc49162b0c2411c0a453018647c127bb503072d85fb834446ee06
7
- data.tar.gz: 86f196bb9e4f8e002de8f529a9ba43fce93d490a18878ea5c22d77919ad9413b0f17d23c6769fc000485b6e18b1be426ba72652e48431e645ff5c74db4d6e0d6
6
+ metadata.gz: 2a4822d69e4e8c0f67095a1a4c746d5fff22357ec6dd8d423d83bee6156e7e8bbc13e25582ce9ac8f5dcd37c240d98eefd355ee6c420d7dedaf69460be2cf982
7
+ data.tar.gz: c2976c681d6dc75f0d5aeb2b9cc8ec4eb673fa8613a3e1ea0d1c687126a2efd288e2dd50ae39105bb87682ab45ac9c050f0541e64549c1d195b11694f67e7916
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'settings'
4
+
5
+ module CodeGenerator
6
+ include Settings
7
+
8
+ def self.call
9
+ Array.new(Settings::CODE_LENGTH) { rand(Settings::RANDOM_RANGE) }
10
+ end
11
+ end
@@ -1,26 +1,43 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'yaml'
4
+ require_relative 'validations'
3
5
  require_relative 'guess_handler'
4
6
  require_relative 'user'
7
+ require_relative 'settings'
8
+ require_relative 'code_generator'
9
+ require_relative 'stats'
5
10
 
6
11
  class Game
7
12
  include GuessHandler
13
+ include Validations
14
+ include CodeGenerator
15
+ include Settings
8
16
 
9
- attr_reader :secret_code, :hint_counter, :attempts, :hints, :name
17
+ attr_reader :secret_code, :user
10
18
 
11
- def initialize(user, difficulty)
12
- @secret_code = (1..4).to_a.map { |_num| rand(1..6) }
13
- @hint_counter = 0
14
- @attempts = { all: difficulty[:attempts], used: 0 }
15
- @hints = { all: difficulty[:hints], used: 0 }
16
- @name = user.name
19
+ def initialize(user)
20
+ @secret_code = CodeGenerator.call
21
+ @user = user
17
22
  end
18
23
 
19
24
  def give_a_hint
20
- return 0 if @hints[:available].zero?
25
+ return 0 if @user.hints[:used] >= @user.hints[:all]
21
26
 
22
- @hints[:all] -= 1
23
- @hints[:used] += 1
27
+ @user.hints[:used] += 1
24
28
  @secret_code[@hints[:used]]
25
29
  end
26
30
  end
31
+
32
+ game = Game.new(User.new('kirill', 'medium'))
33
+ print game.respond_to_guess(game, '2222', [1, 2, 3, 4]) # +
34
+ print game.respond_to_guess(game, '2222', [2, 2, 2, 4]) # +++
35
+ print game.respond_to_guess(game, '2211', [1, 2, 3, 4]) # +-
36
+ print game.respond_to_guess(game, '2221', [1, 2, 3, 2]) # +--
37
+ print game.respond_to_guess(game, '1212', [2, 1, 2, 1]) # ----
38
+ print game.respond_to_guess(game, '6666', [1, 1, 1, 1]) # []
39
+ print game.respond_to_guess(game, '6666', [6, 6, 6, 6]) # win
40
+ print game.respond_to_guess(game, '1234', [5, 3, 6, 3]) # -
41
+ print game.respond_to_guess(game, '5363', [1, 2, 3, 4]) # -
42
+ print game.respond_to_guess(game, '1235', [5, 5, 5, 5]) # +
43
+ print game.respond_to_guess(game, '1535', [5, 5, 5, 5]) # ++
@@ -2,15 +2,14 @@
2
2
 
3
3
  module GuessHandler
4
4
  def respond_to_guess(game, input, code)
5
+ validate_guess(input)
5
6
  @result = []
6
7
  @code_clone = code.clone
7
8
  @numbers = input.split('').map(&:to_i)
8
9
  return 'win' if @numbers == code
9
10
 
10
11
  assess_guess(@code_clone, @numbers)
11
-
12
- game.attempts[:used] += 1
13
- game.attempts[:used] >= game.attempts[:all] ? 'loss' : @result.delete_if { |value| (1..6).include?(value) }.sort
12
+ attempts_handler(game)
14
13
  end
15
14
 
16
15
  def assess_guess(code, input)
@@ -26,4 +25,15 @@ module GuessHandler
26
25
  input.slice!(@numbers.index(el)) end
27
26
  end
28
27
  end
28
+
29
+ def attempts_handler(game)
30
+ game.user.attempts[:used] += 1
31
+ if game.user.attempts[:used] >= game.user.attempts[:all]
32
+ 'loss'
33
+ else
34
+ @result.delete_if do |value|
35
+ (1..6).include?(value)
36
+ end.sort
37
+ end
38
+ end
29
39
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Settings
4
+ LEVEL = {
5
+ 'easy' => { attempts: 15, hints: 2 },
6
+ 'medium' => { attempts: 10, hints: 1 },
7
+ 'hell' => { attempts: 5, hints: 1 }
8
+ }.freeze
9
+ CODE_LENGTH = 4
10
+ RANDOM_RANGE = (1..6).freeze
11
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Stats
4
+ def self.save_game(user)
5
+ @data = File.exist?('stats.yml') ? YAML.load_file('stats.yml') : []
6
+ @data << user
7
+ File.new('stats.yml', 'w') unless File.exist?('stats.yml')
8
+ File.open('stats.yml', 'w') { |file| file.write(@data.to_yaml) }
9
+ end
10
+
11
+ def self.show_stats
12
+ @data = File.exist?('stats.yml') ? YAML.load_file('stats.yml') : []
13
+ print @data
14
+ end
15
+ end
@@ -0,0 +1,34 @@
1
+ ---
2
+ - !ruby/object:User
3
+ name: dima
4
+ difficulty:
5
+ :attempts: 10
6
+ :hints: 1
7
+ attempts:
8
+ :all: 10
9
+ :used: 0
10
+ hints:
11
+ :all: 1
12
+ :used: 0
13
+ - !ruby/object:User
14
+ name: stacy
15
+ difficulty:
16
+ :attempts: 5
17
+ :hints: 1
18
+ attempts:
19
+ :all: 5
20
+ :used: 0
21
+ hints:
22
+ :all: 1
23
+ :used: 0
24
+ - !ruby/object:User
25
+ name: kirill
26
+ difficulty:
27
+ :attempts: 5
28
+ :hints: 1
29
+ attempts:
30
+ :all: 5
31
+ :used: 0
32
+ hints:
33
+ :all: 1
34
+ :used: 0
@@ -1,9 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'validations'
4
+ require_relative 'settings'
5
+
3
6
  class User
4
- attr_reader :name
7
+ include Validations
8
+ include Settings
9
+
10
+ attr_reader :name, :difficulty, :attempts, :hints
5
11
 
6
- def initialize(name)
12
+ def initialize(name, difficulty)
13
+ validate_name(name)
14
+ validate_difficulty(difficulty)
7
15
  @name = name
16
+ @difficulty = Settings::LEVEL[difficulty]
17
+ @attempts = { all: @difficulty[:attempts], used: 0 }
18
+ @hints = { all: @difficulty[:hints], used: 0 }
8
19
  end
9
20
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Validations
4
+ def validate_name(name)
5
+ raise "Name shouldn't be empty" if name.empty?
6
+ 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
+ end
10
+
11
+ def validate_guess(guess)
12
+ raise "Name shouldn't be empty" if guess.empty?
13
+ raise 'Name should be at least 3 characters long' if guess.length < 4
14
+ raise 'Name shouldn\'t be more than 4 characters long' if guess.length > 4
15
+ end
16
+
17
+ def validate_difficulty(difficulty)
18
+ raise "Input shouldn't be empty" if difficulty.empty?
19
+ return if difficulty.match?(/easy|medium|hell/)
20
+
21
+ raise 'You should enter one of the following options: easy, medium, hell'
22
+ end
23
+ end
@@ -3,5 +3,5 @@
3
3
  require_relative 'game'
4
4
 
5
5
  module CodebreakerKirill
6
- VERSION = '0.2.13'
6
+ VERSION = '0.2.16'
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: 0.2.13
4
+ version: 0.2.16
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-17 00:00:00.000000000 Z
11
+ date: 2022-08-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -27,9 +27,14 @@ files:
27
27
  - Rakefile
28
28
  - lib/codebreaker_kirill.rb
29
29
  - lib/codebreaker_kirill/autoload.rb
30
+ - lib/codebreaker_kirill/code_generator.rb
30
31
  - lib/codebreaker_kirill/game.rb
31
32
  - lib/codebreaker_kirill/guess_handler.rb
33
+ - lib/codebreaker_kirill/settings.rb
34
+ - lib/codebreaker_kirill/stats.rb
35
+ - lib/codebreaker_kirill/stats.yml
32
36
  - lib/codebreaker_kirill/user.rb
37
+ - lib/codebreaker_kirill/validations.rb
33
38
  - lib/codebreaker_kirill/version.rb
34
39
  - sig/codebreaker_kirill.rbs
35
40
  homepage: