gem_codebreaker_amidasd 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 65f81d2ecb7263634807f5d042e8d513534dcf7420dd8469e77672078860060b
4
- data.tar.gz: c30e98b582e54cf7adea32993e74e9033359b532183a98de51f95b6a2025b2a0
3
+ metadata.gz: 0451e0d4dca3b9dc06d00238a954f85785a5d22ddc1152eec280a0232b114ef3
4
+ data.tar.gz: ecd2f7224e7498e9cb21efc75ff8b1fb0f745ad8b6fc749cee68e3d11b921c4e
5
5
  SHA512:
6
- metadata.gz: 3677c98735935df4d0cb7b48c864a59489138eec00bc7370aa54ae4fe4a2c5927f4087710840a352416d1d9ef4cbd2484aa26b91c21e4be55f836b2b6a6efe6e
7
- data.tar.gz: d5d987b906b62309208b98ca1339c29cd867809e6ff468d873a006e9bee5fd549748af18aab2c341868c2ffa0bfe7ebe11ce5153650ad329ef5e93d7b85fb786
6
+ metadata.gz: 913273f23abb4115d12a5989afd1acf6efbf4a96cf4349c118df2b6e746ecd4abaa575b55058e25bbe4ff8624d02cb8a72ceaed8e08846eb742425f70d00c808
7
+ data.tar.gz: 38221ef02935867890bdd3f494888c235861928103febd5605bda1fd280e5fc390ef14a8568fd9e9a515456aba70ccd73662ac529512aa2f6d5361cf6e7e2428
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gem_codebreaker_amidasd (0.1.6)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ coderay (1.1.2)
11
+ colorize (0.8.1)
12
+ diff-lcs (1.3)
13
+ docile (1.3.2)
14
+ fasterer (0.5.1)
15
+ colorize (~> 0.7)
16
+ ruby_parser (>= 3.13.0)
17
+ jaro_winkler (1.5.3)
18
+ json (2.2.0)
19
+ method_source (0.9.2)
20
+ parallel (1.17.0)
21
+ parser (2.6.3.0)
22
+ ast (~> 2.4.0)
23
+ pry (0.12.2)
24
+ coderay (~> 1.1.0)
25
+ method_source (~> 0.9.0)
26
+ rainbow (3.0.0)
27
+ rake (10.5.0)
28
+ rspec (3.8.0)
29
+ rspec-core (~> 3.8.0)
30
+ rspec-expectations (~> 3.8.0)
31
+ rspec-mocks (~> 3.8.0)
32
+ rspec-core (3.8.2)
33
+ rspec-support (~> 3.8.0)
34
+ rspec-expectations (3.8.4)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.8.0)
37
+ rspec-mocks (3.8.1)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.8.0)
40
+ rspec-support (3.8.2)
41
+ rubocop (0.69.0)
42
+ jaro_winkler (~> 1.5.1)
43
+ parallel (~> 1.10)
44
+ parser (>= 2.6)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 1.4.0, < 1.7)
48
+ ruby-progressbar (1.10.1)
49
+ ruby_parser (3.13.1)
50
+ sexp_processor (~> 4.9)
51
+ sexp_processor (4.12.1)
52
+ simplecov (0.16.1)
53
+ docile (~> 1.1)
54
+ json (>= 1.8, < 3)
55
+ simplecov-html (~> 0.10.0)
56
+ simplecov-html (0.10.2)
57
+ unicode-display_width (1.6.0)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ bundler (~> 2.0)
64
+ fasterer (~> 0.5.1)
65
+ gem_codebreaker_amidasd!
66
+ pry (~> 0.12.2)
67
+ rake (~> 10.0)
68
+ rspec (~> 3.8)
69
+ rubocop (~> 0.69.0)
70
+ simplecov (~> 0.16.1)
71
+
72
+ BUNDLED WITH
73
+ 2.0.2
@@ -1,4 +1,7 @@
1
1
  require 'gem_codebreaker_amidasd/version'
2
2
  require 'gem_codebreaker_amidasd/db_utility'
3
3
  require 'gem_codebreaker_amidasd/user'
4
- require 'gem_codebreaker_amidasd/gem_codebreaker'
4
+ require 'gem_codebreaker_amidasd/game'
5
+ require 'gem_codebreaker_amidasd/statistic'
6
+ module Codebreaker
7
+ end
@@ -2,23 +2,22 @@ module GemCodebreakerAmidasd
2
2
  require 'yaml'
