andrii_codebreaker 0.1.4 → 0.1.8

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: f3027085006ffe0d292e9f98fab0592e60bad72a65891bdc88636462c02a0467
4
- data.tar.gz: 3e9ccb14889cd81faa40b812b586c8e82467d94eb0ec110c800c3981c4c96cf1
3
+ metadata.gz: 1a01bba68d09ade8b529e4f4ced95cdbcb32a89d6dc2dc38a9b3f4f89d9bcad6
4
+ data.tar.gz: b19b78984073b7017509da2df5159bcc422ff6359432a88257e84a517369e95b
5
5
  SHA512:
6
- metadata.gz: b3ed0df86dc3ddb50e0111996dfa8ad431cc1d4b7e30900ad1cc2ff43b4a153da0a153cf77c53f232e40623ba7b1ee22a7b28e79130bad79728b51ed4e1e259f
7
- data.tar.gz: 9e3ec45080ac93dfa06cb58cf70acaa5707bbacbfcaeeb04408f1738c4afca10a10e4065ba4d52ab633bf2389b3d082677b4d1a7e8f5c5ef8e8b75005fa86a13
6
+ metadata.gz: b51935c0eb2382f41d9fb8c6859d507eaf7cf21ca7aad9a9f386262ad9b95130af5fdceab79ee424a7b906c934a906bf9358e49316b73e588aebadd5b613939f
7
+ data.tar.gz: 594d24a0cd286e63d0b6ab241080d629d4b2bc9bbc5648b57cdae0e159997381587ed7e90c1063e6bd5cc75ab99e6449bbff638e70c2b9c0a43b01bff354d15c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- andrii_codebreaker (0.1.4)
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,12 +2,11 @@
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
10
- attr_accessor :hints_left, :attempts_left
8
+ attr_reader :player_name, :difficulties, :attempts_total, :hints_total, :secret_code, :difficulty
9
+ attr_accessor :hints_left, :attempts_left, :secret_code_copy
11
10
 
12
11
  CONST_COMMAND = {
13
12
  start: 'start',
@@ -18,65 +17,82 @@ 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
27
+ @secret_code_copy = @secret_code.chars
28
+ @hints_total = @difficulties.hint
29
+ @attempts_total = @difficulties.attempts
30
+ @difficulty = @difficulties.difficulty
32
31
  end
33
32
 
34
- def show_hints
35
- hints_left
33
+ def hints
34
+ return unless @hints_total > @hints_left
35
+
36
+ @hints_left += 1
37
+ @secret_code_copy.pop
36
38
  end
37
39
 
38
40
  def compare_codes(guess)
39
- return '++++' if exact_match(guess)
41
+ return '' if !guess.match(/^[1-6]{4}$/) && !guess.match(/^\?$/)
42
+ return WIN if guess == @secret_code
43
+
44
+ result = ''
45
+ index = []
46
+ code_guess = string_to_integer(guess)
47
+ secret_code = string_to_integer(@secret_code)
48
+
49
+ result, index = check_plus(code_guess, result, secret_code, index)
50
+
51
+ delete_index(code_guess, index)
52
+ delete_index(secret_code, index)
40
53
 
41
- ('+' * pluses(guess)) + ('-' * minuses(guess))
54
+ check_minus(code_guess, result, secret_code, index)
42
55
  end
43
56
 
44
57
  private
45
58
 
46
- def exact_match(guess)
47
- @secret_code == guess
48
- end
59
+ def check_plus(*info)
60
+ code_guess, result, secret_code, index = info
49
61
 
50
- def pluses(guess)
51
- zipped(guess)
52
- .select { |el| el[0] == el[1] }
53
- .count
62
+ code_guess.each_index do |i|
63
+ if code_guess[i] == secret_code[i]
64
+ result += PLUS
65
+ index << i
66
+ end
67
+ end
68
+ [result, index]
54
69
  end
55
70
 
56
- def minuses(guess)
57
- return 0 if exact_match(guess)
58
-
59
- arr = delete_pairs(guess)
60
- arr[1].each do |number|
61
- next unless arr[0].include?(number)
71
+ def check_minus(*info)
72
+ code_guess, result, secret_code, index = info
73
+ code_guess.each do |i|
74
+ next unless secret_code.include?(i)
62
75
 
63
- arr[0].delete_at(arr[0].index(number))
76
+ result += MINUS
77
+ index = secret_code.index(i)
78
+ secret_code.delete_at(index)
64
79
  end
65
- arr[1].size - arr[0].size
80
+ result
66
81
  end
67
82
 
68
- def zipped(guess)
69
- @secret_code.chars.zip(guess.chars)
83
+ def string_to_integer(input_code)
84
+ array_int = []
85
+ array_chars = input_code.chars
86
+ array_chars.each { |i| array_int << i.to_i }
87
+ array_int
70
88
  end
71
89
 
72
- def delete_pairs(guess)
73
- zipped(guess)
74
- .delete_if { |el| el[0] == el[1] }
75
- .transpose
90
+ def delete_index(array, index)
91
+ array.map!.with_index { |item, i| item unless index.include?(i) }.compact!
76
92
  end
77
93
 
78
94
  def generate_code
79
- (Array.new(CODE_LENGTH_COUNT) { rand(CODE_START_LENGTH..CODE_LENGTH) }).join.to_s
95
+ (CODE_START_LENGTH..CODE_LENGTH_COUNT).map { rand(CODE_START_LENGTH..CODE_LENGTH) }.join
80
96
  end
81
97
  end
82
98
  end
@@ -2,25 +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(user)
15
- return unless user.hints_total > user.hints_left
13
+ private
16
14
 
17
- user.hints_left += 1
15
+ def validates_name(name)
16
+ name if valid_name?(name)
18
17
  end
19
18
 
20
- private
21
-
22
- def validate_name
23
- @name = validates_name(name)
19
+ def valid_name?(name)
20
+ name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
24
21
  end
25
22
  end
26
23
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AndriiCodebreaker
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.8'
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.4
4
+ version: 0.1.8
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.3.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