codebreaker_core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 30fa8a45cc138e6b75de69123e0dcbe96911771c889d130175ac529b71348a99
4
+ data.tar.gz: 7bbcff28e91ed1861ee8c2f7350076b10f31e3873c17aae26c16c64618cfdc0e
5
+ SHA512:
6
+ metadata.gz: fceb508138d3b79078edd253254e5d02b5c9f4637b912732090cd00e41e8aefaad458c095918164ad84c921feeb1f103744a08ee7c54b708318f064ba042daba
7
+ data.tar.gz: 43a9ed318dea000020c992f850b80c163ec62707e9828d780bf6605cd28e7fb38451154b6e1b94b16d69321db247da8c15a92504833872ecbb5414af8685fc2f
@@ -0,0 +1,23 @@
1
+ speedups:
2
+ rescue_vs_respond_to: true
3
+ module_eval: true
4
+ shuffle_first_vs_sample: true
5
+ for_loop_vs_each: true
6
+ each_with_index_vs_while: false
7
+ map_flatten_vs_flat_map: true
8
+ reverse_each_vs_reverse_each: true
9
+ select_first_vs_detect: true
10
+ sort_vs_sort_by: true
11
+ fetch_with_argument_vs_block: true
12
+ keys_each_vs_each_key: true
13
+ hash_merge_bang_vs_hash_brackets: true
14
+ block_vs_symbol_to_proc: true
15
+ proc_call_vs_yield: true
16
+ gsub_vs_tr: true
17
+ select_last_vs_reverse_detect: true
18
+ getter_vs_attr_reader: true
19
+ setter_vs_attr_writer: true
20
+
21
+ exclude_paths:
22
+ # - 'vendor/**/*.rb'
23
+ # - 'db/schema.rb'
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/
10
+ /lib/codebreaker_core/*.yml
11
+ /spec/temp/*.yml
12
+ *.gem
13
+
14
+ # rspec failure tracking
15
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,12 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'vendor/**/*'
4
+ Layout/LineLength:
5
+ Max: 120
6
+ Metrics/BlockLength:
7
+ Exclude:
8
+ - 'spec/**/*.rb'
9
+ Style/FrozenStringLiteralComment:
10
+ Enabled: false
11
+ Style/Documentation:
12
+ Enabled: false
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at suslovichb@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
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 codebreaker_core.gemspec
6
+ gemspec
7
+
8
+ gem 'simplecov', require: false, group: :test
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ codebreaker_core (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ diff-lcs (1.3)
11
+ docile (1.3.2)
12
+ jaro_winkler (1.5.4)
13
+ json (2.3.0)
14
+ parallel (1.19.1)
15
+ parser (2.6.5.0)
16
+ ast (~> 2.4.0)
17
+ rainbow (3.0.0)
18
+ rake (12.3.3)
19
+ rspec (3.9.0)
20
+ rspec-core (~> 3.9.0)
21
+ rspec-expectations (~> 3.9.0)
22
+ rspec-mocks (~> 3.9.0)
23
+ rspec-core (3.9.0)
24
+ rspec-support (~> 3.9.0)
25
+ rspec-expectations (3.9.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-mocks (3.9.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.9.0)
31
+ rspec-support (3.9.0)
32
+ rubocop (0.78.0)
33
+ jaro_winkler (~> 1.5.1)
34
+ parallel (~> 1.10)
35
+ parser (>= 2.6)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ ruby-progressbar (~> 1.7)
38
+ unicode-display_width (>= 1.4.0, < 1.7)
39
+ ruby-progressbar (1.10.1)
40
+ simplecov (0.17.1)
41
+ docile (~> 1.1)
42
+ json (>= 1.8, < 3)
43
+ simplecov-html (~> 0.10.0)
44
+ simplecov-html (0.10.2)
45
+ unicode-display_width (1.6.0)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ bundler (~> 2.1.2)
52
+ codebreaker_core!
53
+ rake (~> 12.3.1)
54
+ rspec (~> 3.9.0)
55
+ rubocop (~> 0.78.0)
56
+ simplecov
57
+
58
+ BUNDLED WITH
59
+ 2.1.2
@@ -0,0 +1,39 @@
1
+ # CodebreakerCore
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/codebreaker_core`. 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 'codebreaker_core'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install codebreaker_core
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]/codebreaker_core. 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
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the CodebreakerCore project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/codebreaker_core/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'codebreaker_core'
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__)
@@ -0,0 +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
@@ -0,0 +1,35 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'codebreaker_core/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'codebreaker_core'
7
+ spec.version = CodebreakerCore::VERSION
8
+ spec.authors = ['Bohdan Suslovich']
9
+ spec.email = ['suslovichb@gmail.com']
10
+
11
+ spec.summary = 'Codebreaker-core'
12
+ spec.description = 'Gem for Codebreaker game'
13
+ spec.homepage = 'https://github.com/suslovichb/codebreaker_core'
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ # if spec.respond_to?(:metadata)
18
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
19
+ # else
20
+ # raise "RubyGems 2.0 or newer is required to protect against " \
21
+ # "public gem pushes."
22
+ # end
23
+
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_development_dependency 'bundler', '~> 2.1.2'
32
+ spec.add_development_dependency 'rake', '~> 12.3.1'
33
+ spec.add_development_dependency 'rspec', '~> 3.9.0'
34
+ spec.add_development_dependency 'rubocop', '~> 0.78.0'
35
+ end
@@ -0,0 +1,9 @@
1
+ require 'codebreaker_core/version'
2
+ require 'codebreaker_core/game'
3
+ require 'codebreaker_core/games_statistics'
4
+ require 'codebreaker_core/utils/data_manager'
5
+
6
+ module CodebreakerCore
7
+ # class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,93 @@
1
+ require 'yaml'
2
+ require_relative 'utils/code_generator'
3
+
4
+ module CodebreakerCore
5
+ class Game
6
+ include CodeGenerator
7
+
8
+ GUESS_PLACE = '+'.freeze
9
+ GUESS_PRESENCE = '-'.freeze
10
+ DEFAULT_USERNAME = 'Unknown Player'.freeze
11
+ DIFFICULTIES = [{ name: 'easy', attempts: 15, hints: 2, rank: 30 },
12
+ { name: 'medium', attempts: 10, hints: 1, rank: 20 },
13
+ { name: 'hell', attempts: 5, hints: 1, rank: 10 }].freeze
14
+
15
+ attr_reader :difficulty_data, :attempts_used, :hints_set
16
+
17
+ attr_accessor :username
18
+
19
+ def initialize(difficulty_name: 'easy', username: DEFAULT_USERNAME)
20
+ @difficulty_data = DIFFICULTIES.find { |d| d[:name] == difficulty_name }
21
+ @secret_code = generate_code
22
+ @hints_set = @secret_code.sample(@difficulty_data[:hints])
23
+ @attempts_used = 0
24
+ @username = username
25
+ end
26
+
27
+ def hint
28
+ return false if @hints_set.empty?
29
+
30
+ @hints_set.pop
31
+ end
32
+
33
+ def hints_used
34
+ @difficulty_data[:hints] - @hints_set.length
35
+ end
36
+
37
+ def game_data
38
+ { name: @username,
39
+ difficulty: @difficulty_data,
40
+ attempts_used: @attempts_used,
41
+ hints_used: hints_used }
42
+ end
43
+
44
+ def make_attempt(guess)
45
+ return false if lose?
46
+
47
+ validate_guess(guess)
48
+ @attempts_used += 1
49
+ guess_code = guess.to_s.each_char.map(&:to_i)
50
+ return '++++' if guess_code == @secret_code
51
+ return '' if (@secret_code & guess_code).empty?
52
+
53
+ matched = match_indices(guess_code)
54
+ pairs_number = calc_pairs(guess_code: guess_code, exclude: matched)
55
+ GUESS_PLACE * matched.length + GUESS_PRESENCE * pairs_number
56
+ end
57
+
58
+ def win?(guess)
59
+ guess.to_s.each_char.map(&:to_i) == @secret_code
60
+ end
61
+
62
+ def lose?
63
+ @attempts_used >= @difficulty_data[:attempts]
64
+ end
65
+
66
+ private
67
+
68
+ def match_indices(guess_code)
69
+ indices = []
70
+ guess_code.each_with_index do |item, index|
71
+ indices << index if item == @secret_code[index]
72
+ end
73
+ indices
74
+ end
75
+
76
+ def calc_pairs(guess_code:, exclude: [])
77
+ delete_excluded = ->(arr) { arr.reject.with_index { |_, index| exclude.include?(index) } }
78
+ secret_array = delete_excluded.call(@secret_code)
79
+ guess_array = delete_excluded.call(guess_code)
80
+ guess_array.each { |item| secret_array.delete_at(secret_array.index(item) || secret_array.length) }
81
+ guess_array.length - secret_array.length
82
+ end
83
+
84
+ def validate_guess(guess)
85
+ raise ArgumentError, 'Incorrect guess code' unless guess.to_s.match(/^[1-6]{4}$/)
86
+ end
87
+
88
+ def validate_name(name)
89
+ raise ArgumentError, 'Usernme must be String' unless name.instance_of? String
90
+ raise ArgumentError, 'Incorrect username' unless name.match(/^[0-9a-zA-Z]{3,20}$/)
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'utils/data_manager'
2
+
3
+ module CodebreakerCore
4
+ class GamesStatistics
5
+ include DataManager
6
+
7
+ attr_accessor :games, :statistics_file
8
+
9
+ def initialize(statistics_file_path)
10
+ @statistics_file = statistics_file_path
11
+ @games = load_games
12
+ end
13
+
14
+ def add_game(game)
15
+ @games << game.game_data
16
+ end
17
+
18
+ def save(file_path = @statistics_file)
19
+ sort_games
20
+ data = { games: @games }
21
+ save_to_yml(data, file_path)
22
+ end
23
+
24
+ def sort_games
25
+ @games.sort_by! { |g| [g[:difficulty][:rank], g[:attempts_used], g[:hints_used]] }
26
+ end
27
+
28
+ def load_games(file_path = @statistics_file)
29
+ data = pull_from_yml(file_path) if File.exist?(file_path)
30
+ return [] unless data
31
+
32
+ data[:games]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module CodeGenerator
2
+ def generate_code
3
+ Array.new(4) { rand(1..6) }
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'yaml'
2
+
3
+ module DataManager
4
+ def save_to_yml(data_to_save, file)
5
+ data = YAML.dump(data_to_save)
6
+ File.write(file, data, mode: 'w')
7
+ end
8
+
9
+ def pull_from_yml(file_path)
10
+ file = File.open(file_path)
11
+ data = YAML.safe_load(file, [Symbol], [], true)
12
+ file.close
13
+ data
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module CodebreakerCore
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codebreaker_core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bohdan Suslovich
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.9.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.9.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.78.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.78.0
69
+ description: Gem for Codebreaker game
70
+ email:
71
+ - suslovichb@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".fasterer.yml"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".rubocop.yml"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - codebreaker_core.gemspec
89
+ - lib/codebreaker_core.rb
90
+ - lib/codebreaker_core/game.rb
91
+ - lib/codebreaker_core/games_statistics.rb
92
+ - lib/codebreaker_core/utils/code_generator.rb
93
+ - lib/codebreaker_core/utils/data_manager.rb
94
+ - lib/codebreaker_core/version.rb
95
+ homepage: https://github.com/suslovichb/codebreaker_core
96
+ licenses: []
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubygems_version: 3.0.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Codebreaker-core
117
+ test_files: []