alex_codebreaker 0.1.13 → 0.2.0

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: c5a6d38c548011653e19a27a4104ea2d1c489e4da6b40a668eb8f65692113f5e
4
- data.tar.gz: 3196886d889cc891d472944d7e8f27373c4466835a1a87222266faa575917996
3
+ metadata.gz: 1ed766ccc180a19700ccdb2fec162515ae5b51e9f2d7c88424af8ceae4bafa1a
4
+ data.tar.gz: aaa26c5b86142266d6627d53f291de59a5241b989cdeb82f2c773275bb0a3e0b
5
5
  SHA512:
6
- metadata.gz: bd9759580e561f62ef281b33038c12dad441f38f2f57b5034fc79e676e3e738f4ef0715a4a5f5407c14dbecda581e35fe6027a22b5d74c99371f1d917ea89aca
7
- data.tar.gz: 55344c2fc1a93e3b8487982a7e01775be6062107d685ce2866cd816cc03223f5143179cc02ffec4178f823bedd9c55475c607734310c105d5c51bd6ebb5a3a81
6
+ metadata.gz: 785681dd0102056240fe6b62b976c1488e3c43d5a998b51079eae948515862af639f214a495522a5e9ba90d010e1f569ca0851e6383bd9c2a2a817e6da24967f
7
+ data.tar.gz: c4a34a499ddc1a6b577c78aa523621d4ee3458b9ae0532f863adeb35a023a7f728041cf3950c248580f6d3f4ad402dae5850ad758afcacdc8535442cc72dab22
@@ -2,28 +2,41 @@ module AlexCodebreaker
2
2
  class Game
3
3
  include AlexCodebreaker::Modules::ArgumentsValidation
4
4
  FILENAME_EXTENSION = '.yml'.freeze
5
+ STATUS = { in_game: :in_game, win: :win, lose: :lose }.freeze
5
6
 
6
- attr_reader :secret_code, :session, :game_id
7
+ attr_reader :secret_code, :session, :game_id, :status, :used_hints, :current_comparison
7
8
 
8
9
  def initialize(outer_id: nil)
9
10
  @game_id = outer_id if outer_id
10
11
  @secret_code = AlexCodebreaker::SecretCodeGenerator.new.secret_code
11
12
  @secret_code_for_hint = @secret_code.clone.uniq
12
13
  @session = Session.new
14
+ @used_hints = []
15
+ @status = STATUS[:in_game]
13
16
  save_game
14
17
  end
15
18
 
19
+ def add_name(name)
20
+ save_game if @session.add_name(name)
21
+ end
22
+
23
+ def add_difficulty(difficulty)
24
+ save_game if @session.add_difficulty(difficulty)
25
+ end
26
+
16
27
  def hint
17
28
  return unless @session.check_hints
18
29
 
19
30
  process_hint
31
+ save_game
20
32
  end
21
33
 
22
34
  def guess(user_input)
23
- return unless guess_validation(user_input) && @session.check_attempts
35
+ return unless guess_validation(user_input)
24
36
 
25
- save_game
26
37
  compare_codes(user_input)
38
+ check_win_lose
39
+ save_game
27
40
  end
28
41
 
29
42
  private
@@ -34,20 +47,26 @@ module AlexCodebreaker
34
47
  else
35
48
  @secret_code_for_hint.delete(@secret_code_for_hint.sample)
36
49
  end
37
- save_game
38
- hint
50
+ @used_hints << hint
39
51
  end
40
52
 
41
53
  def compare_codes(user_input)
42
54
  user_input = format_user_input(user_input)
43
55
  comparison = Comparison.new(user_input, @secret_code.clone)
44
- comparison.response
56
+ @current_comparison = comparison.response
45
57
  end
46
58
 
47
59
  def format_user_input(user_input)
48
60
  user_input.chars.map(&:to_i)
49
61
  end
50
62
 
63
+ def check_win_lose
64
+ checking_result = @session.check_attempts
65
+ return @status = STATUS[:win] if @current_comparison == AlexCodebreaker::Modules::Settings::WIN_COMPARISON
66
+
67
+ @status = STATUS[:lose] unless checking_result
68
+ end
69
+
51
70
  def save_game
52
71
  @game_id ||= AlexCodebreaker::IDGenerator.new.unique_id
53
72
  unless File.directory?(AlexCodebreaker.configuration.games_folder_path)
@@ -2,9 +2,10 @@ module AlexCodebreaker
2
2
  module Modules
3
3
  module Settings
4
4
  MATCHING = { place: '+', presence: '-' }.freeze
5
+ CODE_LENGTH = 4
6
+ WIN_COMPARISON = MATCHING[:place] * CODE_LENGTH
5
7
  CODE_MIN_DIGIT = 1
6
8
  CODE_MAX_DIGIT = 6
7
- CODE_LENGTH = 4
8
9
  MIN_PLAYER_NAME_LENGTH = 3
9
10
  MAX_PLAYER_NAME_LENGTH = 20
10
11
  end
@@ -12,6 +12,7 @@ module AlexCodebreaker
12
12
  def initialize
13
13
  @attempts_used = INITIAL_ATTEMPTS_USED
14
14
  @hints_used = INITIAL_HINTS_USED
15
+ @time = Time.new
15
16
  end
16
17
 
17
18
  def add_name(given_name)
@@ -34,9 +35,9 @@ module AlexCodebreaker
34
35
 
35
36
  def check_attempts
36
37
  @attempts_used += 1
37
- return unless @attempts_used < @attempts_total
38
+ return if @attempts_used >= @attempts_total
38
39
 
39
- @attempts_used
40
+ true
40
41
  end
41
42
 
42
43
  def save_winner_statistic
@@ -46,6 +47,8 @@ module AlexCodebreaker
46
47
  File.open(path, 'a') { |file| file.write(to_yaml) }
47
48
  end
48
49
 
50
+ private
51
+
49
52
  def check_folder_existence
50
53
  return if File.directory?(AlexCodebreaker.configuration.winners_folder_path)
51
54
 
@@ -1,5 +1,5 @@
1
1
  module AlexCodebreaker
2
2
  module Version
3
- VERSION = '0.1.13'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alex_codebreaker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleksandr Loza
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-31 00:00:00.000000000 Z
11
+ date: 2020-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler