codebreaker_bo 0.1.4 → 0.1.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: 88a70ddf89ed560f9d096907eac75c768cef0b7a4c27e52b453905bef4679049
4
- data.tar.gz: 27a3f505dc0024cff4175d0f1c7c0b144a62d79f9121fd4287f71715860ac5bd
3
+ metadata.gz: 9944c5101dc90e5ff2ef8fa18e8692c76a17ee53377560ed1004d7ccf0431fd8
4
+ data.tar.gz: 9050baa0eeaaaee224a1b458d4c71eeff6269b57645eab0f35d33ca3901a60e7
5
5
  SHA512:
6
- metadata.gz: 4f48bea86b1be586809aef0121ec82c492d4b077e7292423affe69814a55d4dbdc504dfede511c6c2d4759a426c287b7feb7e69447c057be46a57505b3293433
7
- data.tar.gz: c6d829aecc343d4a5bf3cd4ff306051b9c1327276cb36749c44ec76a2bf1399c8c08ec17d83f75568111d35e36dedbd5f51852576551c2e9d6b10cfb69544647
6
+ metadata.gz: e40e2ff6dee2172547206c1bb62da931b411675c4ba0a88c431a031d7d844666011080af6210406c2143fbd41c1aa22d302f2743d173e55c64e722c748b1b250
7
+ data.tar.gz: eccc9f39f593bd893e8f8b61fa9c6fee27a0fdc549d3d0c5c2b9963c4435319c60c783559cd18ec59a294fb898d45a57e6cfcfab199a91e8e4e86804b91ef959
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- codebreaker_bo (0.1.3)
4
+ codebreaker_bo (0.1.5)
5
5
  json (~> 2.6.1)
6
6
 
7
7
  GEM
data/data/statistic.yml CHANGED
@@ -0,0 +1,13 @@
1
+ ---
2
+ - :user_name: WEWEE
3
+ :difficulty: easy
4
+ :attempts_total: 15
5
+ :attempts_used: 15
6
+ :hints_total: 2
7
+ :hints_used: 2
8
+ - :user_name: TWTWTWT
9
+ :difficulty: medium
10
+ :attempts_total: 10
11
+ :attempts_used: 10
12
+ :hints_total: 1
13
+ :hints_used: 1
data/e_attempts ADDED
@@ -0,0 +1,20 @@
1
+
2
+ From: /home/bo/rubypr/codebreaker/lib/codebreaker/Console.rb:75 Console#game_process:
3
+
4
+ 74: def game_process
5
+ => 75: binding.pry
6
+ 76: if !game.decrease_attempts == game.secret_code && 'lose'
7
+ 77: p game
8
+ 78: game_actions
9
+ 79: end
10
+ 80:
11
+ 81: # if !game.used_attempts.nil? && !game.used_attempts.zero?
12
+ 82: # p game
13
+ 83: # game_actions
14
+ 84: # else
15
+ 85: # p 'attempts finished'
16
+ 86: # lose
17
+ 87: # new_process
18
+ 88: # end
19
+ 89: end
20
+
@@ -6,7 +6,7 @@ module Constants
6
6
 
7
7
  MEDIUM = { attempts: 10,
8
8
  hints: 1 }.freeze
