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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b265a9601800fa81f1b0ccff86b65c5d57066b4
4
- data.tar.gz: e8f10788313f1e9faf38c2e644175f0aae8a5d6f
3
+ metadata.gz: 26d01f387e4fb94d8102ab2a0f6e2ed6a2d898ef
4
+ data.tar.gz: bee194613ef7e7df4a2bad11920a5fe8ed6e9f72
5
5
  SHA512:
6
- metadata.gz: bfc794183bf2b29fa43d958a6259707755bfcb9bdaf7d64b9b1122e27c8a5f5e9c8dee520b67576db3b73ea89d53964e31c0cdfa6db685c5a189a0702643cd95
7
- data.tar.gz: d4eeede3205907fed91e63293c867bf1ee7f758feb000564e50c4524fe683645f602ff38ccdba5cedc5044023d30e6f342fe90ffe763a4a955e45959b52b5550
6
+ metadata.gz: fac781566dc456a832bb6834a5f928f200a943e5eece7ee821b2fb8c0fd76f93c6355c361db9d81d3fffa15964e55ac60d503e458621d8f41d10373294044aec
7
+ data.tar.gz: 2f9646f64a4a0b5d9ff49c18e3936a6527afa370a354465c4159636c02e4a8911a37a66a6a40456f7f0809d6b13c52bb3df3d17c104e4d036489e6072f7f6992
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mastermind_ruby (0.0.3)
4
+ mastermind_ruby (0.0.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Rakefile CHANGED
@@ -24,3 +24,7 @@ end
24
24
  task :run do
25
25
  trap(0) { system "exe/mastermind" }
26
26
  end
27
+
28
+ task :console do
29
+ trap(0) { system "irb -r'mastermind_ruby'" }
30
+ end
@@ -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 4.times.map { AVAILABLE_CHARACTERS.sample }
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
- @solution ||= Code.parse('BBBB')
22
+ def self.solution(length)
23
+ new length.times.map{ 'B' }
25
24
  end
26
25
 
27
- # Checks if a code is valid (must be 4 characters long and can only contain the available characters)
26
+ # Returns if a code is valid (matches the code pattern (R, Y, G, O, M, P))
28
27
  def valid?
29
- @code.join =~ CODE_PATTERN
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
@@ -2,7 +2,7 @@ module MastermindRuby
2
2
  class Game
3
3
  attr_reader :try_count
4
4
 
5
- def initialize(ui, solution: Code.random)
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 for the playername on the UI
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.solution?
45
+ if result_solution?(result)
32
46
  stop
33
47
  else
34
48
  @try_count += 1
@@ -1,3 +1,3 @@
1
1
  module MastermindRuby
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mastermind_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yves Siegrist