andrii_codebreaker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e1607581ffecd6cfb9a39facf2faac691b9e0a202fb84beb49abf204cada9cb7
4
+ data.tar.gz: 48026a23d738ca88f541744730a7d815c8a3e79d4c71126855783c20bf817203
5
+ SHA512:
6
+ metadata.gz: d6d0086e5960049a94be3964254e615b78c5af76212d435dd8b9e162522b4af4e8969ffc5af04cc11be887884205ee1809034a0c14f233989897db2d61b21488
7
+ data.tar.gz: eaba1452c2d889ebf2fc2a992aa2d6a4671449b4d6e5851ba1f99732a3565e032d4278f822d221f12c10db48d210994cfad31c43793c842c76704c710f75edd7
data/.fasterer.yml ADDED
@@ -0,0 +1,19 @@
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
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ SuggestExtensions: false
4
+
5
+ Style/Documentation:
6
+ Enabled: false
7
+
8
+ Style/FrozenStringLiteralComment:
9
+ Enabled: false
10
+
11
+ Metrics/LineLength:
12
+ Max: 120
13
+
14
+ Metrics/BlockLength:
15
+ Exclude:
16
+ - 'Rakefile'
17
+ - '**/*.rake'
18
+ - 'spec/**/*.rb'
19
+ - '*.gemspec'
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ codebreaker-gem
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ andrii_codebreaker (0.1.0)
5
+ simplecov
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ colorize (0.8.1)
12
+ diff-lcs (1.4.4)
13
+ docile (1.4.0)
14
+ fasterer (0.9.0)
15
+ colorize (~> 0.7)
16
+ ruby_parser (>= 3.14.1)
17
+ parallel (1.21.0)
18
+ parser (3.0.2.0)
19
+ ast (~> 2.4.1)
20
+ rainbow (3.0.0)
21
+ rake (13.0.6)
22
+ regexp_parser (2.1.1)
23
+ rexml (3.2.5)
24
+ rspec (3.10.0)
25
+ rspec-core (~> 3.10.0)
26
+ rspec-expectations (~> 3.10.0)
27
+ rspec-mocks (~> 3.10.0)
28
+ rspec-core (3.10.1)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-expectations (3.10.1)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.10.0)
33
+ rspec-mocks (3.10.2)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.10.0)
36
+ rspec-support (3.10.2)
37
+ rubocop (1.22.2)
38
+ parallel (~> 1.10)
39
+ parser (>= 3.0.0.0)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ regexp_parser (>= 1.8, < 3.0)
42
+ rexml
43
+ rubocop-ast (>= 1.12.0, < 2.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 1.4.0, < 3.0)
46
+ rubocop-ast (1.12.0)
47
+ parser (>= 3.0.1.1)
48
+ ruby-progressbar (1.11.0)
49
+ ruby_parser (3.17.0)
50
+ sexp_processor (~> 4.15, >= 4.15.1)
51
+ sexp_processor (4.15.3)
52
+ simplecov (0.21.2)
53
+ docile (~> 1.1)
54
+ simplecov-html (~> 0.11)
55
+ simplecov_json_formatter (~> 0.1)
56
+ simplecov-html (0.12.3)
57
+ simplecov_json_formatter (0.1.3)
58
+ unicode-display_width (2.1.0)
59
+
60
+ PLATFORMS
61
+ x86_64-linux
62
+
63
+ DEPENDENCIES
64
+ andrii_codebreaker!
65
+ fasterer
66
+ rake
67
+ rspec
68
+ rubocop
69
+
70
+ BUNDLED WITH
71
+ 2.2.29
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Andrii
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 ADDED
@@ -0,0 +1,39 @@
1
+ # AndriiCodebreaker
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/andrii_codebreaker`. 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 'andrii_codebreaker'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install andrii_codebreaker
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 the created tag, 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]/andrii_codebreaker.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'andrii_codebreaker'
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 ADDED
@@ -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,88 @@
1
+ require_relative '../andrii_codebreaker'
2
+ module AndriiCodebreaker
3
+ class Game
4
+ include Validate
5
+ include Model
6
+ include Statistic
7
+ attr_reader :secret_code
8
+ attr_accessor :secret_code_copy
9
+
10
+ CONST_COMMAND = {
11
+ start: 'start',
12
+ rules: 'rules',
13
+ stats: 'stats',
14
+ exit: 'exit'
15
+ }.freeze
16
+
17
+ WIN_MATRIX = ['+', '+', '+', '+'].freeze
18
+
19
+ def initialize
20
+ @secret_code = generate_code
21
+ @secret_code_copy = @secret_code.dup
22
+ end
23
+
24
+ def save_result(user, file)
25
+ save(user, file)
26
+ end
27
+
28
+ def hints(user)
29
+ return unless user.hints_total > user.hints_now
30
+
31
+ user.hints_now += 1
32
+ @secret_code_copy.pop
33
+ end
34
+
35
+ def compare_codes(user_code)
36
+ matches, user_code, code_copy = right_place(user_code)
37
+ number_secret_code(user_code, matches, code_copy)
38
+ end
39
+
40
+ private
41
+
42
+ def string_to_integer(input_code)
43
+ array_int = []
44
+ array_chars = input_code.chars
45
+ array_chars.each { |i| array_int << i.to_i }
46
+ array_int
47
+ end
48
+
49
+ def right_place(user_code)
50
+ code_copy = @secret_code.dup
51
+ matches = []
52
+ user_code.each_index do |i|
53
+ next unless @secret_code[i] == user_code[i]
54
+
55
+ matches.unshift('+')
56
+ user_code[i] = nil
57
+ code_copy[i] = false
58
+ end
59
+ [matches, user_code, code_copy]
60
+ end
61
+
62
+ def number_secret_code(user_code, matches, code_copy)
63
+ amount_numbers_secret = Hash.new(0)
64
+ amount_numbers_now = Hash.new(0)
65
+ code_copy.each { |number| amount_numbers_secret[number.to_s] += 1 }
66
+ user_code.each do |element|
67
+ next unless include_element?(code_copy, element) &&
68
+ amount_of_element?(amount_numbers_now, amount_numbers_secret, element)
69
+
70
+ matches.push('-')
71
+ amount_numbers_now[element.to_s] += 1
72
+ end
73
+ matches
74
+ end
75
+
76
+ def include_element?(given_array, element)
77
+ given_array.include? element
78
+ end
79
+
80
+ def amount_of_element?(amount_numbers_now, amount_numbers_secret, element)
81
+ amount_numbers_now[element.to_s] < amount_numbers_secret[element.to_s]
82
+ end
83
+
84
+ def generate_code
85
+ Array.new(4) { rand(1..6) }
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,22 @@
1
+ require 'yaml'
2
+ module AndriiCodebreaker
3
+ module Model
4
+ def load_file(file)
5
+ return nil if File.empty?(file)
6
+
7
+ save_data(file)
8
+ rescue Errno::ENOENT
9
+ nil
10
+ end
11
+
12
+ def save_data(file)
13
+ File.open(file, 'r') do |filename|
14
+ YAML.safe_load(filename)
15
+ end
16
+ end
17
+
18
+ def save(user, file)
19
+ File.open(file, 'a') { |f| YAML.dump(user, f) }
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ module AndriiCodebreaker
2
+ module Statistic
3
+ def statistics
4
+ data = YAML.load_stream(File.open('result.yml'))
5
+ data.sort_by { |item| [item.difficulty, -item.attempts_now, item.hints_now] }.reverse
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,28 @@
1
+ module AndriiCodebreaker
2
+ class User
3
+ DIFFICULTY = {
4
+ easy: { attempts: 15, hints: 2 },
5
+ medium: { attempts: 10, hints: 1 },
6
+ hell: { attempts: 5, hints: 1 }
7
+ }.freeze
8
+
9
+ attr_reader :name, :difficulty, :attempts_total, :hints_total
10
+ attr_accessor :hints_now, :attempts_now
11
+
12
+ def initialize(name, difficulty)
13
+ @name = name
14
+ @difficulty = difficulty
15
+ @hints_now = 0
16
+ @attempts_now = 0
17
+ set_count_attempt
18
+ end
19
+
20
+ private
21
+
22
+ def set_count_attempt
23
+ difficult = DIFFICULTY[@difficulty.to_sym]
24
+ @hints_total = difficult[:hints]
25
+ @attempts_total = difficult[:attempts]
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ module AndriiCodebreaker
2
+ module Validate
3
+ def valide_name(name)
4
+ name if valid_name?(name)
5
+ end
6
+
7
+ def validate_guess(code)
8
+ code.match(/^[1-6]{4}$/)
9
+ end
10
+
11
+ private
12
+
13
+ def valid_name?(name)
14
+ name.length >= 3 && name.length <= 20
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module AndriiCodebreaker
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,6 @@
1
+ require_relative 'andrii_codebreaker/model'
2
+ require_relative 'andrii_codebreaker/user'
3
+ require_relative 'andrii_codebreaker/validate/validate'
4
+ require_relative 'andrii_codebreaker/statistic'
5
+ require_relative 'andrii_codebreaker/game'
6
+ require_relative 'andrii_codebreaker/version'
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: andrii_codebreaker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrii
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fasterer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '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'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Game in Codebreaker
84
+ email:
85
+ - andrii.okhrimets@rubygarage.org
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".fasterer.yml"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".ruby-gemset"
94
+ - ".ruby-version"
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - lib/andrii_codebreaker.rb
103
+ - lib/andrii_codebreaker/game.rb
104
+ - lib/andrii_codebreaker/model.rb
105
+ - lib/andrii_codebreaker/statistic.rb
106
+ - lib/andrii_codebreaker/user.rb
107
+ - lib/andrii_codebreaker/validate/validate.rb
108
+ - lib/andrii_codebreaker/version.rb
109
+ homepage: https://github.com/Andrii-RubyGarage/Codebreker-Gem
110
+ licenses:
111
+ - MIT
112
+ metadata:
113
+ homepage_uri: https://github.com/Andrii-RubyGarage/Codebreker-Gem
114
+ source_code_uri: https://github.com/Andrii-RubyGarage/Codebreker-Gem
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 2.7.2
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.1.4
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Game in Codebreaker
134
+ test_files: []