codebreakergem 0.1.0 → 0.1.1

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: d308c8da983de4bdeab9cdffcf8890aa55cc55a0c422f72d098f5d6bd7eca091
4
- data.tar.gz: bf005159bc397aec7ab910d5f85a323e1fffc2b841de90b839a2259d067aa600
3
+ metadata.gz: fc84aeac7b60f6690d26ee273f833ad1f08625022b4761c5d80f85a16d006b60
4
+ data.tar.gz: 45b2c7667372df83d9cd1a292944e772c2f240c415b8d4a13b2bacebfe3cecf0
5
5
  SHA512:
6
- metadata.gz: c415c85f4e8c849ddc9593c03689b5c6b89502f353b31540fea9bf86558e6c370fccf05773ae7f0e5f71892c74fd0a16ada2d70577aa0e97f48ed818c83f3a3e
7
- data.tar.gz: b6fb10c87ab4822b25c338c2ca87e9266dc21faf0d2c39a91d6d267f9c5190d1cb660bc1c56d9becae4501b21260dc9af8896b340ddb06d32db552bdbc0e1d4e
6
+ metadata.gz: dfd99b62121c6eddf7245b5f3a32519e148483fdd8ad1794958233c66aba9fa7fb3e9b0d7b276e8a089233d7e00fd6f727514df90c9f0fb2c3db25a9358af57a
7
+ data.tar.gz: 4dfbb4ab4be8e3224c859abe3986d9f9b9ea017ab25562fe07adfa764e556c50b7d0a31f2d6f6657266fc4594649c843a9a213ef3922d83a65dfb1120779cfdb
data/Gemfile CHANGED
@@ -1,6 +1,11 @@
1
- source "https://rubygems.org"
2
-
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in codebreakergem.gemspec
6
- gemspec
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'byebug', '~> 9.0', '>= 9.0.6'
6
+ gem 'fasterer', '~> 0.8.0'
7
+ gem 'i18n', '~> 0.7.0'
8
+ gem 'pry', '~> 0.10.3'
9
+ gem 'rspec', '~> 3.9'
10
+ gem 'rubocop', '~> 0.76.0'
11
+ gem 'simplecov', '~> 0.12.0'
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console CHANGED
@@ -1,14 +1,15 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "codebreakergem"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'codebreakergem'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -1,42 +1,43 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "codebreakergem/version"
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "codebreakergem"
8
- spec.version = Codebreakergem::VERSION
9
- spec.authors = ["Sasha"]
10
- spec.email = ["sasha"]
11
-
12
- spec.summary = %q{Codebreaker gem}
13
- spec.description = %q{A logic game where you need to guess number}
14
- spec.homepage = "https://github.com/SashaZhuravskiy/CodebreakerGemRepo/"
15
- spec.license = "MIT"
16
-
17
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
- # to allow pushing to a single host or delete this section to allow pushing to any host.
19
- if spec.respond_to?(:metadata)
20
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
-
22
- spec.metadata["homepage_uri"] = spec.homepage
23
- spec.metadata["source_code_uri"] = "https://github.com/SashaZhuravskiy/CodebreakerGemRepo/"
24
- spec.metadata["changelog_uri"] = "https://github.com/SashaZhuravskiy/CodebreakerGemRepo/"
25
- else
26
- raise "RubyGems 2.0 or newer is required to protect against " \
27
- "public gem pushes."
28
- end
29
-
30
- # Specify which files should be added to the gem when it is released.
31
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
- end
35
- spec.bindir = "exe"
36
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
- spec.require_paths = ["lib"]
38
-
39
- spec.add_development_dependency "bundler", "~> 1.17"
40
- spec.add_development_dependency "rake", "~> 10.0"
41
- spec.add_development_dependency "rspec", "~> 3.0"
42
- end
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'codebreakergem/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'codebreakergem'
9
+ spec.version = Codebreakergem::VERSION
10
+ spec.authors = ['Sasha']
11
+ spec.email = ['sasha']
12
+
13
+ spec.summary = 'Codebreaker gem'
14
+ spec.description = 'A logic game where you need to guess number'
15
+ spec.homepage = 'https://github.com/SashaZhuravskiy/CodebreakerGemRepo/'
16
+ spec.license = 'MIT'
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
22
+
23
+ spec.metadata['homepage_uri'] = spec.homepage
24
+ spec.metadata['source_code_uri'] = 'https://github.com/SashaZhuravskiy/CodebreakerGemRepo/'
25
+ spec.metadata['changelog_uri'] = 'https://github.com/SashaZhuravskiy/CodebreakerGemRepo/'
26
+ else
27
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
28
+ 'public gem pushes.'
29
+ end
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
33
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
34
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
35
+ end
36
+ spec.bindir = 'exe'
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = ['lib']
39
+
40
+ spec.add_development_dependency 'bundler', '~> 1.17'
41
+ spec.add_development_dependency 'rake', '~> 10.0'
42
+ spec.add_development_dependency 'rspec', '~> 3.0'
43
+ end
@@ -1,6 +1,120 @@
1
- require "codebreakergem/version"
2
-
3
- module Codebreakergem
4
- class Error < StandardError; end
5
- # Your code goes here...
6
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreakergem
4
+ class Game
5
+ include Checker
6
+
7
+ DIGIT_NUMBER = 4
8
+ DIFFICULTIES = [Difficulty.new(name: 'Easy', attempts: 15, hints: 2, order: 3),
9
+ Difficulty.new(name: 'Medium', attempts: 10, hints: 1, order: 2),
10
+ Difficulty.new(name: 'Hell', attempts: 5, hints: 1, order: 1)].freeze
11
+ COMMANDS = %w[hint exit].freeze
12
+ GUESS_PLACE = '+'
13
+ GUESS_PRESENCE = '-'
14
+ FILE = 'lib\data\data.yml'
15
+
16
+ attr_reader :secret_code, :user
17
+ attr_accessor :difficulty, :statistics
18
+
19
+ def initialize(user)
20
+ @user = user
21
+ @secret_code = generate_code
22
+ end
23
+
24
+ def show_hint
25
+ if difficulty.hints.positive?
26
+ difficulty.hints -= 1
27
+ secret_code[difficulty.hints]
28
+ else
29
+ I18n.t(:no_hints)
30
+ end
31
+ end
32
+
33
+ def try_guess(guess)
34
+ return I18n.t(:out_of_attempts) unless difficulty.attempts.positive?
35
+
36
+ if guess == secret_code
37
+ save
38
+ difficulty.attempts = 0
39
+ return I18n.t(:victory_message)
40
+ end
41
+
42
+ difficulty.attempts -= 1
43
+ make_guess(guess)
44
+ end
45
+
46
+ def validate_guess(guess)
47
+ return false if guess.empty?
48
+
49
+ guess.match?(/\D/) ? COMMANDS.include?(guess) : true
50
+ end
51
+
52
+ def save
53
+ source_difficulty = DIFFICULTIES.find { |item| item.name == difficulty.name }
54
+ statistic = Statistic.new(name: user.name, difficulty: difficulty, attempts_total: source_difficulty.attempts,
55
+ hints_total: source_difficulty.hints)
56
+ File.file?(FILE) ? FileWorker.add_to_file(FILE, statistic) : FileWorker.write_to_file(FILE, statistic)
57
+ end
58
+
59
+ class << self
60
+ def rules
61
+ I18n.t(:rules)
62
+ end
63
+
64
+ def stats
65
+ index = 1
66
+ result = I18n.t(:stats_header)
67
+ data = FileWorker.read_from_file(FILE)
68
+ return unless data
69
+
70
+ if data.is_a?(Array)
71
+ sorted_data = data.select { |item| item.is_a? Statistic }.sort_by { |item| [item.difficulty.order, item.attempts_used, item.hints_used] }
72
+ sorted_data.each do |item|
73
+ result += "#{index}\t #{item.name}\t #{item.difficulty.name}\t #{item.attempts_total}\t #{item.attempts_used}\t #{item.hints_total}\t #{item.hints_used}\n"
74
+ index += 1
75
+ end
76
+ else
77
+ result += "1\t #{data.name}\t #{data.difficulty.name}\t #{data.attempts_total}\t #{data.attempts_used}\t #{data.hints_total}\t #{data.hints_used}\n"
78
+ end
79
+ result
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def generate_code
86
+ DIGIT_NUMBER.times.map { rand(1..6) }.join
87
+ end
88
+
89
+ def find_place(guess, code)
90
+ result = ''
91
+ (0...guess.length).each do |index|
92
+ next unless guess[index] == code[index]
93
+
94
+ result += GUESS_PLACE
95
+ code[index] = ' '
96
+ guess[index] = '_'
97
+ end
98
+ result
99
+ end
100
+
101
+ def find_presence(guess, code)
102
+ result = ''
103
+ (0...guess.length).each do |index|
104
+ position = code.index(guess[index])
105
+ if position
106
+ result += GUESS_PRESENCE
107
+ code[position] = ' '
108
+ end
109
+ end
110
+ result
111
+ end
112
+
113
+ def make_guess(guess)
114
+ result = ''
115
+ code_copy = secret_code.dup
116
+ result += find_place(guess, code_copy)
117
+ result + find_presence(guess, code_copy)
118
+ end
119
+ end
120
+ end
@@ -1,3 +1,5 @@
1
- module Codebreakergem
2
- VERSION = "0.1.0"
3
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreakergem
4
+ VERSION = '0.1.1'
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebreakergem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sasha
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-21 00:00:00.000000000 Z
11
+ date: 2019-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler