mastermind-scott 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: 54f48223242e2c5c2c3ad37bf66c1f21b9d1263f
4
+ data.tar.gz: 2c07c5c92922212b91b4139d7077843b304e9646
5
+ SHA512:
6
+ metadata.gz: 80abb136e8c64473a6f9481651070043f44b95acbcfc821c51cf49f5b69c2a49642e94aa6f0c5317da3c8a84f32b3c02297c58fa623f81599b36663b4c9e0c5b
7
+ data.tar.gz: b137c5ad1f7784c767ff0b858d5a55975fb079472e4e07829772953c568f54efc5ca60a475d71bf171c26de7cab8315e54723f2dce39dcad88dac422c9fba030
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib_dir = File.expand_path('../lib/*.rb', __dir__)
4
+ $LOAD_PATH.unshift(lib_dir)
5
+ require 'cli'
6
+
7
+ CLI.new($stdin, $stdout).call
data/lib/cli.rb ADDED
@@ -0,0 +1,55 @@
1
+ require "game_prompts"
2
+ require "game"
3
+
4
+ class CLI
5
+ attr_reader :input,
6
+ :output,
7
+ :messages,
8
+ :command
9
+
10
+ def initialize(input, output)
11
+ @input = input
12
+ @output = output
13
+ @messages = GamePrompts.new
14
+ @command = ""
15
+ end
16
+
17
+ def call
18
+ output.puts @messages.mastermind_logo
19
+ output.puts @messages.intro_message
20
+ until quit?
21
+ output.puts @messages.player_input
22
+ @command = input.gets.strip.downcase
23
+ welcome_options
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+
31
+ def welcome_options
32
+ case
33
+ when play?
34
+ game = Game.new(input, output, messages)
35
+ game.play
36
+ when instructions?
37
+ output.puts @messages.instructions
38
+ when quit?
39
+ output.puts @messages.quit
40
+ else
41
+ output.puts @messages.invalid
42
+ end
43
+ end
44
+
45
+ def play?
46
+ command == "p" || command == "play"
47
+ end
48
+
49
+ def instructions?
50
+ command == "i" || command == "instructions"
51
+ end
52
+
53
+ def quit?
54
+ command == "q" || command == "quit"
55
+ end
data/lib/colors.rb ADDED
@@ -0,0 +1,25 @@
1
+ class Colors
2
+ attr_reader :answer
3
+ def initialize
4
+ @answer = ['r','r','r','r']
5
+ end
6
+
7
+ def secret_answer
8
+ @answer = []
9
+ 4.times do
10
+ answer << shuffle.pop
11
+ end
12
+ answer
13
+ end
14
+
15
+ def answer_options
16
+ ["r","g","b","y"]
17
+ end
18
+
19
+ def shuffle
20
+ answer_options.shuffle
21
+ end
22
+
23
+ end
24
+
25
+ color = Colors.new
data/lib/game.rb ADDED
@@ -0,0 +1,133 @@
1
+ require_relative '../lib/colors'
2
+ require_relative '../lib/game_prompts'
3
+ require_relative "../lib/validate"
4
+ require_relative '../lib/table'
5
+
6
+
7
+
8
+ class Game
9
+ attr_reader :input,
10
+ :output,
11
+ :messages,
12
+ :answer,
13
+ :judge,
14
+ :command,
15
+ :guess_count,
16
+ :table,
17
+ :start_time
18
+
19
+ def initialize(input, output, messages)
20
+ @answer = Colors.new.secret_answer
21
+ @judge = Validate.new(@answer)
22
+ @start_time = Time.now
23
+ @table = Table.new
24
+ @input = input
25
+ @output = output
26
+ @messages = messages
27
+ @command = ""
28
+ @guess_count = 0
29
+ @old_guess = []
30
+ end
31
+
32
+ def play
33
+ output.puts messages.game_start
34
+ output.puts table.show
35
+ until win? || lose?
36
+ output.print messages.guess_prompt
37
+ @command = input.gets.chomp.downcase
38
+ guess_counter
39
+ turn_evaluation
40
+ end
41
+ end
42
+
43
+
44
+ private
45
+
46
+ def turn_evaluation
47
+ output.puts messages.mastermind_logo
48
+ table_update
49
+ output.puts table.show
50
+ case
51
+ when quit?
52
+ abort(messages.quit)
53
+ when win?
54
+ output.puts messages.winner(answer, guess_count, minutes, seconds)
55
+ when !valid_size? || !valid_letters?
56
+ output.puts messages.guess_again
57
+ @guess_count -= 1
58
+ when lose?
59
+ output.puts messages.lose
60
+ else validator
61
+ output.puts messages.after_guess(command, number_correct, position_right)
62
+ output.puts messages.guess_count(guess_count)
63
+ end
64
+ end
65
+
66
+ def guess_counter
67
+ @guess_count += 1
68
+ end
69
+
70
+ def table_update
71
+ table.update(guess_count, command, number_correct, position_right, answer) if valid_size? && valid_letters?
72
+ end
73
+
74
+ def seconds
75
+ total_time % 60
76
+ end
77
+
78
+ def minutes
79
+ total_time / 60
80
+ end
81
+
82
+
83
+ def total_time
84
+ (end_time - @start_time).to_i
85
+ end
86
+
87
+ def end_time
88
+ Time.now
89
+ end
90
+
91
+ def win?
92
+ judge.correct?(parsed_guess)
93
+ end
94
+
95
+ def valid_size?
96
+ judge.size?(command)
97
+ # command.length == 4
98
+ end
99
+
100
+ def valid_letters?
101
+ judge.letters?(command)
102
+ end
103
+
104
+ def lose?
105
+ @guess_count >= 10
106
+ end
107
+
108
+ def quit?
109
+ command == "q" || command == "quit"
110
+ end
111
+
112
+ def validator
113
+ number_correct
114
+ position_right
115
+ end
116
+
117
+ def parsed_guess
118
+ @command.chars
119
+ end
120
+
121
+ def number_correct
122
+ judge.number_correct(parsed_guess)
123
+ end
124
+
125
+ def position_right
126
+ judge.position_check(parsed_guess)
127
+ end
128
+
129
+ def save_guess
130
+ @old_guess << @command
131
+ end
132
+
133
+ end
@@ -0,0 +1,78 @@
1
+ class GamePrompts
2
+ attr_reader :intro_message # => nil
3
+
4
+ def intro_message
5
+ %x( say "so check me out")
6
+ "Welcome to MASTERMIND\n"+
7
+ "Would you like to (p)lay, read the (i)nstructions, or (q)uit?"
8
+ end
9
+
10
+ def lose
11
+ "Nice try you dummy." + play_again
12
+ end
13
+
14
+ def winner(answer, guess_count, minutes, seconds)
15
+ "WINNER! You guessed the sequence '#{answer.join('').upcase}' with #{guess_count} guesses in #{minutes} minutes and #{seconds} seconds.\n(p)lay again or (q)uit?"
16
+ end
17
+
18
+ def game_start
19
+ "I have generated a beginner sequence with four elements made up of: (r)ed, (g)reen, (b)lue, and (y)ellow. Use (q)uit at any time to end the game."
20
+ end
21
+
22
+ def mastermind_logo
23
+ puts
24
+ %q{
25
+ _ _ _
26
+ | | (_) | |
27
+ _ __ ___ __ _ ___| |_ ___ _ __ _ __ ___ _ _ __ __| |
28
+ | '_ ` _ \ / _` / __| __/ _ \ '__| '_ ` _ \| | '_ \ / _` |
29
+ | | | | | | (_| \__ \ || __/ | | | | | | | | | | | (_| |
30
+ |_| |_| |_|\__,_|___/\__\___|_| |_| |_| |_|_|_| |_|\__,_|
31
+ }
32
+
33
+ end
34
+
35
+ def play
36
+ "begin the game."
37
+ end
38
+
39
+ def player_input
40
+ "Enter Choice: "
41
+ end
42
+
43
+ def instructions
44
+ "A secret combination of colors has been chosen at random.\n\nYour job is to guess the correct sequence in 10 tries or less.\n\nIf you manage to win, you'll join the elite as a mastermind.\n\nIf you lose, the shambolic state of your life will be confirmed.\n\nGood luck!...you'll need it.\n\nPress (p) to play."
45
+ end
46
+
47
+ def quit
48
+ "Your father was right about you, you are a quitter."
49
+ end
50
+
51
+ def invalid
52
+ "Your argument is invalid. Try again."
53
+ end
54
+
55
+ def guess_prompt
56
+ "Take your guess: "
57
+ end
58
+
59
+ def guess_again
60
+ "The guess must only be 4 colors and either r, g, b, or y."
61
+ end
62
+
63
+ def guess_count(guess_count)
64
+ if guess_count == 1
65
+ "You have taken #{guess_count} guess."
66
+ else
67
+ "You have taken #{guess_count} guesses."
68
+ end
69
+ end
70
+
71
+ def after_guess(guess,number_correct, position_right)
72
+ "Your guess '#{guess.upcase}' has #{number_correct} correct colors with #{position_right} in the correct position."
73
+ end
74
+
75
+ def play_again
76
+ "(p)lay again or (q)uit?"
77
+ end
78
+ end
data/lib/table.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'terminal-table'
2
+
3
+ class Table
4
+ attr_reader :table,
5
+ :rows
6
+
7
+ def initialize
8
+ @rows = []
9
+ @rows << ['Answer', '????', 'XX','XX']
10
+ @table = create_table
11
+ table_style
12
+
13
+ end
14
+
15
+ def table_style
16
+ table.style = {:width => 40, :padding_left => 3, :border_x => "=", :border_i => "X"}
17
+ end
18
+
19
+ def show
20
+ table
21
+ end
22
+
23
+ def hidden_answer(answer)
24
+ @rows[0] = ['Answer', answer.join(''), 'XX','XX']
25
+ end
26
+
27
+ def update(guess_number, guess,number_correct, position, answer)
28
+ if guess_number < 10
29
+ @rows << [guess_number, guess, number_correct ,position]
30
+ else
31
+ hidden_answer(answer)
32
+ end
33
+ @table = create_table
34
+ table_style
35
+ end
36
+
37
+ def create_table
38
+ Terminal::Table.new :headings => ['Round', 'G', 'C', 'P'],
39
+ :rows => rows
40
+ end
41
+ end
data/lib/validate.rb ADDED
@@ -0,0 +1,48 @@
1
+ # determine how many colors correct
2
+ # determine how many positions correct
3
+ # verify user guess with answer array
4
+ # track number of guesses - guess 1, guess 2, etc. && "you have X number of guess left"
5
+ require_relative '../lib/colors'
6
+
7
+
8
+ class Validate
9
+ attr_reader :answer,
10
+ :guess
11
+
12
+ def initialize(answer)
13
+ @answer = answer
14
+ end
15
+
16
+ def position_check(guess)
17
+ difference = 0
18
+ 4.times do |index|
19
+ difference += 1 if guess[index] == @answer[index]
20
+ end
21
+ difference
22
+ end
23
+
24
+ def number_correct(guess)
25
+ number_correct = 0
26
+ new_answer = @answer
27
+ new_answer.each do |letter|
28
+ if guess.include?(letter)
29
+ guess.delete_at(guess.index(letter))
30
+ number_correct += 1
31
+ end
32
+ end
33
+ number_correct
34
+ end
35
+
36
+ def correct?(user_guess)
37
+ user_guess == @answer
38
+ end
39
+
40
+ def letters?(guess)
41
+ valid_letters = ['r', 'g', 'b', 'y']
42
+ guess.chars.all? { |letter| valid_letters.include?(letter) }
43
+ end
44
+
45
+ def size?(guess)
46
+ guess.length == 4
47
+ end
48
+ end
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'mastermind-scott'
3
+ s.version = '1.0.0'
4
+ s.date = '2014-11-12'
5
+ s.summary = "Try to guess the secret code."
6
+ s.description = "The Mastermind Game"
7
+ s.authors = ["Scott Crawford"]
8
+ s.email = 'scottcrawford03@gmail.com'
9
+ s.files = %w[mastermind-scott.gemspec
10
+ mastermind.rb
11
+ lib/cli.rb
12
+ lib/colors.rb
13
+ lib/game_prompts.rb
14
+ lib/table.rb
15
+ lib/validate.rb
16
+ lib/game.rb ]
17
+ s.homepage = 'http://rubygems.org/gems/mastermind-scott'
18
+ s.license = 'SMC'
19
+ s.executables << 'mastermind-scott'
20
+ end
data/mastermind.rb ADDED
@@ -0,0 +1,5 @@
1
+ lib_dir = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib_dir)
3
+ require 'cli'
4
+
5
+ CLI.new($stdin, $stdout).call
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mastermind-scott
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Scott Crawford
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: The Mastermind Game
14
+ email: scottcrawford03@gmail.com
15
+ executables:
16
+ - mastermind-scott
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/mastermind-scott
21
+ - lib/cli.rb
22
+ - lib/colors.rb
23
+ - lib/game.rb
24
+ - lib/game_prompts.rb
25
+ - lib/table.rb
26
+ - lib/validate.rb
27
+ - mastermind-scott.gemspec
28
+ - mastermind.rb
29
+ homepage: http://rubygems.org/gems/mastermind-scott
30
+ licenses:
31
+ - SMC
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.4.3
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Try to guess the secret code.
53
+ test_files: []