gem_codebreaker_amidasd 0.1.8 → 0.1.9

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: dd057153601e7d60b3aa17c1c5682568ce4b83f0f171533030cb9c8cd84ff467
4
- data.tar.gz: 1ec08c3b80147a81c074fc788f2b360aa58cd4dba2ec4cb3780310fe040ed3bf
3
+ metadata.gz: 5b392407822fdf8a8de4a5c82a586cef805e59e3cc0886a23aaad729ec6e514e
4
+ data.tar.gz: 3ed5f6a1a6cb8930a939a57dd69a408610252b6fa3760169aa9632003d3d6996
5
5
  SHA512:
6
- metadata.gz: cf9b9f40c32b081bd7038ef6e7d9c46f11427c4f3000dc8dc5df6fb34b6af8713b17a14d49da7dcedd9c489f2edeb82017aedef2d40a05b13feccbc4e82d2ee1
7
- data.tar.gz: f9326d6f38d304da9952ce1783b9379f6b53625af7e87049d05baa1759319c3a5f1225064fc0ebc68691fa40df99a3547954f0e7654cfa396ddf1b203bb2d5e4
6
+ metadata.gz: 446853aa06ec6cf502b5f871c81bb651a9fc7e070143a73e68cc9dc74c633e8eaa6abe0f58b504d8446cbd034bea9f92235b87a296cded7ec751a1d450a0362b
7
+ data.tar.gz: 84e3dac8acc19ac7e578bfe3b125e75b39d59743bc85b65cd66445ce69f027b2615cff20cfd29af2c34551c9e13d69a57061317863d3386ea971138dd3e7739f
Binary file
@@ -10,9 +10,7 @@ module GemCodebreakerAmidasd
10
10
  end
11
11
 
12
12
  def load_yaml_db(yml_db = PATH_CODEBREAKER_DB)
13
- return YAML.load_file(yml_db) if File.exist?(yml_db)
14
-
15
- []
13
+ return YAML.load_file(yml_db) if File.exist?(yml_db); []
16
14
  end
17
15
 
18
16
  def add_in_db(array:, user:, game:)
@@ -19,7 +19,10 @@ module GemCodebreakerAmidasd
19
19
 
20
20
  def initialize(other_difficulty: {})
21
21
  @difficulty_hash = DIFFICULTY_HASH.merge(other_difficulty)
22
- @array_hints = []
22
+ @total_count_attempt = 0
23
+ @total_count_hints = 0
24
+ @count_plus = 0
25
+ @count_minus = 0
23
26
  @length_code = 4
24
27
  @min_num = 1
25
28
  @max_num = 6
@@ -35,8 +38,6 @@ module GemCodebreakerAmidasd
35
38
  def setDifficulty(difficulty)
36
39
  return unless @difficulty_hash.key? difficulty
37
40
 
38
- generate_secret_code
39
- secret_code_shuffle
40
41
  @difficulty = difficulty
41
42
  @total_count_attempt = @difficulty_hash[difficulty][:total_count_attempt]
42
43
  @total_count_hints = @difficulty_hash[difficulty][:total_count_hints]
@@ -45,53 +46,47 @@ module GemCodebreakerAmidasd
45
46
  def gets_hint
46
47
  empty_result
47
48
  return add_error(ERRORS[:HintsEnd]) unless @total_count_hints > @count_hints
48
- return add_error(ERRORS[:NoCluesAvailable]) unless @secret_code_rand
49
+ return add_error(ERRORS[:NoCluesAvailable]) unless secret_code_rand
49
50
 
50
51
  @count_hints += 1
51
- @hint = @secret_code_rand.pop
52
+ @hint = secret_code_rand.pop
52
53
  end
53
54
 
54
- def secret_code_shuffle
55
- @secret_code_rand = @secret_code.clone
56
- @secret_code_rand.shuffle!
57
- @secret_code_rand.uniq!
55
+ def secret_code
56
+ @secret_code ||= Array.new(@length_code) { rand(@min_num..@max_num) }
58
57
  end
59
58
 