3
3
 
4
4
  class DbUtility
5
+ PATH_CODEBREAKER_DB = './db/codebreaker_db.yml'.freeze
6
+
5
7
  class << self
6
- def save_yaml_db(user, yml_db)
7
- File.write(yml_db, user.to_yaml)
8
+ def save_yaml_db(array, yml_db = PATH_CODEBREAKER_DB)
9
+ File.write(yml_db, array.to_yaml)
8
10
  end
9
11
 
10
- def load_yaml_db(yml_db)
11
- return unless File.exist?(yml_db)
12
+ def load_yaml_db(yml_db = PATH_CODEBREAKER_DB)
13
+ return YAML.load_file(yml_db) if File.exist?(yml_db)
12
14
 
13
- YAML.load_file(yml_db)
15
+ []
14
16
  end
15
17
 
16
- def add_db(user, path)
17
- db = load_yaml_db(path)
18
- db ||= []
19
- db << user
20
- db = db.sort_by { |value| [value.total_count_attempt, value.count_attempt, value.count_hint] }
21
- save_yaml_db(db, path)
18
+ def add_in_db(array:, user:, game:)
19
+ stats = Statistic.new(user: user, game: game)
20
+ array << stats
22
21
  end
23
22
  end
24
23
  end
@@ -0,0 +1,113 @@
1
+ module GemCodebreakerAmidasd
2
+ DIFFICULTY_HASH = { easy: { total_count_attempt: 15, total_count_hints: 2 },
3
+ medium: { total_count_attempt: 10, total_count_hints: 1 },
4
+ hell: { total_count_attempt: 5, total_count_hints: 1 } }.freeze
5
+
6
+ ERRORS = { WrongCode: :WrongCode,
7
+ HintsEnd: :HintsEnd,
8
+ NoCluesAvailable: :NoCluesAvailable }.freeze
9
+
10
+ STATUS = { process_game: :process_game,
11
+ win: :win,
12
+ lose: :lose }.freeze
13
+
14
+ class Game
15
+ attr_reader :difficulty_hash, :difficulty
16
+ attr_reader :length_code, :max_num, :min_num
17
+ attr_reader :total_count_attempt, :total_count_hints, :error, :count_plus, :count_minus, :hint
18
+ attr_reader :count_attempt, :secret_code, :secret_code_rand, :count_hints, :status
19
+
20
+ def initialize(other_difficulty: {})
21
+ @difficulty_hash = DIFFICULTY_HASH.merge(other_difficulty)
22
+ @array_hints = []
23
+ @length_code = 4
24
+ @min_num = 1
25
+ @max_num = 6
26
+ @count_attempt = 0
27
+ @count_hints = 0
28
+ @status = STATUS[:process_game]
29
+ end
30
+
31
+ def string_secretcode
32
+ secret_code.join('')
33
+ end
34
+
35
+ def setDifficulty(difficulty)
36
+ return unless @difficulty_hash.key? difficulty
37
+
38
+ generate_secret_code
39
+ secret_code_shuffle
40
+ @difficulty = difficulty
41
+ @total_count_attempt = @difficulty_hash[difficulty][:total_count_attempt]
42
+ @total_count_hints = @difficulty_hash[difficulty][:total_count_hints]
43
+ end
44
+
45
+ def gets_hint
46
+ empty_result
47
+ return add_error(ERRORS[:HintsEnd]) unless @total_count_hints > @count_hints
48
+ return add_error(ERRORS[:NoCluesAvailable]) unless @secret_code_rand
49
+
50
+ @count_hints += 1
51
+ @hint = @secret_code_rand.pop
52
+ end
53
+
54
+ def secret_code_shuffle
55
+ @secret_code_rand = @secret_code.clone
56
+ @secret_code_rand.shuffle!
57
+ @secret_code_rand.uniq!
58
+ end
59
+
60
+ def generate_secret_code
61
+ @secret_code = []
62
+ @length_code.times do
63
+ @secret_code << rand(@min_num..@max_num)
64
+ end
65
+ end
66
+
67
+ def guess_code(code_attempt)
68
+ empty_result
69
+ input_code = to_array(code_attempt)
70
+ return add_error(ERRORS[:WrongCode]) unless valid_code? input_code
71
+
72
+ cache_secret_code = secret_code.clone
73
+ prepare_arrays(input_code, cache_secret_code)
74
+ status = STATUS[:win] unless cache_secret_code
75
+ @count_attempt += 1
76
+ @status = STATUS[:lose] if @total_count_attempt <= @count_attempt && status == STATUS[:process_game]
77
+ end
78
+
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
84
+ end
85
+ del_index.each do |index|
86
+ input_code.delete_at(index)
87
+ cache_secret_code.delete_at(index)
88
+ end
89
+ cache_secret_code.each { |value| @count_minus += 1 if input_code.include?(value) }
90
+ end
91
+
92
+ private
93
+
94
+ def empty_result
95
+ @error = nil
96
+ @count_plus = 0
97
+ @count_minus = 0
98
+ @hint = 0
99
+ end
100
+
101
+ def add_error(error)
102
+ (@error = error) && nil
103
+ end
104
+
105
+ def valid_code?(input)
106
+ input.size == length_code && input.all? { |number| number.between? min_num, max_num }
107
+ end
108
+
109
+ def to_array(str)
110
+ str.to_i.digits.reverse
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,13 @@
1
+ module GemCodebreakerAmidasd
2
+ class Statistic
3
+ attr_reader :user, :game
4
+ def initialize(user:, game:)
5
+ @user = user
6
+ @game = game
7
+ end
8
+
9
+ def self.sort_array(array)
10
+ array.sort_by! { |value| [value.game.total_count_attempt, value.game.count_attempt, value.game.count_hints] }
11
+ end
12
+ end
13
+ end
@@ -1,26 +1,16 @@
1
1
  module GemCodebreakerAmidasd
