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 +4 -4
- data/Gemfile +11 -6
- data/Rakefile +8 -6
- data/bin/console +15 -14
- data/codebreakergem.gemspec +43 -42
- data/lib/codebreakergem.rb +120 -6
- data/lib/codebreakergem/version.rb +5 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc84aeac7b60f6690d26ee273f833ad1f08625022b4761c5d80f85a16d006b60
|
4
|
+
data.tar.gz: 45b2c7667372df83d9cd1a292944e772c2f240c415b8d4a13b2bacebfe3cecf0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dfd99b62121c6eddf7245b5f3a32519e148483fdd8ad1794958233c66aba9fa7fb3e9b0d7b276e8a089233d7e00fd6f727514df90c9f0fb2c3db25a9358af57a
|
7
|
+
data.tar.gz: 4dfbb4ab4be8e3224c859abe3986d9f9b9ea017ab25562fe07adfa764e556c50b7d0a31f2d6f6657266fc4594649c843a9a213ef3922d83a65dfb1120779cfdb
|
data/Gemfile
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
4
|
-
require
|
5
|
-
|
6
|
-
|
7
|
-
#
|
8
|
-
|
9
|
-
|
10
|
-
#
|
11
|
-
#
|
12
|
-
|
13
|
-
|
14
|
-
|
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__)
|
data/codebreakergem.gemspec
CHANGED
@@ -1,42 +1,43 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
spec.
|
9
|
-
spec.
|
10
|
-
spec.
|
11
|
-
|
12
|
-
|
13
|
-
spec.
|
14
|
-
spec.
|
15
|
-
spec.
|
16
|
-
|
17
|
-
|
18
|
-
#
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
spec.metadata[
|
24
|
-
spec.metadata[
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
#
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
spec.
|
37
|
-
spec.
|
38
|
-
|
39
|
-
|
40
|
-
spec.add_development_dependency
|
41
|
-
spec.add_development_dependency
|
42
|
-
|
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
|
data/lib/codebreakergem.rb
CHANGED
@@ -1,6 +1,120 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module Codebreakergem
|
4
|
-
class
|
5
|
-
|
6
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2019-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|