mastermind-Alex 1.0.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: 86fdc2a4df4e2e541f43007426a83e020e8edbda
4
+ data.tar.gz: 4bccb125aac4106eff2edcc514a885c3a091d7a5
5
+ SHA512:
6
+ metadata.gz: 5876527cd1564b6e6e894c023b0b4ea8ad89e7cf8689c6df13b0aaa938b7b8e4cf1cde9aa8736deb4307b4a25b1a5e9effa9acc268980f078f69794768e8bf14
7
+ data.tar.gz: 4622e38a255cc0cf8d880f1fecf7daf04658501595230acaea353bd98f2de8c9c438127b57744c08c7f5d79e138411c49078a47673baa0d65c577685c547ebd6
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ Mastermind!!!
2
+ Guess the code (a combination of red, green, yellow, and blue) that the computer generates in the least amount of turns.
3
+
4
+ Game features:
5
+ ASCII art
6
+ color
7
+ timer
8
+ number of turns
9
+
10
+
11
+
12
+ Algorithm
13
+
14
+ Start Game
15
+ Display Menu >>> Play, Instructions, Quit
16
+ if Play is selected
17
+ Generate a secret code
18
+ Prompt player for first guess
19
+ Check that input is valid (correct tounumber of characters, case sensitive..etc)
20
+ Evaluate move to see if it matches the secret code
21
+ if it matches - display winner and prompt player to quit or play again
22
+ if it doesn't match - giver player clues (right colors, right positions...etc)
23
+ prompt player for next guess
24
+ evaluate input... repeat until player matches secret code
25
+
26
+ if instructions are selected
27
+ display instructions
28
+ prompt player to play game
29
+
30
+ if quit is selected
31
+ quit game
data/lib/cli.rb ADDED
@@ -0,0 +1,50 @@
1
+ require_relative 'messages'
2
+ require_relative 'evaluate_input'
3
+ require_relative 'game1'
4
+ #gem 'colorize'
5
+
6
+
7
+ class Cli
8
+ attr_reader :user_input,
9
+ :messages,
10
+ :instream,
11
+ :outstream,
12
+ :eval_input
13
+
14
+ def initialize(instream, outstream)
15
+ @user_input = ""
16
+ @messages = Messages.new
17
+ @instream = instream
18
+ @outstream = outstream
19
+ end
20
+
21
+ def call
22
+ outstream.puts messages.intro
23
+ loop do
24
+ outstream.puts messages.command_request
25
+ user_input = instream.gets.strip
26
+ eval_input = EvaluateInput.new(user_input)
27
+ break if eval_input.exit?
28
+ process_initial_commands(eval_input)
29
+ end
30
+ outstream.puts messages.ending
31
+ end
32
+
33
+ def process_initial_commands(eval_input)
34
+ if eval_input.exit?
35
+ outstream.puts messages.quit_game
36
+ elsif eval_input.play?
37
+ game = Game1.new(instream, outstream, messages)
38
+ game.play
39
+ elsif eval_input.instructions?
40
+ outstream.puts messages.game_instructions
41
+ else
42
+ outstream.puts messages.not_valid_input
43
+ end
44
+ end
45
+
46
+
47
+
48
+ end
49
+
50
+ #Cli.new($stdin, $stdout).call
@@ -0,0 +1,15 @@
1
+ #generates a random code of 4 characters
2
+
3
+ class CodeGenerator
4
+ attr_reader :array,
5
+ :new_code
6
+
7
+ def initialize
8
+ @array = ['r','r','r','r','g','g','g','g','b','b','b','b','y','y','y','y']
9
+ @new_code = []
10
+ end
11
+
12
+ def secret_code
13
+ @new_code = @array.shuffle!.pop(4) #could use shift instead of pop?
14
+ end
15
+ end
@@ -0,0 +1,43 @@
1
+ # take two arrays:
2
+ #test that input is array with 4 characters
3
+ #compare input
4
+ #provide feedback about number of color matches in guess compared to secret code
5
+ #provide feedback about number of position and color matches in guess compared to secret
6
+ class CompareCodes
7
+ attr_reader :code,
8
+ :guess
9
+
10
+ def initialize(secret_code, evaluated_input)
11
+ @code = secret_code
12
+ @guess = evaluated_input
13
+ @counter = 0
14
+ end
15
+
16
+
17
+
18
+ def match?
19
+ puts "this is code #{@code} and this is guess #{@guess}"
20
+ @code == @guess
21
+ end
22
+
23
+ def winner?
24
+ match?
25
+ end
26
+
27
+ def position_color_match
28
+ correct_position = @guess.each_with_index.count {|color,position| color == @code[position]}
29
+ correct_position
30
+ end
31
+
32
+ def color_match
33
+ copy = @code.dup
34
+ @guess.each do |matching_character|
35
+ if copy.include?(matching_character)
36
+ @counter += 1
37
+ copy.delete_at(copy.index(matching_character))
38
+ end
39
+ end
40
+ @counter
41
+ #puts "you have #{@counter} of the colors in the correct position"
42
+ end
43
+ end
File without changes
@@ -0,0 +1,50 @@
1
+ require_relative 'messages'
2
+
3
+ class EvaluateInput
4
+ attr_accessor :input,
5
+ :eval_input
6
+
7
+ def initialize(input)
8
+ @input = input.downcase
9
+ @eval_input = []
10
+ @valid = false
11
+ end
12
+
13
+ #def convert_to_array
14
+ # eval_input = input.split(//)
15
+ # end
16
+
17
+ def finished?
18
+ input == 'q' || input =='quit'
19
+ end
20
+
21
+ def exit?
22
+ input == "q" || input == "quit"
23
+ #puts "press q to exit game"
24
+
25
+ end
26
+
27
+
28
+ def input_is_valid?
29
+ eval_input = input.split(//)
30
+ eval_input.all? {|char| char == "r" || char == "b" || char == "y" || char == "g" }
31
+ end
32
+
33
+ def length_short?
34
+ #eval_input = input.split(//)
35
+ input_is_valid? && input.split(//).length < 4
36
+ end
37
+
38
+ def length_long?
39
+ #eval_input = input.split(//)
40
+ input_is_valid? && input.split(//).length > 4
41
+ end
42
+
43
+ def play?
44
+ input == "p" || input == "play"
45
+ end
46
+
47
+ def instructions?
48
+ input == "i" || input == "instructions"
49
+ end
50
+ end
data/lib/game1.rb ADDED
@@ -0,0 +1,72 @@
1
+ require_relative 'code_generator'
2
+ require_relative 'messages'
3
+ require_relative 'evaluate_input'
4
+ require_relative 'compare_codes'
5
+ require 'time_diff'
6
+
7
+
8
+ class Game1
9
+ attr_accessor :instream,
10
+ :outstream,
11
+ :messages,
12
+ :mastermind_code
13
+
14
+
15
+ def initialize(instream, outstream, messages)
16
+ self.instream = instream
17
+ self.outstream = outstream
18
+ self.messages = Messages.new
19
+ self.mastermind_code = CodeGenerator.new
20
+ @guess = 0
21
+ end
22
+
23
+ def generate_secret_code
24
+ @secret_code = mastermind_code.secret_code
25
+ end
26
+
27
+ def play
28
+ @start_time = Time.now
29
+ generate_secret_code
30
+ outstream.puts messages.game_intro
31
+ until won?
32
+ outstream.puts messages.game_command_request
33
+ user_input = instream.gets.strip
34
+ evaluated_input = EvaluateInput.new(user_input)
35
+ break if user_input == "q" || user_input == "quit"
36
+ process_game_commands(evaluated_input)
37
+ end
38
+ end
39
+
40
+ def process_game_commands(evaluated_input)
41
+ if evaluated_input.length_short?
42
+ outstream.puts messages.too_short
43
+ elsif evaluated_input.length_long?
44
+ outstream.puts messages.too_long
45
+ elsif evaluated_input.input_is_valid?
46
+ compare_codes(evaluated_input)
47
+ else
48
+ outstream.puts messages.not_valid_input
49
+ end
50
+ end
51
+
52
+ def compare_codes(evaluated_input)
53
+ @guess += 1
54
+ guess_code = evaluated_input.input.chars
55
+ eval_guess = CompareCodes.new(@secret_code, guess_code)
56
+ if eval_guess.match? == false
57
+ outstream.puts messages.no_match
58
+ outstream.puts messages.color_match(eval_guess.color_match)
59
+ outstream.puts messages.position_color_match(eval_guess.position_color_match)
60
+ elsif eval_guess.match?
61
+ duration = Time.diff(@start_time, Time.now)
62
+ outstream.puts messages.winner
63
+ outstream.puts messages.duration(duration[:minute], duration[:second], @guess)
64
+ outstream.puts messages.program_instructions
65
+ @won = true
66
+ end
67
+ end
68
+
69
+ def won?
70
+ @won
71
+ end
72
+ end
data/lib/messages.rb ADDED
@@ -0,0 +1,110 @@
1
+ require_relative 'compare_codes'
2
+ require 'colorize'
3
+
4
+ class Messages
5
+
6
+ def intro
7
+ print "\e[2J\e[f"
8
+ puts' _____ __ .__ .___ '.magenta
9
+ puts' / \ _____ _______/ |_ ___________ _____ |__| ____ __| _/ '.magenta
10
+ puts' / \ / \__ \ / ___/\ __\/ __ \_ __ \/ \| |/ \ / __ | '.magenta
11
+ puts' / Y \/ __ \_\___ \ | | \ ___/| | \/ Y Y \ | | \/ /_/ | '.magenta
12
+ puts' \____|__ (____ /____ > |__| \___ >__| |__|_| /__|___| /\____ | '.magenta
13
+ puts' \/ \/ \/ \/ \/ \/ \/ '.magenta
14
+
15
+
16
+
17
+ puts "\n" + program_instructions
18
+ end
19
+
20
+ def program_instructions
21
+ "To Play Enter '(p)lay', For Instructions Enter '(i)nstructions', To Quit At Anytime Enter '(q)uit' "
22
+ end
23
+
24
+ def command_request
25
+ "MAIN MENU: Enter a command[(q)uit, (i)nstructions, or (p)play] : "
26
+ end
27
+
28
+ def game_instructions
29
+ puts "Once the game begins, a secret code will be generated consisting of 4 elements, "\
30
+
31
+ "#{red}, #{yellow}, #{green}, #{blue}. This code will be hidden from you."\
32
+ " Your task is to guess the code in the least number of trys.\nAlong the way,"\
33
+ "you will be given hints about the code, until you guess the correct code.\n"\
34
+ "Guess wisely, he who has the least amount of guesses wins!\n The code consists of 4 elements: "
35
+ end
36
+
37
+ def red
38
+ "(r)ed".red
39
+ end
40
+
41
+ def duration(minutes, seconds, tries)
42
+ "It took you #{tries} tries over #{minutes} minutes and #{seconds} seconds to finish."
43
+ end
44
+
45
+ def color_match(color_match)
46
+ "You have #{color_match} of the correct #{colors}"
47
+ end
48
+
49
+ def position_color_match(position_color_match)
50
+ puts "you have #{position_color_match} #{colors} in the correct position"
51
+ end
52
+
53
+ def colors
54
+ "c".red + "o".yellow + "l".blue + "o".green + "r".red + "(s)".yellow
55
+ end
56
+
57
+ def yellow
58
+ "(y)ellow".yellow
59
+ end
60
+
61
+ def green
62
+ "(g)reen".green
63
+ end
64
+
65
+ def blue
66
+ "(b)lue".blue
67
+ end
68
+
69
+ def ending
70
+ "Good bye."
71
+ end
72
+
73
+ def game_intro
74
+ "I have generated a beginner sequence with four elements made up of:"\
75
+ "#{red},#{green}, #{blue}, and #{yellow}."
76
+ end
77
+
78
+ def game_command_request
79
+ "Enter colors (or 'q' for quit):"
80
+ end
81
+
82
+ def try_indicator(tries)
83
+ "This is try number #{tries}"
84
+ end
85
+
86
+ def quit_game
87
+ "Exiting game. Press 'q' to quit"
88
+ end
89
+
90
+ def not_valid_input
91
+ "Input not valid. Please enter (r)ed, (g)reen, (b)lue, or (y)ellow"
92
+ end
93
+
94
+ def winner
95
+ "YOU".red + " WON".blue + "!!!".green + "!!!".yellow
96
+ end
97
+
98
+ def no_match
99
+ "NOT A MATCH".magenta
100
+ end
101
+
102
+ def too_short
103
+ "The code you entered is too short."
104
+ end
105
+
106
+ def too_long
107
+ "The code you entered is too long."
108
+ end
109
+
110
+ end
data/mastermind.rb ADDED
@@ -0,0 +1,6 @@
1
+ lib_dir = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib_dir)
3
+
4
+ require 'cli'
5
+
6
+ Cli.new($stdin, $stdout).call
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mastermind-Alex
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Robinson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: mastermind game made in ruby
14
+ email: alexandra.scott335@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/cli.rb
21
+ - lib/code_generator.rb
22
+ - lib/compare_codes.rb
23
+ - lib/display_info.rb
24
+ - lib/evaluate_input.rb
25
+ - lib/game1.rb
26
+ - lib/messages.rb
27
+ - mastermind.rb
28
+ homepage: https://rubygems.org/gems/AlexRobinson
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.4.3
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: mastermind
52
+ test_files: []