codebreaker_z 1.0.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 +10 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/codebreaker.gemspec +26 -0
- data/lib/codebreaker/console.rb +43 -0
- data/lib/codebreaker/game.rb +56 -0
- data/lib/codebreaker/version.rb +3 -0
- data/lib/codebreaker.rb +4 -0
- data/storage/.gitignore +1 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d620ad95bdf9e675803c8f06360d09fd62247bb3
|
4
|
+
data.tar.gz: 9a1d680fba7671a3cdfce80cc22a9f96b4cc47b3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 40f1be53b6cb015e52c362d67aa10167a3c221f11e2733666525e7b1694a680ad5dc0523c0e1bf7f51707e8ed6dff7d967b5e09fb491e9fdf9dabaff85d065d9
|
7
|
+
data.tar.gz: ef52c32f7f40c69ff4023631b139197a3835f17703bfaba7d0897b079ecf206fb3d66a36964f2005cb455ccdc9d98dc33b2ef88b68febd4498b4a1e4cbb45ff5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
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. 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
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "codebreaker"
|
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
|
data/bin/setup
ADDED
data/codebreaker.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'codebreaker/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "codebreaker_z"
|
8
|
+
spec.version = Codebreaker::VERSION
|
9
|
+
spec.authors = ["Oleksandr Zaitsev"]
|
10
|
+
spec.email = ["olexandr.zaitsev.92@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Write a short summary, because Rubygems requires one.}
|
13
|
+
spec.description = %q{Write a longer description or delete this line.}
|
14
|
+
spec.homepage = "https://github.com/oleksandrzaitsev92/codebreaker"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler"#, "~> 1.13"
|
24
|
+
spec.add_development_dependency "rake"#, "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Console
|
2
|
+
def start
|
3
|
+
catch :finish do
|
4
|
+
loop do
|
5
|
+
@game = Codebreaker::Game.new
|
6
|
+
round
|
7
|
+
save_game_result
|
8
|
+
try_again
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def round
|
14
|
+
until @game.ended?
|
15
|
+
print 'Your guess: '
|
16
|
+
input = gets.to_s.strip
|
17
|
+
input == 'hint' ? use_hint : guess(input)
|
18
|
+
end
|
19
|
+
puts "The round is over, you #{@game.winner? ? 'won' : 'loose'}!"
|
20
|
+
end
|
21
|
+
|
22
|
+
def guess(input)
|
23
|
+
puts @game.guess(input) || 'Incorrect input!'
|
24
|
+
end
|
25
|
+
|
26
|
+
def use_hint
|
27
|
+
puts @game.hint || 'All hints are used!'
|
28
|
+
end
|
29
|
+
|
30
|
+
def save_game_result
|
31
|
+
print 'Do you want to save result? (y/n): '
|
32
|
+
@game.save if confirmation
|
33
|
+
end
|
34
|
+
|
35
|
+
def try_again
|
36
|
+
print 'Do you want to try again? (y/n): '
|
37
|
+
throw :finish unless confirmation
|
38
|
+
end
|
39
|
+
|
40
|
+
def confirmation
|
41
|
+
gets.to_s.strip.casecmp('y').zero?
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Codebreaker
|
2
|
+
class Game
|
3
|
+
ATTEMPTS_COUNT = 10
|
4
|
+
HINTS_COUNT = 3
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@secret_code = generate_code
|
8
|
+
@attempt = 0
|
9
|
+
@hint = 0
|
10
|
+
@is_winner = false
|
11
|
+
end
|
12
|
+
|
13
|
+
def generate_code
|
14
|
+
@secret_code = Array.new(4){ rand(6) + 1 }.join
|
15
|
+
end
|
16
|
+
|
17
|
+
def guess(input)
|
18
|
+
return if input.length != 4
|
19
|
+
@attempt += 1
|
20
|
+
secret = @secret_code.chars.map(&:to_i)
|
21
|
+
result = []
|
22
|
+
input.chars.map(&:to_i).each_with_index do |digit, index|
|
23
|
+
if digit == secret[index]
|
24
|
+
secret[index] = nil
|
25
|
+
result << '+'
|
26
|
+
elsif secret.include? digit
|
27
|
+
secret[secret.find_index(digit)] = nil
|
28
|
+
result << '-'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
normalized_output = result.sort.join
|
32
|
+
@is_winner = true if normalized_output == '++++'
|
33
|
+
normalized_output
|
34
|
+
end
|
35
|
+
|
36
|
+
def hint
|
37
|
+
return unless HINTS_COUNT > @hint
|
38
|
+
@hint += 1
|
39
|
+
@secret_code[rand(4)]
|
40
|
+
end
|
41
|
+
|
42
|
+
def winner?
|
43
|
+
@is_winner
|
44
|
+
end
|
45
|
+
|
46
|
+
def ended?
|
47
|
+
ATTEMPTS_COUNT == @attempt || @is_winner
|
48
|
+
end
|
49
|
+
|
50
|
+
def save
|
51
|
+
YAML::Store.new('storage/storage.yml').transaction do |storage|
|
52
|
+
storage[Time.now] = "attempts taken: #{@attempt}; hints used: #{@hint}; result: #{(winner? ? 'won' : 'loose')}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/codebreaker.rb
ADDED
data/storage/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.yml
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codebreaker_z
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Oleksandr Zaitsev
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-20 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: '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
|
+
description: Write a longer description or delete this line.
|
56
|
+
email:
|
57
|
+
- olexandr.zaitsev.92@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- bin/console
|
67
|
+
- bin/setup
|
68
|
+
- codebreaker.gemspec
|
69
|
+
- lib/codebreaker.rb
|
70
|
+
- lib/codebreaker/console.rb
|
71
|
+
- lib/codebreaker/game.rb
|
72
|
+
- lib/codebreaker/version.rb
|
73
|
+
- storage/.gitignore
|
74
|
+
homepage: https://github.com/oleksandrzaitsev92/codebreaker
|
75
|
+
licenses: []
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.5.1
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Write a short summary, because Rubygems requires one.
|
97
|
+
test_files: []
|