andrii_codebreaker 0.1.6 → 0.1.7

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: 850117beac1f8b9441630a8a0f97ef5a6c374e671dce56bedc85d9f43b74d828
4
- data.tar.gz: c52fce0dcceb07cb99bb0ce693b7e38469b380e4310c990e9783cf53acb4fbb7
3
+ metadata.gz: 614726c17f68e0208588be0daa78170ff2dca52f3cbb48017faa565b96673a0c
4
+ data.tar.gz: fd3c636ff49aaace2fda7055aa2ce90155709e0c56edb889cad671d6e4f43105
5
5
  SHA512:
6
- metadata.gz: 614863a6b0e5d83c2e3752d5244146fc8aaebf1452dcdb367172da66bb8c6c9c6a838fc347e4af237d57792b76676b30dc55224acf1d7cc963cc0a746aa5e00a
7
- data.tar.gz: 32267d352720c6009cd3b4bff6f3c130e1dae175f6b90b423c1b4ff8ac42459f9b252b0934d55c2fc0e4a22118d2c89f72637083c8ee4a9def75b16ae6331dc0
6
+ metadata.gz: 0c5603c20de947e67d699355cec83a2f4160da98d133c8516b10bd45cff5ccfc76600092f9ef74ab040d05631ae8032ec08147908dcee0775e4a2f202e618cc3
7
+ data.tar.gz: b6ae17bb6609a3ea2b6dbe9dd78e024d425c31b1428dce613660ab004389bd93d17dd2235b222fe591db4fa6fb2aea3c2b08717f39009a4ecb9ba13f61d96ebc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- andrii_codebreaker (0.1.6)
4
+ andrii_codebreaker (0.1.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -13,5 +13,8 @@ module AndriiCodebreaker
13
13
  hell: { attempts: 5, hints: 1 }
14
14
  }.freeze
15
15
  DIFFICULTY_SORT = { easy: 1, medium: 2, hell: 3 }.freeze
16
+ WIN = '++++'
17
+ MINUS = '-'
18
+ PLUS = '+'
16
19
  end
17
20
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AndriiCodebreaker
4
+ class Difficulty
5
+ include Constant
6
+
7
+ attr_reader :difficulty
8
+
9
+ def initialize(difficulty)
10
+ @difficulty = difficulty
11
+ end
12
+
13
+ def hint
14
+ difficult[:hints]
15
+ end
16
+
17
+ def attempts
18
+ difficult[:attempts]
19
+ end
20
+
21
+ def difficult
22
+ DIFFICULTY[@difficulty.to_sym]
23
+ end
24
+ end
25
+ end
@@ -2,11 +2,10 @@
2
2
 
3
3
  module AndriiCodebreaker
4
4
  class Game
5
- include Validate
6
5
  include Statistic
7
6
  include Constant
8
7
 
9
- attr_reader :player_name, :difficulty, :attempts_total, :hints_total, :secret_code
8
+ attr_reader :player_name, :difficulties, :attempts_total, :hints_total, :secret_code
10
9
  attr_accessor :hints_left, :attempts_left, :secret_code_copy
11
10
 