2
2
  class User
3
- attr_accessor :name,
4
- :difficulty,
5
- :total_count_attempt,
6
- :count_attempt,
7
- :total_count_hints,
8
- :count_hint
3
+ MIN_LENGTH_NAME = 3
4
+ MAX_LENGTH_NAME = 20
5
+
6
+ attr_reader :name
9
7
 
10
8
  def initialize(name:)
11
9
  @name = name
12
10
  end
13
11
 
14
- def set_params(difficulty:, total_count_attempt:, count_attempt:, total_count_hints:, count_hint:)
15
- @difficulty = difficulty
16
- @total_count_attempt = total_count_attempt
17
- @count_attempt = count_attempt
18
- @total_count_hints = total_count_hints
19
- @count_hint = count_hint
20
- end
21
-
22
- def self.validtion_name(name)
23
- name.is_a?(String) && !name.empty? && name.length.between?(3, 20)
12
+ def self.valid_name?(name:)
13
+ name.is_a?(String) && !name.empty? && name.length.between?(MIN_LENGTH_NAME, MAX_LENGTH_NAME)
24
14
  end
25
15
  end
26
16
  end
@@ -1,3 +1,3 @@
1
1
  module GemCodebreakerAmidasd
2
- VERSION = '0.1.5'.freeze
2
+ VERSION = '0.1.6'.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.5
4
+ version: 0.1.6
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-14 00:00:00.000000000 Z
11
+ date: 2019-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,7 @@ files:
122
122
  - ".travis.yml"
123
123
  - CODE_OF_CONDUCT.md
124
124
  - Gemfile
125
+ - Gemfile.lock
125
126
  - LICENSE.txt
126
127
  - README.md
127
128
  - Rakefile
@@ -133,10 +134,12 @@ files:
133
134
  - gem_codebreaker_amidasd-0.1.2.gem
134
135
  - gem_codebreaker_amidasd-0.1.3.gem
135
136
  - gem_codebreaker_amidasd-0.1.4.gem
137
+ - gem_codebreaker_amidasd-0.1.5.gem
136
138
  - gem_codebreaker_amidasd.gemspec
137
139
  - lib/gem_codebreaker_amidasd.rb
138
140
  - lib/gem_codebreaker_amidasd/db_utility.rb
139
- - lib/gem_codebreaker_amidasd/gem_codebreaker.rb
141
+ - lib/gem_codebreaker_amidasd/game.rb
142
+ - lib/gem_codebreaker_amidasd/statistic.rb
140
143
  - lib/gem_codebreaker_amidasd/user.rb
141
144
  - lib/gem_codebreaker_amidasd/version.rb
142
145
  homepage: https://github.com/Amidasd/gem_codebreaker_amidasd
@@ -158,8 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
161
  - !ruby/object:Gem::Version
