codebreaker_kirill 0.2.15 → 0.2.19
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 +4 -4
- data/lib/codebreaker_kirill/autoload.rb +6 -2
- data/lib/codebreaker_kirill/code_generator.rb +1 -3
- data/lib/codebreaker_kirill/game.rb +4 -10
- data/lib/codebreaker_kirill/guess_handler.rb +19 -5
- data/lib/codebreaker_kirill/settings.rb +5 -5
- data/lib/codebreaker_kirill/stats.rb +7 -5
- data/lib/codebreaker_kirill/user.rb +8 -8
- data/lib/codebreaker_kirill/validations.rb +9 -5
- data/lib/codebreaker_kirill/version.rb +1 -1
- data/lib/codebreaker_kirill.rb +3 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b9aaf5b0f263d94273709be21b31f7e2ab0ddb63a31009cb034f42c4bebaaca
|
4
|
+
data.tar.gz: 4208f814b1eb72d29a934f7a0df9f784619aeee25185d1746c25fe8376b7f6c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f36bb58e2da8a9471c2a51958bc55e41652e4334134c1d5a408cb1c8e0cae2686cc3882b2f46976cb8f39079a09243634d30b1d53e92a18ed750809c3b069412
|
7
|
+
data.tar.gz: 332303c30565be7172daa10ad33bc52d6df886a1a50cdbdfe43e673e857859f761dcebacfcd4eb20cdd56ff4553b6e8a76b8be6f2513246b9b7f64206ef92d5a
|
@@ -1,18 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require_relative 'validations'
|
5
|
-
require_relative 'guess_handler'
|
6
|
-
require_relative 'user'
|
7
|
-
require_relative 'settings'
|
3
|
+
require_relative 'autoload'
|
8
4
|
require_relative 'code_generator'
|
9
|
-
require_relative '
|
5
|
+
require_relative 'user'
|
10
6
|
|
11
7
|
class Game
|
12
|
-
include GuessHandler
|
13
|
-
include Validations
|
14
8
|
include CodeGenerator
|
15
|
-
include Settings
|
16
9
|
|
17
10
|
attr_reader :secret_code, :user
|
18
11
|
|
@@ -23,7 +16,8 @@ class Game
|
|
23
16
|
|
24
17
|
def give_a_hint
|
25
18
|
return 0 if @user.hints[:used] >= @user.hints[:all]
|
19
|
+
|
26
20
|
@user.hints[:used] += 1
|
27
|
-
@secret_code[@hints[:used]]
|
21
|
+
@secret_code[@user.hints[:used]]
|
28
22
|
end
|
29
23
|
end
|
@@ -1,17 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require_relative 'autoload'
|
4
|
+
require_relative 'game'
|
5
|
+
|
6
|
+
class GuessHandler
|
4
7
|
def respond_to_guess(game, input, code)
|
5
|
-
validate_guess(input)
|
8
|
+
Validations.validate_guess(input)
|
9
|
+
return game.give_a_hint if input == 'hint'
|
10
|
+
|
6
11
|
@result = []
|
7
12
|
@code_clone = code.clone
|
8
13
|
@numbers = input.split('').map(&:to_i)
|
9
14
|
return 'win' if @numbers == code
|
10
15
|
|
11
16
|
assess_guess(@code_clone, @numbers)
|
12
|
-
|
13
|
-
game.user.attempts[:used] += 1
|
14
|
-
game.user.attempts[:used] >= game.attempts[:all] ? 'loss' : @result.delete_if { |value| (1..6).include?(value) }.sort
|
17
|
+
attempts_handler(game)
|
15
18
|
end
|
16
19
|
|
17
20
|
def assess_guess(code, input)
|
@@ -27,4 +30,15 @@ module GuessHandler
|
|
27
30
|
input.slice!(@numbers.index(el)) end
|
28
31
|
end
|
29
32
|
end
|
33
|
+
|
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
|
42
|
+
end
|
43
|
+
end
|
30
44
|
end
|
@@ -1,11 +1,11 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
class Settings
|
4
|
+
DIFFICULTY = {
|
4
5
|
'easy' => { attempts: 15, hints: 2 },
|
5
6
|
'medium' => { attempts: 10, hints: 1 },
|
6
7
|
'hell' => { attempts: 5, hints: 1 }
|
7
|
-
}
|
8
|
+
}.freeze
|
8
9
|
CODE_LENGTH = 4
|
9
|
-
RANDOM_RANGE = (1..6)
|
10
|
-
|
10
|
+
RANDOM_RANGE = (1..6).freeze
|
11
11
|
end
|
@@ -1,13 +1,15 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Stats
|
2
4
|
def self.save_game(user)
|
3
|
-
@data = File.exist?(
|
5
|
+
@data = File.exist?('stats.yml') ? YAML.load_file('stats.yml') : []
|
4
6
|
@data << user
|
5
|
-
File.new(
|
6
|
-
File.open(
|
7
|
+
File.new('stats.yml', 'w') unless File.exist?('stats.yml')
|
8
|
+
File.open('stats.yml', 'w') { |file| file.write(@data.to_yaml) }
|
7
9
|
end
|
8
10
|
|
9
11
|
def self.show_stats
|
10
|
-
@data = File.exist?(
|
12
|
+
@data = File.exist?('stats.yml') ? YAML.load_file('stats.yml') : []
|
11
13
|
print @data
|
12
14
|
end
|
13
15
|
end
|
@@ -1,19 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require_relative "validations"
|
3
|
-
require_relative "settings"
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
include Settings
|
3
|
+
require_relative 'autoload'
|
4
|
+
require_relative 'settings'
|
8
5
|
|
6
|
+
class User
|
9
7
|
attr_reader :name, :difficulty, :attempts, :hints
|
10
8
|
|
11
9
|
def initialize(name, difficulty)
|
12
|
-
validate_name(name)
|
13
|
-
validate_difficulty(difficulty)
|
10
|
+
Validations.validate_name(name)
|
11
|
+
Validations.validate_difficulty(difficulty)
|
14
12
|
@name = name
|
15
|
-
@difficulty = Settings::
|
13
|
+
@difficulty = Settings::DIFFICULTY[difficulty]
|
16
14
|
@attempts = { all: @difficulty[:attempts], used: 0 }
|
17
15
|
@hints = { all: @difficulty[:hints], used: 0 }
|
18
16
|
end
|
19
17
|
end
|
18
|
+
|
19
|
+
User.new('kirill', 'hell')
|
@@ -1,19 +1,23 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Validations
|
4
|
+
def self.validate_name(name)
|
3
5
|
raise "Name shouldn't be empty" if name.empty?
|
4
6
|
raise 'Name should be string' if name.class != String
|
5
7
|
raise 'Name should be at least 3 characters long' if name.length < 3
|
6
8
|
raise "Name shouldn't be more than 20 characters long" if name.length > 20
|
7
9
|
end
|
8
10
|
|
9
|
-
def validate_guess(guess)
|
11
|
+
def self.validate_guess(guess)
|
10
12
|
raise "Name shouldn't be empty" if guess.empty?
|
11
13
|
raise 'Name should be at least 3 characters long' if guess.length < 4
|
12
14
|
raise 'Name shouldn\'t be more than 4 characters long' if guess.length > 4
|
13
15
|
end
|
14
16
|
|
15
|
-
def validate_difficulty(difficulty)
|
17
|
+
def self.validate_difficulty(difficulty)
|
16
18
|
raise "Input shouldn't be empty" if difficulty.empty?
|
17
|
-
|
19
|
+
return if difficulty.match?(/easy|medium|hell/)
|
20
|
+
|
21
|
+
raise 'You should enter one of the following options: easy, medium, hell'
|
18
22
|
end
|
19
23
|
end
|
data/lib/codebreaker_kirill.rb
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
require_relative 'codebreaker_kirill/version'
|
4
4
|
require_relative 'codebreaker_kirill/game'
|
5
5
|
require_relative 'codebreaker_kirill/user'
|
6
|
+
require_relative 'codebreaker_kirill/guess_handler'
|
7
|
+
require_relative 'codebreaker_kirill/settings'
|
8
|
+
require_relative 'codebreaker_kirill/stats'
|
6
9
|
|
7
10
|
module CodebreakerKirill
|
8
11
|
class Error < StandardError; 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.
|
4
|
+
version: 0.2.19
|
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-
|
11
|
+
date: 2022-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|