new_super_codebreaker_2021 0.3.5 → 0.4.3

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: 1a82b958460bacd3f2058c0358f7d03934b817439cb09a35ce7afd9665cd579a
4
- data.tar.gz: fa3eb2c2f1f60f4a3005c6aa3c91b9cfe01e189794ddc0372264234862855718
3
+ metadata.gz: b01bc7a275dbe137e6879ba102273580840d82b6073f218b69777734569d1fa2
4
+ data.tar.gz: a2fad34a1922ca14d468330854af5a8e77502648a69976871fdc97dc3ef0af71
5
5
  SHA512:
6
- metadata.gz: 313ff20be1e825b6665c27e2faccf4285a4b24fe7f69bb63b7fed3b703f3e36767c9d5e9d5bee94a318debb68fd7393b3c52107d28b5c5a9d0bae78027070ded
7
- data.tar.gz: d2fdecb7035dec146a97fe2faa0d931a53d6154063f9733c9ba88cdcafc721e70aa6321569f1033b3149fe214dd14909e5a5ff34ca072834fdc8371c2504733d
6
+ metadata.gz: 1d7119bb3c56e309023745790971210f91f86413d959499b102991f9124679392a89fb372acaca64c61bf3095f674a355e50ae6372b627427d486487fdd030b1
7
+ data.tar.gz: 962f3f97f315a8b754cfb8f2c7288550563b8caa88eae34e7d59f6f6e038f738663f335042cda618524387286b68ef1fb07522e946f7d178440feadba886e228
data/lib/game.rb CHANGED
@@ -9,14 +9,18 @@ module NewSuperCodebreaker2021
9
9
  include ShowContent
10
10
  include DBMethods
11
11
 
12
+ def initialize
13
+ @code = generate_code
14
+ end
15
+
16
+ GUESS_COMMANDS = %i[hint rules exit].freeze
12
17
  START_COMMANDS = %i[start rules stats exit].freeze
13
18
  DIFFICULTY_COMMANDS = %i[easy medium hell exit].freeze
14
- GUESS_COMMANDS = %i[hint rules exit].freeze
15
19
  AFTER_GAME_COMMANDS = %i[start save exit].freeze
16
20
  YES_NO_COMMANDS = %i[yes no].freeze
17
21
 
18
22
  def chose_command(command)
19
- START_COMMANDS.include?(command.to_sym) ? command.to_sym : false
23
+ check_input(command, START_COMMANDS)
20
24
  end
21
25
 
22
26
  def take_name(input_name)
@@ -28,11 +32,7 @@ module NewSuperCodebreaker2021
28
32
  end
29
33
 
30
34
  def chose_difficulty(difficulty)
31
- DIFFICULTY_COMMANDS.include?(difficulty.to_sym) ? difficulty.to_sym : false
32
- end
33
-
34
- def generate_code
35
- Array.new(4) { rand(1..6) }
35
+ check_input(difficulty, DIFFICULTY_COMMANDS)
36
36
  end
37
37
 
38
38
  def user_guess(code)
@@ -44,42 +44,37 @@ module NewSuperCodebreaker2021
44
44
  end
45
45
  end
46
46
 
47
- def take_hint(user, code, used_hints)
47
+ def take_hint(user, used_hints)
48
+ code_copy = @code.dup
48
49
  if user.hints_total > user.hints_used
49
50
  user.hints_used += 1
50
- used_hints.each { |hint| code.delete(hint) }
51
- code.sample
51
+ used_hints.each { |hint| code_copy.delete(hint) }
52
+ code_copy.sample
52
53
  else
53
54
  false
54
55
  end
55
56
  end
56
57
 
57
58
  def after_game_commands(command)
58
- if command.to_i.zero? && AFTER_GAME_COMMANDS.include?(command.to_sym)
59
- command.to_sym
60
- else false
61
- end
59
+ check_input(command, AFTER_GAME_COMMANDS)
62
60
  end
63
61
 
64
62
  def attempt_to_start(command)
