mastermind_cli 0.0.1

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: 9716e1fe50869ee4aa6920691aaad94f0a7eea3d
4
+ data.tar.gz: 3f0d544d03a6d089d9ad88c53dba963b15a070ec
5
+ SHA512:
6
+ metadata.gz: 11c16308c6414c9182c4fb942045121da904ae2d83d96494c3c5475be38f81aae0d01a718de31ed3599b4a2fa88f279dded1e9903f169d91e614a758ed75cb12
7
+ data.tar.gz: c2316594b65bb87c57b82b100335e0e4ad07618df5d7607909a5d918a932f992c54f02f6b81f775c1c765dd913f3b0fd40beca686aa6c8e60e5306d87ff06462
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mastermind_cli.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # MastermindCli
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/mastermind_cli`. 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 'mastermind_cli'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mastermind_cli
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. 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]/mastermind_cli.
36
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mastermind_cli"
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,4 @@
1
+ require 'mastermind_cli'
2
+
3
+ game = Mastermind.new
4
+ game.play
@@ -0,0 +1,72 @@
1
+ class Board
2
+
3
+ COLORS = %w(r b g y p o)
4
+ attr_reader :win_code
5
+ def initialize
6
+ @board = []
7
+ setup
8
+ create_code
9
+ end
10
+
11
+ def setup
12
+ 12.times do |row|
13
+ @board[row] = %w(_)*4, []
14
+ end
15
+ end
16
+
17
+ def create_code
18
+ @win_code = %w(r b g y)
19
+ end
20
+
21
+ def render
22
+ puts "~"*40
23
+ (1..12).each do |row|
24
+ puts "#{@board[-row][0]}#{@board[-row][1]}"
25
+ puts ""
26
+ end
27
+ puts "~"*40
28
+ end
29
+
30
+ def full?(turn)
31
+ turn > 11
32
+ end
33
+
34
+ def winning_combination?(turn)
35
+ row = turn - 1
36
+ @board[row][0] == @win_code
37
+ end
38
+
39
+ def add_move(move, row)
40
+ check_feedbacks(move)
41
+ @board[row] = move, @feedback
42
+ end
43
+
44
+ def red_feedbacks(move)
45
+ 4.times do |idx|
46
+ if move[idx] == @win_code[idx]
47
+ @hash[idx] = "R"
48
+ end
49
+ end
50
+ end
51
+
52
+
53
+ def white_feedbacks(move)
54
+ move.each do |color|
55
+ 4.times do |idx|
56
+ if color == @win_code[idx]
57
+ @hash[idx] = "W" unless @hash.has_key?(idx)
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ def check_feedbacks(move)
64
+ @feedback = []
65
+ @hash = {}
66
+ red_feedbacks(move)
67
+ white_feedbacks(move)
68
+ @hash.each do |key, val|
69
+ @feedback << val
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,55 @@
1
+ require './board.rb'
2
+ require './player.rb'
3
+
4
+ class Mastermind
5
+
6
+ def initialize
7
+ @turn = 0
8
+ @board = Board.new
9
+ @player = Player.new(@board)
10
+ end
11
+
12
+ def greetings
13
+ puts "="*40
14
+ puts "Welcome to CLI Mastermind".center(40)
15
+ puts "Made by FrenchToast".center(40)
16
+ puts "Available Colors [r b g y p o]".center(40)
17
+ puts "To make move, enter 4 colors".center(40)
18
+ puts "For example: rbyg".center(40)
19
+ puts "="*40
20
+ end
21
+
22
+ def play
23
+ greetings
24
+ until game_over?
25
+ @board.render
26
+ @player.make_move(@turn)
27
+ @turn +=1
28
+ end
29
+ end
30
+
31
+
32
+ def game_over?
33
+ victory || lose
34
+ end
35
+
36
+ def victory
37
+ if @board.winning_combination?(@turn)
38
+ @board.render
39
+ puts "Congratulations, you win!"
40
+ puts "Your code was: #{@board.win_code}"
41
+ return true
42
+ end
43
+ false
44
+ end
45
+
46
+ def lose
47
+ if @board.full?(@turn)
48
+ @board.render
49
+ puts "I'm Sorry, you lose."
50
+ puts "Winning combination was: #{@board.win_code}"
51
+ return true
52
+ end
53
+ false
54
+ end
55
+ end
@@ -0,0 +1,36 @@
1
+ class Player
2
+
3
+ def initialize(board)
4
+ @board = board
5
+ @move = nil
6
+ end
7
+
8
+
9
+ def make_move(turn)
10
+ row = turn
11
+ get_input
12
+ @board.add_move(@move, row)
13
+ end
14
+
15
+
16
+ def get_input
17
+ puts "Enter move: "
18
+ arr = gets.chomp.split('')
19
+ if input_valid?(arr)
20
+ @move = arr
21
+ else
22
+ get_input
23
+ end
24
+ end
25
+
26
+
27
+ def input_valid?(arr)
28
+ if arr.length != 4
29
+ puts "Please input four letters"
30
+ return false
31
+ else
32
+ arr.all? {|chr| @board.class::COLORS.include?(chr)}
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,3 @@
1
+ module MastermindCli
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "mastermind_cli/version"
2
+ require "mastermind_cli/mastermind.rb"
3
+ require "mastermind_cli/board.rb"
4
+ require "mastermind_cli/player.rb"
5
+
6
+ module MastermindCli
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mastermind_cli.gemspec
4
+ gemspec
@@ -0,0 +1,36 @@
1
+ # MastermindCli
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/mastermind_cli`. 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 'mastermind_cli'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mastermind_cli
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. 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]/mastermind_cli.
36
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mastermind_cli"
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
@@ -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,72 @@
1
+ class Board
2
+
3
+ COLORS = %w(r b g y p o)
4
+ attr_reader :win_code
5
+ def initialize
6
+ @board = []
7
+ setup
8
+ create_code
9
+ end
10
+
11
+ def setup
12
+ 12.times do |row|
13
+ @board[row] = %w(_)*4, []
14
+ end
15
+ end
16
+
17
+ def create_code
18
+ @win_code = %w(r b g y)
19
+ end
20
+
21
+ def render
22
+ puts "~"*40
23
+ (1..12).each do |row|
24
+ puts "#{@board[-row][0]}#{@board[-row][1]}"
25
+ puts ""
26
+ end
27
+ puts "~"*40
28
+ end
29
+
30
+ def full?(turn)
31
+ turn > 11
32
+ end
33
+
34
+ def winning_combination?(turn)
35
+ row = turn - 1
36
+ @board[row][0] == @win_code
37
+ end
38
+
39
+ def add_move(move, row)
40
+ check_feedbacks(move)
41
+ @board[row] = move, @feedback
42
+ end
43
+
44
+ def red_feedbacks(move)
45
+ 4.times do |idx|
46
+ if move[idx] == @win_code[idx]
47
+ @hash[idx] = "R"
48
+ end
49
+ end
50
+ end
51
+
52
+
53
+ def white_feedbacks(move)
54
+ move.each do |color|
55
+ 4.times do |idx|
56
+ if color == @win_code[idx]
57
+ @hash[idx] = "W" unless @hash.has_key?(idx)
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ def check_feedbacks(move)
64
+ @feedback = []
65
+ @hash = {}
66
+ red_feedbacks(move)
67
+ white_feedbacks(move)
68
+ @hash.each do |key, val|
69
+ @feedback << val
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,59 @@
1
+ require './board.rb'
2
+ require './player.rb'
3
+
4
+ class Mastermind
5
+
6
+ def initialize
7
+ @turn = 0
8
+ @board = Board.new
9
+ @player = Player.new(@board)
10
+ end
11
+
12
+ def greetings
13
+ puts "="*40
14
+ puts "Welcome to CLI Mastermind".center(40)
15
+ puts "Made by FrenchToast".center(40)
16
+ puts "Available Colors [r b g y p o]".center(40)
17
+ puts "To make move, enter 4 colors".center(40)
18
+ puts "For example: rbyg".center(40)
19
+ puts "="*40
20
+ end
21
+
22
+ def play
23
+ greetings
24
+ until game_over?
25
+ @board.render
26
+ @player.make_move(@turn)
27
+ @turn +=1
28
+ end
29
+ end
30
+
31
+
32
+ def game_over?
33
+ victory || lose
34
+ end
35
+
36
+ def victory
37
+ if @board.winning_combination?(@turn)
38
+ @board.render
39
+ puts "Congratulations, you win!"
40
+ puts "Your code was: #{@board.win_code}"
41
+ return true
42
+ end
43
+ false
44
+ end
45
+
46
+ def lose
47
+ if @board.full?(@turn)
48
+ @board.render
49
+ puts "I'm Sorry, you lose."
50
+ puts "Winning combination was: #{@board.win_code}"
51
+ return true
52
+ end
53
+ false
54
+ end
55
+ end
56
+
57
+
58
+ game = Mastermind.new
59
+ game.play
@@ -0,0 +1,36 @@
1
+ class Player
2
+
3
+ def initialize(board)
4
+ @board = board
5
+ @move = nil
6
+ end
7
+
8
+
9
+ def make_move(turn)
10
+ row = turn
11
+ get_input
12
+ @board.add_move(@move, row)
13
+ end
14
+
15
+
16
+ def get_input
17
+ puts "Enter move: "
18
+ arr = gets.chomp.split('')
19
+ if input_valid?(arr)
20
+ @move = arr
21
+ else
22
+ get_input
23
+ end
24
+ end
25
+
26
+
27
+ def input_valid?(arr)
28
+ if arr.length != 4
29
+ puts "Please input four letters"
30
+ return false
31
+ else
32
+ arr.all? {|chr| @board.class::COLORS.include?(chr)}
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,3 @@
1
+ module MastermindCli
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "mastermind_cli/version"
2
+
3
+ module MastermindCli
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mastermind_cli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mastermind_cli"
8
+ spec.version = MastermindCli::VERSION
9
+ spec.authors = ["Thomas Lo"]
10
+ spec.email = ["thomasjinlo@gmail.com"]
11
+
12
+ spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
13
+ spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "TODO: Put your gem's website or public repo URL here."
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.11"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mastermind_cli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mastermind_cli"
8
+ spec.version = MastermindCli::VERSION
9
+ spec.authors = ["Thomas Lo"]
10
+ spec.email = ["thomasjinlo@gmail.com"]
11
+
12
+ spec.summary = %q{Mastermind}
13
+ spec.description = %q{Command line game}
14
+ spec.homepage = "https://github.com/thomasjinlo/mastermind_cli.git"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.11"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mastermind_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Lo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-10 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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
+ description: Command line game
42
+ email:
43
+ - thomasjinlo@gmail.com
44
+ executables:
45
+ - mastermind_cli
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - exe/mastermind_cli
56
+ - lib/mastermind_cli.rb
57
+ - lib/mastermind_cli/board.rb
58
+ - lib/mastermind_cli/mastermind.rb
59
+ - lib/mastermind_cli/player.rb
60
+ - lib/mastermind_cli/version.rb
61
+ - mastermind_cli.gemspec
62
+ - mastermind_cli/.gitignore
63
+ - mastermind_cli/Gemfile
64
+ - mastermind_cli/README.md
65
+ - mastermind_cli/Rakefile
66
+ - mastermind_cli/bin/console
67
+ - mastermind_cli/bin/setup
68
+ - mastermind_cli/lib/mastermind_cli.rb
69
+ - mastermind_cli/lib/mastermind_cli/board.rb
70
+ - mastermind_cli/lib/mastermind_cli/mastermind.rb
71
+ - mastermind_cli/lib/mastermind_cli/player.rb
72
+ - mastermind_cli/lib/mastermind_cli/version.rb
73
+ - mastermind_cli/mastermind_cli.gemspec
74
+ homepage: https://github.com/thomasjinlo/mastermind_cli.git
75
+ licenses: []
76
+ metadata:
77
+ allowed_push_host: https://rubygems.org
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.8
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Mastermind
98
+ test_files: []