codebreaker_smn 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: 6af57218aba00ae00628d80a3311829ac1509188e0e75ea45ec63e1f480179b9
4
- data.tar.gz: a43b99fad4edeb401671e6c679ad8278af02ed2c21db1f19883b3756a2d626ab
3
+ metadata.gz: 818dedfd723a81147690ad0ee563663536f138cc119ae7c05837ff593a518194
4
+ data.tar.gz: 7a759cf89765076b62a5b51605400332bf74bc83800c8cf47224ec295b8cbc09
5
5
  SHA512:
6
- metadata.gz: e740c30d89f44a7b4e2013796e332d3d9a966b2235a45df6c197ef173e4173b9bbe6f2ace15348a64f8dbce7534f5f889f5c413b8f52a664c7da5f22f3f9ab57
7
- data.tar.gz: 2e329a83adf789d7b686d2346a45324b4c6500191018cc01e7e4ac2bd838b2a6fe0eb0248bb4d918c5bc55ec89aab6c5a6889f0fc440cb7eab3ee592c89205c2
6
+ metadata.gz: 1d606c9062e04896f11c577867d69a4e33094d6d48b0e646e192df505b663ad124809a631a0c8693e6de4361699e9dcdd6339423567484196d00ccbf066e97e7
7
+ data.tar.gz: e5f06e3439ac902bf1d4109bb075d72be8e83c28ab67d3e25b441376c30015d1d2fd2930ea82c43b399a7a20fd21649cb6e16c243a5c35c827e991daca9c8548
data/.rspec CHANGED
@@ -1,3 +1,3 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml CHANGED
@@ -1,7 +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.1
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.1
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in codebreaker.gemspec
4
- gemspec
5
-
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in codebreaker.gemspec
4
+ gemspec
5
+
6
6
  gem 'simplecov', require: false, group: :test