159
162
  version: '0'
160
163
  requirements: []
161
- rubyforge_project:
162
- rubygems_version: 2.7.9
164
+ rubygems_version: 3.0.4
163
165
  signing_key:
164
166
  specification_version: 4
165
167
  summary: Codebreaker game
@@ -1,118 +0,0 @@
1
- module GemCodebreakerAmidasd
2
- DIFFICULTY_HASH = { easy: { total_count_attempt: 15, total_count_hints: 2 },
3
- medium: { total_count_attempt: 10, total_count_hints: 1 },
4
- hell: { total_count_attempt: 5, total_count_hints: 1 } }.freeze
5
-
6
- class GemCodebreaker
7
- attr_accessor :difficulty_hash, :difficulty
8
- attr_accessor :length_code, :max_num, :min_num
9
- attr_reader :total_count_attempt, :total_count_hints, :error, :win, :count_plus, :count_minus, :hint
10
- attr_reader :count_attempt, :secret_code, :array_hints
11
-
12
- def initialize(other_difficulty: {})
13
- @difficulty_hash = DIFFICULTY_HASH.merge(other_difficulty)
14
- @array_hints = []
15
- @length_code = 4
16
- @min_num = 1
17
- @max_num = 6
18
- @count_attempt = 0
19
- generate_secret_code
20
- empty_result
21
- end
22
-
23
- def string_secretcode
24
- @secret_code.join('')
25
- end
26
-
27
- def set_difficulty(difficulty)
28
- return unless @difficulty_hash.key? difficulty
29
-
30
- @difficulty = difficulty
31
- @total_count_attempt = @difficulty_hash[difficulty][:total_count_attempt]
32
- @total_count_hints = @difficulty_hash[difficulty][:total_count_hints]
33
- end
34
-
35
- def gets_hint
36
- return_hint
37
- end
38
-
39
- def generate_secret_code
40
- @secret_code = []
41
- @length_code.times do
42
- @secret_code << rand(@min_num..@max_num)
43
- end
44
- end
45
-
46
- def guess_code(code_attempt)
47
- check_code(code_attempt)
48
- end
49
-
50
- private
51
-
52
- def empty_result
53
- @error = nil
54
- @count_plus = 0
55
- @count_minus = 0
56
- @hint = 0
57
- end
58
-
59
- def return_hint
60
- empty_result
61
- cache_array = search_hint
62
- return unless @error.nil?
63
-
64
- max = cache_array.length
65
- rand_index = rand(0..(max - 1))
66
- @hint = cache_array[rand_index]
67
- @array_hints << @hint
68
- @hint
69
- end
70
-
71
- def search_hint
72
- cache_array = []
73
- @secret_code.each { |val| cache_array << val if !@array_hints.include?(val) && !cache_array.include?(val) }
74
- @error = :HintsEnd unless @total_count_hints > @array_hints.size
75
- @error = :NoCluesAvailable if cache_array.empty?
76
- cache_array
77
- end
78
-
79
- def check_code(code_attempt)
80
- correct_code = to_array(code_attempt)
81
- empty_result
82
- @error = :WrongCode unless code_attempt.match(/[1-6]+$/)
83
- @error = :WrongCode unless correct_code.size == @length_code
84
- return nil if @error == :WrongCode
85
-
86
- status_game(correct_code)
87
- @count_attempt += 1
88
- @win = false if @total_count_attempt <= @count_attempt && @win.nil?
89
- nil
90
- end
91
-
92
- def status_game(correct_code)
93
- cache_code = correct_code
94
- cache_secret_code = @secret_code.clone
95
- prepare_arrays(correct_code, cache_code, cache_secret_code)
96
- @win = true if cache_secret_code.empty?
97
- @count_plus = @length_code - cache_secret_code.size
98
- count_minus = 0
99
- cache_secret_code.each { |value| count_minus += 1 if cache_code.include?(value) }
100
- @count_minus = count_minus
101
- end
102
-
103
- def prepare_arrays(correct_code, cache_code, cache_secret_code)
104
- num = @secret_code.size
105
- while num >= 0
106
- if @secret_code[num] == correct_code[num]
107
- cache_code.delete_at(num)
108
- cache_secret_code.delete_at(num)
109
- end
110
- num -= 1
111
- end
112
- end
113
-
114
- def to_array(str)
115
- str.to_i.digits.reverse
116
- end
117
- end
118
- end