vl_codebreaker 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec6c216ea326c2664b521c98e383c521177a34f9
4
+ data.tar.gz: 86949e2862b42035dccd66fd5f2b0beb4e2c750a
5
+ SHA512:
6
+ metadata.gz: 544d08f80be88e323b3fb3e5bdf393f3e3311f3037ed246577ee00783243adf21323c6c37d884eb411d2dd6d92ea9455a4886cb6c43a58f49289c1ed1f775f85
7
+ data.tar.gz: 044387daf4cb8819af72e9a419264cf83e40c37c229dfd7505c3401577bef3c8ac58793d5a9deaa58fab31849cab166dd368a2ecfc8603dc1f93cb7d8934de03
@@ -0,0 +1,10 @@
1
+ /.idea/
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in codebreaker.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 VladLysiuk
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.
@@ -0,0 +1,55 @@
1
+ # Codebreaker
2
+
3
+ _Codebreaker_ is a logic game in which a code-breaker tries to break a secret code created by a code-maker. The code-maker, which will be played by the application we’re going to write, creates a secret code of four numbers between 1 and 6.
4
+
5
+ The code-breaker then gets some number of chances to break the code. In each turn, the code-breaker makes a guess of four numbers. The code-maker then marks the guess with up to four `+` and `-` signs.
6
+
7
+ A `+` indicates an exact match: one of the numbers in the guess is the same as one of the numbers in the secret code and in the same position.
8
+
9
+ A `-` indicates a number match: one of the numbers in the guess is the same as one of the numbers in the secret code but in a different position.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'vl_codebreaker'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install vl_codebreaker
26
+
27
+ ## Usage
28
+
29
+ First, you need to require codebreaker in your script:
30
+
31
+ ```ruby
32
+ require 'codebreaker'
33
+ ```
34
+
35
+ After that, you can start the game and test gameplay:
36
+ ```ruby
37
+ console = Codebreaker::Console.new
38
+ console.play
39
+ ```
40
+
41
+ ## Development
42
+
43
+ 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.
44
+
45
+ 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).
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/VladLysiuk/Codebreaker.
50
+
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
55
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,15 @@
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
15
+ Codebreaker::Console.new.play
@@ -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,36 @@
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 = "vl_codebreaker"
8
+ spec.version = Codebreaker::VERSION
9
+ spec.authors = ["Vlad Lysiuk"]
10
+ spec.email = ["lysiukvlad@gmail.com"]
11
+
12
+ spec.summary = %q{Excellent exercise for mastering testing skills with RSpec.}
13
+ spec.description = %q{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/VladLysiuk/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
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.13"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec"
36
+ end
@@ -0,0 +1,3 @@
1
+ require "codebreaker/version"
2
+ require "codebreaker/game"
3
+ require "codebreaker/console"
@@ -0,0 +1,48 @@
1
+ module Codebreaker
2
+ class Console
3
+ attr_accessor :game
4
+
5
+ def initialize
6
+ @game = Game.new
7
+ end
8
+
9
+ def play
10
+ intro
11
+ until game.completed?
12
+ case answ = get_answer
13
+ when /^[1-6]{4}/ then game.mark_user_code(answ)
14
+ when /^[Hh]{1}/ then game.show_hint
15
+ else puts 'You entered wrong value, pal. Try again'
16
+ end
17
+ end
18
+ puts game.won? ? 'Great job! You guessed secret code!' : 'Oh, you exceed the number of available attempts'
19
+ save_result_request
20
+ play_again_request
21
+ end
22
+
23
+ private
24
+ def intro
25
+ puts 'Welcome to Codebreaker!'
26
+ puts 'You need to break randomly generated secret code'
27
+ puts 'You can input code or request hint(with H or h)'
28
+ puts 'So, lets go!'
29
+ end
30
+
31
+ def save_result_request
32
+ puts 'Do you wanna save result?'
33
+ game.save_result if get_answer =~ /[Yy]/
34
+ end
35
+
36
+ def play_again_request
37
+ puts 'Do you wanna play again?'
38
+ return unless get_answer =~ /[Yy]/
39
+ @game = Game.new
40
+ system('clear')
41
+ play
42
+ end
43
+
44
+ def get_answer
45
+ gets.chomp
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,80 @@
1
+ module Codebreaker
2
+ class Game
3
+ HINTS_COUNT, REMAINING_ATTEMPTS = 3, 7
4
+ attr_reader :user_code, :hints_count, :remaining_attempts
5
+
6
+ def initialize
7
+ @secret_code, @user_code = ' ', ' '
8
+ @hints_count, @remaining_attempts = HINTS_COUNT, REMAINING_ATTEMPTS
9
+ @started_at = Time.now
10
+ start
11
+ end
12
+
13
+ def won?
14
+ @user_code == '++++'
15
+ end
16
+
17
+ def lost?
18
+ @remaining_attempts == 0
19
+ end
20
+
21
+ def completed?
22
+ lost? || won?
23
+ end
24
+
25
+ def mark_user_code(guess)
26
+ @user_code = ''
27
+ guess = guess.chars
28
+ secret = @secret_code.chars
29
+
30
+ guess.each_with_index do |digit, index|
31
+ next unless digit == secret[index]
32
+ secret[index], guess[index] = nil
33
+ @user_code << '+'
34
+ end
35
+
36
+ [secret, guess].each(&:compact!)
37
+
38
+ secret.each do |digit|
39
+ next unless guess.include?(digit)
40
+ guess[guess.index(digit)] = nil
41
+ @user_code << '-'
42
+ end
43
+
44
+ puts "Your code is: #{@user_code}"
45
+ puts "Remaining attempts: #{@remaining_attempts -= 1}"
46
+ end
47
+
48
+ def show_hint
49
+ return puts 'Hints are no longer available' if hints_count == 0
50
+ return puts 'You guessed all digits. pal. Just think a little bit.' if user_code =~ /^[+-]{4}/
51
+
52
+ puts '----------------------HINT----------------------'
53
+ @secret_code.each_char.with_index(0) do |char, index|
54
+ break puts "Unknown digit is: #{char}" unless user_code[index] =~ /[+-]/
55
+ end
56
+ puts "Hints left: #{@hints_count -= 1}"
57
+ end
58
+
59
+ def save_result
60
+ time = playing_time
61
+
62
+ puts 'Input your name: '
63
+ player_name = gets.chomp
64
+
65
+ File.open('data.txt', 'a+') do |f|
66
+ f.write "Name: #{player_name} \n"
67
+ f.write "Playing time: #{time} sec \n\n"
68
+ end
69
+ end
70
+
71
+ private
72
+ def playing_time
73
+ (Time.now - @started_at).round
74
+ end
75
+
76
+ def start
77
+ @secret_code = Array.new(4) { rand(1..6) }.join
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module Codebreaker
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vl_codebreaker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Vlad Lysiuk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-08 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: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: '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: 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
+ - lysiukvlad@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - codebreaker.gemspec
72
+ - lib/codebreaker.rb
73
+ - lib/codebreaker/console.rb
74
+ - lib/codebreaker/game.rb
75
+ - lib/codebreaker/version.rb
76
+ homepage: https://github.com/VladLysiuk/Codebreaker
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ allowed_push_host: https://rubygems.org
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.5.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Excellent exercise for mastering testing skills with RSpec.
101
+ test_files: []