codebreaker_bo 0.1.3 → 0.1.4

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: fe9a8ad138ec2d9cff2eaf88dc58cdd3492a8c62137c1288e912581576f4f214
4
- data.tar.gz: de6f9f5157baa191c19aea4053da0e2e2b05bd47a92a433d8bd99336cf71c9c4
3
+ metadata.gz: 88a70ddf89ed560f9d096907eac75c768cef0b7a4c27e52b453905bef4679049
4
+ data.tar.gz: 27a3f505dc0024cff4175d0f1c7c0b144a62d79f9121fd4287f71715860ac5bd
5
5
  SHA512:
6
- metadata.gz: 905d2f0cd0efa4ea97beeaea463b78a873c24d0d6ea0082c86a7f9f6eb4a12ac0afd2543ddc6ad1513bc8043f2541f1d41efd81076aa0ac97db2388460e714bd
7
- data.tar.gz: 3596da0b4e91ba3758fe7de20b588c29fc3e4257e99c20921f3f3b9e7102f5a43a63fdc39143f45fbd38d2b1d155e801c07abe2f9735c525536bd3cb0d240b97
6
+ metadata.gz: 4f48bea86b1be586809aef0121ec82c492d4b077e7292423affe69814a55d4dbdc504dfede511c6c2d4759a426c287b7feb7e69447c057be46a57505b3293433
7
+ data.tar.gz: c6d829aecc343d4a5bf3cd4ff306051b9c1327276cb36749c44ec76a2bf1399c8c08ec17d83f75568111d35e36dedbd5f51852576551c2e9d6b10cfb69544647
File without changes
@@ -1,15 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'validation'
3
4
  module Codebreaker
4
5
  class BaseClass
5
- def valid?
6
- @errors.empty?
6
+ include Validator
7
+ attr_accessor :errors
8
+
9
+ def initialize
10
+ @errors = []
7
11
  end
8
12
 
9
- private
13
+ def handle_errors(text)
14
+ @errors << text
15
+ end
10
16
 
11
17
  def show_errors
12
- @errors.each { |error| p error } unless valid?
18
+ data = errors.flatten
19
+ clear_errors
20
+ data
21
+ end
22
+
23
+ def clear_errors
24
+ @errors = []
13
25
  end
14
26
  end
15
27
  end
@@ -6,7 +6,7 @@ module Constants
6
6
 
7
7
  MEDIUM = { attempts: 10,
8
8
  hints: 1 }.freeze
