mastermind_ruby 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/Rakefile +4 -0
- data/lib/mastermind_ruby/code.rb +11 -13
- data/lib/mastermind_ruby/console_interface.rb +14 -2
- data/lib/mastermind_ruby/game.rb +20 -6
- data/lib/mastermind_ruby/version.rb +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: 26d01f387e4fb94d8102ab2a0f6e2ed6a2d898ef
|
4
|
+
data.tar.gz: bee194613ef7e7df4a2bad11920a5fe8ed6e9f72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fac781566dc456a832bb6834a5f928f200a943e5eece7ee821b2fb8c0fd76f93c6355c361db9d81d3fffa15964e55ac60d503e458621d8f41d10373294044aec
|
7
|
+
data.tar.gz: 2f9646f64a4a0b5d9ff49c18e3936a6527afa370a354465c4159636c02e4a8911a37a66a6a40456f7f0809d6b13c52bb3df3d17c104e4d036489e6072f7f6992
|
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
data/lib/mastermind_ruby/code.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module MastermindRuby
|
2
2
|
class Code
|
3
3
|
AVAILABLE_CHARACTERS = %w(R Y G O M P)
|
4
|
-
CODE_PATTERN = /\A[#{AVAILABLE_CHARACTERS.join("")}]{4}\z/
|
5
4
|
|
6
5
|
def initialize(code)
|
7
6
|
@code = code
|
@@ -11,27 +10,22 @@ module MastermindRuby
|
|
11
10
|
# Params:
|
12
11
|
# +str+:: the string which is to parse to a code object
|
13
12
|
def self.parse(str)
|
14
|
-
new str.split('')
|
13
|
+
new str.upcase.split('')
|
15
14
|
end
|
16
15
|
|
17
16
|
# Method to generate a code
|
18
|
-
def self.random
|
19
|
-
new
|
17
|
+
def self.random(length)
|
18
|
+
new length.times.map { AVAILABLE_CHARACTERS.sample }
|
20
19
|
end
|
21
20
|
|
22
21
|
# Returns a code object containing 'BBBB'
|
23
|
-
def self.solution
|
24
|
-
|
22
|
+
def self.solution(length)
|
23
|
+
new length.times.map{ 'B' }
|
25
24
|
end
|
26
25
|
|
27
|
-
#
|
26
|
+
# Returns if a code is valid (matches the code pattern (R, Y, G, O, M, P))
|
28
27
|
def valid?
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
# Returns true if code is 'BBBB'
|
33
|
-
def solution?
|
34
|
-
self == Code.solution
|
28
|
+
self === /\A[#{AVAILABLE_CHARACTERS.join}]+\z/
|
35
29
|
end
|
36
30
|
|
37
31
|
# Get assesment for solution
|
@@ -89,6 +83,10 @@ module MastermindRuby
|
|
89
83
|
end
|
90
84
|
end
|
91
85
|
|
86
|
+
def ===(other)
|
87
|
+
other === to_s
|
88
|
+
end
|
89
|
+
|
92
90
|
def [](index)
|
93
91
|
@code[index]
|
94
92
|
end
|
@@ -10,6 +10,7 @@ module MastermindRuby
|
|
10
10
|
# Must have methods
|
11
11
|
# * :read_playername
|
12
12
|
# * :read_next_guess
|
13
|
+
# * :read_code_length
|
13
14
|
# * :display_welcome_message
|
14
15
|
# * :display_assessment
|
15
16
|
# * :display_invalid_code
|
@@ -23,6 +24,18 @@ module MastermindRuby
|
|
23
24
|
gets.chomp
|
24
25
|
end
|
25
26
|
|
27
|
+
# Method which reads the code length
|
28
|
+
# Return the length of the code you want as an Fixnum
|
29
|
+
def read_code_length
|
30
|
+
print 'How long should the code be (Hit enter for default): '
|
31
|
+
code = gets.strip.to_i
|
32
|
+
if code != 0
|
33
|
+
code
|
34
|
+
else
|
35
|
+
4
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
26
39
|
# Method which is called when the next turn is initiated
|
27
40
|
# Return next guess as Code object
|
28
41
|
# Params:
|
@@ -36,9 +49,8 @@ module MastermindRuby
|
|
36
49
|
# Params:
|
37
50
|
# +playername+:: the playername which was read before with :read_playername
|
38
51
|
def display_welcome_message(playername)
|
39
|
-
puts '-------- Mastermind Ruby Project -------'
|
40
52
|
puts "Hello #{playername}, your code is generated!"
|
41
|
-
puts "Avaliable Characters:\t#{MastermindRuby::Code::AVAILABLE_CHARACTERS.join("\t")}"
|
53
|
+
puts "Avaliable Characters:\t#{MastermindRuby::Code::AVAILABLE_CHARACTERS.join("\t")} (Case insensitive)"
|
42
54
|
end
|
43
55
|
|
44
56
|
# Method which is called when the guess is evaluated
|
data/lib/mastermind_ruby/game.rb
CHANGED
@@ -2,7 +2,7 @@ module MastermindRuby
|
|
2
2
|
class Game
|
3
3
|
attr_reader :try_count
|
4
4
|
|
5
|
-
def initialize(ui, solution:
|
5
|
+
def initialize(ui, solution: nil)
|
6
6
|
@solution_code = solution
|
7
7
|
@ui = ui
|
8
8
|
@try_count = 1
|
@@ -10,25 +10,39 @@ module MastermindRuby
|
|
10
10
|
end
|
11
11
|
|
12
12
|
# Method to start a game
|
13
|
-
# Requests by calling read_playername
|
13
|
+
# Requests by calling :read_playername on the UI for the playername
|
14
|
+
# Requests by calling :read_code_length on the UI for the code_length
|
15
|
+
# Requests by calling :display_welcome_message on the UI for displaying the welcome message
|
14
16
|
def start
|
15
17
|
@started = true
|
16
18
|
@playername = @ui.read_playername
|
19
|
+
@code_length = @ui.read_code_length
|
20
|
+
@solution_code ||= Code.random(@code_length)
|
17
21
|
@ui.display_welcome_message(@playername)
|
18
22
|
run
|
19
23
|
end
|
20
24
|
|
21
25
|
private
|
22
26
|
|
27
|
+
# Checks if the guess has a valid length
|
28
|
+
def guess_valid_length?(guess)
|
29
|
+
guess === /\A.{#{@code_length}}\z/
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns if the result passed is the solution
|
33
|
+
def result_solution?(result)
|
34
|
+
result == Code.solution(@code_length)
|
35
|
+
end
|
36
|
+
|
23
37
|
# Method to run the game (prevent from dying if the solution was not found yet)
|
24
|
-
# Requests by calling read_next_guess for a new guess on the UI
|
25
|
-
# Shows an assessment on the UI by calling display_assessment
|
38
|
+
# Requests by calling :read_next_guess for a new guess on the UI
|
39
|
+
# Shows an assessment on the UI by calling :display_assessment
|
26
40
|
def run
|
27
41
|
while running?
|
28
42
|
guess = @ui.read_next_guess(@try_count)
|
29
|
-
if guess.valid?
|
43
|
+
if guess_valid_length?(guess) && guess.valid?
|
30
44
|
result = guess.assessment_for_solution(@solution_code)
|
31
|
-
if result
|
45
|
+
if result_solution?(result)
|
32
46
|
stop
|
33
47
|
else
|
34
48
|
@try_count += 1
|