codebreakergem 0.1.12 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt CHANGED
@@ -1,21 +1,21 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2019 Sasha
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Sasha
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,43 +1,43 @@
1
- # Codebreakergem
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/codebreakergem`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'codebreakergem'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install codebreakergem
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/codebreakergem. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
-
37
- ## License
38
-
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
-
41
- ## Code of Conduct
42
-
43
- Everyone interacting in the Codebreakergem project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/codebreakergem/blob/master/CODE_OF_CONDUCT.md).
1
+ # Codebreakergem
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/codebreakergem`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'codebreakergem'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install codebreakergem
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/codebreakergem. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Codebreakergem project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/codebreakergem/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
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
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,15 +1,15 @@
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
+ #!/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/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,43 +1,39 @@
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
+ # 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
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.' unless spec.respond_to?(:metadata)
21
+
22
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
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
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = 'exe'
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ['lib']
35
+
36
+ spec.add_development_dependency 'bundler', '~> 1.17'
37
+ spec.add_development_dependency 'rake', '~> 10.0'
38
+ spec.add_development_dependency 'rspec', '~> 3.0'
39
+ end
@@ -1,12 +1,12 @@
1
- # frozen_string_literal: true
2
-
3
- module Codebreakergem
4
- class Difficulty
5
- attr_reader :title, :order
6
- attr_accessor :attempts, :hints
7
-
8
- def initialize
9
- raise NotImplementedError
10
- end
11
- end
12
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreakergem
4
+ class Difficulty
5
+ attr_reader :title, :order
6
+ attr_accessor :attempts, :hints
7
+
8
+ def initialize
9
+ raise NotImplementedError
10
+ end
11
+ end
12
+ end
@@ -1,12 +1,12 @@
1
- # frozen_string_literal: true
2
-
3
- module Codebreakergem
4
- class Easy < Difficulty
5
- def initialize
6
- @title = 'Easy'
7
- @attempts = 15
8
- @hints = 2
9
- @order = 3
10
- end
11
- end
12
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreakergem
4
+ class Easy < Difficulty
5
+ def initialize
6
+ @title = 'Easy'
7
+ @attempts = 15
8
+ @hints = 2
9
+ @order = 3
10
+ end
11
+ end
12
+ end
@@ -1,12 +1,12 @@
1
- # frozen_string_literal: true
2
-
3
- module Codebreakergem
4
- class Hell < Difficulty
5
- def initialize
6
- @title = 'Hell'
7
- @attempts = 5
8
- @hints = 1
9
- @order = 1
10
- end
11
- end
12
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreakergem
4
+ class Hell < Difficulty
5
+ def initialize
6
+ @title = 'Hell'
7
+ @attempts = 5
8
+ @hints = 1
9
+ @order = 1
10
+ end
11
+ end
12
+ end
@@ -1,12 +1,12 @@
1
- # frozen_string_literal: true
2
-
3
- module Codebreakergem
4
- class Medium < Difficulty
5
- def initialize
6
- @title = 'Medium'
7
- @attempts = 10
8
- @hints = 1
9
- @order = 2
10
- end
11
- end
12
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreakergem
4
+ class Medium < Difficulty
5
+ def initialize
6
+ @title = 'Medium'
7
+ @attempts = 10
8
+ @hints = 1
9
+ @order = 2
10
+ end
11
+ end
12
+ end
@@ -1,25 +1,29 @@
1
- # frozen_string_literal: true
2
-
3
- module Codebreakergem
4
- class FileWorker
5
- PERMITTED_CLASSES = [Statistic, Difficulty, Easy, Medium, Hell].freeze
6
-
7
- class << self
8
- def read_from_file(filename)
9
- return nil unless File.file?(Game::FILE)
10
-
11
- YAML.safe_load(File.read(filename), PERMITTED_CLASSES, [], true)
12
- end
13
-
14
- def add_to_file(filename, data)
15
- data_in_file = Array(YAML.safe_load(File.read(filename), PERMITTED_CLASSES, [], true))
16
- data_in_file << data
17
- write_to_file(filename, data_in_file)
18
- end
19
-
20
- def write_to_file(filename, data)
21
- File.open(filename, 'w') { |file| file.write(data.to_yaml) }
22
- end
23
- end
24
- end
25
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreakergem
4
+ class FileWorker
5
+ PERMITTED_CLASSES = [Symbol].freeze
6
+
7
+ class << self
8
+ def read_from_file(filename)
9
+ return unless File.file?(Game::FILE)
10
+
11
+ YAML.safe_load(File.read(filename), PERMITTED_CLASSES, [], true)
12
+ end
13
+
14
+ def add_to_file(filename, data)
15
+ if File.file?(filename)
16
+ data_in_file = YAML.safe_load(File.read(filename), PERMITTED_CLASSES, [], true)
17
+ data_in_file.is_a?(Array) ? data_in_file << data : data_in_file = [data_in_file, data]
18
+ write_to_file(filename, data_in_file)
19
+ else
20
+ write_to_file(filename, data)
21
+ end
22
+ end
23
+
24
+ def write_to_file(filename, data)
25
+ File.open(filename, 'w') { |file| file.write(data.to_yaml) }
26
+ end
27
+ end
28
+ end
29
+ end
data/lib/classes/game.rb CHANGED
@@ -1,116 +1,103 @@
1
- # frozen_string_literal: true
2
-
3
- module Codebreakergem
4
- class Game
5
- include Checker
6
-
7
- DIGIT_NUMBER = 4
8
- DIFFICULTIES = [Easy.new, Medium.new, Hell.new].freeze
9
- COMMANDS = %w[hint exit].freeze
10
- GUESS_PLACE = '+'
11
- GUESS_PRESENCE = '-'
12
- FILE = File.expand_path(__dir__ + '/../data/data.yml')
13
-
14
- attr_reader :secret_code, :user
15
- attr_accessor :difficulty, :statistics
16
-
17
- def initialize(user)
18
- @user = user
19
- @secret_code = generate_code
20
- end
21
-
22
- def show_hint
23
- return I18n.t(:no_hints) unless difficulty.hints.positive?
24
-
25
- difficulty.hints -= 1
26
- secret_code[difficulty.hints]
27
- end
28
-
29
- def try_guess(guess)
30
- if guess == secret_code
31
- save
32
- difficulty.attempts = 0
33
- return I18n.t(:victory_message)
34
- end
35
-
36
- difficulty.attempts -= 1
37
- return I18n.t(:out_of_attempts) unless difficulty.attempts.positive?
38
-
39
- make_guess(guess)
40
- end
41
-
42
- def validate_guess(guess)
43
- return false if guess.empty?
44
-
45
- guess.match?(/\D/) ? COMMANDS.include?(guess) : true
46
- end
47
-
48
- def save
49
- source_difficulty = DIFFICULTIES.find { |item| item.title == difficulty.title }
50
- statistic = Statistic.new(name: user.name, difficulty: difficulty, attempts_total: source_difficulty.attempts,
51
- hints_total: source_difficulty.hints)
52
- File.file?(FILE) ? FileWorker.add_to_file(FILE, statistic) : FileWorker.write_to_file(FILE, statistic)
53
- end
54
-
55
- class << self
56
- def rules
57
- I18n.t(:rules)
58
- end
59
-
60
- def stats
61
- index = 0
62
- result = I18n.t(:stats_header)
63
- data = Array(FileWorker.read_from_file(FILE))
64
- return result unless data
65
-
66
- sorted_data = data.sort_by { |item| [item.difficulty.order, item.attempts_used, item.hints_used] }
67
- sorted_data.each { |item| result += stats_constructor(index += 1, item) }
68
- result
69
- end
70
-
71
- private
72
-
73
- def stats_constructor(index, item)
74
- "#{index}\t #{item.name}\t #{item.difficulty.title}\t #{item.attempts_total}\t " \
75
- "#{item.attempts_used}\t #{item.hints_total}\t #{item.hints_used}\n"
76
- end
77
- end
78
-
79
- private
80
-
81
- def generate_code
82
- DIGIT_NUMBER.times.map { rand(1..6) }.join
83
- end
84
-
85
- def find_place(guess, code)
86
- result = ''
87
- (0...guess.length).each do |index|
88
- next unless guess[index] == code[index]
89
-
90
- result += GUESS_PLACE
91
- code[index] = ' '
92
- guess[index] = '_'
93
- end
94
- result
95
- end
96
-
97
- def find_presence(guess, code)
98
- result = ''
99
- (0...guess.length).each do |index|
100
- position = code.index(guess[index])
101
- if position
102
- result += GUESS_PRESENCE
103
- code[position] = ' '
104
- end
105
- end
106
- result
107
- end
108
-
109
- def make_guess(guess)
110
- result = ''
111
- code_copy = secret_code.dup
112
- result += find_place(guess, code_copy)
113
- result + find_presence(guess, code_copy)
114
- end
115
- end
116
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreakergem
4
+ class Game
5
+ include Checker
6
+
7
+ DIGIT_NUMBER = 4
8
+ CORRECT_PLACE_SYMBOL = '+'
9
+ INCORRECT_PLACE_SYMBOL = '-'
10
+ FILE = File.expand_path(__dir__ + '/../data/data.yml')
11
+
12
+ attr_reader :secret_code, :user
13
+ attr_accessor :difficulty, :statistics
14
+
15
+ def initialize(user, difficulty_title)
16
+ @user = user
17
+ @difficulty = difficulty_factory(difficulty_title)
18
+ @secret_code = generate_code
19
+ end
20
+
21
+ def show_hint
22
+ return I18n.t(:no_hints) unless difficulty.hints.positive?
23
+
24
+ difficulty.hints -= 1
25
+ secret_code[difficulty.hints]
26
+ end
27
+
28
+ def try_guess(guess)
29
+ if guess == secret_code
30
+ save
31
+ difficulty.attempts = 0
32
+ return I18n.t(:victory_message)
33
+ end
34
+
35
+ difficulty.attempts -= 1
36
+ return I18n.t(:out_of_attempts) unless difficulty.attempts.positive?
37
+
38
+ make_guess(guess)
39
+ end
40
+
41
+ def save
42
+ source = difficulty_factory(difficulty.title)
43
+ FileWorker.add_to_file(FILE, to_h(source, source.attempts - difficulty.attempts, source.hints - difficulty.hints))
44
+ end
45
+
46
+ def to_h(source_difficulty, attempts_used, hints_used)
47
+ { name: user.name,
48
+ difficulty: difficulty.title,
49
+ order: difficulty.order,
50
+ attempts_total: source_difficulty.attempts,
51
+ attempts_used: attempts_used,
52
+ hints_total: source_difficulty.hints,
53
+ hints_used: hints_used }
54
+ end
55
+
56
+ private
57
+
58
+ def used_calculation(initial, actual)
59
+ initial - actual
60
+ end
61
+
62
+ def generate_code
63
+ DIGIT_NUMBER.times.map { rand(1..6) }.join
64
+ end
65
+
66
+ def find_place(guess, code)
67
+ result = ''
68
+ (0...guess.length).each do |index|
69
+ next unless guess[index] == code[index]
70
+
71
+ result += CORRECT_PLACE_SYMBOL
72
+ code[index] = ' '
73
+ guess[index] = '_'
74
+ end
75
+ result
76
+ end
77
+
78
+ def find_presence(guess, code)
79
+ result = ''
80
+ (0...guess.length).each do |index|
81
+ position = code.index(guess[index])
82
+ if position
83
+ result += INCORRECT_PLACE_SYMBOL
84
+ code[position] = ' '
85
+ end
86
+ end
87
+ result
88
+ end
89
+
90
+ def make_guess(guess)
91
+ code_copy = secret_code.dup
92
+ find_place(guess, code_copy) + find_presence(guess, code_copy)
93
+ end
94
+
95
+ def difficulty_factory(difficulty_title)
96
+ case difficulty_title
97
+ when 'Easy' then Easy.new
98
+ when 'Medium' then Medium.new
99
+ when 'Hell' then Hell.new
100
+ end
101
+ end
102
+ end
103
+ end