mastermind-scott 2.0.3 → 2.0.4
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 +4 -4
- data/lib/cli.rb +27 -28
- data/lib/colors.rb +2 -5
- data/lib/game.rb +5 -9
- data/lib/game_prompts.rb +25 -26
- data/lib/table.rb +10 -11
- data/lib/validate.rb +11 -16
- data/mastermind-scott.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9851f1b85be4ced658bbb73d916f908dd712c37
|
4
|
+
data.tar.gz: 84912780e04ef850cea0a290be52518de76ea953
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7219d4abcb82c7aa2ef6ca20a355955f7b99886db0571f59cc12c3fa8e370715358dafdb0478a9c743656d826d1c073aa7f928c5450c13a5a495e57b4bba06a
|
7
|
+
data.tar.gz: 0ba50930f055d2d6de3774b8dd9187e2a5c9de3122da7c01ea181c838bdc65bcb0664b01364f6748cf5f4c51b3794718a361177024593d62af27972e56b0ba29
|
data/lib/cli.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'game_prompts'
|
2
|
+
require 'game'
|
3
3
|
|
4
4
|
class CLI
|
5
5
|
attr_reader :input,
|
@@ -11,12 +11,12 @@ class CLI
|
|
11
11
|
@input = input
|
12
12
|
@output = output
|
13
13
|
@messages = GamePrompts.new
|
14
|
-
@command =
|
14
|
+
@command = ''
|
15
15
|
end
|
16
16
|
|
17
17
|
def call
|
18
|
-
|
19
|
-
|
18
|
+
output.puts @messages.mastermind_logo
|
19
|
+
output.puts @messages.intro_message
|
20
20
|
until quit?
|
21
21
|
output.puts @messages.player_input
|
22
22
|
@command = input.gets.strip.downcase
|
@@ -27,29 +27,28 @@ end
|
|
27
27
|
|
28
28
|
private
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
output.puts @messages.invalid
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def play?
|
46
|
-
command == "p" || command == "play"
|
30
|
+
def welcome_options
|
31
|
+
case
|
32
|
+
when play?
|
33
|
+
game = Game.new(input, output, messages)
|
34
|
+
game.play
|
35
|
+
when instructions?
|
36
|
+
output.puts @messages.instructions
|
37
|
+
when quit?
|
38
|
+
output.puts @messages.quit
|
39
|
+
else
|
40
|
+
output.puts @messages.invalid
|
47
41
|
end
|
42
|
+
end
|
48
43
|
|
49
|
-
|
50
|
-
|
51
|
-
|
44
|
+
def play?
|
45
|
+
command == 'p' || command == 'play'
|
46
|
+
end
|
52
47
|
|
53
|
-
|
54
|
-
|
55
|
-
|
48
|
+
def instructions?
|
49
|
+
command == 'i' || command == 'instructions'
|
50
|
+
end
|
51
|
+
|
52
|
+
def quit?
|
53
|
+
command == 'q' || command == 'quit'
|
54
|
+
end
|
data/lib/colors.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
class Colors
|
2
2
|
attr_reader :answer
|
3
3
|
def initialize
|
4
|
-
@answer =
|
4
|
+
@answer = %w(r r r r)
|
5
5
|
end
|
6
6
|
|
7
7
|
def secret_answer
|
@@ -13,13 +13,10 @@ class Colors
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def answer_options
|
16
|
-
|
16
|
+
%w(r g b y)
|
17
17
|
end
|
18
18
|
|
19
19
|
def shuffle
|
20
20
|
answer_options.shuffle
|
21
21
|
end
|
22
|
-
|
23
22
|
end
|
24
|
-
|
25
|
-
color = Colors.new
|
data/lib/game.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
require_relative '../lib/colors'
|
2
2
|
require_relative '../lib/game_prompts'
|
3
|
-
require_relative
|
3
|
+
require_relative '../lib/validate'
|
4
4
|
require_relative '../lib/table'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
6
|
class Game
|
9
7
|
attr_reader :input,
|
10
8
|
:output,
|
@@ -24,7 +22,7 @@ class Game
|
|
24
22
|
@input = input
|
25
23
|
@output = output
|
26
24
|
@messages = messages
|
27
|
-
@command =
|
25
|
+
@command = ''
|
28
26
|
@guess_count = 0
|
29
27
|
@old_guess = []
|
30
28
|
end
|
@@ -40,8 +38,7 @@ class Game
|
|
40
38
|
end
|
41
39
|
end
|
42
40
|
|
43
|
-
|
44
|
-
private
|
41
|
+
private
|
45
42
|
|
46
43
|
def turn_evaluation
|
47
44
|
turn_visuals
|
@@ -70,7 +67,7 @@ private
|
|
70
67
|
end
|
71
68
|
|
72
69
|
def you_win
|
73
|
-
|
70
|
+
output.puts messages.winner(answer, guess_count, minutes, seconds)
|
74
71
|
end
|
75
72
|
|
76
73
|
def turn_visuals
|
@@ -121,7 +118,7 @@ private
|
|
121
118
|
end
|
122
119
|
|
123
120
|
def quit?
|
124
|
-
command ==
|
121
|
+
command == 'q' || command == 'quit'
|
125
122
|
end
|
126
123
|
|
127
124
|
def validator
|
@@ -144,5 +141,4 @@ private
|
|
144
141
|
def save_guess
|
145
142
|
@old_guess << @command
|
146
143
|
end
|
147
|
-
|
148
144
|
end
|
data/lib/game_prompts.rb
CHANGED
@@ -4,42 +4,41 @@ class GamePrompts
|
|
4
4
|
attr_reader :intro_message # => nil
|
5
5
|
|
6
6
|
def intro_message
|
7
|
-
|
7
|
+
` say "so check me out"`
|
8
8
|
"Welcome to MASTERMIND\n".green +
|
9
|
-
|
9
|
+
'Would you like to (p)lay, read the (i)nstructions, or (q)uit?'.light_blue
|
10
10
|
end
|
11
11
|
|
12
12
|
def lose
|
13
|
-
|
13
|
+
'Nice try you ' + 'DUMMY. '.blue.bold + play_again
|
14
14
|
end
|
15
15
|
|
16
16
|
def winner(answer, guess_count, minutes, seconds)
|
17
|
-
"WINNER! You guessed the sequence '#{answer.join('').upcase}' with #{guess_count} guesses in #{minutes} minutes and #{seconds} seconds
|
17
|
+
"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?"
|
18
18
|
end
|
19
19
|
|
20
20
|
def game_start
|
21
|
-
"I have generated a beginner sequence with four elements made up of:\n" +
|
21
|
+
"I have generated a beginner sequence with four elements made up of:\n" + '(r)'.red + 'ed, ' + '(g)'.green + 'reen, ' + '(b)'.light_blue + 'lue, ' + 'and ' + '(y)'.yellow + "ellow.\n Use (q)uit at any time to end the game.\n\n"
|
22
22
|
end
|
23
23
|
|
24
24
|
def mastermind_logo
|
25
|
-
|
26
|
-
%q{
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
25
|
+
puts
|
26
|
+
%q{
|
27
|
+
_ _ _
|
28
|
+
| | (_) | |
|
29
|
+
_ __ ___ __ _ ___| |_ ___ _ __ _ __ ___ _ _ __ __| |
|
30
|
+
| '_ ` _ \ / _` / __| __/ _ \ '__| '_ ` _ \| | '_ \ / _` |
|
31
|
+
| | | | | | (_| \__ \ || __/ | | | | | | | | | | | (_| |
|
32
|
+
|_| |_| |_|\__,_|___/\__\___|_| |_| |_| |_|_|_| |_|\__,_|
|
33
|
+
}.cyan
|
35
34
|
end
|
36
35
|
|
37
36
|
def play
|
38
|
-
|
37
|
+
'begin the game.'
|
39
38
|
end
|
40
39
|
|
41
40
|
def player_input
|
42
|
-
|
41
|
+
'Enter Choice: '.yellow
|
43
42
|
end
|
44
43
|
|
45
44
|
def instructions
|
@@ -47,34 +46,34 @@ class GamePrompts
|
|
47
46
|
end
|
48
47
|
|
49
48
|
def quit
|
50
|
-
|
49
|
+
'Your father was right about you, you are a quitter.'.red
|
51
50
|
end
|
52
51
|
|
53
52
|
def invalid
|
54
|
-
|
53
|
+
'Your argument is invalid. Try again.'.red
|
55
54
|
end
|
56
55
|
|
57
56
|
def guess_prompt
|
58
|
-
|
57
|
+
'Take your guess: '.yellow
|
59
58
|
end
|
60
59
|
|
61
60
|
def guess_again
|
62
|
-
|
61
|
+
'The guess must only be 4 colors and either' + ' r, '.red + 'g, '.green + 'b, '.light_blue + 'or y.'.yellow
|
63
62
|
end
|
64
63
|
|
65
64
|
def guess_count(guess_count)
|
66
65
|
if guess_count == 1
|
67
|
-
|
66
|
+
'You have taken' + " #{guess_count}".red + ' guess.'
|
68
67
|
else
|
69
|
-
|
68
|
+
'You have taken ' + " #{guess_count}".red + ' guesses.'
|
70
69
|
end
|
71
70
|
end
|
72
71
|
|
73
|
-
def after_guess(guess,number_correct, position_right)
|
74
|
-
|
72
|
+
def after_guess(guess, number_correct, position_right)
|
73
|
+
'Your guess ' + "'#{guess.upcase}'".magenta + ' has ' + "#{number_correct}".magenta + ' correct colors with ' + "#{position_right}".magenta + ' in the correct position.'
|
75
74
|
end
|
76
75
|
|
77
76
|
def play_again
|
78
|
-
|
77
|
+
'(p)lay again or (q)uit?'
|
79
78
|
end
|
80
79
|
end
|
data/lib/table.rb
CHANGED
@@ -5,15 +5,14 @@ class Table
|
|
5
5
|
:rows
|
6
6
|
|
7
7
|
def initialize
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
@rows = []
|
9
|
+
@rows << ['Answer', '????', 'XX', 'XX']
|
10
|
+
@table = create_table
|
11
|
+
table_style
|
13
12
|
end
|
14
13
|
|
15
14
|
def table_style
|
16
|
-
table.style = {:
|
15
|
+
table.style = { width: 40, padding_left: 3, border_x: '=', border_i: 'X' }
|
17
16
|
end
|
18
17
|
|
19
18
|
def show
|
@@ -21,12 +20,12 @@ class Table
|
|
21
20
|
end
|
22
21
|
|
23
22
|
def hidden_answer(answer)
|
24
|
-
|
23
|
+
@rows[0] = ['Answer', answer.join(''), 'XX', 'XX']
|
25
24
|
end
|
26
25
|
|
27
|
-
def update(guess_number, guess,number_correct, position, answer)
|
26
|
+
def update(guess_number, guess, number_correct, position, answer)
|
28
27
|
if guess_number < 10
|
29
|
-
@rows << [guess_number, guess, number_correct
|
28
|
+
@rows << [guess_number, guess, number_correct, position]
|
30
29
|
else
|
31
30
|
hidden_answer(answer)
|
32
31
|
end
|
@@ -35,7 +34,7 @@ class Table
|
|
35
34
|
end
|
36
35
|
|
37
36
|
def create_table
|
38
|
-
Terminal::Table.new :
|
39
|
-
:
|
37
|
+
Terminal::Table.new headings: %w(Round Guess Color Pos),
|
38
|
+
rows: rows
|
40
39
|
end
|
41
40
|
end
|
data/lib/validate.rb
CHANGED
@@ -1,10 +1,5 @@
|
|
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
1
|
require_relative '../lib/colors'
|
6
2
|
|
7
|
-
|
8
3
|
class Validate
|
9
4
|
attr_reader :answer,
|
10
5
|
:guess
|
@@ -16,21 +11,21 @@ class Validate
|
|
16
11
|
def position_check(guess)
|
17
12
|
difference = 0
|
18
13
|
4.times do |index|
|
19
|
-
|
20
|
-
|
14
|
+
difference += 1 if guess[index] == @answer[index]
|
15
|
+
end
|
21
16
|
difference
|
22
17
|
end
|
23
18
|
|
24
19
|
def number_correct(guess)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
20
|
+
number_correct = 0
|
21
|
+
new_answer = @answer
|
22
|
+
new_answer.each do |letter|
|
23
|
+
if guess.include?(letter)
|
24
|
+
guess.delete_at(guess.index(letter))
|
25
|
+
number_correct += 1
|
26
|
+
end
|
31
27
|
end
|
32
|
-
|
33
|
-
number_correct
|
28
|
+
number_correct
|
34
29
|
end
|
35
30
|
|
36
31
|
def correct?(user_guess)
|
@@ -38,7 +33,7 @@ class Validate
|
|
38
33
|
end
|
39
34
|
|
40
35
|
def letters?(guess)
|
41
|
-
valid_letters =
|
36
|
+
valid_letters = %w(r g b y)
|
42
37
|
guess.chars.all? { |letter| valid_letters.include?(letter) }
|
43
38
|
end
|
44
39
|
|
data/mastermind-scott.gemspec
CHANGED