lkr-codebreaker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a948948ff2bb60da3871d149614d5ed7e60e875281ba17fedac58bab146f14ed
4
+ data.tar.gz: 10540077c5b0145af825cf81990f385a4b588516c48272f0a163cdb47adcb34d
5
+ SHA512:
6
+ metadata.gz: 55a1f2d7ebfee728186cb1c89540530e7cb198fca596165e9f1b7e260b3d7f575e305584fd90acda02a76319b6eb78c454c10b3fa7ed06cac0a5bbcf9f265752
7
+ data.tar.gz: e8ef3c40043834d71df01549dac7de1531ae7ccf25ae74f15b531e49457cb928a9566386758a8a3931b123db9b5d896e42d7636d69ac639240954b030bbfa152
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in codebreaker.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lkr-codebreaker (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.1)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ lkr-codebreaker!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.16.2
data/README.md ADDED
@@ -0,0 +1,44 @@
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
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'codebreaker'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install codebreaker
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'codebreaker'
25
+ ui = Codebreaker::UserInterface.new
26
+ ui.main_menu
27
+ ```
28
+ ## Development
29
+
30
+ 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.
31
+
32
+ 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).
33
+
34
+ ## Contributing
35
+
36
+ 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](http://contributor-covenant.org) code of conduct.
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the Codebreaker project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/codebreaker/blob/master/CODE_OF_CONDUCT.md).
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(__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
@@ -0,0 +1,36 @@
1
+
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 = "lkr-codebreaker"
8
+ spec.version = Codebreaker::VERSION
9
+ spec.authors = ["Sergey Kozhemyako"]
10
+ spec.email = ["therealloker@gmail.com"]
11
+
12
+ spec.summary = %q{Codebreaker game}
13
+ spec.license = "MIT"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ # if spec.respond_to?(:metadata)
18
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
19
+ # else
20
+ # raise "RubyGems 2.0 or newer is required to protect against " \
21
+ # "public gem pushes."
22
+ # end
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| 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.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
@@ -0,0 +1,67 @@
1
+ module Codebreaker
2
+ class Game
3
+ ATTEMPTS = 10
4
+ HINTS = 4
5
+
6
+ attr_reader :used_attempts, :used_hints
7
+
8
+ def initialize
9
+ @secret_code = generate
10
+ @shuffled_secret_code = @secret_code.shuffle
11
+ @used_attempts = 0
12
+ @used_hints = 0
13
+ end
14
+
15
+ def hint
16
+ @used_hints += 1
17
+ @shuffled_secret_code.pop
18
+ end
19
+
20
+ def make_guess(user_code)
21
+ return unless code_valid?(user_code)
22
+ @used_attempts += 1
23
+ @user_code = user_code.chars.map(&:to_i)
24
+ return '++++' if @user_code == @secret_code
25
+ exact_matches + number_matches
26
+ end
27
+
28
+ def save_result(username:, game_status:, file_name: 'player_results.txt')
29
+ File.open(file_name, 'a') do |file|
30
+ file.puts("Name: #{username}")
31
+ file.puts("Game status: #{game_status}")
32
+ file.puts("Attempts used: #{@used_attempts}")
33
+ file.puts("Hints used: #{@used_hints}")
34
+ file.puts('*' * 40)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def generate
41
+ Array.new(4) { rand(1..6) }
42
+ end
43
+
44
+ def code_valid?(user_code)
45
+ user_code.match(/^[1-6]{4}$/)
46
+ end
47
+
48
+ def exact_matches
49
+ @zipped_codes = @secret_code.zip(@user_code).delete_if { |el| el[0] == el[1] }
50
+ '+' * (@secret_code.length - @zipped_codes.length)
51
+ end
52
+
53
+ def number_matches
54
+ transposed = @zipped_codes.transpose
55
+ secret_array = transposed[0]
56
+ user_array = transposed[1]
57
+
58
+ user_array.each do |el|
59
+ if secret_array.include?(el)
60
+ index = secret_array.index(el)
61
+ secret_array.delete_at(index)
62
+ end
63
+ end
64
+ '-' * (@zipped_codes.length - secret_array.length)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,110 @@
1
+ require_relative 'game'
2
+ require_relative 'user_interface_helper'
3
+
4
+ module Codebreaker
5
+ class UserInterface
6
+ include UiHelper
7
+
8
+ def main_menu
9
+ greeting_message
10
+ gets.chomp == 'exit' ? bye : play
11
+ end
12
+
13
+ def play
14
+ create_new_game
15
+ start_game_message
16
+ playing
17
+ lost unless attempts?
18
+ score
19
+ after_game_menu
20
+ end
21
+
22
+ private
23
+
24
+ def create_new_game
25
+ @game = Game.new
26
+ end
27
+
28
+ def playing
29
+ while attempts?
30
+ used_attempts_message
31
+ input = gets.chomp
32
+ if call_hint?(input)
33
+ show_hint
34
+ else
35
+ result = @game.make_guess(input)
36
+ result ? puts(result) : incorrect_format_message
37
+ end
38
+ if won?(result)
39
+ won
40
+ break
41
+ end
42
+ end
43
+ end
44
+
45
+ def attempts?
46
+ @game.used_attempts < Game::ATTEMPTS
47
+ end
48
+
49
+ def hints?
50
+ @game.used_hints < Game::HINTS
51
+ end
52
+
53
+ def call_hint?(input)
54
+ input.match(/^h$/)
55
+ end
56
+
57
+ def show_hint
58
+ hints? ? puts(@game.hint) : no_hints_message
59
+ end
60
+
61
+ def show_result(result)
62
+ puts result
63
+ end
64
+
65
+ def won?(result)
66
+ result == '++++'
67
+ end
68
+
69
+ def won
70
+ @game_status = 'won'
71
+ won_message
72
+ end
73
+
74
+ def lost
75
+ @game_status = 'lost'
76
+ lost_message
77
+ end
78
+
79
+ def score
80
+ puts "Attempts used: #{@game.used_attempts}"
81
+ puts "Hints used: #{@game.used_hints}"
82
+ end
83
+
84
+ def after_game_menu
85
+ after_game_message
86
+ choise = gets.chomp[/^[yns]/]
87
+
88
+ if choise == 'y' then play end
89
+ if choise == 'n' then bye end
90
+ if choise == 's' then save end
91
+ main_menu
92
+ end
93
+
94
+ def save
95
+ name = ask_name
96
+ @game.save_result(username: name, game_status: @game_status)
97
+ saved_result_message
98
+ end
99
+
100
+ def ask_name
101
+ puts 'Please, type your name:'
102
+ gets.chomp
103
+ end
104
+
105
+ def bye
106
+ puts 'Bye!'
107
+ exit
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,41 @@
1
+ require_relative 'game'
2
+
3
+ module UiHelper
4
+ def greeting_message
5
+ puts '***Welcome to the Codebreaker game!***'
6
+ puts "Enter any characters to start playing or 'exit' for exit"
7
+ end
8
+
9
+ def start_game_message
10
+ puts "Please, enter your code to make guess or 'h' to get a hint"
11
+ puts "You have #{Codebreaker::Game::ATTEMPTS} attempts and #{Codebreaker::Game::HINTS} hints"
12
+ end
13
+
14
+ def used_attempts_message
15
+ puts "You used #{@game.used_attempts} attempts." if @game.used_attempts > 0
16
+ end
17
+
18
+ def no_hints_message
19
+ puts 'You used all of hints!'
20
+ end
21
+
22
+ def incorrect_format_message
23
+ puts 'Incoorect format! Please enter 4 digits from 1 to 6'
24
+ end
25
+
26
+ def won_message
27
+ puts '***Congratulations, you won!***'
28
+ end
29
+
30
+ def lost_message
31
+ puts 'You used all of attempts. You lost :('
32
+ end
33
+
34
+ def after_game_message
35
+ puts 'Do you want to play again(y/n) or save score(s)?'
36
+ end
37
+
38
+ def saved_result_message
39
+ puts 'Your result has been saved'
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module Codebreaker
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'codebreaker/version'
2
+ require 'codebreaker/game'
3
+ require 'codebreaker/user_interface'
4
+
5
+ module Codebreaker end
data/lib/main.rb ADDED
@@ -0,0 +1,4 @@
1
+ require_relative 'codebreaker/user_interface'
2
+
3
+ ui = Codebreaker::UserInterface.new
4
+ ui.main_menu
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lkr-codebreaker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Kozhemyako
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-23 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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
+ - therealloker@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - README.md
65
+ - Rakefile
66
+ - bin/console
67
+ - bin/setup
68
+ - codebreaker.gemspec
69
+ - lib/codebreaker.rb
70
+ - lib/codebreaker/game.rb
71
+ - lib/codebreaker/user_interface.rb
72
+ - lib/codebreaker/user_interface_helper.rb
73
+ - lib/codebreaker/version.rb
74
+ - lib/main.rb
75
+ homepage:
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.7.4
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Codebreaker game
99
+ test_files: []