9
- HELL = { attempts: 1,
9
+ HELL = { attempts: 5,
10
10
  hints: 1 }.freeze
11
11
  PLUS = '+'
12
12
  MINUS = '-'
@@ -15,4 +15,5 @@ module Constants
15
15
  LENGTH_RANGE = (3..20).freeze
16
16
  RANGE = (1..6).freeze
17
17
  NUMBER_OF_DIGITS = 4
18
+ PATH = './data/statistic.yml'
18
19
  end
@@ -2,12 +2,28 @@
2
2
 
3
3
  module Codebreaker
4
4
  class Game < BaseClass
5
- attr_accessor :user_hints
6
- attr_reader :secret_code, :total_attempts, :used_attempts, :level, :stats
5
+ attr_reader :secret_code, :total_attempts, :used_attempts, :total_hints, :used_hints, :stats, :level
7
6
 
8
7
  def initialize
9
8
  super()
10
9
  @secret_code = generate_code
10
+ @stat = Statistic.new
11
+ end
12
+
13
+ def decrease_attempts
14
+ return secret_code && 'lose' if @used_attempts.zero?
15
+
16
+ @used_attempts -= 1
17
+ end
18
+
19
+ def save_stats
20
+ hash = { user_name: @player_name, difficulty: level, attempts_total: total_attempts, attempts_used: used_attempts,
21
+ hints_total: total_hints, hints_used: used_hints }
22
+ @stat.collect_statistic(hash)
23
+ end
24
+
25
+ def load_stats
26
+ @stat.show_statistic
11
27
  end
12
28
 
13
29
  def create_settings(name, level)
@@ -38,7 +54,8 @@ module Codebreaker
38
54
  return unless @level.nil?
39
55
 
40
56
  @level = level
41
- @user_hints = choice_level[:hints]
57
+ @used_hints = choice_level[:hints]
58
+ @total_hints = choice_level[:hints]
42
59
  @total_attempts = choice_level[:attempts]
43
60
  @used_attempts = choice_level[:attempts]
44
61
  end
@@ -46,83 +63,46 @@ module Codebreaker
46
63
  def check_guess(numbers)
47
64
  return show_errors unless guess_valid?(numbers)
48
65
 
49
- decrease_attempts
50
66
  array_choose_numbers = numbers.to_s.each_char.map(&:to_i)
51
67
  return win if secret_code == array_choose_numbers
52
68
 
69
+ decrease_attempts
53
70
  codebreaker_result(array_choose_numbers)
54
71
  end
55
72
 
56
73
  def use_hint
57
- return 'Hints are over' if @user_hints.zero?
74
+ return 'Hints are over' if @used_hints.zero?
58
75
 
59
- @user_hints -= 1
76
+ @used_hints -= 1
60
77
  secret_code.sample
61
78
  end
62
79
 
63
- def collect_statistic
64
- @stats = [{
65
- user_name: @player_name,
66
- game_status: 'WIN',
67
- difficulty: level,
68
- attempts_total: total_attempts,
69
- attempts_used: used_attempts,
70
- hints_total: user_hints,
71
- hints_used: use_hint
72
- }]
73
- save_statistic
74
- end
75
-
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
80
  def win
86
81
  "#{Constants::PLUS * Constants::WIN_NUMBER}(win)"
87
82
  end
88
83
 
89
84
  private
90
85
 
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
97
-
98
- def stats_load
99
- @items = YAML.load_file('./data/statistic.yml')
100
- end
101
-
102
- def multi_sort
103
- @items.sort_by! { |game| [game[:difficulty], game[:attempts_used], game[:hints_total]] }
104
- end
105
-
106
86
  def generate_code
107
87
  Array.new(Constants::NUMBER_OF_DIGITS) { rand Constants::RANGE }
108
88
  end
109
89
 
110
90
  def codebreaker_result(array_choose_numbers)
111
- result = []
91
+ full_match = []
112
92
  array_choose_numbers.each_with_index do |code, index|
113
93
  next unless code == secret_code[index]
114
94
 
115
- result << Constants::PLUS
95
+ full_match << code
116
96
  array_choose_numbers[index] = nil
117
97
  end
118
- array_choose_numbers.compact.uniq.each { |number| result << Constants::MINUS if secret_code.include? number }
119
- result.join
98
+ partial_match = array_choose_numbers.compact.uniq.select { |number| secret_code.include? number }
99
+ partial_match = partial_match.reject { |number| full_match.include? number }
100
+ create_result(full_match, partial_match)
120
101
  end
121
102
 
122
- def decrease_attempts
123
- return secret_code && 'lose' if @used_attempts.zero?
124
-
125
- @used_attempts -= 1
103
+ def create_result(full_match, partial_match)
104
+ result = full_match.map { Constants::PLUS } + partial_match.map { Constants::MINUS }
105
+ result.join
126
106
  end
127
107
  end
128
108
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Statistic
5
+ def collect_statistic(hash)
6
+ @stat = [{
7
+ user_name: hash[:user_name],
8
+ difficulty: hash[:difficulty],
9
+ attempts_total: hash[:attempts_total],
10
+ attempts_used: hash[:attempts_used],
11
+ hints_total: hash[:hints_total],
12
+ hints_used: hash[:hints_used]
13
+ }]
14
+ save_statistic
15
+ end
16
+
17
+ def show_statistic
18
+ @items = YAML.load_file(Constants::PATH)
19
+ @items.sort_by! { |game| [game[:difficulty], game[:attempts_used], game[:hints_used]] }
20
+ @items.each_with_index.map do |stat, index|
21
+ [index.next, stat[:user_name], stat[:difficulty], stat[:attempts_total],
22
+ stat[:attempts_used], stat[:hints_total], stat[:hints_used]]
23
+ end
24
+ end
25
+
26
+ def save_statistic
27
+ yaml_data = YAML.load_file(Constants::PATH)
28
+ yaml_data = [] if yaml_data.nil?
29
+ yaml_data << @stat
30
+ File.open(Constants::PATH, 'r+') { |file| file.write YAML.dump(yaml_data.flatten) }
31
+ end
32
+ end
33
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Codebreaker
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.5'
5
5
  end
data/lib/codebreaker.rb CHANGED
@@ -7,4 +7,5 @@ module Codebreaker
7
7
  require 'codebreaker/game'
8
8
  require 'codebreaker/user'
9
9
  require 'codebreaker/validation'
10
+ require 'codebreaker/statistic'
10
11
  end
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.4
4
+ version: 0.1.5
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-28 00:00:00.000000000 Z
11
+ date: 2022-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -173,12 +173,12 @@ files:
173
173
  - README.md
174
174
  - Rakefile
175
175
  - data/statistic.yml
176
+ - e_attempts
176
177
  - lib/codebreaker.rb
177
178
  - lib/codebreaker/base_class.rb
178
179
  - lib/codebreaker/constant.rb
179
180
  - lib/codebreaker/game.rb
180
- - lib/codebreaker/statistic.yml
181
- - lib/codebreaker/test_console.rb
181
+ - lib/codebreaker/statistic.rb
182
182
  - lib/codebreaker/user.rb
183
183
  - lib/codebreaker/validation.rb
184
184
  - lib/codebreaker/version.rb
File without changes
@@ -1,16 +0,0 @@
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