60
- def generate_secret_code
61
- @secret_code = []
62
- @length_code.times do
63
- @secret_code << rand(@min_num..@max_num)
64
- end
59
+ def secret_code_rand
60
+ @secret_code_rand ||= secret_code.clone.shuffle!
65
61
  end
66
62
 
67
63
  def guess_code(code_attempt)
68
64
  empty_result
69
65
  input_code = to_array(code_attempt)
66
+ return @status = STATUS[:win] if input_code == secret_code
67
+
70
68
  return add_error(ERRORS[:WrongCode]) unless valid_code? input_code
71
69
 
72
- cache_secret_code = secret_code.clone
73
- prepare_arrays(input_code, cache_secret_code)
74
- @status = STATUS[:win] if cache_secret_code.empty?
70
+ check_results(input_code)
75
71
  @count_attempt += 1
76
- @status = STATUS[:lose] if @total_count_attempt <= @count_attempt && @status == STATUS[:process_game]
72
+ @status = STATUS[:lose] if @total_count_attempt <= @count_attempt
77
73
  end
78
74
 
79
- def prepare_arrays(input_code, cache_secret_code)
80
- del_index = []
81
- @secret_code.each_with_index do |val, index|
82
- del_index << index if val == input_code[index]
83
- @count_plus += 1 if val == input_code[index]
75
+ private
76
+
77
+ def check_results(input_code)
78
+ secret_code_temp = secret_code.map.with_index do |value,key|
79
+ next input_code[key] = nil if value == input_code[key]; value
84
80
  end
85
81
 
86
- del_index.reverse.each do |index|
87
- input_code.delete_at(index)
88
- cache_secret_code.delete_at(index)
82
+ @count_plus = secret_code_temp.count(nil)
83
+ secret_code_temp = secret_code_temp.map.with_index do |value, |
84
+ next nil if value && input_code.include?(value)
85
+ value
89
86
  end
90
- cache_secret_code.each { |value| @count_minus += 1 if input_code.include?(value) }
87
+ @count_minus = secret_code_temp.count(nil) - @count_plus
91
88
  end
92
89
 
93
- private
94
-
95
90
  def empty_result
96
91
  @error = nil
97
92
  @count_plus = 0
@@ -108,7 +103,7 @@ module GemCodebreakerAmidasd
108
103
  end
109
104
 
110
105
  def to_array(str)
111
- str.to_i.digits.reverse
106
+ str.chars.map(&:to_i)
112
107
  end
113
108
  end
114
109
  end
@@ -1,9 +1,10 @@
1
1
  module GemCodebreakerAmidasd
2
2
  class Statistic
3
- attr_reader :user, :game
3
+ attr_reader :user, :game, :date
4
4
  def initialize(user:, game:)
5
5
  @user = user
6
6
  @game = game
7
+ @date = DateTime.now
7
8
  end
8
9
 
9
10
  def self.sort_array(array)
@@ -10,7 +10,7 @@ module GemCodebreakerAmidasd
10
10
  end
11
11
 
12
12
  def self.valid_name?(name:)
13
- name.is_a?(String) && !name.empty? && name.length.between?(MIN_LENGTH_NAME, MAX_LENGTH_NAME)
13
+ name.length.between?(MIN_LENGTH_NAME, MAX_LENGTH_NAME)
14
14
  end
15
15
  end
16
16
  end
@@ -1,3 +1,3 @@
1
1
  module GemCodebreakerAmidasd
2
- VERSION = '0.1.8'.freeze
2
+ VERSION = '0.1.9'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gem_codebreaker_amidasd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amidasd
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-16 00:00:00.000000000 Z
11
+ date: 2019-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -136,6 +136,7 @@ files:
136
136
  - gem_codebreaker_amidasd-0.1.5.gem
137
137
  - gem_codebreaker_amidasd-0.1.6.gem
138
138
  - gem_codebreaker_amidasd-0.1.7.gem
139
+ - gem_codebreaker_amidasd-0.1.8.gem
139
140
  - gem_codebreaker_amidasd.gemspec
140
141
  - lib/gem_codebreaker_amidasd.rb
141
142
  - lib/gem_codebreaker_amidasd/db_utility.rb