data/Gemfile.lock CHANGED
@@ -1,43 +1,43 @@
1
- PATH
2
- remote: .
3
- specs:
4
- codebreaker_smn (0.1.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- diff-lcs (1.3)
10
- docile (1.3.2)
11
- json (2.2.0)
12
- rake (10.5.0)
13
- rspec (3.8.0)
14
- rspec-core (~> 3.8.0)
15
- rspec-expectations (~> 3.8.0)
16
- rspec-mocks (~> 3.8.0)
17
- rspec-core (3.8.2)
18
- rspec-support (~> 3.8.0)
19
- rspec-expectations (3.8.4)
20
- diff-lcs (>= 1.2.0, < 2.0)
21
- rspec-support (~> 3.8.0)
22
- rspec-mocks (3.8.1)
23
- diff-lcs (>= 1.2.0, < 2.0)
24
- rspec-support (~> 3.8.0)
25
- rspec-support (3.8.2)
26
- simplecov (0.16.1)
27
- docile (~> 1.1)
28
- json (>= 1.8, < 3)
29
- simplecov-html (~> 0.10.0)
30
- simplecov-html (0.10.2)
31
-
32
- PLATFORMS
33
- x64-mingw32
34
-
35
- DEPENDENCIES
36
- bundler (~> 2.0)
37
- codebreaker_smn!
38
- rake (~> 10.0)
39
- rspec (~> 3.0)
40
- simplecov
41
-
42
- BUNDLED WITH
43
- 2.0.2
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ codebreaker_smn (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ docile (1.3.2)
11
+ json (2.2.0)
12
+ rake (10.5.0)
13
+ rspec (3.8.0)
14
+ rspec-core (~> 3.8.0)
15
+ rspec-expectations (~> 3.8.0)
16
+ rspec-mocks (~> 3.8.0)
17
+ rspec-core (3.8.2)
18
+ rspec-support (~> 3.8.0)
19
+ rspec-expectations (3.8.4)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-mocks (3.8.1)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-support (3.8.2)
26
+ simplecov (0.16.1)
27
+ docile (~> 1.1)
28
+ json (>= 1.8, < 3)
29
+ simplecov-html (~> 0.10.0)
30
+ simplecov-html (0.10.2)
31
+
32
+ PLATFORMS
33
+ x64-mingw32
34
+
35
+ DEPENDENCIES
36
+ bundler (~> 2.0)
37
+ codebreaker_smn!
38
+ rake (~> 10.0)
39
+ rspec (~> 3.0)
40
+ simplecov
41
+
42
+ BUNDLED WITH
43
+ 2.0.2
data/LICENSE.txt CHANGED
@@ -1,21 +1,21 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2019 Someone443
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 Someone443
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,39 +1,39 @@
1
- # Codebreaker
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`. 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'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install 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 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.
36
-
37
- ## License
38
-
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
1
+ # Codebreaker
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`. 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'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install 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 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.
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 CHANGED
@@ -1,6 +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
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "codebreaker_smn"
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
+
3
+ require "bundler/setup"
4
+ require "codebreaker_smn"
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__)
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,44 +1,44 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "codebreaker_smn/version"
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "codebreaker_smn"
8
- spec.version = CodebreakerSmn::VERSION
9
- spec.authors = ["Someone443"]
10
- spec.email = ["kostyan.12@ukr.net"]
11
-
12
- spec.summary = "Codebreaker game"
13
- spec.description = "Codebreaker is a logic game in which a code-breaker tries to break a secret code created by a code-maker."
14
- spec.homepage = "https://github.com/Someone443/codebreaker"
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/Someone443/codebreaker"
24
- spec.metadata["changelog_uri"] = "https://github.com/Someone443/codebreaker"
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.required_ruby_version = '~> 2.5'
40
-
41
- spec.add_development_dependency 'bundler', '~> 2.0'
42
- spec.add_development_dependency 'rake', '~> 10.0'
43
- spec.add_development_dependency 'rspec', '~> 3.0'
44
- end
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "codebreaker_smn/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "codebreaker_smn"
8
+ spec.version = CodebreakerSmn::VERSION
9
+ spec.authors = ["Someone443"]
10
+ spec.email = ["kostyan.12@ukr.net"]
11
+
12
+ spec.summary = "Codebreaker game"
13
+ spec.description = "Codebreaker is a logic game in which a code-breaker tries to break a secret code created by a code-maker."
14
+ spec.homepage = "https://github.com/Someone443/codebreaker"
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/Someone443/codebreaker"
24
+ spec.metadata["changelog_uri"] = "https://github.com/Someone443/codebreaker"
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.required_ruby_version = '~> 2.5'
40
+
41
+ spec.add_development_dependency 'bundler', '~> 2.0'
42
+ spec.add_development_dependency 'rake', '~> 10.0'
43
+ spec.add_development_dependency 'rspec', '~> 3.0'
44
+ end
@@ -1,3 +1,3 @@
1
- module CodebreakerSmn
2
- VERSION = "0.1.0"
3
- end
1
+ module CodebreakerSmn
2
+ VERSION = "0.1.1"
3
+ end
@@ -1,145 +1,168 @@
1
- require_relative './codebreaker_smn/version.rb'
2
- require_relative './codebreaker_smn/helpers/validation_helper.rb'
3
- require_relative './codebreaker_smn/helpers/errors/validation_error.rb'
4
- require 'yaml'
5
-
6
- module CodebreakerSmn
7
- class Game
8
- include ValidationHelper
9
-
10
- DIFFICULTIES = {
11
- easy: {attempts: 15, hints: 2},
12
- medium: {attempts: 10, hints: 1},
13
- hell: {attempts: 5, hints: 1}
14
- }
15
-
16
- attr_reader :code, :state, :difficulty
17
- attr_accessor :username, :results
18
-
19
- def initialize
20
- new_game
21
- @results = load_results
22
- end
23
-
24
- def new_game
25
- reset_params
26
- @state = :new
27
- end
28
-
29
- def start
30
- generate_code
31
- @state = :started
32
- end
33
-
34
- def win
35
- @state = :win
36
- end
37
-
38
- def game_over
39
- @state = :game_over
40
- end
41
-
42
- def load_results
43
- YAML.load_file('./db/results.yml')
44
- rescue SystemCallError
45
- []
46
- end
47
-
48
- def save_results
49
- if @state == :win
50
-
51
- attempts_total = DIFFICULTIES[@difficulty.to_sym][:attempts]
52
- attempts_used = attempts_total - @attempts
53
- hints_total = DIFFICULTIES[@difficulty.to_sym][:hints]
54
- hints_used = hints_total - @hints
55
-
56
- @results << {name: @username,
57
- difficulty: @difficulty,
58
- attempts_total: attempts_total,
59
- attempts_used: attempts_used,
60
- hints_total: hints_total,
61
- hints_used: hints_used}
62
-
63
- File.write('./db/results.yml', @results.to_yaml)
64
- end
65
- end
66
-
67
- def high_scores
68
- puts 'Name, Difficulty, Attempts Total, Attempts Used, Hints Total, Hints Used'
69
- @results.each do |result|
70
- puts %Q(#{result[:name]} #{result[:difficulty]} #{result[:attempts_total].to_s} #{result[:attempts_used].to_s} #{result[:hints_total].to_s} #{result[:hints_used].to_s})
71
- end
72
- end
73
-
74
- def generate_code
75
- @code = Array.new(4) { rand(1..6) }
76
- end
77
-
78
- def guess_code(input)
79
- validate_guess(input)
80
- result = ""
81
- if @attempts > 1 && @state == :started
82
- @attempts -= 1
83
- result = process_guess(input)
84
- win if result == "++++"
85
- elsif @attempts == 1 && @state == :started
86
- result = process_guess(input)
87
- result == "++++" ? win : game_over
88
- else
89
- game_over
90
- end
91
- result
92
- end
93
-
94
- def process_guess(input)
95
- temp_code = @code.clone
96
- input = input.split("")
97
- result = ""
98
- exclude_indexes = []
99
- input.each_with_index do |char, index|
100
- if temp_code.include?(char.to_i) && (temp_code[index] == char.to_i)
101
- result << "+"
102
- exclude_indexes << index
103
- end
104
- end
105
-
106
- exclude_indexes.reverse_each do |index|
107
- input.delete_at(index)
108
- temp_code.delete_at(index)
109
- end
110
-
111
- temp_code.each_with_index do |char, index|
112
- if input.include?(char.to_s)
113
- result << "-"
114
- temp_code[index] = nil # Wrong output when code = 1455, input = 5133
115
- end
116
- end
117
-
118
- result
119
- end
120
-
121
- def get_hint
122
- if @hints > 0 && @state == :started
123
- @hints -= 1
124
- code.sample
125
- else
126
- 'No hints left!'
127
- end
128
- end
129
-
130
- def set_difficulty(level)
131
- @difficulty = level
132
- @attempts = DIFFICULTIES[level.to_sym][:attempts]
133
- @hints = DIFFICULTIES[level.to_sym][:hints]
134
- end
135
-
136
- private
137
-
138
- attr_writer :code, :state, :difficulty
139
- attr_accessor :attempts, :hints
140
-
141
- def reset_params
142
- @username, @difficulty = nil
143
- end
144
- end
145
- end
1
+ require_relative './codebreaker_smn/version.rb'
2
+ require_relative './codebreaker_smn/helpers/validation_helper.rb'
3
+ require_relative './codebreaker_smn/helpers/errors/validation_error.rb'
4
+ require 'yaml'
5
+
6
+ module CodebreakerSmn
7
+ class Game
8
+ include ValidationHelper
9
+ # Codebreaker class
10
+ # Codemaker class
11
+ # DB Helper class/module
12
+ # Stats class
13
+ # Output class -> logic regarding returning messages on errors/hints/help/etc.
14
+ # Validation class
15
+ # Custom errors class
16
+
17
+ DIFFICULTIES = {
18
+ easy: {attempts: 15, hints: 2},
19
+ medium: {attempts: 10, hints: 1},
20
+ hell: {attempts: 5, hints: 1}
21
+ }
22
+
23
+ attr_reader :code, :state, :difficulty
24
+ attr_accessor :username, :attempts, :hints, :results
25
+
26
+ def initialize
27
+ new_game
28
+ @results = load_results
29
+ end
30
+
31
+ def new_game
32
+ reset_params
33
+ @state = :new
34
+ end
35
+
36
+ def start
37
+ generate_code
38
+ @state = :started
39
+ end
40
+
41
+ def win
42
+ @state = :win
43
+ end
44
+
45
+ def game_over
46
+ @state = :game_over
47
+ end
48
+
49
+ def load_results # TODO!!!! db path should be provided from Console
50
+ YAML.load_file('./db/results.yml')
51
+ rescue SystemCallError
52
+ []
53
+ end
54
+
55
+ def save_results # TODO!!!!
56
+ if @state == :win
57
+
58
+ attempts_total = DIFFICULTIES[@difficulty.to_sym][:attempts]
59
+ attempts_used = attempts_total - @attempts
60
+ hints_total = DIFFICULTIES[@difficulty.to_sym][:hints]
61
+ hints_used = hints_total - @hints
62
+
63
+ @results << {name: @username,
64
+ difficulty: @difficulty,
65
+ attempts_total: attempts_total,
66
+ attempts_used: attempts_used,
67
+ hints_total: hints_total,
68
+ hints_used: hints_used,
69
+ date: Date.today}
70
+
71
+ File.write('./db/results.yml', @results.to_yaml)
72
+ end
73
+ end
74
+
75
+ def high_scores # TODO!!!!
76
+ attempts_total = DIFFICULTIES[@difficulty.to_sym][:attempts]
77
+ attempts_used = attempts_total - @attempts
78
+ hints_total = DIFFICULTIES[@difficulty.to_sym][:hints]
79
+ hints_used = hints_total - @hints
80
+
81
+ { name: @username,
82
+ difficulty: @difficulty,
83
+ attempts_total: attempts_total,
84
+ attempts_used: attempts_used,
85
+ hints_total: hints_total,
86
+ hints_used: hints_used,
87
+ date: Date.today }
88
+
89
+ # This method should combine and sort stat inside @results var and just return this @results var
90
+ # We should return @results from just last game -> Console or Web app should form Stat on its own
91
+ #puts 'Name, Difficulty, Attempts Total, Attempts Used, Hints Total, Hints Used'
92
+ #@results.each do |result|
93
+ # puts %Q(#{result[:name]} #{result[:difficulty]} #{result[:attempts_total].to_s} #{result[:attempts_used].to_s} #{result[:hints_total].to_s} #{result[:hints_used].to_s})
94
+ #end
95
+ end
96
+
97
+ def generate_code
98
+ @code = Array.new(4) { rand(1..6) }
99
+ end
100
+
101
+ def guess_code(input)
102
+ validate_guess(input)
103
+ result = ""
104
+ if @attempts > 1 && @state == :started
105
+ @attempts -= 1
106
+ result = process_guess(input)
107
+ win if result == "++++"
108
+ elsif @attempts == 1 && @state == :started
109
+ @attempts -= 1
110
+ result = process_guess(input)
111
+ result == "++++" ? win : game_over
112
+ else
113
+ game_over
114
+ end
115
+ result
116
+ end
117
+
118
+ def process_guess(input)
119
+ temp_code = @code.clone
120
+ input = input.split("")
121
+ result = ""
122
+ exclude_indexes = []
123
+ input.each_with_index do |char, index|
124
+ if temp_code.include?(char.to_i) && (temp_code[index] == char.to_i)
125
+ result << "+"
126
+ exclude_indexes << index
127
+ end
128
+ end
129
+
130
+ exclude_indexes.reverse_each do |index|
131
+ input.delete_at(index)
132
+ temp_code.delete_at(index)
133
+ end
134
+
135
+ temp_code.each_with_index do |char, index|
136
+ if input.include?(char.to_s) # input.uniq.include? or temp_code.uniq.include?
137
+ result << "-"
138
+ temp_code[index] = nil # We have to check if char is one in the input or several -> count of matches in input
139
+ end # code = 1455, input = 5133
140
+ end
141
+
142
+ result
143
+ end
144
+
145
+ def get_hint # TODO!
146
+ if @hints > 0 && @state == :started
147
+ @hints -= 1
148
+ code.sample
149
+ else
150
+ 'No hints left!'
151
+ end
152
+ end
153
+
154
+ def set_difficulty(level)
155
+ @difficulty = level
156
+ @attempts = DIFFICULTIES[level.to_sym][:attempts]
157
+ @hints = DIFFICULTIES[level.to_sym][:hints]
158
+ end
159
+
160
+ private
161
+
162
+ attr_writer :code, :state, :difficulty
163
+
164
+ def reset_params
165
+ @username, @difficulty = nil
166
+ end
167
+ end
168
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebreaker_smn
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
  - Someone443
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-01 00:00:00.000000000 Z
11
+ date: 2019-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler