code_brkr_game_training 0.7.2 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b318c26b1c0fbbb3f410d204862100077cc1211f4a6fbd3f2c191e13e4418764
4
- data.tar.gz: 882dab2737baf0cdcb0c8d182f09b8a6a59612ed7b1fc1208336265b6f1d3ef2
3
+ metadata.gz: e08993644da18256d3cd3720ec7ccc939ee82074a1effb12659116931c4d2a10
4
+ data.tar.gz: e10409ead753bdc0bd7f3cb6c89bdea87ca335c1b481abed9f912b36dd341370
5
5
  SHA512:
6
- metadata.gz: 0b214b380fa55616ad912eb09f09c8cdb9c5796174af14dc17d837f78badf62720bb2b4e02bfb0455703be2f8e0f5f6f2795f186308036a1dc1073e4a5ed1eed
7
- data.tar.gz: 5fa53619996367d8de6a0befbf8477421e86bfcaddf31119d156774580c78e8ad9aa02c44011ea30c1dfdc93803b88cd5ad6a03439184ac1f67429f6f53087f6
6
+ metadata.gz: 7ed4ebe5861682f5a3c77cc6e46de4e9d0b5dad2d6eca1390e223cc9208676d9821d19fbb65a2ccaf839c058e04622072b65b07696238ecf66418f804527e854
7
+ data.tar.gz: 9375923f2eca595e325eec041bd2852c15da1e7d79a9e4f9b9308110f9ca32332f57c18da3b409befb24c52151a6987287f4c9e9f91c33cc4de42de601735235
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code_brkr_game_training (0.7.2)
4
+ code_brkr_game_training (0.7.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -9,7 +9,7 @@ module CodeBrkrGameTraining
9
9
  GAME_NUMBERS = { from: 1, to: 6, count: 4 }.freeze
10
10
 
11
11
  def initialize
12
- @secret_digit_arr = code_generator
12
+ @secret_digit_arr = generate_code
13
13
  end
14
14
 
15
15
  def to_s
@@ -18,23 +18,22 @@ module CodeBrkrGameTraining
18
18
 
19
19
  def code_check(user_digits_arr)
20
20
  check_array_code_format(user_digits_arr)
21
- alg_result = code_check_algorithm(@secret_digit_arr.clone, user_digits_arr)
22
- result = {
23
- in_position: GAME_NUMBERS[:count] - alg_result[:miss_indexes].length,
24
- out_of_position: alg_result[:out_of_positions].length
21
+
22
+ {
23
+ in_positions: check_in_positions(user_digits_arr),
24
+ out_of_positions: check_out_positions(user_digits_arr),
25
+ not_guessed: check_not_guessed(user_digits_arr)
25
26
  }
26
- result[:not_guessed] = GAME_NUMBERS[:count] - result[:in_position] - result[:out_of_position]
27
- result
28
27
  end
29
28
 
30
- def random_secret_digit_according_indexes(exclude_indexes)
29
+ def random_digit(exclude_indexes)
31
30
  rand_index = @secret_digit_arr.each_index.reject { |index| exclude_indexes.include? index }.sample
32
31
  { index: rand_index, digit: @secret_digit_arr[rand_index] }
33
32
  end
34
33
 
35
34
  private
36
35
 
37
- def code_generator
36
+ def generate_code
38
37
  Array.new(GAME_NUMBERS[:count]) { rand(GAME_NUMBERS[:from]..GAME_NUMBERS[:to]) }
39
38
  end
40
39
 
@@ -46,14 +45,25 @@ module CodeBrkrGameTraining
46
45
  code_arr.each { |digit| raise GameError, 'incorrect_code_format' unless digits_range_arr.member? digit }
47
46
  end
48
47
 
49
- def code_check_algorithm(system_arr, user_arr)
50
- miss_indexes_arr = user_arr.each_index.reject { |user_index| user_arr[user_index] == system_arr[user_index] }
51
- system_arr.select!.with_index { |_, sys_index| miss_indexes_arr.include? sys_index }
52
- out_of_positions = miss_indexes_arr.collect do |miss_index|
53
- found = system_arr.find_index { |sys_num| user_arr[miss_index] == sys_num }
54
- found.nil? ? nil : system_arr.delete_at(found)
55
- end
56
- { miss_indexes: miss_indexes_arr, out_of_positions: out_of_positions.compact }
48
+ def check_miss_indexes(user_arr)
49
+ GAME_NUMBERS[:count].times.reject { |user_index| user_arr[user_index] == @secret_digit_arr[user_index] }
50
+ end
51
+
52
+ def check_in_positions(user_arr)
53
+ GAME_NUMBERS[:count] - check_miss_indexes(user_arr).length
54
+ end
55
+
56
+ def check_out_positions(user_arr)
57
+ miss_indexes = check_miss_indexes(user_arr)
58
+ system_digits = @secret_digit_arr.select.with_index { |_, system_index| miss_indexes.include? system_index }
59
+ miss_indexes.collect do |miss_index|
60
+ found = system_digits.find_index { |system_num| user_arr[miss_index] == system_num }
61
+ found.nil? ? nil : system_digits.delete_at(found)
62
+ end.compact.length
63
+ end
64
+
65
+ def check_not_guessed(user_arr)
66
+ GAME_NUMBERS[:count] - check_in_positions(user_arr) - check_out_positions(user_arr)
57
67
  end
58
68
  end
59
69
  end
@@ -21,11 +21,11 @@ module CodeBrkrGameTraining
21
21
  .collect { |difficulty| difficulty[:name] }
22
22
  end
23
23
 
24
- def initialize(dfclt_val)
25
- @value_name = dfclt_val
24
+ def initialize(difficulty_value)
25
+ @value_name = difficulty_value
26
26
  validate
27
27
 
28
- @init_values = GAME_DIFFICULTIES[dfclt_val.to_sym]
28
+ @init_values = GAME_DIFFICULTIES[difficulty_value.to_sym]
29
29
  @actual_values = @init_values.clone
30
30
  end
31
31
 
@@ -24,7 +24,7 @@ module CodeBrkrGameTraining
24
24
  def code_guessing_attempt(digits_arr)
25
25
  @difficulty.guessing_attempts_decrement!
26
26
  result = @code.code_check(digits_arr)
27
- if result[:in_position] == Code::GAME_NUMBERS[:count]
27
+ if result[:in_positions] == Code::GAME_NUMBERS[:count]
28
28
  result = { win: true }
29
29
  elsif !@difficulty.guessing_attempts_available?
30
30
  result = { loss: true, сorrect_answer: @code.to_s }
@@ -34,7 +34,7 @@ module CodeBrkrGameTraining
34
34
 
35
35
  def game_hint
36
36
  @difficulty.hints_decrement!
37
- hint = @code.random_secret_digit_according_indexes(@hints_indexes)
37
+ hint = @code.random_digit(@hints_indexes)
38
38
  @hints_indexes << hint[:index]
39
39
  hint[:digit]
40
40
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # The main module is re-opened here for declaring gem version
4
4
  module CodeBrkrGameTraining
5
- VERSION = '0.7.2'
5
+ VERSION = '0.7.5'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code_brkr_game_training
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-25 00:00:00.000000000 Z
11
+ date: 2020-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fasterer