9
- HELL = { attempts: 5,
9
+ HELL = { attempts: 1,
10
10
  hints: 1 }.freeze
11
11
  PLUS = '+'
12
12
  MINUS = '-'
@@ -1,32 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
-
4
3
  module Codebreaker
5
4
  class Game < BaseClass
6
5
  attr_accessor :user_hints
7
- attr_reader :secret_code, :total_attempts, :used_attempts, :level, :errors
6
+ attr_reader :secret_code, :total_attempts, :used_attempts, :level, :stats
8
7
 
9
8
  def initialize
10
- @errors = []
11
- @secret_code = generate_code
12
9
  super()
10
+ @secret_code = generate_code
13
11
  end
14
12
 
15
13
  def create_settings(name, level)
16
14
  player_name(name)
17
15
  create_level(level)
18
- show_errors
16
+
17
+ show_errors unless valid?
19
18
  end
20
19
 
21
20
  def player_name(name)
22
- @player_name = User.new(name).name
21
+ user = User.new(name)
22
+ return @player_name = user.name if user.valid?
23
+
24
+ errors << user.errors
23
25
  end
24
26
 
25
27
  def accept_level(level)
26
28
  level = level.upcase
27
29
  Object.const_get "Constants::#{level}"
28
30
  rescue StandardError
29
- @errors << 'entered level is not present'
31
+ handle_errors('entered level is not present')
30
32
  nil
31
33
  end
32
34
 
@@ -42,15 +44,13 @@ module Codebreaker
42
44
  end
43
45
 
44
46
  def check_guess(numbers)
45
- return @errors unless guess_valid?(numbers)
47
+ return show_errors unless guess_valid?(numbers)
46
48
 
47
49
  decrease_attempts
48
50
  array_choose_numbers = numbers.to_s.each_char.map(&:to_i)
49
51
  return win if secret_code == array_choose_numbers
50
52
 
51
- result = []
52
- codebreaker_result(array_choose_numbers, result)
53
- result.join
53
+ codebreaker_result(array_choose_numbers)
54
54
  end
55
55
 
56
56
  def use_hint
@@ -60,39 +60,55 @@ module Codebreaker
60
60
  secret_code.sample
61
61
  end
62
62
 
63
- def save_statistic
64
- stats = {
63
+ def collect_statistic
64
+ @stats = [{
65
65
  user_name: @player_name,
66
66
  game_status: 'WIN',
67
67
  difficulty: level,
68
68
  attempts_total: total_attempts,
69
69
  attempts_used: used_attempts,
70
- hints_total: user_hints
71
- }
70
+ hints_total: user_hints,
71
+ hints_used: use_hint
72
+ }]
73
+ save_statistic
74
+ end
72
75
 
73
- File.open('./data/statistic.yml', 'a') { |f| f.write YAML.dump(stats.flatten) }
76
+ def show_statistic
77
+ stats_load
78
+ multi_sort
79
+ @items.each_with_index.map do |stat, index|
80
+ [index.next, stat[:user_name], stat[:difficulty], stat[:attempts_total],
81
+ stat[:attempts_used], stat[:hints_total], stat[:hints_total]]
82
+ end
83
+ end
84
+
85
+ def win
86
+ "#{Constants::PLUS * Constants::WIN_NUMBER}(win)"
74
87
  end
75
88
 
76
89
  private
77
90
 
78
- def guess_valid?(match_code)
79
- @errors << 'match_code not number' unless match_code.is_a?(Numeric)
91
+ def save_statistic
92
+ yaml_data = YAML.load_file('./data/statistic.yml')
93
+ yaml_data = [] if yaml_data.nil?
94
+ yaml_data << stats
95
+ File.open('./data/statistic.yml', 'r+') { |file| file.write YAML.dump(yaml_data.flatten) }
96
+ end
80
97
 
81
- input_data = match_code.to_s
82
- @errors << 'invalid_input_data' unless input_data =~ Constants::DESIRED_NUMBER
98
+ def stats_load
99
+ @items = YAML.load_file('./data/statistic.yml')
100
+ end
83
101
 
84
- valid?
102
+ def multi_sort
103
+ @items.sort_by! { |game| [game[:difficulty], game[:attempts_used], game[:hints_total]] }
85
104
  end
86
105
 
87
106
  def generate_code
88
107
  Array.new(Constants::NUMBER_OF_DIGITS) { rand Constants::RANGE }
89
108
  end
90
109
 
91
- def win
92
- "#{Constants::PLUS * Constants::WIN_NUMBER}(win)"
93
- end
94
-
95
- def codebreaker_result(array_choose_numbers, result)
110
+ def codebreaker_result(array_choose_numbers)
111
+ result = []
96
112
  array_choose_numbers.each_with_index do |code, index|
97
113
  next unless code == secret_code[index]
98
114
 
@@ -100,6 +116,7 @@ module Codebreaker
100
116
  array_choose_numbers[index] = nil
101
117
  end
102
118
  array_choose_numbers.compact.uniq.each { |number| result << Constants::MINUS if secret_code.include? number }
119
+ result.join
103
120
  end
104
121
 
105
122
  def decrease_attempts
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # require 'yaml'
4
+ #
5
+ # require 'codebreaker'
6
+ # require_relative 'console'
7
+ # require 'pry'
8
+ #
9
+ # c = Console.new
10
+ # c.choose_action
11
+ # c.multi_sort
12
+ # c.take_stats
13
+
14
+ # p c.registration
15
+ # p c
16
+ # p c.game_process
@@ -2,22 +2,12 @@
2
2
 
3
3
  module Codebreaker
4
4
  class User < BaseClass
5
- attr_accessor :errors
6
5
  attr_reader :name
7
6
 
8
7
  def initialize(player_name)
9
- @errors = []
10
- validation_name(player_name)
11
- @errors.empty? ? @name = player_name : show_errors
12
8
  super()
13
- end
14
-
15
- def validation_name(player_name)
16
- if player_name.is_a?(String)
17
- @errors << 'error min length or max length' unless Constants::LENGTH_RANGE.include?(player_name.length)
18
- else
19
- @errors << 'name is not string'
20
- end
9
+ validation_name(player_name)
10
+ @name = player_name if valid?
21
11
  end
22
12
  end
23
13
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ module Validator
5
+ def valid?
6
+ errors.empty?
7
+ end
8
+
9
+ private
10
+
11
+ def guess_valid?(match_code)
12
+ handle_errors('match code not number') unless match_code.is_a?(Numeric)
13
+
14
+ input_data = match_code.to_s
15
+ handle_errors('invalid_input_data') unless input_data =~ Constants::DESIRED_NUMBER
16
+
17
+ valid?
18
+ end
19
+
20
+ def validation_name(player_name)
21
+ if player_name.is_a?(String)
22
+ errors << 'error min length or max length' unless Constants::LENGTH_RANGE.include?(player_name.length)
23
+ else
24
+ errors << 'name is not string'
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Codebreaker
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
  end
data/lib/codebreaker.rb CHANGED
@@ -6,4 +6,5 @@ module Codebreaker
6
6
  require 'codebreaker/base_class'
7
7
  require 'codebreaker/game'
8
8
  require 'codebreaker/user'
9
+ require 'codebreaker/validation'
9
10
  end
data/s ADDED
@@ -0,0 +1,14 @@
1
+
2
+ From: /home/bo/rubypr/codebreaker/lib/codebreaker/console.rb:95 Console#game_process:
3
+
4
+ 91: def game_process
5
+ 92: if !game.used_attempts.nil? && !game.used_attempts.zero?
6
+ 93: game_actions
7
+ 94: else
8
+ => 95: binding.pry
9
+ 96: p 'attempts finished'
10
+ 97: lose
11
+ 98: new_process
12
+ 99: end
13
+ 100: end
14
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebreaker_bo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oladko Bohdan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-24 00:00:00.000000000 Z
11
+ date: 2022-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -172,14 +172,17 @@ files:
172
172
  - LICENSE.txt
173
173
  - README.md
174
174
  - Rakefile
175
+ - data/statistic.yml
175
176
  - lib/codebreaker.rb
176
177
  - lib/codebreaker/base_class.rb
177
- - lib/codebreaker/console.rb
178
178
  - lib/codebreaker/constant.rb
179
179
  - lib/codebreaker/game.rb
180
180
  - lib/codebreaker/statistic.yml
181
+ - lib/codebreaker/test_console.rb
181
182
  - lib/codebreaker/user.rb
183
+ - lib/codebreaker/validation.rb
182
184
  - lib/codebreaker/version.rb
185
+ - s
183
186
  - sig/codebreaker.rbs
184
187
  homepage: https://github.com/bogdansev1/codebreaker_gem
185
188
  licenses:
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'codebreaker'
4
- #
5
- # u = Codebreaker::User.new('Bpgdan')
6
- # p u
7
-
8
- g = Codebreaker::Game.new
9
- # name = gets.chomp
10
- # level = gets.chomp
11
- p g
12
- g.create_settings('Vkjkk', 'easy')
13
- p g
14
- # p g.check_guess(4444)
15
- # p g.used_attempts