master_mind 0.1.2

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
+ SHA1:
3
+ metadata.gz: 45b5225ff35f9f1cf6cdf3f5a15b0f337511804b
4
+ data.tar.gz: 587ad99a71299fe4bfedaf3c07220c3cf72eaa39
5
+ SHA512:
6
+ metadata.gz: 107d183340d74937ea239bc36d3b6f7292dd30a6f17b7101f8e27f0588869f820bd1469d4653f7e9bb4e930c4f7b8c8434807d3e9e50668f8899f1b83a3bf613
7
+ data.tar.gz: 055693ad2a6b12a27a75fcd524a4ec2e256527eeae955a8eaa17f91b79a8e6e73f32fc8222cb44d9d595a8cbf32487d8b5f27c3e55fe026732815462a47331c4
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /lib/mastermind/codesnip
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.5
@@ -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 mastermind.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ guard 'rspec', cmd: "bundle exec rspec" do
2
+ # watch /lib/ files
3
+ watch(%r{^lib/(.+).rb$}) do |m|
4
+ "spec/#{m[1]}_spec.rb"
5
+ end
6
+
7
+ # watch /spec/ files
8
+ watch(%r{^spec/(.+).rb$}) do |m|
9
+ "spec/#{m[1]}.rb"
10
+ end
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Oreoluwa Akinniranye
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,45 @@
1
+ # Mastermind
2
+
3
+ Mastermind has an interesting history.
4
+ But your single task is to guess the colors the computer has in memory in the shortest possible time.
5
+
6
+ OR
7
+
8
+ read the background information
9
+
10
+ Join me in making the next storm of command-line game!!!!
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'mastermind'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install mastermind
27
+
28
+ ## Usage
29
+
30
+ TODO: Write usage instructions here
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/mastermind` for an interactive prompt that will allow you to experiment.
35
+
36
+ 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).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/andela-oakinniranye/mastermind. 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.
41
+
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rspec/core/rake_task'
2
+ require "bundler/gem_tasks"
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |task|
5
+ task.rspec_opts = ['--color', '--format', 'documentation']
6
+ end
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mastermind"
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/mastermind ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mastermind"
5
+
6
+
7
+ game = Mastermind::Main.new
8
+ game.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
data/circle.yml ADDED
@@ -0,0 +1,6 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.2.2
4
+ dependencies:
5
+ pre:
6
+ - gem install bundler -v '~> 1.10'
data/game_file.yml ADDED
@@ -0,0 +1 @@
1
+ --- []
data/lib/mastermind.rb ADDED
@@ -0,0 +1,24 @@
1
+ require "mastermind/version"
2
+ require "fileutils"
3
+ require "colorize"
4
+ require "active_support/core_ext/string/inflections"
5
+ require "yaml"
6
+ require 'humanize'
7
+ require 'singleton'
8
+
9
+ require 'mastermind/helper'
10
+
11
+
12
+ module Mastermind
13
+ # Your code goes here...
14
+ end
15
+
16
+ # Dir.glob('./lib/*').each do |folder|
17
+ # Dir.glob(folder +"/*.rb").each do |file|
18
+ # require file unless file.include? 'extension'
19
+ # end
20
+ # end
21
+ APP_ROOT = Pathname.new(File.expand_path('../../', __FILE__))
22
+ Dir[APP_ROOT.join('lib', 'mastermind', '*.rb')].each { |f| require f unless f.include? 'extension'}
23
+ Dir[APP_ROOT.join('lib', 'mastermind', 'datastore', '*.rb')].each { |f| require f}
24
+ Dir[APP_ROOT.join('lib', 'mastermind', 'extensions', '*.rb')].each { |f| require f}
@@ -0,0 +1,63 @@
1
+ module Mastermind
2
+ module Datastore
3
+ class YmlStore
4
+ include Singleton
5
+
6
+ attr_accessor :filename
7
+ def create_file_if_not_exist file_name, file_type= 'yml', mode = 'w'
8
+ file_name = add_file_extension_if_not_present(file_name, file_type)
9
+ ::File.open(file_name, mode) unless File.exists? file_name
10
+ end
11
+
12
+ def save file_name, data, mode = 'w', file_type='yml'
13
+ create_file_if_not_exist file_name
14
+ ::File.open(file_name, mode) do |yml_data|
15
+ ::YAML.dump data, yml_data
16
+ end
17
+ end
18
+
19
+ def save_top_ten file_name='topten.yaml', data
20
+ save(file_name, data, 'w')
21
+ end
22
+
23
+ def fetch file_name
24
+ create_file_if_not_exist unless File.exists? file_name
25
+ data = ::File.read(file_name)
26
+ data
27
+ end
28
+
29
+ def fetch_yml file_name
30
+ create_file_if_not_exist file_name
31
+ ::YAML::load_file(file_name)
32
+ end
33
+
34
+ def fetch_multiple_records file_name
35
+ list = []
36
+ ::YAML.load_stream(File.read(file_name)){ |record|
37
+ list << record
38
+ }
39
+ list
40
+ end
41
+
42
+ def filename=(file_name, mode='w')
43
+ create_file_if_not_exist file_name
44
+ end
45
+
46
+ def method_missing(method, *args)
47
+ missing_method = method.to_s
48
+ if missing_method.match(/save_([a-z]+)/)
49
+ self.save(*args, 'w+', $1)
50
+ elsif missing_method.match(/fetch_([a-z]+)/)
51
+ self.fetch(*args, $1)
52
+ else
53
+ raise NoMethodError
54
+ end
55
+ end
56
+
57
+ def add_file_extension_if_not_present(file_name, file_type = 'yml')
58
+ file_name += '.' + file_type unless file_name.match(/\w+\.[a-z]+/)
59
+ file_name
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,102 @@
1
+ module Mastermind
2
+ class Game
3
+ alias_method :old_actions, :actions
4
+ attr_reader :game_save_data
5
+ alias_method :old_get_player, :get_player
6
+ alias_method :new_old_play, :play
7
+
8
+ def get_player
9
+ old_get_player
10
+ check_if_user_has_saved_game
11
+ end
12
+
13
+ def check_if_user_has_saved_game
14
+ @game_save_data ||= SaveGame.new
15
+ player = @game_save_data.fetch_player(@player.name)
16
+ if player
17
+ input = get_input(@response.message_if_user_has_saved_game.message)
18
+ continue_saved_game(player) if ['y', 'yes'].include? input
19
+ end
20
+ end
21
+
22
+ def continue_saved_game(player)
23
+ @player = player
24
+ load_saved_game
25
+ end
26
+
27
+ def actions
28
+ new_actions = { 'pause' => 'play_later',
29
+ 'pl' => 'play_later'
30
+ }
31
+ action_s = old_actions
32
+ action_s = old_actions.merge(new_actions) unless @trial_count >= ALLOWED_TRIALS
33
+ action_s
34
+ end
35
+
36
+ def play_later
37
+ @player.save_game(@time_started, @colors, @trial_count)
38
+ end
39
+
40
+ def load_saved_game
41
+ @trial_count = @player.guesses_at_pause_count
42
+ @colors = @player.color_combo
43
+ @character_count = @colors.length
44
+ time_used = @player.time_started - @player.time_at_game_save
45
+ @time_started = Time.now.to_i - time_used
46
+ shuffle_colors_hash
47
+ instructions
48
+ end
49
+ end
50
+
51
+ class Message
52
+ def continue_later
53
+ set_attr(message: "Can't play again?\nYou can continue later", status: :paused)
54
+ end
55
+
56
+ def message_if_user_has_saved_game
57
+ set_attr(message: "You currently have a game saved.\nWould you like to continue?\nEnter (y) for Yes\nEnter (n) for No!", status: :has_saved_game)
58
+ end
59
+
60
+ # def message_if_user_continues_saved_game
61
+ # set_attr(message: "")
62
+ # end
63
+ end
64
+
65
+ class Player
66
+ attr_reader :save_game_worker, :time_started, :color_combo, :guesses_at_pause_count, :time_at_game_save
67
+ alias_method :old_to_h, :to_h
68
+
69
+ def save_game(*game_data)
70
+ @save_game_worker ||= SaveGame.new
71
+ make_save_attr(game_data[0], game_data[1], game_data[2]) unless game_data.empty?
72
+ @save_game_worker.save_record(self)
73
+
74
+ end
75
+
76
+ def make_save_attr(time_started, color_combo, guesses_at_pause_count)
77
+ @time_started = time_started
78
+ @color_combo = color_combo
79
+ @guesses_at_pause_count = guesses_at_pause_count
80
+ @time_at_game_save = Time.now.to_i
81
+ end
82
+
83
+ def to_h
84
+ player = old_to_h
85
+ player[:time_started] = @time_started
86
+ player[:color_combo] = @color_combo
87
+ player[:guesses_at_pause_count] = @guesses_at_pause_count
88
+ player[:time_at_game_save] = @time_at_game_save
89
+ player
90
+ end
91
+
92
+ def set_save_attr(game_data = {})
93
+ # check if game_data is a game or a hash
94
+ @name = game_data[:name]
95
+ @time_started = game_data[:time_started]
96
+ @color_combo = game_data[:color_combo]
97
+ @guesses_at_pause_count = game_data[:guesses_at_pause_count]
98
+ @time_at_game_save = game_data[:time_at_game_save]
99
+ @guesses = @time_taken = nil
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,95 @@
1
+ require 'mastermind'
2
+
3
+ module Mastermind
4
+
5
+ # include Helper
6
+
7
+ # module Helper
8
+ # def get_input(message)
9
+ # # puts "Whatever it is"
10
+ # message
11
+ # end
12
+ #
13
+ # def send_message(message)
14
+ #
15
+ # end
16
+ # end
17
+
18
+ class Main
19
+ alias_method :old_play, :play
20
+ def play
21
+ level = get_input(@response.level_select.message).to_i
22
+ @game.play(level)
23
+ end
24
+ end
25
+
26
+ class Game
27
+ attr_reader :level
28
+
29
+ # alias_method :old_initialize, :initialize
30
+ alias_method :old_play, :play
31
+
32
+ def play(level = 1)
33
+ level = 1 if level < 1
34
+ level = 3 if level > 3
35
+ convert_level(level)
36
+
37
+ old_play
38
+ end
39
+
40
+ def convert_level(level = 1)
41
+ # @character_count = 4 + (2 * (level - 1))
42
+ color_count = 4 + (1 * (level - 1))
43
+ #this ought not be, however useful for the simple gem
44
+ #which basically uses the generates colors based on the arrays
45
+ #if colors were repeated the commented method above would have been better
46
+
47
+ @character_count = color_count
48
+ additional_colors = {'O' => '(o)range',
49
+ 'P' => '(p)urple',
50
+ 'C' => '(c)yan',
51
+ 'V' => '(v)iolet',
52
+ 'I' => '(i)ndigo',
53
+ 'A' => '(a)mber'
54
+ }
55
+ @@all_colors_hash.merge!(additional_colors)
56
+ @@color_array = @@all_colors_hash.keys.sample(color_count)
57
+ end
58
+
59
+ def generate_colors
60
+ @colors = @@color_array.sample(@character_count)
61
+ @color_values_from_all_colors_array = @colors.map{|color| @@all_colors_hash[color] }
62
+ @color_values_from_all_colors_array.shuffle!
63
+ end
64
+
65
+ def instructions
66
+ send_message(@response.instructions(@color_values_from_all_colors_array).message) if @colors
67
+ @response.message
68
+ end
69
+
70
+
71
+ def get_game_input
72
+ input = get_input(@response.trial_count(@trial_count, @colors.join ,@color_values_from_all_colors_array).message)
73
+ input
74
+ end
75
+
76
+ end
77
+
78
+ class Message
79
+ alias_method :old_trial_count, :trial_count
80
+
81
+ def level_select
82
+ set_attr(message: "To start the game select a level you would like to play:\nEnter (1) for Beginner,\nEnter (2) for Intermediate,\nEnter (3) for Advanced.", status: :level_select)
83
+ end
84
+
85
+ def trial_count(trial_count, correct_sequence, colors = nil)
86
+ remaining_trials = Game::ALLOWED_TRIALS - trial_count
87
+ if trial_count == 0
88
+ instructions(colors)
89
+ else
90
+ old_trial_count(trial_count, correct_sequence)
91
+ end
92
+ end
93
+
94
+ end
95
+ end