sarcasm-codebreaker 0.2.5

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: 7ef5a8537ad6fd287b360bb26e28c7af5d4fd4dc
4
+ data.tar.gz: 3b94a7ab1d5966934aaa988408acc19b2bf97ab2
5
+ SHA512:
6
+ metadata.gz: 37812831ff3c29670b823d1a8de4862d6d27c5d5a9f0c6e5a5fb74acab32b71086ce6a37d423470d0536d94a226a22d29cb585cb1f1b9feb9dfc45eb1d633d50
7
+ data.tar.gz: 46231d5f986c311a227503301cfc48a0d97efe1e8988fda24af387595431eaf7cdd87aa6f63fd9d1c61421c6efa68abdfe56b24cb84d52f76e79b36e02ece7e0
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,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in codebreaker.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 SarCasm
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,41 @@
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. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
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 "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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
Binary file
@@ -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 = "sarcasm-codebreaker"
8
+ spec.version = Codebreaker::VERSION
9
+ spec.authors = ["SarCasm"]
10
+ spec.email = ["sarcasm008@gmail.com"]
11
+
12
+ spec.summary = %q{Gem for RubyGarage courses}
13
+ spec.description = %q{Codebreaker game. Play & have fun}
14
+ spec.homepage = "http://this.page"
15
+ spec.license = "MIT"
16
+
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables << 'codebreaker'
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ end
data/exe/codebreaker ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "codebreaker"
5
+ require_relative "../lib/codebreaker/console_interface.rb"
6
+ Codebreaker::ConsoleInterface.new.start
@@ -0,0 +1,106 @@
1
+ module Codebreaker
2
+ class ConsoleInterface
3
+
4
+ MAX_SYM_RANGE = 16
5
+
6
+ Welcome_text = 'Welcome to my codebreaker game!'
7
+ Game_start_text = "\nThe game starts right now!"
8
+ Diff_select_text = 'Select difficulty'
9
+ Same_diff_tex = '[Enter] to play same difficulty'
10
+ IndexErorr_text = 'Wrong number of arguments'
11
+ Guesses_took_0 = 'It took you'
12
+ Guesses_took_1 = 'guesses'
13
+ Ask_for_guess_text= "Your guess: "
14
+ Score_is_text = 'Your score is:'
15
+ The_answer_text = 'The answer was'
16
+ Win_text = 'You won!'
17
+ Lose_text = 'You lost, noob!'
18
+ Replay_text = 'Wanna replay? (y/n)'
19
+ Bye_text = 'Bye!'
20
+
21
+
22
+
23
+ def start
24
+ welcome
25
+ play
26
+ goodbye
27
+ end
28
+
29
+ def welcome
30
+ puts Welcome_text
31
+ @session = Game.new
32
+ end
33
+
34
+ def play
35
+ loop do
36
+ select_difficulty
37
+ difficulty_info
38
+ guessing
39
+ @played = true
40
+ results
41
+ ask_for_replay == 'y' ? next : break
42
+ end
43
+ end
44
+
45
+ def select_difficulty
46
+ puts "#{Diff_select_text} #{Game::GAME_SETTINGS.keys.inspect.delete(':')}"
47
+ puts Same_diff_tex if @played
48
+ diff = gets
49
+ puts Game_start_text
50
+ if @played && diff == "\n"
51
+ @session.restart
52
+ else
53
+ diff.chomp!
54
+ @session.start diff.to_sym
55
+ end
56
+ end
57
+
58
+ def difficulty_info
59
+ puts "
60
+ Symbol range: #{@session.symbols_range}
61
+ Symbols: #{@session.symbols_count}
62
+ Attempts: #{@session.attempts}
63
+ Hints: #{@session.hints_left}"
64
+ end
65
+
66
+ def guessing
67
+ loop do
68
+ print Ask_for_guess_text
69
+ guess = gets.chomp
70
+ if guess == 'hint'
71
+ p @session.hint.map {|x| x.is_a?(Fixnum) ? x.to_s(MAX_SYM_RANGE) : '*'}
72
+ else
73
+ begin
74
+ code = guess.split('').map {|x| x.to_i(MAX_SYM_RANGE) }
75
+ response = @session.guess code
76
+ puts "#{'+' * response[0] + '-' * response[1]}"
77
+ rescue IndexError
78
+ puts IndexErorr_text
79
+ next
80
+ end
81
+ end
82
+ break unless @session.state == :playing
83
+ end
84
+ end
85
+
86
+ def results
87
+ if @session.state == :won
88
+ puts Win_text
89
+ puts "#{Guesses_took_0} #{@session.attempts_taken} #{Guesses_took_1}"
90
+ puts "#{Score_is_text} #{@session.score}"
91
+ elsif @session.state == :lost
92
+ puts Lose_text
93
+ puts "#{The_answer_text} #{@session.secret}"
94
+ end
95
+ end
96
+
97
+ def ask_for_replay
98
+ puts Replay_text
99
+ answer = gets.chomp
100
+ end
101
+
102
+ def goodbye
103
+ puts Bye_text
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,126 @@
1
+ module Codebreaker
2
+ class Game
3
+ attr_reader(:max_attempts, :state, :score, :secret,
4
+ :attempts, :secret, :hints_left, :elements_revealed,
5
+ :symbols_count, :symbols_range)
6
+
7
+ GAME_SETTINGS = { easy: { attempts: 10, symbols_count: 3, symbols_range: 6, hints: 3 },
8
+ medium: { attempts: 10, symbols_count: 4, symbols_range: 6, hints: 1 },
9
+ hard: { attempts: 20, symbols_count: 6, symbols_range: 6, hints: 1 },
10
+ very_hard: { attempts: 25, symbols_count: 4, symbols_range: 15, hints: 1 },
11
+ gosu: { attempts: 35, symbols_count: 6, symbols_range: 15, hints: 0 },
12
+ mlg: { attempts: 1, symbols_count: 4, symbols_range: 6, hints: 0 } }
13
+
14
+ def initialize
15
+ @state = :initialized
16
+ end
17
+
18
+ def start(diff = :medium)
19
+ @settings = diff
20
+ settings = GAME_SETTINGS[diff]
21
+ @max_attempts = settings[:attempts]
22
+ @attempts = @max_attempts
23
+ @symbols_count = settings[:symbols_count]
24
+ @symbols_range = settings[:symbols_range]
25
+ @secret = generate_code
26
+ @hints_left = settings[:hints]
27
+ @elements_revealed = []
28
+ @state = :playing
29
+ end
30
+
31
+ def restart
32
+ start(@settings)
33
+ end
34
+
35
+ def guess(code)
36
+ fail IndexError if code.length != @secret.length
37
+
38
+ if code == @secret
39
+ win
40
+ else
41
+ @attempts -= 1
42
+ lose if @attempts.zero?
43
+ end
44
+ secret = @secret.dup
45
+ [extract_exact_matches(secret, code), extract_close_matches(secret, code)]
46
+ end
47
+
48
+ def win?
49
+ @state == :won
50
+ end
51
+ alias_method :won?, :win?
52
+
53
+ def lose?
54
+ @state == :lost
55
+ end
56
+ alias_method :lost?, :lose?
57
+
58
+ def win
59
+ @score = (10 - @attempts) * @symbols_count * @symbols_range + @hints_left*20
60
+ @state = :won
61
+ end
62
+
63
+ def lose
64
+ @score = 0
65
+ @state = :lost
66
+ end
67
+
68
+ def hint?
69
+ @hints_left > 0
70
+ end
71
+
72
+ def attempts_taken
73
+ @max_attempts - @attempts
74
+ end
75
+
76
+ def hint
77
+ if @hints_left.zero?
78
+ fail 'No more hints available'
79
+ else
80
+ random_symbol_index = 0
81
+ loop do
82
+ random_symbol_index = rand(@secret.size)
83
+ break unless @elements_revealed.include? random_symbol_index
84
+ end
85
+ @elements_revealed << random_symbol_index
86
+ @hints_left -= 1
87
+ hint = ([nil]*@secret.size)
88
+ hint[random_symbol_index] = @secret[random_symbol_index]
89
+ hint
90
+ end
91
+ end
92
+
93
+ private
94
+ def extract_exact_matches(secret, guess)
95
+ matches = 0
96
+ guess_dup = guess.dup
97
+ guess_dup.each_index do |i|
98
+ if guess_dup[i] == @secret[i]
99
+ matches += 1
100
+ guess[i] = nil
101
+ secret[i] = nil
102
+ end
103
+ end
104
+ secret.compact!
105
+ guess.compact!
106
+ matches
107
+ end
108
+
109
+ def extract_close_matches(secret, guess)
110
+ matches = 0
111
+ guess.each.with_index do |sym, i|
112
+ if secret.include? sym
113
+ matches +=1
114
+ secret[ secret.index(sym) ] = nil
115
+ end
116
+ end
117
+ matches
118
+ end
119
+
120
+ def generate_code
121
+ @symbols_count.times.with_object([]) do |n, code|
122
+ code << (1..@symbols_range).to_a.sample
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,3 @@
1
+ module Codebreaker
2
+ VERSION = "0.2.5"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "codebreaker/version"
2
+ require_relative 'codebreaker/game.rb'
3
+
4
+ module Codebreaker
5
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sarcasm-codebreaker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.5
5
+ platform: ruby
6
+ authors:
7
+ - SarCasm
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-26 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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 game. Play & have fun
56
+ email:
57
+ - sarcasm008@gmail.com
58
+ executables:
59
+ - codebreaker
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - codebreaker-0.2.4.gem
74
+ - codebreaker.gemspec
75
+ - exe/codebreaker
76
+ - lib/codebreaker.rb
77
+ - lib/codebreaker/console_interface.rb
78
+ - lib/codebreaker/game.rb
79
+ - lib/codebreaker/version.rb
80
+ homepage: http://this.page
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.8
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Gem for RubyGarage courses
104
+ test_files: []