codebreaker_kirill 0.2.9 → 0.2.12

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: 889ad79dfdf70727a2d29978966f658f2f183a52161ccf1cb21398c3466f7527
4
- data.tar.gz: 9b30190b95bf32cb415af6346f1e5cd38e23387c5beb5579624048e72be922e2
3
+ metadata.gz: 0ec08f93cc594d4d2bba8f889b64239f032cc6a4c80fb9dab8b662937da56db0
4
+ data.tar.gz: 937f0f2cbec8314935a1a08f856b9d965fb86b83541d7f2b183f2f3fa1c90247
5
5
  SHA512:
6
- metadata.gz: a40aeb816cb9080455363424cd31ae95f26e2a8dba3a6d33b6d30a1bc2acafb6e711f32a4973983a909e0987df6291312964a240b6ea506af018532c3269cc41
7
- data.tar.gz: 3d3ed3727941bd89efdbe0b6b99b9e382bce3b8d5073aaa4c3a136f036223a853c2fcbab2d7f1d28544c227ac37318f9bf9a8cbb9d6df39da2cded0d43b549a0
6
+ metadata.gz: 730e044734ce4b4308a00f426b0c17081a98e7b86d894257350863ca6c234ab191bf5723e8b3fcd49a6881f33571324d6063f81ab68c8ad55ea40c1d9563d6d4
7
+ data.tar.gz: 92e0936a01317cfe7452e6c6c97c89c44ef53e2c56d82e8946e3e947ce57f0378fe8364a39940b00cc9dec0458479e77cfe63d47f58e299c7768f367577aee5b
@@ -11,16 +11,27 @@ class Game
11
11
  def initialize(user, difficulty)
12
12
  @secret_code = (1..4).to_a.map { |_num| rand(1..6) }
13
13
  @hint_counter = 0
14
- @attempts = { available: difficulty[:attempts], used: 0 }
15
- @hints = { available: difficulty[:hints], used: 0 }
14
+ @attempts = { all: difficulty[:attempts], used: 0 }
15
+ @hints = { all: difficulty[:hints], used: 0 }
16
16
  @name = user.name
17
17
  end
18
18
 
19
19
  def give_a_hint
20
20
  return 0 if @hints[:available].zero?
21
21
 
22
- @hints[:available] -= 1
22
+ @hints[:all] -= 1
23
23
  @hints[:used] += 1
24
24
  @secret_code[@hints[:used]]
25
25
  end
26
26
  end
27
+
28
+ game = Game.new(User.new("kirill"), { attempts: 10, hints: 2 })
29
+ print game.respond_to_guess(game, "2222", [1,2,3,4]) # +
30
+ print game.respond_to_guess(game, "2222", [2,2,2,4]) # +++
31
+ print game.respond_to_guess(game, "2211", [1,2,3,4]) # +-
32
+ print game.respond_to_guess(game, "2221", [1,2,3,2]) # +--
33
+ print game.respond_to_guess(game, "1212", [2,1,2,1]) # ----
34
+ print game.respond_to_guess(game, "6666", [1,1,1,1]) # []
35
+ print game.respond_to_guess(game, "6666", [6,6,6,6]) # win
36
+ print game.respond_to_guess(game, "1234", [5,3,6,3]) # -
37
+ print game.respond_to_guess(game, "5363", [1,2,3,4]) # -
@@ -2,24 +2,22 @@
2
2
 
3
3
  module GuessHandler
4
4
  def respond_to_guess(game, input, code)
5
- result = []
6
- numbers = input.split('').map(&:to_i)
7
- return 'win' if numbers == code
5
+ @result = []
6
+ @code_clone = code.clone
7
+ @numbers = input.split('').map(&:to_i)
8
+ return 'win' if @numbers == code
8
9
 
9
- game.assess_guess(numbers, code, result)
10
- game.attempts[:available] -= 1
10
+ assess_guess(@code_clone, @numbers)
11
11
  game.attempts[:used] += 1
12
- game.attempts[:available].zero? ? 'loss' : result
12
+ game.attempts[:used] >= game.attempts[:all] ? 'loss' : @result.delete_if { |value| (1..6).include?(value) }.sort
13
13
  end
14
14
 
15
- def assess_guess(numbers, code, result)
16
- code_copy = code.clone
17
- numbers.each_with_index do |element, index|
18
- if element == code[index]
19
- result << '+'
20
- elsif code_copy.include?(element)
21
- result << '-'
22
- code_copy.delete_at(code_copy.find_index(element))
15
+ def assess_guess(code, input)
16
+ input.each_with_index { |element, index| @code_clone[index] = '+' if element == @code_clone[index] }
17
+ @result = code.each_with_index do |el, index|
18
+ if input.include?(el)
19
+ @code_clone[index] = '-'
20
+ input.slice!(@numbers.index(el))
23
21
  end
24
22
  end
25
23
  end
@@ -3,5 +3,5 @@
3
3
  require_relative 'game'
4
4
 
5
5
  module CodebreakerKirill
6
- VERSION = '0.2.9'
6
+ VERSION = '0.2.12'
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebreaker_kirill
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Dudchenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-16 00:00:00.000000000 Z
11
+ date: 2022-08-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: