ep-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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1197175cc02d8d728d46cfc042eec332b3dbb248
4
+ data.tar.gz: 6f2d93756c2c3c152f410bcfe1aa3f33c3c04149
5
+ SHA512:
6
+ metadata.gz: 51b4a0e05ab2e9da827a34e96e5fb43386062e59f6283b074ef3cd3d2f92ae4660c05e5c05a2baf63f9154187baa50ac79cc0aeff5ea85927fcc092104318569
7
+ data.tar.gz: 6a078c77a7902e747fc52a9cd3145e9011775d4a1cf1d17bbd39d3ed24deecf7ab6487fd75f5aa842b098c9e5ed518042c1b41a20dcdb0890689f6ba203f8efd
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ codebreaker
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.4.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ep-codebreaker.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 TODO: Write your name
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
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'ep-codebreaker'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ep-codebreaker
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'bundler/setup' # if you use bundler
23
+ require 'ep-codebreaker'
24
+ require 'ep-codebreaker/console_app'
25
+
26
+ app = Codebreaker::ConsoleApp.new
27
+ app.run
28
+ ```
29
+
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/epoberezhny/Codebreaker.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
data/Rakefile ADDED
@@ -0,0 +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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'ep-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(__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
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ dependencies:
2
+ pre:
3
+ - gem install bundler
@@ -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 'ep-codebreaker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ep-codebreaker"
8
+ spec.version = Codebreaker::VERSION
9
+ spec.authors = ['Eugene Poberezhny']
10
+ spec.email = ['evgeny345@gmail.com']
11
+
12
+ spec.summary = %q{Codebreaker game}
13
+ spec.description = ''
14
+ spec.homepage = 'https://github.com/epoberezhny/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'] = "TODO: Set to 'http://mygemserver.com'"
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.14"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'messages'
4
+
5
+ module Codebreaker
6
+ class ConsoleApp
7
+ def initialize
8
+ @game = Game.new
9
+ end
10
+
11
+ def run
12
+ Messages.logo
13
+ Messages.welcome
14
+
15
+ loop do
16
+ Messages.run
17
+ print '> '
18
+
19
+ case gets.chomp
20
+ when '1' then play
21
+ when '2' then puts high_scores
22
+ when '0'
23
+ Messages.bye
24
+ break
25
+ else Messages.wrong_option
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def play
33
+ @game.start
34
+ Messages.play
35
+
36
+ until @game.finished?
37
+ print '>>> '
38
+ input = gets.chomp
39
+
40
+ case input
41
+ when '1' then puts @game.use_hint, "\n"
42
+ when '0' then return
43
+ else check_guess(input)
44
+ end
45
+ end
46
+
47
+ if @game.won?
48
+ Messages.win(@game.answer, @game.tries)
49
+ save_score
50
+ else
51
+ Messages.lose(@game.answer)
52
+ end
53
+ end
54
+
55
+ def high_scores
56
+ scores = @game.high_scores
57
+
58
+ return 'No scores' if scores.empty?
59
+
60
+ scores.map
61
+ .with_index { |player, i| format("%2i #{player.formatted}", i + 1) }
62
+ .unshift(format('%2s %10s %5s', '', 'Name', 'Tries'))
63
+ end
64
+
65
+ def check_guess(input)
66
+ puts @game.check_guess(input)
67
+ Messages.tries_left(@game.tries_left)
68
+ rescue ArgumentError => ex
69
+ puts ex.message, "\n"
70
+ end
71
+
72
+ def save_score
73
+ print 'Do you want to save your score? y/[n]: '
74
+
75
+ return unless gets.chomp.casecmp('y').zero?
76
+
77
+ begin
78
+ print 'Type your name: '
79
+ @game.save_score(gets.chomp)
80
+ puts 'Your score was saved'
81
+ rescue ArgumentError => ex
82
+ puts ex.message, "\n"
83
+ retry
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Game
5
+ attr_reader :tries_left
6
+
7
+ def start
8
+ @code = Array.new(LENGTH) { rand(MIN..MAX) }.join
9
+ @hint = @code.chars.sample
10
+ @tries_left = TRIES
11
+ @finished = false
12
+ @won = false
13
+ end
14
+
15
+ def check_guess(input)
16
+ verify input
17
+
18
+ @tries_left -= 1
19
+
20
+ result = check_input(@code.chars, input.chars)
21
+ define_stage result
22
+ result
23
+ end
24
+
25
+ def use_hint
26
+ @hint
27
+ end
28
+
29
+ def finished?
30
+ @finished
31
+ end
32
+
33
+ def won?
34
+ @won
35
+ end
36
+
37
+ def tries
38
+ TRIES - @tries_left
39
+ end
40
+
41
+ def answer
42
+ @code if finished?
43
+ end
44
+
45
+ def save_score(name, file_name = 'scores.yml')
46
+ player = Player.new(name, tries)
47
+
48
+ scores = process_file(file_name)
49
+ scores << player
50
+ scores = scores.min_by(10, &:tries)
51
+
52
+ File.open(file_name, 'w') { |f| f.write scores.to_yaml }
53
+ end
54
+
55
+ def high_scores(file_name = 'scores.yml')
56
+ process_file file_name
57
+ end
58
+
59
+ private
60
+
61
+ def check_input(code_chars, input_chars)
62
+ num_of_pluses = LENGTH.times.count { |i| code_chars[i] == input_chars[i] }
63
+
64
+ input_chars.each do |char|
65
+ next unless code_chars.include? char
66
+ code_chars.delete_at(code_chars.index(char))
67
+ end
68
+
69
+ num_of_minuses = LENGTH - num_of_pluses - code_chars.size
70
+
71
+ ('+' * num_of_pluses) + ('-' * num_of_minuses)
72
+ end
73
+
74
+ def define_stage(result)
75
+ if result == ('+' * LENGTH)
76
+ @finished = true
77
+ @won = true
78
+ elsif @tries_left.zero?
79
+ @finished = true
80
+ end
81
+ end
82
+
83
+ def process_file(file_name)
84
+ return [] if !File.exist?(file_name) || File.zero?(file_name)
85
+ YAML.load_file(file_name)
86
+ end
87
+
88
+ def verify(input)
89
+ msg = "Guesses must consist of #{LENGTH} digits from #{MIN} to #{MAX}"
90
+ raise(ArgumentError, msg) if !input.is_a?(String) || !input.match?(REGEXP)
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ module Messages
5
+ class << self
6
+ def logo
7
+ puts " ]]]] ]]]] ]]]]] ]]]]] \n" \
8
+ "]] ]] ]] ]] ]] ]] ]] \n" \
9
+ "]] ]] ]] ]] ]] ]]]] \n" \
10
+ "]] ]] ]] ]] ]] ]] ]] \n" \
11
+ " ]]]] ]]]] ]]]]] ]]]]] \n" \
12
+ "\n" \
13
+ "]]]]] ]]]]] ]]]]] ]]]] ]] ]] ]]]]] ]]]]] \n" \
14
+ "]] ]] ]] ]] ]] ]] ]] ]] ]] ]] ]] ]] \n" \
15
+ "]]]]] ]]]]] ]]]] ]]]]]] ]]]] ]]]] ]]]]] \n" \
16
+ "]] ]] ]] ]] ]] ]] ]] ]] ]] ]] ]] ]] \n" \
17
+ "]]]]] ]] ]] ]]]]] ]] ]] ]] ]] ]]]]] ]] ]] \n" \
18
+ "\n"
19
+ end
20
+
21
+ def welcome
22
+ puts 'Welcome to the Codebreaker game! Please choose one of the next options:'
23
+ end
24
+
25
+ def run
26
+ puts "\n" \
27
+ "1 - play \n" \
28
+ "2 - high scores \n" \
29
+ "0 - exit \n" \
30
+ "\n"
31
+ end
32
+
33
+ def play
34
+ puts "Let's go! \n" \
35
+ "\n" \
36
+ "1 - hint \n" \
37
+ "0 - end \n" \
38
+ "\n"
39
+ end
40
+
41
+ def win(answer, tries)
42
+ puts 'Congratulations! You are the winner!'
43
+ puts "It was #{answer} and you did it in #{tries} tries", "\n"
44
+ end
45
+
46
+ def lose(answer)
47
+ puts 'Sorry, you lose(((', "It was #{answer}"
48
+ end
49
+
50
+ def tries_left(tries_left)
51
+ puts "#{tries_left} tries left", "\n"
52
+ end
53
+
54
+ def bye
55
+ puts 'Good luck!'
56
+ end
57
+
58
+ def wrong_option
59
+ puts 'Wrong option'
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Player
5
+ attr_reader :name, :tries
6
+
7
+ def initialize(name, tries)
8
+ self.name = name
9
+ @tries = tries
10
+ end
11
+
12
+ def formatted
13
+ format('%10s %5i', @name, @tries)
14
+ end
15
+
16
+ def as_json
17
+ { name: @name, tries: @tries }
18
+ end
19
+
20
+ def to_json(*options)
21
+ as_json.to_json(*options)
22
+ end
23
+
24
+ private
25
+
26
+ def name=(value)
27
+ msg = 'The name must be between 1 and 10 characters long'
28
+ raise(ArgumentError, msg) unless (1...10).cover? value.length
29
+ @name = value
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Codebreaker
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+ require 'ep-codebreaker/version'
5
+ require 'ep-codebreaker/player'
6
+ require 'ep-codebreaker/game'
7
+
8
+ module Codebreaker
9
+ MIN = 1
10
+ MAX = 6
11
+ LENGTH = 4
12
+
13
+ REGEXP = Regexp.new("^[#{MIN}-#{MAX}]{#{LENGTH}}$")
14
+
15
+ TRIES = 10
16
+
17
+ private_constant :MIN, :MAX, :LENGTH, :REGEXP, :TRIES
18
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ep-codebreaker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Eugene Poberezhny
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-12 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: ''
56
+ email:
57
+ - evgeny345@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".ruby-gemset"
65
+ - ".ruby-version"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - circle.yml
74
+ - ep-codebreaker.gemspec
75
+ - lib/ep-codebreaker.rb
76
+ - lib/ep-codebreaker/console_app.rb
77
+ - lib/ep-codebreaker/game.rb
78
+ - lib/ep-codebreaker/messages.rb
79
+ - lib/ep-codebreaker/player.rb
80
+ - lib/ep-codebreaker/version.rb
81
+ homepage: https://github.com/epoberezhny/Codebreaker
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.6.11
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Codebreaker game
105
+ test_files: []