codebreaker_kirill 1.1.0 → 1.2.3
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/Gemfile.lock +1 -1
- data/lib/codebreaker_kirill/autoload.rb +4 -3
- data/lib/codebreaker_kirill/game.rb +10 -14
- data/lib/codebreaker_kirill/guess_handler.rb +15 -26
- data/lib/codebreaker_kirill/settings.rb +8 -3
- data/lib/codebreaker_kirill/stats.rb +2 -17
- data/lib/codebreaker_kirill/user.rb +6 -4
- data/lib/codebreaker_kirill/validations.rb +9 -5
- data/lib/codebreaker_kirill/version.rb +1 -1
- 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: 13184f386f8e6c28fce445b1bd7849afe194a85129b8ade51d8a72c58da0faae
|
4
|
+
data.tar.gz: a5c9fdd868c7f244304685f542fbc38cd9a238efb42e842b84043934762b3b32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz: '
|
6
|
+
metadata.gz: 38cc6d0005882e01699e6997749d451f82f99c37c06f41bfb5f4a48a7159a38605b60d8c7e7fe9617147148e1ce544e9cfd74ad24643267a602fbd99920e1657
|
7
|
+
data.tar.gz: '09e7d0b555b122db1e004623082a695d1b62ff519b136eba6e35ac52073317bf788e8c30c76569b399eea4d24814ac00492f88c6a9389e33934f64c2fe65b029'
|
data/Gemfile.lock
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'yaml'
|
4
|
-
require_relative 'validations'
|
5
|
-
require_relative 'guess_handler'
|
6
4
|
require_relative 'settings'
|
7
5
|
require_relative 'code_generator'
|
8
|
-
require_relative '
|
6
|
+
require_relative 'validations'
|
7
|
+
require_relative 'guess_handler'
|
9
8
|
require_relative 'user'
|
9
|
+
require_relative 'game'
|
10
|
+
require_relative 'stats'
|
@@ -1,8 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative 'autoload'
|
4
|
-
require_relative 'code_generator'
|
5
|
-
require_relative 'user'
|
6
4
|
|
7
5
|
class Game
|
8
6
|
include CodeGenerator
|
@@ -15,25 +13,23 @@ class Game
|
|
15
13
|
end
|
16
14
|
|
17
15
|
def give_a_hint
|
18
|
-
return nil if @user.
|
16
|
+
return nil if @user.hints_used >= @user.level[:hints]
|
19
17
|
|
20
|
-
@user.
|
21
|
-
@secret_code.shuffle[@user.
|
18
|
+
@user.hints_used += 1
|
19
|
+
@secret_code.shuffle[@user.hints_used]
|
22
20
|
end
|
23
21
|
|
24
|
-
def respond_to_guess(input
|
25
|
-
@user.
|
26
|
-
status = status(input,
|
27
|
-
resolved_code = GuessHandler.new(input,
|
28
|
-
{ status: status,
|
29
|
-
end
|
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'
|
30
27
|
|
31
|
-
|
32
|
-
@user.attempts[:used] += 1
|
28
|
+
{ status: status, response: resolved_code, secret_code: code }
|
33
29
|
end
|
34
30
|
|
35
31
|
def status(input, code)
|
36
|
-
return 'loss' if @user.
|
32
|
+
return 'loss' if @user.attempts_used >= @user.level[:attempts]
|
37
33
|
return 'win' if input.each_char.map(&:to_i) == code
|
38
34
|
|
39
35
|
'in_game'
|
@@ -3,47 +3,36 @@
|
|
3
3
|
require_relative 'autoload'
|
4
4
|
|
5
5
|
class GuessHandler
|
6
|
+
include Settings
|
7
|
+
|
6
8
|
def initialize(input, code)
|
7
|
-
|
9
|
+
Validations.validate_guess(input)
|
8
10
|
@input = input.each_char.map(&:to_i)
|
9
11
|
@code = code.clone
|
10
12
|
@result = []
|
11
13
|
end
|
12
14
|
|
13
15
|
def call
|
14
|
-
|
15
|
-
|
16
|
+
check_same_indexes
|
17
|
+
check_different_indexes
|
16
18
|
@result
|
17
19
|
end
|
18
20
|
|
19
|
-
def
|
20
|
-
input.
|
21
|
-
next unless
|
21
|
+
def check_same_indexes
|
22
|
+
@input.each_index do |index|
|
23
|
+
next unless @input[index] == @code[index]
|
22
24
|
|
23
|
-
@result << Settings::
|
24
|
-
input[
|
25
|
-
code[index] = ''
|
25
|
+
@result << Settings::POSITIVE_RESPONSE
|
26
|
+
@input[index], @code[index] = nil
|
26
27
|
end
|
27
|
-
input.delete('')
|
28
|
-
code.delete('')
|
29
28
|
end
|
30
29
|
|
31
|
-
def
|
32
|
-
input.each_with_index do |
|
33
|
-
next unless code.include?(
|
30
|
+
def check_different_indexes
|
31
|
+
@input.each_with_index do |value, _index|
|
32
|
+
next unless !value.nil? && @code.include?(value)
|
34
33
|
|
35
|
-
@result << Settings::
|
36
|
-
input[input.find_index(
|
37
|
-
code[code.find_index(element)] = ''
|
34
|
+
@result << Settings::NEGATIVE_RESPONSE
|
35
|
+
@input[@input.find_index(value)], @code[@code.find_index(value)] = nil
|
38
36
|
end
|
39
37
|
end
|
40
|
-
|
41
|
-
private
|
42
|
-
|
43
|
-
def validation(input)
|
44
|
-
Validations.validate_guess(input)
|
45
|
-
rescue StandardError => e
|
46
|
-
puts e.message
|
47
|
-
nil
|
48
|
-
end
|
49
38
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require_relative 'autoload'
|
4
|
+
|
5
|
+
module Settings
|
4
6
|
DIFFICULTY = {
|
5
7
|
'easy' => { attempts: 15, hints: 2 },
|
6
8
|
'medium' => { attempts: 10, hints: 1 },
|
@@ -8,6 +10,9 @@ class Settings
|
|
8
10
|
}.freeze
|
9
11
|
CODE_LENGTH = 4
|
10
12
|
RANDOM_RANGE = (1..6).freeze
|
11
|
-
|
12
|
-
|
13
|
+
POSITIVE_RESPONSE = '+'
|
14
|
+
NEGATIVE_RESPONSE = '-'
|
15
|
+
NAME_MIN_LENGTH = 3
|
16
|
+
NAME_MAX_LENGTH = 20
|
17
|
+
GUESS_LENGTH = 4
|
13
18
|
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require_relative 'user'
|
3
|
+
require_relative 'autoload'
|
5
4
|
|
6
5
|
class Stats
|
7
6
|
def self.save_game(user)
|
@@ -13,20 +12,6 @@ class Stats
|
|
13
12
|
|
14
13
|
def self.show_stats
|
15
14
|
@data = File.exist?('stats.yml') ? YAML.load_file('stats.yml') : []
|
16
|
-
@data.sort_by! { |user| [user.
|
17
|
-
@data.each_with_index do |user, index|
|
18
|
-
stats_format(user, index)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.stats_format(user, index)
|
23
|
-
index += 1
|
24
|
-
puts "Rating: #{index}", "Name: #{user.name}",
|
25
|
-
"Difficulty: #{user.difficulty}",
|
26
|
-
"Available Attempts: #{user.attempts[:all]}",
|
27
|
-
"Used Attempts: #{user.attempts[:used]}",
|
28
|
-
"Available Hints: #{user.hints[:all]}",
|
29
|
-
"Used Hints: #{user.hints[:used]}",
|
30
|
-
'-----------------------'
|
15
|
+
@data.sort_by! { |user| [user.level[:attempts], user.attempts_used, user.hints_used] }
|
31
16
|
end
|
32
17
|
end
|
@@ -1,18 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative 'autoload'
|
4
|
-
require_relative 'settings'
|
5
4
|
|
6
5
|
class User
|
7
|
-
|
6
|
+
include Settings
|
7
|
+
|
8
|
+
attr_accessor :attempts_used, :hints_used
|
9
|
+
attr_reader :difficulty, :name, :level
|
8
10
|
|
9
11
|
def initialize(name, difficulty)
|
10
12
|
validation(name, difficulty)
|
11
13
|
@difficulty = difficulty
|
12
14
|
@name = name
|
13
15
|
@level = Settings::DIFFICULTY[@difficulty]
|
14
|
-
@
|
15
|
-
@
|
16
|
+
@attempts_used = 0
|
17
|
+
@hints_used = 0
|
16
18
|
end
|
17
19
|
|
18
20
|
def validation(name, difficulty)
|
@@ -1,23 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'autoload'
|
4
|
+
|
3
5
|
class Validations
|
6
|
+
include Settings
|
7
|
+
|
4
8
|
def self.validate_name(name)
|
5
9
|
raise "Name shouldn't be empty" if name.empty?
|
6
10
|
raise 'Name should be string' if name.class != String
|
7
|
-
raise 'Name should be at least 3 characters long' if name.length <
|
8
|
-
raise "Name shouldn't be more than 20 characters long" if name.length >
|
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
|
9
13
|
end
|
10
14
|
|
11
15
|
def self.validate_guess(guess)
|
12
16
|
raise "Guess shouldn't be empty" if guess.empty?
|
13
|
-
raise
|
14
|
-
raise 'Guess shouldn\'t be more than 4 characters long' if guess.length >
|
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
|
15
19
|
end
|
16
20
|
|
17
21
|
def self.validate_difficulty(difficulty)
|
18
22
|
raise "Input shouldn't be empty" if difficulty.empty?
|
19
23
|
return if Settings::DIFFICULTY.keys.include?(difficulty)
|
20
24
|
|
21
|
-
raise
|
25
|
+
raise "You should enter one of the following options: #{Settings::DIFFICULTY.keys.join(', ')}"
|
22
26
|
end
|
23
27
|
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: 1.
|
4
|
+
version: 1.2.3
|
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-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|