codebreaker_artem 0.1.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: 9c88c65d0fa6ae4bdd41e3b1ce05f11137dce3b3
4
+ data.tar.gz: 1ad08458a52be8115bb38474fdef6c1e09cf5af9
5
+ SHA512:
6
+ metadata.gz: 42986204e19d0020009e04ac91a50038bb65470130c9742446126727f304897bb0d71134f86a1f76e4e1d550b41c9b2ea4e9b767fd90c73571df62ae96f006e7
7
+ data.tar.gz: 3e0737d766aa7aba1c1c219ed3ac9b15670c443838e3801ad3c75d74d7cf4873a8877245e1c4916aed82c606d2789b13255d3f3e57ae17721e0369ac75f75403
data/.gitignore ADDED
@@ -0,0 +1,15 @@
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
13
+
14
+ /score
15
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 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.15.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in codebreaker_artem.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # CodebreakerArtem
2
+
3
+ Codebreaker is a logic game in which a code-breaker tries to break a secret code created by a code-maker.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'codebreaker_artem'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install codebreaker_artem
20
+
21
+ ## Usage
22
+
23
+ ### To play a game type the following:
24
+ ```ruby
25
+ game = CodebreakerArtem::Game.new
26
+ game.play
27
+ ```
28
+
29
+ The code-maker, which will be played by the application, creates a secret code of four numbers between 1 and 6.
30
+
31
+ 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.
32
+
33
+ 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.
34
+
35
+ 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.
36
+
37
+ 1. Start game - When a new game was started, the game generates secret code. The code should have 4 items.
38
+
39
+ 2. Code-breaker submits guess - The code-breaker propose a guess, and the system replies by marking the guess according to the marking algorithm.
40
+
41
+ 3. Code-breaker wins game - The code-breaker propose a guess that matches the secret code exactly. The system responds by marking the guess with four + signs.
42
+
43
+ 4. Code-breaker loses game - After some number of turns, the game tells the code-breaker that the game is over (need to decide how many turns and whether to reveal the code).
44
+
45
+ 5. Code-breaker plays again - After the game is won or lost, the system prompts the code-breaker to play again. If the code-breaker indicates yes, a new game begins. If the code-breaker indicates no, the system shuts down.
46
+
47
+ 6. Code-breaker requests hint - At any time during a game, the code-breaker can request a hint, at which point the system reveals one of the numbers in the secret code.
48
+
49
+ 7. Code-breaker saves score - After the game is won or lost, the code-breaker can opt to save information about the game: who (initials?), score, and time.
50
+
51
+ ## Development
52
+
53
+ 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.
54
+
55
+ 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).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/codebreaker_artem.
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_artem"
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
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "codebreaker_artem/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "codebreaker_artem"
8
+ spec.version = CodebreakerArtem::VERSION
9
+ spec.authors = ["b-artem"]
10
+ spec.email = ["ba.artyom@gmail.com"]
11
+
12
+ spec.summary = %q{Codebreaker game.}
13
+ spec.description = %q{Codebreaker is a logic game in which a code-breaker" \
14
+ " tries to break a secret code created by a code-maker.}
15
+ spec.homepage = "https://github.com/b-artem/codebreaker_artem"
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.15"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
@@ -0,0 +1,169 @@
1
+ module CodebreakerArtem
2
+ class Game
3
+ MAX_GUESS_NUMBER = 10
4
+
5
+ def initialize
6
+ initial_values_set
7
+ end
8
+
9
+ def play
10
+ start
11
+ loop do
12
+ print "Please enter your guess no. #{@guess_count}: "
13
+ mark_guess(submit_guess)
14
+ return lose if @guess_count >= MAX_GUESS_NUMBER
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def start
21
+ initial_values_set
22
+ generate_secret_code
23
+ welcome_msg
24
+ end
25
+
26
+ def initial_values_set
27
+ @guess_count = 0
28
+ @score = 0
29
+ @numbers_guess_count = [0, 0, 0, 0]
30
+ @hint_available = true
31
+ end
32
+
33
+ def generate_secret_code
34
+ @secret_code = ''
35
+ 4.times { @secret_code << rand(1..6).to_s }
36
+ end
37
+
38
+ def welcome_msg
39
+ print "Welcome to the Codebreaker Game!\n" \
40
+ "New secret code has just been generated\n" \
41
+ "You have #{MAX_GUESS_NUMBER} attempts\n" \
42
+ "Type 'hint' to take a hint\n" \
43
+ "Type 'exit' to exit the game\n\n"
44
+ end
45
+
46
+ def submit_guess
47
+ guess = $stdin.gets.chomp
48
+ exit?(guess)
49
+ return false if take_hint(guess)
50
+ return wrong_code_length unless guess.length == 4
51
+ return wrong_code_numbers unless guess =~ /[1-6]{4}/
52
+ @guess_count += 1
53
+ guess
54
+ end
55
+
56
+ def mark_guess(guess)
57
+ return false unless guess
58
+ counts = plus_minus_count(guess)
59
+ score_set(counts[0], counts[1])
60
+ puts mark = '' << ('+' * counts[0]) << ('-' * counts[1])
61
+ return win if mark == '++++'
62
+ mark
63
+ end
64
+
65
+ def plus_minus_count(guess)
66
+ plus_count = 0
67
+ minus_count = 0
68
+ guess.each_char.with_index do |number, index|
69
+ if number == @secret_code[index]
70
+ plus_count += 1
71
+ @numbers_guess_count[index] += 1
72
+ elsif @secret_code.include? number
73
+ minus_count += 1
74
+ end
75
+ end
76
+ [plus_count, minus_count]
77
+ end
78
+
79
+ def score_set(plus_count, minus_count)
80
+ @score = (plus_count * 3) + (minus_count * 1) +
81
+ (MAX_GUESS_NUMBER - @guess_count + 1)
82
+ end
83
+
84
+ def win
85
+ puts "\nCONGRATULATIONS! You've won the Codebreaker game!"
86
+ finish_game
87
+ end
88
+
89
+ def lose
90
+ puts "You've used all your attempts (#{MAX_GUESS_NUMBER})" \
91
+ " and LOST THE GAME\n"
92
+ finish_game
93
+ end
94
+
95
+ def finish_game
96
+ puts "The secret code was #{@secret_code}\n"
97
+ save_score
98
+ play_again
99
+ end
100
+
101
+ def save_score
102
+ return false unless yes? { 'Do you want to save your score? (y/n): ' }
103
+ begin
104
+ Dir.mkdir('./score') unless File.exist?('./score')
105
+ file = File.new('./score/score.txt', 'a')
106
+ rescue
107
+ puts "Can't create file './score/score.txt'"
108
+ return
109
+ end
110
+ write_score_to_file file
111
+ file.close
112
+ end
113
+
114
+ def write_score_to_file(file)
115
+ print 'Please enter your name: '
116
+ name = $stdin.gets.chomp
117
+ file << "Name: #{name}\n"
118
+ file << "Time: #{Time.now}\n"
119
+ file << "Score: #{@score}\n\n"
120
+ puts "Your data was added to a file #{file.path}"
121
+ end
122
+
123
+ def take_hint(input = nil)
124
+ return false unless input =~ /hint/i
125
+ return false unless hint_available?
126
+ position = @numbers_guess_count.index(@numbers_guess_count.min)
127
+ secret_number = @secret_code[position]
128
+ puts "HINT: Number #{secret_number} is in position #{position + 1}"
129
+ @hint_available = false
130
+ secret_number
131
+ end
132
+
133
+ def hint_available?
134
+ return puts 'You have only one hint' unless @hint_available
135
+ true
136
+ end
137
+
138
+ def play_again
139
+ exit unless yes? { 'Would you like to play one more time? (y/n): ' }
140
+ start
141
+ end
142
+
143
+ def exit?(input)
144
+ exit if input =~ /exit/
145
+ false
146
+ end
147
+
148
+ def yes?
149
+ print yield if block_given?
150
+ loop do
151
+ answer = $stdin.gets.chomp
152
+ exit?(answer)
153
+ return false if answer =~ /^n/i
154
+ return true if answer =~ /^y/i
155
+ puts "Please enter 'y' or 'n': "
156
+ end
157
+ end
158
+
159
+ def wrong_code_length
160
+ puts 'Code contains 4 numbers, please try again'
161
+ false
162
+ end
163
+
164
+ def wrong_code_numbers
165
+ puts 'All numbers are between 1 and 6, please try again'
166
+ false
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,3 @@
1
+ module CodebreakerArtem
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "codebreaker_artem/version"
2
+ require 'codebreaker_artem/game'
3
+
4
+ module CodebreakerArtem
5
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codebreaker_artem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - b-artem
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-06 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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
+ Codebreaker is a logic game in which a code-breaker" \
57
+ " tries to break a secret code created by a code-maker.
58
+ email:
59
+ - ba.artyom@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".ruby-version"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - codebreaker_artem.gemspec
74
+ - lib/codebreaker_artem.rb
75
+ - lib/codebreaker_artem/game.rb
76
+ - lib/codebreaker_artem/version.rb
77
+ homepage: https://github.com/b-artem/codebreaker_artem
78
+ licenses: []
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.6.11
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Codebreaker game.
101
+ test_files: []