codebreaker_kirill 0.2.11 → 0.2.14

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: b56e6128500cf9b60f533450600297dd84435d87473fa09a932d17c53ca0df10
4
- data.tar.gz: cc1517ccf27ea8e363a1250d8ee07f47dc4004c1a9ad25777f2812fe0dbbd9d8
3
+ metadata.gz: 0de2a53a4fe181807c25613515789140af1f979b44ef0bdf1b764b8180361c8c
4
+ data.tar.gz: 1dc0fa0e432c13b0fc41522cfc6f02783b3472ba651e7b00b0b189e8f7420270
5
5
  SHA512:
6
- metadata.gz: 37d10f8888309fdda4b16e4b2d621482ec0880c35e5226d31b2cbad4a59332d8587b59a422eae818afb3da765be73535044b960c181f97c84167775710565aa3
7
- data.tar.gz: cfef8bdb0e0d66b00b1fdb982567f4c574988279c1e51aeafd5e00773839517476fb2de51be67441ceb0da6ce7410c5c28703dadc621d7d536e1030e98371242
6
+ metadata.gz: 16207ff585ee6b27b23332791a7ec74929fa84ad71811e4461ce5c718d2c52ef9a84dcb439b8bc8283d0eeb470605c7286b0a57143aa94f7da1d6bb39e141d78
7
+ data.tar.gz: 2e9aa3cd9e2ae12bf31965e0fc24571a10259fbb98ab61525acc8555c0ea88f0ae5cd4866fd0cc11533c20ae64a2a67959a4739d52e0ac7bcd4e91d17f8cbc48
@@ -0,0 +1,9 @@
1
+ require_relative 'settings'
2
+
3
+ module CodeGenerator
4
+ include Settings
5
+
6
+ def self.call
7
+ Array.new(Settings::CODE_LENGTH) { rand(Settings::RANDOM_RANGE) }
8
+ end
9
+ end
@@ -1,26 +1,29 @@
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?
21
-
22
- @hints[:all] -= 1
23
- @hints[:used] += 1
25
+ return 0 if @user.hints[:used] >= @user.hints[:all]
26
+ @user.hints[:used] += 1
24
27
  @secret_code[@hints[:used]]
25
28
  end
26
29
  end
@@ -2,22 +2,29 @@
2
2
 
3
3
  module GuessHandler
4
4
  def respond_to_guess(game, input, code)
5
+ validate_guess(input)
5
6
  @result = []
7
+ @code_clone = code.clone
6
8
  @numbers = input.split('').map(&:to_i)
7
9
  return 'win' if @numbers == code
8
10
 
9
- assess_guess(code, @numbers)
11
+ assess_guess(@code_clone, @numbers)
12
+
10
13
  game.attempts[:used] += 1
11
14
  game.attempts[:used] >= game.attempts[:all] ? 'loss' : @result.delete_if { |value| (1..6).include?(value) }.sort
12
15
  end
13
16
 
14
17
  def assess_guess(code, input)
15
- input.each_with_index { |element, index| code[index] = '+' if element == code[index] }
18
+ @numbers.each_with_index do |element, index|
19
+ if element == code[index]
20
+ code[index] = '+'
21
+ input[@numbers.index(element)] = '' end
22
+ end
23
+
16
24
  @result = code.each_with_index do |el, index|
17
- if input.include?(el)
25
+ if @numbers.include?(el)
18
26
  code[index] = '-'
19
- input.slice!(@numbers.index(el))
20
- end
27
+ input.slice!(@numbers.index(el)) end
21
28
  end
22
29
  end
23
30
  end
@@ -0,0 +1,11 @@
1
+ module Settings
2
+
3
+ LEVEL = {
4
+ 'easy' => { attempts: 15, hints: 2 },
5
+ 'medium' => { attempts: 10, hints: 1 },
6
+ 'hell' => { attempts: 5, hints: 1 }
7
+ }
8
+ CODE_LENGTH = 4
9
+ RANDOM_RANGE = (1..6)
10
+
11
+ end
@@ -0,0 +1,13 @@
1
+ class Stats
2
+ def self.save_game(user)
3
+ @data = File.exist?("stats.yml") ? YAML.load_file("stats.yml") : []
4
+ @data << user
5
+ File.new("stats.yml", 'w') unless File.exist?("stats.yml")
6
+ File.open("stats.yml", 'w') { |file| file.write(@data.to_yaml) }
7
+ end
8
+
9
+ def self.show_stats
10
+ @data = File.exist?("stats.yml") ? YAML.load_file("stats.yml") : []
11
+ print @data
12
+ end
13
+ 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,19 @@
1
1
  # frozen_string_literal: true
2
+ require_relative "validations"
3
+ require_relative "settings"
2
4
 
3
5
  class User
4
- attr_reader :name
6
+ include Validations
7
+ include Settings
5
8
 
6
- def initialize(name)
9
+ attr_reader :name, :difficulty, :attempts, :hints
10
+
11
+ def initialize(name, difficulty)
12
+ validate_name(name)
13
+ validate_difficulty(difficulty)
7
14
  @name = name
15
+ @difficulty = Settings::LEVEL[difficulty]
16
+ @attempts = { all: @difficulty[:attempts], used: 0 }
17
+ @hints = { all: @difficulty[:hints], used: 0 }
8
18
  end
9
19
  end
@@ -0,0 +1,19 @@
1
+ module Validations
2
+ def validate_name(name)
3
+ raise "Name shouldn't be empty" if name.empty?
4
+ raise 'Name should be string' if name.class != String
5
+ raise 'Name should be at least 3 characters long' if name.length < 3
6
+ raise "Name shouldn't be more than 20 characters long" if name.length > 20
7
+ end
8
+
9
+ def validate_guess(guess)
10
+ raise "Name shouldn't be empty" if guess.empty?
11
+ raise 'Name should be at least 3 characters long' if guess.length < 4
12
+ raise 'Name shouldn\'t be more than 4 characters long' if guess.length > 4
13
+ end
14
+
15
+ def validate_difficulty(difficulty)
16
+ raise "Input shouldn't be empty" if difficulty.empty?
17
+ raise 'You should enter one of the following options: easy, medium, hell' unless difficulty.match?(/easy|medium|hell/)
18
+ end
19
+ end
@@ -3,5 +3,5 @@
3
3
  require_relative 'game'
4
4
 
5
5
  module CodebreakerKirill
6
- VERSION = '0.2.11'
6
+ VERSION = '0.2.14'
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.11
4
+ version: 0.2.14
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: