mastermind_game_cli 0.0.2

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: c0596676c4357fd92539bd9b355c2789a4f00fae
4
+ data.tar.gz: da21589c5a46d031fe0f5ca1ee6e37e8956d7ff6
5
+ SHA512:
6
+ metadata.gz: 5db6ea552ef262dad3e085a17400d45bb727940523fb8c0a9a050cf4b51c00e03a01a81798ea0f9d1084259a80369921ef966cade430f68cf9bbadf673942c01
7
+ data.tar.gz: 577ee827084aaaf0f0499b185ff60d73a3c5aa887aa5e2703542631453d80d2f1ec239e8d8b746df66841d0ec7f84f5c57c4b76d7b81b137a198a6d813575e7c
data/.gitignore ADDED
@@ -0,0 +1,53 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+
52
+ #rspec
53
+ .rspec
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mastermind_game_cli.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
7
+ gem 'colorize'
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mastermind_game_cli (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ colorize (0.8.1)
10
+ diff-lcs (1.2.5)
11
+ rake (10.5.0)
12
+ rspec (3.5.0)
13
+ rspec-core (~> 3.5.0)
14
+ rspec-expectations (~> 3.5.0)
15
+ rspec-mocks (~> 3.5.0)
16
+ rspec-core (3.5.4)
17
+ rspec-support (~> 3.5.0)
18
+ rspec-expectations (3.5.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.5.0)
21
+ rspec-mocks (3.5.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.5.0)
24
+ rspec-support (3.5.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.13)
31
+ colorize
32
+ mastermind_game_cli!
33
+ rake (~> 10.0)
34
+ rspec
35
+
36
+ BUNDLED WITH
37
+ 1.13.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Joseluis Laso
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,64 @@
1
+ # MastermindGameCli
2
+
3
+ ## Mastermind RUBY version for console
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/mastermind-game-cli.svg)](https://badge.fury.io/rb/mastermind-game-cli)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'mastermind_game_cli'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mastermind_game_cli
22
+
23
+
24
+ ## Usage
25
+
26
+ clone the project in your computer
27
+
28
+ ```
29
+ git clone https://github.com/jlaso/mastermind-game-cli
30
+ cd mastermind-game-cli
31
+ bundle install
32
+ ```
33
+
34
+ and you are able to run play
35
+
36
+ ```
37
+ ruby bin/game.rb
38
+ ```
39
+
40
+
41
+ or you can add to your project adding this line to your `Gemfile`
42
+
43
+ ```
44
+ gem 'mastermind-game-cli'
45
+ ```
46
+
47
+
48
+ More detailed info [here](https://rubygems.org/gems/mastermind-game-cli)
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ 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).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jlaso/mastermind-game-cli.
59
+
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
64
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/game.rb ADDED
@@ -0,0 +1,81 @@
1
+ require_relative '../lib/mastermind_game_cli/game'
2
+ require 'colorize'
3
+
4
+ puts 'Welcome to mastermind!'
5
+
6
+ num_pos = 0
7
+
8
+ until num_pos > 3 and num_pos < 10 do
9
+ puts 'How many numbers do you want to have the sequence?'
10
+ num_pos = gets.chomp.to_i
11
+ end
12
+
13
+ def help
14
+ puts ''
15
+ puts '================================'
16
+ puts '| This is the help you can get |'
17
+ puts '*==============================*'
18
+ puts '| /help => this help screen |'
19
+ puts '| /exit => abandon |'
20
+ puts '| /show => show the board again|'
21
+ puts '| /surrender => surrender |'
22
+ puts '| /mark n => appearances of n |'
23
+ puts '| /hint => ask for a hint |'
24
+ puts '================================'
25
+ puts ''
26
+ end
27
+
28
+ puts "Great, you selected #{num_pos}, hold on please!"
29
+
30
+ help
31
+
32
+ game = MastermindGameCli::Game.new(num_pos)
33
+
34
+ until game.finished? do
35
+ puts 'Try a guess! (^C to exit)'.blue.on_white
36
+ guess = gets.chomp
37
+
38
+ if guess[0] == '/'
39
+ command = guess.downcase[1..guess.length]
40
+ command = command.split(' ')
41
+ args = command[1..command.length]
42
+ command = command[0]
43
+ case command
44
+ when 'help'
45
+ help
46
+ when 'show'
47
+ game.pretty_print
48
+ when 'surrender'
49
+ puts "This is the sequence I thought '#{game.sequence.value}'"
50
+ Kernel.exit
51
+ when 'exit'
52
+ Kernel.exit
53
+ when 'hint'
54
+ begin
55
+ game.add_hint
56
+ game.pretty_print
57
+ rescue Exception => e
58
+ puts e.message
59
+ end
60
+ when 'mark'
61
+ if args[0].nil?
62
+ puts 'mark should have a number (syntax is /mark n)'
63
+ else
64
+ game.mark = args[0]
65
+ game.pretty_print
66
+ end
67
+ else
68
+ puts "I don't understand '#{command}'. (Hint: ask for /help)"
69
+ end
70
+ else
71
+ begin
72
+ game.guess(guess)
73
+ game.pretty_print
74
+
75
+ rescue Exception => e
76
+ puts "error> #{e.message}"
77
+ end
78
+ end
79
+ end
80
+
81
+ puts 'You win, congratulations!'
@@ -0,0 +1,46 @@
1
+ module MastermindGameCli
2
+
3
+ class Checker
4
+
5
+ def self.check(st1, st2)
6
+ ones = 0
7
+ zeros = 0
8
+ p1 = 0
9
+ st1.each_char do |i1|
10
+ p2 = 0
11
+ st2.each_char do |i2|
12
+ if i2 == i1
13
+ if p1 == p2
14
+ ones += 1
15
+ else
16
+ zeros += 1
17
+ end
18
+ end
19
+ p2 += 1
20
+ end
21
+ p1 +=1
22
+ end
23
+ '1' * ones + '0' * zeros
24
+ end
25
+
26
+ def self.get_matches(st1, st2)
27
+ result = []
28
+ for i in 0..st1.length - 1
29
+ if st1[i] == st2[i]
30
+ result << st2[i]
31
+ end
32
+ end
33
+ result
34
+ end
35
+
36
+ def self.only_digits?(st)
37
+ Float(st) != nil rescue false
38
+ end
39
+
40
+ def self.repeated?(st)
41
+ check(st, st) != '1' * st.length
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,94 @@
1
+ module MastermindGameCli
2
+
3
+ require_relative 'sequence'
4
+ require_relative 'checker'
5
+ require_relative 'guess'
6
+ require 'colorize'
7
+
8
+ class Game
9
+
10
+ attr_reader :sequence
11
+ attr_accessor :mark
12
+
13
+ def initialize(num_pos)
14
+ @mark = nil
15
+ @hints = []
16
+ @num_pos = num_pos
17
+ @finish = false
18
+ @guesses = []
19
+ @sequence = Sequence.new(num_pos)
20
+ end
21
+
22
+ def finished?
23
+ @finish
24
+ end
25
+
26
+ def guess(guess)
27
+ raise "Bad try, I expect a combination of #{@num_pos} digits" unless guess.length.equal? @num_pos
28
+ raise 'Bad try, I expect only digits' unless Checker.only_digits? guess
29
+ raise 'Bad try, I expect a combination without repetitions' if Checker.repeated? guess
30
+ raise 'Bad try, you already tried that one' if @guesses.include? guess
31
+ result = Checker.check(@sequence.value, guess)
32
+ @guesses << Guess.new(guess, result)
33
+ @finish = (result == '1' * @num_pos)
34
+ end
35
+
36
+ def pretty_print
37
+ printf "| %02s | %-#{@num_pos}s | %-#{@num_pos}s |\r\n", '#', 'try', 'res'
38
+ printf "|%04s|%-#{@num_pos+2}s|%-#{@num_pos+2}s|\r\n", '-' * 4, '-'*(@num_pos+2), '-'*(@num_pos+2)
39
+ line = 1
40
+ @guesses.each do |guess|
41
+ printf "| %02d | %-#{@num_pos}s | %-#{@num_pos}s |\r\n", line, pretty_print_string(guess.value, @mark), guess.response
42
+ line += 1
43
+ end
44
+ printf "|%04s|%-#{@num_pos+2}s|%-#{@num_pos+2}s|\r\n", '-' * 4, '-'*(@num_pos+2), '-'*(@num_pos+2)
45
+ end
46
+
47
+ def add_hint
48
+ hint = new_hint
49
+ raise 'not enough info to give a hint' if hint.nil?
50
+ @hints << hint
51
+ end
52
+
53
+ private
54
+ def new_hint
55
+ raise 'Hints are limited to 2' if @hints.length >= 2
56
+ candidates = []
57
+ @guesses.each do |guess|
58
+ matches = Checker.get_matches(@sequence.value, guess.value)
59
+ matches.each do |match|
60
+ candidates << match unless candidates.include? match
61
+ end
62
+ end
63
+ @hints.each do |hint|
64
+ candidates.delete(hint)
65
+ end
66
+ candidates.length > 0 ? candidates[0] : nil
67
+ end
68
+
69
+ private
70
+ def pretty_print_string(st, nb)
71
+ result = ''
72
+ nb = nb.to_i unless nb.nil?
73
+ (0..st.length-1).each do |i|
74
+ ch = st[i]
75
+ if @hints.include? ch
76
+ if @sequence.value[i].to_i.equal? ch.to_i
77
+ result += ch.red
78
+ else
79
+ result += ch.yellow
80
+ end
81
+ else
82
+ if ch.to_i.equal? nb
83
+ result += ch.underline
84
+ else
85
+ result += ch
86
+ end
87
+ end
88
+ end
89
+ result
90
+ end
91
+
92
+ end
93
+
94
+ end
@@ -0,0 +1,15 @@
1
+ module MastermindGameCli
2
+
3
+ class Guess
4
+
5
+ attr_reader :value
6
+ attr_reader :response
7
+
8
+ def initialize(value, response)
9
+ @value = value
10
+ @response = response
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,13 @@
1
+ module MastermindGameCli
2
+ DICTIONARY = '0123456789'
3
+
4
+ class Sequence
5
+
6
+ attr_reader :value
7
+
8
+ def initialize(num_pos)
9
+ @value = DICTIONARY.split('').shuffle.join[0, num_pos]
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module MastermindGameCli
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,9 @@
1
+ module MastermindGameCli
2
+
3
+ require_relative 'mastermind_game_cli/version'
4
+ require_relative 'mastermind_game_cli/game'
5
+ require_relative 'mastermind_game_cli/sequence'
6
+ require_relative 'mastermind_game_cli/guess'
7
+ require_relative 'mastermind_game_cli/checker'
8
+
9
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mastermind_game_cli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mastermind_game_cli'
8
+ spec.version = MastermindGameCli::VERSION
9
+ spec.authors = ['Joseluis Laso']
10
+ spec.email = ['jlaso@joseluislaso.es']
11
+
12
+ spec.summary = 'An old (but funny) game to play on console'
13
+ spec.description = 'A numeric version of the well known mastermind to play on console'
14
+ spec.homepage = 'https://rubygems.org/gems/mastermind-game-cli'
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_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "colorize", "~> 10.0"
27
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mastermind_game_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Joseluis Laso
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-27 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: colorize
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
+ description: A numeric version of the well known mastermind to play on console
56
+ email:
57
+ - jlaso@joseluislaso.es
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/game.rb
69
+ - lib/mastermind_game_cli.rb
70
+ - lib/mastermind_game_cli/checker.rb
71
+ - lib/mastermind_game_cli/game.rb
72
+ - lib/mastermind_game_cli/guess.rb
73
+ - lib/mastermind_game_cli/sequence.rb
74
+ - lib/mastermind_game_cli/version.rb
75
+ - mastermind_game_cli.gemspec
76
+ homepage: https://rubygems.org/gems/mastermind-game-cli
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.8
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: An old (but funny) game to play on console
100
+ test_files: []