codebreaker_bo 0.1.4 → 0.1.5
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/data/statistic.yml +13 -0
- data/e_attempts +20 -0
- data/lib/codebreaker/constant.rb +2 -1
- data/lib/codebreaker/game.rb +31 -51
- data/lib/codebreaker/statistic.rb +33 -0
- data/lib/codebreaker/version.rb +1 -1
- data/lib/codebreaker.rb +1 -0
- metadata +4 -4
- data/lib/codebreaker/statistic.yml +0 -0
- data/lib/codebreaker/test_console.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9944c5101dc90e5ff2ef8fa18e8692c76a17ee53377560ed1004d7ccf0431fd8
|
4
|
+
data.tar.gz: 9050baa0eeaaaee224a1b458d4c71eeff6269b57645eab0f35d33ca3901a60e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e40e2ff6dee2172547206c1bb62da931b411675c4ba0a88c431a031d7d844666011080af6210406c2143fbd41c1aa22d302f2743d173e55c64e722c748b1b250
|
7
|
+
data.tar.gz: eccc9f39f593bd893e8f8b61fa9c6fee27a0fdc549d3d0c5c2b9963c4435319c60c783559cd18ec59a294fb898d45a57e6cfcfab199a91e8e4e86804b91ef959
|
data/Gemfile.lock
CHANGED
data/data/statistic.yml
CHANGED
data/e_attempts
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
[1mFrom:[0m /home/bo/rubypr/codebreaker/lib/codebreaker/Console.rb:75 Console#game_process:
|
3
|
+
|
4
|
+
[1;34m74[0m: [32mdef[0m [1;34mgame_process[0m
|
5
|
+
=> [1;34m75[0m: binding.pry
|
6
|
+
[1;34m76[0m: [32mif[0m !game.decrease_attempts == game.secret_code && [31m[1;31m'[0m[31mlose[1;31m'[0m[31m[0m
|
7
|
+
[1;34m77[0m: p game
|
8
|
+
[1;34m78[0m: game_actions
|
9
|
+
[1;34m79[0m: [32mend[0m
|
10
|
+
[1;34m80[0m:
|
11
|
+
[1;34m81[0m: [1;34m# if !game.used_attempts.nil? && !game.used_attempts.zero?[0m
|
12
|
+
[1;34m82[0m: [1;34m# p game[0m
|
13
|
+
[1;34m83[0m: [1;34m# game_actions[0m
|
14
|
+
[1;34m84[0m: [1;34m# else[0m
|
15
|
+
[1;34m85[0m: [1;34m# p 'attempts finished'[0m
|
16
|
+
[1;34m86[0m: [1;34m# lose[0m
|
17
|
+
[1;34m87[0m: [1;34m# new_process[0m
|
18
|
+
[1;34m88[0m: [1;34m# end[0m
|
19
|
+
[1;34m89[0m: [32mend[0m
|
20
|
+
|
data/lib/codebreaker/constant.rb
CHANGED
@@ -6,7 +6,7 @@ module Constants
|
|
6
6
|
|
7
7
|
MEDIUM = { attempts: 10,
|
8
8
|
hints: 1 }.freeze
|
9
|
-
HELL = { attempts:
|
9
|
+
HELL = { attempts: 5,
|
10
10
|
hints: 1 }.freeze
|
11
11
|
PLUS = '+'
|
12
12
|
MINUS = '-'
|
@@ -15,4 +15,5 @@ module Constants
|
|
15
15
|
LENGTH_RANGE = (3..20).freeze
|
16
16
|
RANGE = (1..6).freeze
|
17
17
|
NUMBER_OF_DIGITS = 4
|
18
|
+
PATH = './data/statistic.yml'
|
18
19
|
end
|
data/lib/codebreaker/game.rb
CHANGED
@@ -2,12 +2,28 @@
|
|
2
2
|
|
3
3
|
module Codebreaker
|
4
4
|
class Game < BaseClass
|
5
|
-
|
6
|
-
attr_reader :secret_code, :total_attempts, :used_attempts, :level, :stats
|
5
|
+
attr_reader :secret_code, :total_attempts, :used_attempts, :total_hints, :used_hints, :stats, :level
|
7
6
|
|
8
7
|
def initialize
|
9
8
|
super()
|
10
9
|
@secret_code = generate_code
|
10
|
+
@stat = Statistic.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def decrease_attempts
|
14
|
+
return secret_code && 'lose' if @used_attempts.zero?
|
15
|
+
|
16
|
+
@used_attempts -= 1
|
17
|
+
end
|
18
|
+
|
19
|
+
def save_stats
|
20
|
+
hash = { user_name: @player_name, difficulty: level, attempts_total: total_attempts, attempts_used: used_attempts,
|
21
|
+
hints_total: total_hints, hints_used: used_hints }
|
22
|
+
@stat.collect_statistic(hash)
|
23
|
+
end
|
24
|
+
|
25
|
+
def load_stats
|
26
|
+
@stat.show_statistic
|
11
27
|
end
|
12
28
|
|
13
29
|
def create_settings(name, level)
|
@@ -38,7 +54,8 @@ module Codebreaker
|
|
38
54
|
return unless @level.nil?
|
39
55
|
|
40
56
|
@level = level
|
41
|
-
@
|
57
|
+
@used_hints = choice_level[:hints]
|
58
|
+
@total_hints = choice_level[:hints]
|
42
59
|
@total_attempts = choice_level[:attempts]
|
43
60
|
@used_attempts = choice_level[:attempts]
|
44
61
|
end
|
@@ -46,83 +63,46 @@ module Codebreaker
|
|
46
63
|
def check_guess(numbers)
|
47
64
|
return show_errors unless guess_valid?(numbers)
|
48
65
|
|
49
|
-
decrease_attempts
|
50
66
|
array_choose_numbers = numbers.to_s.each_char.map(&:to_i)
|
51
67
|
return win if secret_code == array_choose_numbers
|
52
68
|
|
69
|
+
decrease_attempts
|
53
70
|
codebreaker_result(array_choose_numbers)
|
54
71
|
end
|
55
72
|
|
56
73
|
def use_hint
|
57
|
-
return 'Hints are over' if @
|
74
|
+
return 'Hints are over' if @used_hints.zero?
|
58
75
|
|
59
|
-
@
|
76
|
+
@used_hints -= 1
|
60
77
|
secret_code.sample
|
61
78
|
end
|
62
79
|
|
63
|
-
def collect_statistic
|
64
|
-
@stats = [{
|
65
|
-
user_name: @player_name,
|
66
|
-
game_status: 'WIN',
|
67
|
-
difficulty: level,
|
68
|
-
attempts_total: total_attempts,
|
69
|
-
attempts_used: used_attempts,
|
70
|
-
hints_total: user_hints,
|
71
|
-
hints_used: use_hint
|
72
|
-
}]
|
73
|
-
save_statistic
|
74
|
-
end
|
75
|
-
|
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
80
|
def win
|
86
81
|
"#{Constants::PLUS * Constants::WIN_NUMBER}(win)"
|
87
82
|
end
|
88
83
|
|
89
84
|
private
|
90
85
|
|
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
|
97
|
-
|
98
|
-
def stats_load
|
99
|
-
@items = YAML.load_file('./data/statistic.yml')
|
100
|
-
end
|
101
|
-
|
102
|
-
def multi_sort
|
103
|
-
@items.sort_by! { |game| [game[:difficulty], game[:attempts_used], game[:hints_total]] }
|
104
|
-
end
|
105
|
-
|
106
86
|
def generate_code
|
107
87
|
Array.new(Constants::NUMBER_OF_DIGITS) { rand Constants::RANGE }
|
108
88
|
end
|
109
89
|
|
110
90
|
def codebreaker_result(array_choose_numbers)
|
111
|
-
|
91
|
+
full_match = []
|
112
92
|
array_choose_numbers.each_with_index do |code, index|
|
113
93
|
next unless code == secret_code[index]
|
114
94
|
|
115
|
-
|
95
|
+
full_match << code
|
116
96
|
array_choose_numbers[index] = nil
|
117
97
|
end
|
118
|
-
array_choose_numbers.compact.uniq.
|
119
|
-
|
98
|
+
partial_match = array_choose_numbers.compact.uniq.select { |number| secret_code.include? number }
|
99
|
+
partial_match = partial_match.reject { |number| full_match.include? number }
|
100
|
+
create_result(full_match, partial_match)
|
120
101
|
end
|
121
102
|
|
122
|
-
def
|
123
|
-
|
124
|
-
|
125
|
-
@used_attempts -= 1
|
103
|
+
def create_result(full_match, partial_match)
|
104
|
+
result = full_match.map { Constants::PLUS } + partial_match.map { Constants::MINUS }
|
105
|
+
result.join
|
126
106
|
end
|
127
107
|
end
|
128
108
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
class Statistic
|
5
|
+
def collect_statistic(hash)
|
6
|
+
@stat = [{
|
7
|
+
user_name: hash[:user_name],
|
8
|
+
difficulty: hash[:difficulty],
|
9
|
+
attempts_total: hash[:attempts_total],
|
10
|
+
attempts_used: hash[:attempts_used],
|
11
|
+
hints_total: hash[:hints_total],
|
12
|
+
hints_used: hash[:hints_used]
|
13
|
+
}]
|
14
|
+
save_statistic
|
15
|
+
end
|
16
|
+
|
17
|
+
def show_statistic
|
18
|
+
@items = YAML.load_file(Constants::PATH)
|
19
|
+
@items.sort_by! { |game| [game[:difficulty], game[:attempts_used], game[:hints_used]] }
|
20
|
+
@items.each_with_index.map do |stat, index|
|
21
|
+
[index.next, stat[:user_name], stat[:difficulty], stat[:attempts_total],
|
22
|
+
stat[:attempts_used], stat[:hints_total], stat[:hints_used]]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def save_statistic
|
27
|
+
yaml_data = YAML.load_file(Constants::PATH)
|
28
|
+
yaml_data = [] if yaml_data.nil?
|
29
|
+
yaml_data << @stat
|
30
|
+
File.open(Constants::PATH, 'r+') { |file| file.write YAML.dump(yaml_data.flatten) }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/codebreaker/version.rb
CHANGED
data/lib/codebreaker.rb
CHANGED
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.5
|
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-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -173,12 +173,12 @@ files:
|
|
173
173
|
- README.md
|
174
174
|
- Rakefile
|
175
175
|
- data/statistic.yml
|
176
|
+
- e_attempts
|
176
177
|
- lib/codebreaker.rb
|
177
178
|
- lib/codebreaker/base_class.rb
|
178
179
|
- lib/codebreaker/constant.rb
|
179
180
|
- lib/codebreaker/game.rb
|
180
|
-
- lib/codebreaker/statistic.
|
181
|
-
- lib/codebreaker/test_console.rb
|
181
|
+
- lib/codebreaker/statistic.rb
|
182
182
|
- lib/codebreaker/user.rb
|
183
183
|
- lib/codebreaker/validation.rb
|
184
184
|
- lib/codebreaker/version.rb
|
File without changes
|
@@ -1,16 +0,0 @@
|
|
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
|