65
- if command.to_i.zero? && YES_NO_COMMANDS.include?(command.to_sym)
66
- command.to_sym
67
- else false
68
- end
63
+ check_input(command, YES_NO_COMMANDS)
69
64
  end
70
65
 
71
- def compare_codes(secret_code, user_code)
72
- matches, u_char = number_on_right_place(secret_code, user_code)
73
- number_in_secret_code(secret_code, user_code, matches, u_char)
66
+ def compare_codes(user_code)
67
+ matches, u_char = number_on_right_place(user_code)
68
+ number_in_secret_code(user_code, matches, u_char)
74
69
  end
75
70
 
76
71
  private
77
72
 
78
- def number_on_right_place(secret_code, user_code)
73
+ def number_on_right_place(user_code)
79
74
  matches = []
80
75
  u_char = []
81
76
  user_code.each_index do |i|
82
- if secret_code[i] == user_code[i]
77
+ if @code[i] == user_code[i]
83
78
  matches.unshift('+')
84
79
  u_char << user_code[i]
85
80
  end
@@ -87,11 +82,15 @@ module NewSuperCodebreaker2021
87
82
  [matches, u_char]
88
83
  end
89
84
 
90
- def number_in_secret_code(secret_code, user_code, matches, u_char)
85
+ def number_in_secret_code(user_code, matches, u_char)
91
86
  user_code.each do |element|
92
- matches.push('-') if secret_code.include?(element) && !u_char.include?(element)
87
+ matches.push('-') if @code.include?(element) && !u_char.include?(element)
93
88
  end
94
89
  matches
95
90
  end
91
+
92
+ def generate_code
93
+ Array.new(4) { rand(1..6) }
94
+ end
96
95
  end
97
96
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NewSuperCodebreaker2021
4
- VERSION = '0.3.5'
4
+ VERSION = '0.4.3'
5
5
  end
data/lib/show_content.rb CHANGED
@@ -1,8 +1,31 @@
1
+ require 'terminal-table'
2
+
1
3
  module ShowContent
4
+ DIFFICULTY = {
5
+ easy: { attempts: 15, hints: 2 },
6
+ medium: { attempts: 10, hints: 1 },
7
+ hell: { attempts: 5, hints: 1 }
8
+ }.freeze
9
+
2
10
  def show_stats(file)
3
11
  data = YAML.load_file(file) || []
4
- data.sort_by(&:hints_used).sort_by(&:attempts_used).sort_by { |game| -game.difficulty }
12
+ create_table(data.sort_by(&:hints_used).sort_by(&:attempts_used).sort_by { |game| -game.difficulty })
5
13
  rescue Errno::ENOENT
6
14
  []
7
15
  end
16
+
17
+ private
18
+
19
+ def create_table(users)
20
+ table = Terminal::Table.new(headings: ['Rating', 'Name', 'Difficulty', 'Attempts Total',
21
+ 'Attempts Used', 'Hints Total', 'Hints Used'])
22
+ rate = 1
23
+
24
+ users.each do |user|
25
+ table.add_row([rate, user.name, DIFFICULTY.keys[user.difficulty],
26
+ user.attempts_total, user.attempts_used, user.hints_used, user.hints_total])
27
+ rate += 1
28
+ end
29
+ table
30
+ end
8
31
  end
data/lib/validate.rb CHANGED
@@ -16,6 +16,13 @@ module Validate
16
16
  end
17
17
  end
18
18
 
19
+ def check_input(input, command_list)
20
+ if input.to_i.zero? && command_list.include?(input.to_sym)
21
+ input.to_sym
22
+ else false
23
+ end
24
+ end
25
+
19
26
  private
20
27
 
21
28
  def valid_number?(arr_code)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: new_super_codebreaker_2021
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nazar Dakhno
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-05 00:00:00.000000000 Z
11
+ date: 2021-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fasterer
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.21.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: terminal-table
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.1
83
97
  description: Game where you need to guess the secret code
84
98
  email:
85
99
  - dakhnonazar@gmail.com