codebreaker_bo 0.1.3 → 0.1.4
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/data/statistic.yml +0 -0
- data/lib/codebreaker/base_class.rb +16 -4
- data/lib/codebreaker/constant.rb +1 -1
- data/lib/codebreaker/game.rb +43 -26
- data/lib/codebreaker/test_console.rb +16 -0
- data/lib/codebreaker/user.rb +2 -12
- data/lib/codebreaker/validation.rb +28 -0
- data/lib/codebreaker/version.rb +1 -1
- data/lib/codebreaker.rb +1 -0
- data/s +14 -0
- metadata +6 -3
- data/lib/codebreaker/console.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88a70ddf89ed560f9d096907eac75c768cef0b7a4c27e52b453905bef4679049
|
4
|
+
data.tar.gz: 27a3f505dc0024cff4175d0f1c7c0b144a62d79f9121fd4287f71715860ac5bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f48bea86b1be586809aef0121ec82c492d4b077e7292423affe69814a55d4dbdc504dfede511c6c2d4759a426c287b7feb7e69447c057be46a57505b3293433
|
7
|
+
data.tar.gz: c6d829aecc343d4a5bf3cd4ff306051b9c1327276cb36749c44ec76a2bf1399c8c08ec17d83f75568111d35e36dedbd5f51852576551c2e9d6b10cfb69544647
|
data/data/statistic.yml
ADDED
File without changes
|
@@ -1,15 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'validation'
|
3
4
|
module Codebreaker
|
4
5
|
class BaseClass
|
5
|
-
|
6
|
-
|
6
|
+
include Validator
|
7
|
+
attr_accessor :errors
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@errors = []
|
7
11
|
end
|
8
12
|
|
9
|
-
|
13
|
+
def handle_errors(text)
|
14
|
+
@errors << text
|
15
|
+
end
|
10
16
|
|
11
17
|
def show_errors
|
12
|
-
|
18
|
+
data = errors.flatten
|
19
|
+
clear_errors
|
20
|
+
data
|
21
|
+
end
|
22
|
+
|
23
|
+
def clear_errors
|
24
|
+
@errors = []
|
13
25
|
end
|
14
26
|
end
|
15
27
|
end
|
data/lib/codebreaker/constant.rb
CHANGED
data/lib/codebreaker/game.rb
CHANGED
@@ -1,32 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
3
|
module Codebreaker
|
5
4
|
class Game < BaseClass
|
6
5
|
attr_accessor :user_hints
|
7
|
-
attr_reader :secret_code, :total_attempts, :used_attempts, :level, :
|
6
|
+
attr_reader :secret_code, :total_attempts, :used_attempts, :level, :stats
|
8
7
|
|
9
8
|
def initialize
|
10
|
-
@errors = []
|
11
|
-
@secret_code = generate_code
|
12
9
|
super()
|
10
|
+
@secret_code = generate_code
|
13
11
|
end
|
14
12
|
|
15
13
|
def create_settings(name, level)
|
16
14
|
player_name(name)
|
17
15
|
create_level(level)
|
18
|
-
|
16
|
+
|
17
|
+
show_errors unless valid?
|
19
18
|
end
|
20
19
|
|
21
20
|
def player_name(name)
|
22
|
-
|
21
|
+
user = User.new(name)
|
22
|
+
return @player_name = user.name if user.valid?
|
23
|
+
|
24
|
+
errors << user.errors
|
23
25
|
end
|
24
26
|
|
25
27
|
def accept_level(level)
|
26
28
|
level = level.upcase
|
27
29
|
Object.const_get "Constants::#{level}"
|
28
30
|
rescue StandardError
|
29
|
-
|
31
|
+
handle_errors('entered level is not present')
|
30
32
|
nil
|
31
33
|
end
|
32
34
|
|
@@ -42,15 +44,13 @@ module Codebreaker
|
|
42
44
|
end
|
43
45
|
|
44
46
|
def check_guess(numbers)
|
45
|
-
return
|
47
|
+
return show_errors unless guess_valid?(numbers)
|
46
48
|
|
47
49
|
decrease_attempts
|
48
50
|
array_choose_numbers = numbers.to_s.each_char.map(&:to_i)
|
49
51
|
return win if secret_code == array_choose_numbers
|
50
52
|
|
51
|
-
|
52
|
-
codebreaker_result(array_choose_numbers, result)
|
53
|
-
result.join
|
53
|
+
codebreaker_result(array_choose_numbers)
|
54
54
|
end
|
55
55
|
|
56
56
|
def use_hint
|
@@ -60,39 +60,55 @@ module Codebreaker
|
|
60
60
|
secret_code.sample
|
61
61
|
end
|
62
62
|
|
63
|
-
def
|
64
|
-
stats = {
|
63
|
+
def collect_statistic
|
64
|
+
@stats = [{
|
65
65
|
user_name: @player_name,
|
66
66
|
game_status: 'WIN',
|
67
67
|
difficulty: level,
|
68
68
|
attempts_total: total_attempts,
|
69
69
|
attempts_used: used_attempts,
|
70
|
-
hints_total: user_hints
|
71
|
-
|
70
|
+
hints_total: user_hints,
|
71
|
+
hints_used: use_hint
|
72
|
+
}]
|
73
|
+
save_statistic
|
74
|
+
end
|
72
75
|
|
73
|
-
|
76
|
+
def show_statistic
|
77
|
+
stats_load
|
78
|
+
multi_sort
|
79
|
+
@items.each_with_index.map do |stat, index|
|
80
|
+
[index.next, stat[:user_name], stat[:difficulty], stat[:attempts_total],
|
81
|
+
stat[:attempts_used], stat[:hints_total], stat[:hints_total]]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def win
|
86
|
+
"#{Constants::PLUS * Constants::WIN_NUMBER}(win)"
|
74
87
|
end
|
75
88
|
|
76
89
|
private
|
77
90
|
|
78
|
-
def
|
79
|
-
|
91
|
+
def save_statistic
|
92
|
+
yaml_data = YAML.load_file('./data/statistic.yml')
|
93
|
+
yaml_data = [] if yaml_data.nil?
|
94
|
+
yaml_data << stats
|
95
|
+
File.open('./data/statistic.yml', 'r+') { |file| file.write YAML.dump(yaml_data.flatten) }
|
96
|
+
end
|
80
97
|
|
81
|
-
|
82
|
-
@
|
98
|
+
def stats_load
|
99
|
+
@items = YAML.load_file('./data/statistic.yml')
|
100
|
+
end
|
83
101
|
|
84
|
-
|
102
|
+
def multi_sort
|
103
|
+
@items.sort_by! { |game| [game[:difficulty], game[:attempts_used], game[:hints_total]] }
|
85
104
|
end
|
86
105
|
|
87
106
|
def generate_code
|
88
107
|
Array.new(Constants::NUMBER_OF_DIGITS) { rand Constants::RANGE }
|
89
108
|
end
|
90
109
|
|
91
|
-
def
|
92
|
-
|
93
|
-
end
|
94
|
-
|
95
|
-
def codebreaker_result(array_choose_numbers, result)
|
110
|
+
def codebreaker_result(array_choose_numbers)
|
111
|
+
result = []
|
96
112
|
array_choose_numbers.each_with_index do |code, index|
|
97
113
|
next unless code == secret_code[index]
|
98
114
|
|
@@ -100,6 +116,7 @@ module Codebreaker
|
|
100
116
|
array_choose_numbers[index] = nil
|
101
117
|
end
|
102
118
|
array_choose_numbers.compact.uniq.each { |number| result << Constants::MINUS if secret_code.include? number }
|
119
|
+
result.join
|
103
120
|
end
|
104
121
|
|
105
122
|
def decrease_attempts
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# require 'yaml'
|
4
|
+
#
|
5
|
+
# require 'codebreaker'
|
6
|
+
# require_relative 'console'
|
7
|
+
# require 'pry'
|
8
|
+
#
|
9
|
+
# c = Console.new
|
10
|
+
# c.choose_action
|
11
|
+
# c.multi_sort
|
12
|
+
# c.take_stats
|
13
|
+
|
14
|
+
# p c.registration
|
15
|
+
# p c
|
16
|
+
# p c.game_process
|
data/lib/codebreaker/user.rb
CHANGED
@@ -2,22 +2,12 @@
|
|
2
2
|
|
3
3
|
module Codebreaker
|
4
4
|
class User < BaseClass
|
5
|
-
attr_accessor :errors
|
6
5
|
attr_reader :name
|
7
6
|
|
8
7
|
def initialize(player_name)
|
9
|
-
@errors = []
|
10
|
-
validation_name(player_name)
|
11
|
-
@errors.empty? ? @name = player_name : show_errors
|
12
8
|
super()
|
13
|
-
|
14
|
-
|
15
|
-
def validation_name(player_name)
|
16
|
-
if player_name.is_a?(String)
|
17
|
-
@errors << 'error min length or max length' unless Constants::LENGTH_RANGE.include?(player_name.length)
|
18
|
-
else
|
19
|
-
@errors << 'name is not string'
|
20
|
-
end
|
9
|
+
validation_name(player_name)
|
10
|
+
@name = player_name if valid?
|
21
11
|
end
|
22
12
|
end
|
23
13
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
module Validator
|
5
|
+
def valid?
|
6
|
+
errors.empty?
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def guess_valid?(match_code)
|
12
|
+
handle_errors('match code not number') unless match_code.is_a?(Numeric)
|
13
|
+
|
14
|
+
input_data = match_code.to_s
|
15
|
+
handle_errors('invalid_input_data') unless input_data =~ Constants::DESIRED_NUMBER
|
16
|
+
|
17
|
+
valid?
|
18
|
+
end
|
19
|
+
|
20
|
+
def validation_name(player_name)
|
21
|
+
if player_name.is_a?(String)
|
22
|
+
errors << 'error min length or max length' unless Constants::LENGTH_RANGE.include?(player_name.length)
|
23
|
+
else
|
24
|
+
errors << 'name is not string'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/codebreaker/version.rb
CHANGED
data/lib/codebreaker.rb
CHANGED
data/s
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
|
2
|
+
[1mFrom:[0m /home/bo/rubypr/codebreaker/lib/codebreaker/console.rb:95 Console#game_process:
|
3
|
+
|
4
|
+
[1;34m91[0m: [32mdef[0m [1;34mgame_process[0m
|
5
|
+
[1;34m92[0m: [32mif[0m !game.used_attempts.nil? && !game.used_attempts.zero?
|
6
|
+
[1;34m93[0m: game_actions
|
7
|
+
[1;34m94[0m: [32melse[0m
|
8
|
+
=> [1;34m95[0m: binding.pry
|
9
|
+
[1;34m96[0m: p [31m[1;31m'[0m[31mattempts finished[1;31m'[0m[31m[0m
|
10
|
+
[1;34m97[0m: lose
|
11
|
+
[1;34m98[0m: new_process
|
12
|
+
[1;34m99[0m: [32mend[0m
|
13
|
+
[1;34m100[0m: [32mend[0m
|
14
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codebreaker_bo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oladko Bohdan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -172,14 +172,17 @@ files:
|
|
172
172
|
- LICENSE.txt
|
173
173
|
- README.md
|
174
174
|
- Rakefile
|
175
|
+
- data/statistic.yml
|
175
176
|
- lib/codebreaker.rb
|
176
177
|
- lib/codebreaker/base_class.rb
|
177
|
-
- lib/codebreaker/console.rb
|
178
178
|
- lib/codebreaker/constant.rb
|
179
179
|
- lib/codebreaker/game.rb
|
180
180
|
- lib/codebreaker/statistic.yml
|
181
|
+
- lib/codebreaker/test_console.rb
|
181
182
|
- lib/codebreaker/user.rb
|
183
|
+
- lib/codebreaker/validation.rb
|
182
184
|
- lib/codebreaker/version.rb
|
185
|
+
- s
|
183
186
|
- sig/codebreaker.rbs
|
184
187
|
homepage: https://github.com/bogdansev1/codebreaker_gem
|
185
188
|
licenses:
|
data/lib/codebreaker/console.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'codebreaker'
|
4
|
-
#
|
5
|
-
# u = Codebreaker::User.new('Bpgdan')
|
6
|
-
# p u
|
7
|
-
|
8
|
-
g = Codebreaker::Game.new
|
9
|
-
# name = gets.chomp
|
10
|
-
# level = gets.chomp
|
11
|
-
p g
|
12
|
-
g.create_settings('Vkjkk', 'easy')
|
13
|
-
p g
|
14
|
-
# p g.check_guess(4444)
|
15
|
-
# p g.used_attempts
|