12
11
  CONST_COMMAND = {
@@ -18,66 +17,81 @@ module AndriiCodebreaker
18
17
 
19
18
  def initialize(player_name, difficulty)
20
19
  @player_name = player_name
21
- @difficulty = difficulty
20
+ @difficulties = Difficulty.new(difficulty)
22
21
  @attempts_left = 0
23
22
  @hints_left = 0
24
- initialize_class
25
23
  end
26
24
 
27
- def initialize_class
28
- difficult = DIFFICULTY[@difficulty.to_sym]
29
- @hints_total = difficult[:hints]
30
- @attempts_total = difficult[:attempts]
25
+ def start
31
26
  @secret_code = generate_code
32
- @secret_code_copy = @secret_code.dup.chars
27
+ @secret_code_copy = @secret_code.chars
28
+ @hints_total = @difficulties.hint
29
+ @attempts_total = @difficulties.attempts
33
30
  end
34
31
 
35
- def show_hints
36
- hints_left
32
+ def hints
33
+ return unless @hints_total > @hints_left
34
+
35
+ @hints_left += 1
36
+ @secret_code_copy.pop
37
37
  end
38
38
 
39
39
  def compare_codes(guess)
40
- return '++++' if exact_match(guess)
40
+ return '' if !guess.match(/^[1-6]{4}$/) && !guess.match(/^\?$/)
41
+ return WIN if guess == @secret_code
42
+
43
+ result = ''
44
+ index = []
45
+ code_guess = string_to_integer(guess)
46
+ secret_code = string_to_integer(@secret_code)
47
+
48
+ result, index = check_plus(code_guess, result, secret_code, index)
49
+
50
+ delete_index(code_guess, index)
51
+ delete_index(secret_code, index)
41
52
 
42
- ('+' * pluses(guess)) + ('-' * minuses(guess))
53
+ check_minus(code_guess, result, secret_code, index)
43
54
  end
44
55
 
45
56
  private
46
57
 
47
- def exact_match(guess)
48
- @secret_code == guess
49
- end
58
+ def check_plus(*info)
59
+ code_guess, result, secret_code, index = info
50
60
 
51
- def pluses(guess)
52
- zipped(guess)
53
- .select { |el| el[0] == el[1] }
54
- .count
61
+ code_guess.each_index do |i|
62
+ if code_guess[i] == secret_code[i]
63
+ result += '+'
64
+ index << i
65
+ end
66
+ end
67
+ [result, index]
55
68
  end
56
69
 
57
- def minuses(guess)
58
- return 0 if exact_match(guess)
59
-
60
- arr = delete_pairs(guess)
61
- arr[1].each do |number|
62
- next unless arr[0].include?(number)
70
+ def check_minus(*info)
71
+ code_guess, result, secret_code, index = info
72
+ code_guess.each do |i|
73
+ next unless secret_code.include?(i)
63
74
 
64
- arr[0].delete_at(arr[0].index(number))
75
+ result << MINUS
76
+ index = secret_code.index(i)
77
+ secret_code.delete_at(index)
65
78
  end
66
- arr[1].size - arr[0].size
79
+ result
67
80
  end
68
81
 
69
- def zipped(guess)
70
- @secret_code.chars.zip(guess.chars)
82
+ def string_to_integer(input_code)
83
+ array_int = []
84
+ array_chars = input_code.chars
85
+ array_chars.each { |i| array_int << i.to_i }
86
+ array_int
71
87
  end
72
88
 
73
- def delete_pairs(guess)
74
- zipped(guess)
75
- .delete_if { |el| el[0] == el[1] }
76
- .transpose
89
+ def delete_index(array, index)
90
+ array.map!.with_index { |item, i| item unless index.include?(i) }.compact!
77
91
  end
78
92
 
79
93
  def generate_code
80
- (Array.new(CODE_LENGTH_COUNT) { rand(CODE_START_LENGTH..CODE_LENGTH) }).join.to_s
94
+ (CODE_START_LENGTH..CODE_LENGTH_COUNT).map { rand(CODE_START_LENGTH..CODE_LENGTH) }.join
81
95
  end
82
96
  end
83
97
  end
@@ -2,26 +2,22 @@
2
2
 
3
3
  module AndriiCodebreaker
4
4
  class User
5
- include AndriiCodebreaker::Validate
5
+ include Constant
6
6
 
7
7
  attr_reader :name
8
8
 
9
9
  def initialize(name)
10
- @name = name
11
- validate_name
10
+ @name = validates_name(name)
12
11
  end
13
12
 
14
- def hints(game)
15
- return unless game.hints_total > game.hints_left
13
+ private
16
14
 
17
- game.hints_left += 1
18
- game.secret_code_copy.pop
15
+ def validates_name(name)
16
+ name if valid_name?(name)
19
17
  end
20
18
 
21
- private
22
-
23
- def validate_name
24
- @name = validates_name(name)
19
+ def valid_name?(name)
20
+ name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
25
21
  end
26
22
  end
27
23
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AndriiCodebreaker
4
- VERSION = '0.1.6'
4
+ VERSION = '0.1.7'
5
5
  end
@@ -2,9 +2,9 @@
2
2
 
3
3
  require 'yaml'
4
4
  require_relative 'andrii_codebreaker/constants'
5
- require_relative 'andrii_codebreaker/validate/validate'
6
5
  require_relative 'andrii_codebreaker/model'
7
6
  require_relative 'andrii_codebreaker/user'
7
+ require_relative 'andrii_codebreaker/difficulty'
8
8
  require_relative 'andrii_codebreaker/statistic'
9
9
  require_relative 'andrii_codebreaker/version'
10
10
  require_relative 'andrii_codebreaker/game'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: andrii_codebreaker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-13 00:00:00.000000000 Z
11
+ date: 2021-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fasterer
@@ -127,17 +127,16 @@ files:
127
127
  - LICENSE.txt
128
128
  - README.md
129
129
  - Rakefile
130
- - andrii_codebreaker-0.1.5.gem
131
130
  - andrii_codebreaker.gemspec
132
131
  - bin/console
133
132
  - bin/setup
134
133
  - lib/andrii_codebreaker.rb
135
134
  - lib/andrii_codebreaker/constants.rb
135
+ - lib/andrii_codebreaker/difficulty.rb
136
136
  - lib/andrii_codebreaker/game.rb
137
137
  - lib/andrii_codebreaker/model.rb
138
138
  - lib/andrii_codebreaker/statistic.rb
139
139
  - lib/andrii_codebreaker/user.rb
140
- - lib/andrii_codebreaker/validate/validate.rb
141
140
  - lib/andrii_codebreaker/version.rb
142
141
  homepage: https://github.com/Andrii-RubyGarage/Codebreker-Gem
143
142
  licenses:
Binary file
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module AndriiCodebreaker
4
- module Validate
5
- include Constant
6
-
7
- def validates_name(name)
8
- name if valid_name?(name)
9
- end
10
-
11
- def validate_guess(code)
12
- code = string_to_integer(code)
13
- true if check_code_length?(code) && check_numbers?(code)
14
- end
15
-
16
- private
17
-
18
- def string_to_integer(input_code)
19
- array_int = []
20
- array_chars = input_code.chars
21
- array_chars.each { |i| array_int << i.to_i }
22
- array_int
23
- end
24
-
25
- def valid_name?(name)
26
- name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
27
- end
28
-
29
- def check_code_length?(code)
30
- code.length == CODE_LENGTH_COUNT
31
- end
32
-
33
- def check_numbers?(code)
34
- code.all? { |value| value.between?(CODE_START_LENGTH, CODE_LENGTH) }
35
- end
36
- end
37
- end