codeguessing 0.2.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bb1b864076f30ae5faf38e18964a9876dfb9eb32
4
+ data.tar.gz: 38135af1470f0702b26bfe2453bdcfc54a63de15
5
+ SHA512:
6
+ metadata.gz: 239379048e30540ab24d6472e3ec95d79c892d0ca0b9d20106fa57565aeb649df123cc4c7bda2a4c95a81cde1bb9df40b806e0fd9b09e3d2ea3cd1f7e3e7c822
7
+ data.tar.gz: 4c47bbbaa9cd20ea012dec34bc86c859e1196898531d268d4d0d5b5638fb01ffac8a80dd99fc04291c070d71c4fdd57e3a58444ef7e174bf9c5b5d74a7fc8344
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at yaroslav555@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in codeguessing.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 bezrukavyi
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,29 @@
1
+ # Codeguessing
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/codeguessing`. 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 'codeguessing'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install codeguessing
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'codeguessing'
27
+ Codeguessing::Console.new # Console game
28
+ Codeguessing::Game.new # Game model
29
+ ```
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 "codeguessing"
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
@@ -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,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'codeguessing/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "codeguessing"
8
+ spec.version = Codeguessing::VERSION
9
+ spec.authors = ["bezrukavyi"]
10
+ spec.email = ["yaroslav555@gmail.com"]
11
+
12
+ spec.summary = %q{Codeguessing}
13
+ spec.description = %q{It's like Codebreaket, but codeguessing}
14
+ spec.homepage = "https://github.com/bezrukavyi/codeguessing"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "colorize"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.13"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
@@ -0,0 +1,83 @@
1
+ module Codeguessing
2
+ class Console
3
+ def initialize(again = false)
4
+ @path = File.join(File.dirname(__FILE__), 'scores.yml')
5
+ @data = load(@path)
6
+ @game = Game.new(@data)
7
+ return start if again
8
+ knowing
9
+ end
10
+
11
+ def knowing
12
+ puts "Are you ready broke code? (Y/N)"
13
+ if confirm?
14
+ puts '-----------------Rules!----------------------'.yellow
15
+ puts "You need guess secret code. This four-digit number with symbols from 1 to 6".yellow
16
+ puts "You have #{@game.attempts} attempt(s) and #{@game.hint_count} hint(s)".yellow
17
+ puts "If you want get hint write 'hint'".yellow
18
+ puts '---------------------------------------------'.yellow
19
+ return start
20
+ else
21
+ puts 'Goodbie!'
22
+ end
23
+ end
24
+
25
+ def start
26
+ puts "Attempt(s): #{@game.attempts} | Hint(s): #{@game.hint_count}"
27
+ return loose if @game.state == false
28
+ return win if @game.state == true
29
+
30
+ action = gets.chomp
31
+ if action == 'hint'
32
+ puts @game.hint
33
+ return start
34
+ end
35
+ unless @game.valid?(action)
36
+ puts 'Invalid data'.red
37
+ return start
38
+ end
39
+ puts coloring(@game.guess(action))
40
+ start
41
+ end
42
+
43
+ def win
44
+ puts 'You win!'.green
45
+ puts 'Do you want save result? (Y/N)'
46
+ return puts 'Goodbie!' unless confirm?
47
+ puts 'Write your name'
48
+ @game.save(@path, gets.chomp)
49
+ again?
50
+ end
51
+
52
+ def loose
53
+ puts 'You loose!'.red
54
+ again?
55
+ end
56
+
57
+ def again?
58
+ puts 'Do you want start again? (Y/N)'
59
+ if confirm?
60
+ Console.new(true)
61
+ else
62
+ puts '-----------Scores----------'.yellow
63
+ puts @game.scores
64
+ puts '---------------------------'.yellow
65
+ end
66
+ end
67
+
68
+ def confirm?(action = gets.chomp)
69
+ return true if action == 'Y'
70
+ false
71
+ end
72
+
73
+ def coloring(string)
74
+ color_s = ''
75
+ string.chars { |w| color_s += w == '+' ? '+'.green : '-'.red }
76
+ color_s
77
+ end
78
+
79
+ def load(path)
80
+ YAML.load(File.open(path)) if File.exist?(path)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,98 @@
1
+ module Codeguessing
2
+ class Game
3
+ attr_reader :result, :attempts, :hint_count, :state, :scores
4
+ attr_accessor :secret_code
5
+
6
+ MAX_HINT = 2
7
+ MAX_ATTEMPTS = 5
8
+
9
+ def initialize(data = [])
10
+ @secret_code = ''
11
+ 4.times { @secret_code += rand(1..6).to_s }
12
+ @attempts = MAX_ATTEMPTS
13
+ @hint_count = MAX_HINT
14
+ @state = ''
15
+ @scores = data || []
16
+ end
17
+
18
+ def guess(code)
19
+ loose unless check?(use_attempt)
20
+ res = ''
21
+ code.each_char.with_index do |char, i|
22
+ if char == secret_code[i]
23
+ res += '+'
24
+ else
25
+ res += '-'
26
+ end
27
+ end
28
+ win if res == '++++'
29
+ res
30
+ end
31
+
32
+ def hint
33
+ res = ''
34
+ need_index = rand(0...4)
35
+ secret_code.each_char.with_index do |char, index|
36
+ if index == need_index
37
+ res += char
38
+ else
39
+ res += '*'
40
+ end
41
+ end
42
+ return '' unless check?(hint_count)
43
+ use_hint
44
+ res
45
+ end
46
+
47
+ def save(path, name = 'Anonim')
48
+ return false if state != true
49
+ score = cur_score
50
+ score[:name] = name
51
+ @scores << score
52
+ File.new(path, 'w') unless File.exist?(path)
53
+ File.open(path, "r+") do |f|
54
+ f.write(@scores.to_yaml)
55
+ end
56
+ @scores
57
+ end
58
+
59
+ def cur_score
60
+ hash = {}
61
+ self.instance_variables.each do |k, v|
62
+ new_k = k.to_s.gsub('@','').to_sym
63
+ hash[new_k] = self.instance_variable_get(k)
64
+ end
65
+ hash.delete(:scores)
66
+ hash.delete(:result)
67
+ hash.delete(:state)
68
+ hash
69
+ end
70
+
71
+ def valid?(code)
72
+ return true if code =~ /^[1-6]{4}$/s
73
+ false
74
+ end
75
+
76
+ def check?(varible)
77
+ return false if varible == 0
78
+ true
79
+ end
80
+
81
+ def use_attempt
82
+ @attempts -= 1
83
+ end
84
+
85
+ def use_hint
86
+ @hint_count -= 1
87
+ end
88
+
89
+ def win
90
+ @state = true
91
+ end
92
+
93
+ def loose
94
+ @state = false
95
+ end
96
+
97
+ end
98
+ end
@@ -0,0 +1,21 @@
1
+ ---
2
+ - :secret_code: '2246'
3
+ :attempts: 5
4
+ :hint_count: 2
5
+ :name: Yaroslav
6
+ - :secret_code: '1121'
7
+ :attempts: 5
8
+ :hint_count: 2
9
+ :name: Yaroslav
10
+ - :secret_code: '2562'
11
+ :attempts: 5
12
+ :hint_count: 2
13
+ :name: Yaroslav
14
+ - :secret_code: '3445'
15
+ :attempts: 5
16
+ :hint_count: 2
17
+ :name: Yaroslav
18
+ - :secret_code: '2163'
19
+ :attempts: 0
20
+ :hint_count: 0
21
+ :name: Yaroslav
@@ -0,0 +1,3 @@
1
+ module Codeguessing
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'yaml'
2
+ require 'colorize'
3
+ require_relative "codeguessing/version"
4
+ require_relative "codeguessing/game"
5
+ require_relative "codeguessing/console"
6
+
7
+ module Codeguessing
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codeguessing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - bezrukavyi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: It's like Codebreaket, but codeguessing
70
+ email:
71
+ - yaroslav555@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - codeguessing.gemspec
87
+ - lib/codeguessing.rb
88
+ - lib/codeguessing/console.rb
89
+ - lib/codeguessing/game.rb
90
+ - lib/codeguessing/scores.yml
91
+ - lib/codeguessing/version.rb
92
+ homepage: https://github.com/bezrukavyi/codeguessing
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.6
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Codeguessing
116
+ test_files: []