codebreaker_smn 0.1.0
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +43 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/codebreaker_smn.gemspec +44 -0
- data/lib/codebreaker_smn/helpers/errors/validation_error.rb +5 -0
- data/lib/codebreaker_smn/helpers/validation_helper.rb +37 -0
- data/lib/codebreaker_smn/version.rb +3 -0
- data/lib/codebreaker_smn.rb +145 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6af57218aba00ae00628d80a3311829ac1509188e0e75ea45ec63e1f480179b9
|
4
|
+
data.tar.gz: a43b99fad4edeb401671e6c679ad8278af02ed2c21db1f19883b3756a2d626ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e740c30d89f44a7b4e2013796e332d3d9a966b2235a45df6c197ef173e4173b9bbe6f2ace15348a64f8dbce7534f5f889f5c413b8f52a664c7da5f22f3f9ab57
|
7
|
+
data.tar.gz: 2e329a83adf789d7b686d2346a45324b4c6500191018cc01e7e4ac2bd838b2a6fe0eb0248bb4d918c5bc55ec89aab6c5a6889f0fc440cb7eab3ee592c89205c2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +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
|
data/LICENSE.txt
ADDED
@@ -0,0 +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.
|
data/README.md
ADDED
@@ -0,0 +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).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +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__)
|
data/bin/setup
ADDED
@@ -0,0 +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
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Validate parameters
|
4
|
+
module ValidationHelper
|
5
|
+
|
6
|
+
def valid_name?(username)
|
7
|
+
not_empty_string(username) && valid_username_length(username)
|
8
|
+
end
|
9
|
+
|
10
|
+
def validate_guess(input)
|
11
|
+
|
12
|
+
input.split("").each { |char| positive_integer(char.to_i) }
|
13
|
+
|
14
|
+
valid_guess_length(input)
|
15
|
+
valid_digits(input)
|
16
|
+
end
|
17
|
+
|
18
|
+
def not_empty_string(input)
|
19
|
+
(input.is_a? String) && !(input.empty?)
|
20
|
+
end
|
21
|
+
|
22
|
+
def valid_username_length(username)
|
23
|
+
username.size.between?(3, 20)
|
24
|
+
end
|
25
|
+
|
26
|
+
def positive_integer(input)
|
27
|
+
raise ValidationError unless (input.is_a? Integer) && (input.positive?)
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid_guess_length(input)
|
31
|
+
raise ValidationError if !(input.size.eql?(4))
|
32
|
+
end
|
33
|
+
|
34
|
+
def valid_digits(input)
|
35
|
+
raise ValidationError if !(input.match(/[1-6]+/))
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,145 @@
|
|
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
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codebreaker_smn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Someone443
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-07-01 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.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.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: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.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.0'
|
55
|
+
description: Codebreaker is a logic game in which a code-breaker tries to break a
|
56
|
+
secret code created by a code-maker.
|
57
|
+
email:
|
58
|
+
- kostyan.12@ukr.net
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- codebreaker_smn.gemspec
|
74
|
+
- lib/codebreaker_smn.rb
|
75
|
+
- lib/codebreaker_smn/helpers/errors/validation_error.rb
|
76
|
+
- lib/codebreaker_smn/helpers/validation_helper.rb
|
77
|
+
- lib/codebreaker_smn/version.rb
|
78
|
+
homepage: https://github.com/Someone443/codebreaker
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata:
|
82
|
+
allowed_push_host: https://rubygems.org/
|
83
|
+
homepage_uri: https://github.com/Someone443/codebreaker
|
84
|
+
source_code_uri: https://github.com/Someone443/codebreaker
|
85
|
+
changelog_uri: https://github.com/Someone443/codebreaker
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - "~>"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '2.5'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.7.6
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: Codebreaker game
|
106
|
+
test_files: []
|