mastermind-game 0.0.1 → 0.0.2

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: b6d5bc9c19b85777bb6a3b1a77892337bfb0b75c
4
- data.tar.gz: 8e6dede81fcfd1347048900504939e8116fd7ad9
3
+ metadata.gz: 2d8f958732f5aa1360b35da4a723f4cc61e7bb0a
4
+ data.tar.gz: 2c28c828f418f0d2c31717af3886cd9d42a80f6e
5
5
  SHA512:
6
- metadata.gz: 3dbaf6735bc410f513e8fdf125ae5d84ddf171cb146c0e885d32778a238a2490f13b1212dcfa213184ecfe3413f67b9b52cb84364f9c6d1d3bc15f1f1d458133
7
- data.tar.gz: c237db2c005d129d476a5a754ed67c8a7f5eef43a16aee3a154cd3ea99e5973c51b108446d8ee3b01009e75aba2894e8359d69ced94ade5b938a1f638328f827
6
+ metadata.gz: 339ca186466779c1f7cfb01f742225c4c5099c881436721b79d9c27ad3f59143cfc2069dfdb12dcaab8561b05f5e5ef56ba56e825fcf2fd26b40f44de1120c86
7
+ data.tar.gz: 3b48e687f214684a6bfce3bb287b1f93078cb67e8021b3c641c755cadcbc1a1e99ffa590bea36d67648519d2b448dfad1eb401b813eac99acc0fed065640fd52
data/README.md CHANGED
@@ -34,10 +34,11 @@ The `Mastermind::Game` object is the primary point of interaction. Instances of
34
34
 
35
35
  ```ruby
36
36
  # Instantiate a game with a random secret
37
- game = Mastermind::Game.new
37
+ # :codemaker and :codebreaker are optional and can be any object
38
+ game = Mastermind::Game.new(codemaker: ..., codebreaker: ...)
38
39
 
39
40
  # Prepare a guess
40
- guess = [:red, :red, :red, :red]
41
+ guess = Mastermind::Game::Code.from([:red, :red, :red, :red])
41
42
  game.guess(guess)
42
43
 
43
44
  # Find how many turns there have been
@@ -46,7 +47,8 @@ game.attempts # => 1
46
47
  # Check if the game is over
47
48
  game.over? # => false
48
49
 
49
- # Determine who has won (returns a Player instance)
50
+ # Determine who has won
51
+ # Returns the codemaker, the codebreaker, or nil
50
52
  game.winner # => nil
51
53
  ```
52
54
 
@@ -3,10 +3,11 @@ module Mastermind
3
3
  class Controller
4
4
  def setup
5
5
  puts View.introduction
6
- print "How many players will there be? "
6
+ print "How many human players will there be? "
7
7
  number_of_players = Player::Human.get_input.to_i
8
8
 
9
- @player1 = get_player(1)
9
+ @player1 = get_player(1) if number_of_players > 0
10
+ @player1 ||= Player::Computer.new(name: "Computer (1)")
10
11
  @player2 = get_player(2) if number_of_players > 1
11
12
  @player2 ||= Player::Computer.new(name: "Computer")
12
13
  end
@@ -39,7 +40,7 @@ module Mastermind
39
40
 
40
41
  def get_player(number)
41
42
  print "What is Player #{number}'s name? "
42
- Player::Human.new(name: Player::Human.get_input)
43
+ Player::Human.new(name: gets.chomp)
43
44
  end
44
45
 
45
46
  def get_codebreaker
@@ -42,7 +42,7 @@ module Mastermind
42
42
 
43
43
  def self.attempt_line(turn, width: 4)
44
44
  "#{turn.number}:".ljust(4) +
45
- SIDE + guess_bar(turn.guess.sequence) + BLANK * (width - turn.guess.sequence.length) + SIDE +
45
+ SIDE + guess_bar(turn.guess.sequence) + BLANK * (width - turn.guess.length) + SIDE +
46
46
  feedback_line(exact: turn.exact, partial: turn.partial, width: width) + SIDE
47
47
  end
48
48
 
@@ -3,7 +3,7 @@ module Mastermind
3
3
  attr_reader :turns, :codemaker, :codebreaker, :max_attempts
4
4
 
5
5
  def initialize(secret: nil, codemaker: nil, codebreaker: nil)
6
- @secret = (secret && Code.from(secret)) || Code.random
6
+ @secret = secret || Code.random
7
7
  @turns = []
8
8
  @codemaker = codemaker || Player.new(name: "AbstractCodemaker")
9
9
  @codebreaker = codebreaker || Player.new(name: "AbstractCodebreaker")
@@ -18,8 +18,7 @@ module Mastermind
18
18
  @secret.length
19
19
  end
20
20
 
21
- def guess(guess_sequence)
22
- code = Code.from(guess_sequence)
21
+ def guess(code)
23
22
  @turns << Turn.new(
24
23
  guess: code, number: attempts + 1,
25
24
  exact: @secret.exact_matches_with(code),
@@ -6,7 +6,7 @@ module Mastermind
6
6
  attr_reader :color
7
7
 
8
8
  def initialize(color: COLORS.sample)
9
- raise ArgumentError.new("Invalid color.") unless COLORS.include?(color)
9
+ raise ArgumentError.new("Invalid color: #{color}.") unless COLORS.include?(color)
10
10
  @color = color
11
11
  end
12
12
 
@@ -5,8 +5,10 @@ module Mastermind
5
5
  def initialize(game)
6
6
  @game = game
7
7
  # 1. Create the set S of 1296 possible codes
8
- @possible_guesses = Game::Piece::COLORS.repeated_permutation(game.secret_length).to_a
9
- @set = Game::Piece::COLORS.repeated_permutation(game.secret_length).to_a
8
+ @possible_guesses = Game::Piece::COLORS.repeated_permutation(game.secret_length).to_a.map do |permutation|
9
+ Game::Code.from(permutation)
10
+ end
11
+ @set = @possible_guesses.dup
10
12
  @game.turns.each { |turn| prune(turn) }
11
13
  end
12
14
 
@@ -20,10 +22,11 @@ module Mastermind
20
22
  # 2. Make initial guess of 1122
21
23
  def first_guess
22
24
  first_color = Game::Piece::COLORS.sample
23
- second_color = Game::Piece::COLORS.select { |color| color != first_color }.sample
24
- Array.new(@game.secret_length) do |position|
25
+ second_color = Game::Piece::COLORS.reject { |color| color == first_color }.sample
26
+ sequence = Array.new(@game.secret_length) do |position|
25
27
  position < @game.secret_length / 2 ? first_color : second_color
26
28
  end
29
+ Game::Code.from(sequence)
27
30
  end
28
31
 
29
32
  # 5. Choose from the set of guesses with the maximum score, preferring members of S
@@ -36,21 +39,20 @@ module Mastermind
36
39
 
37
40
  # 3. Remove from S any code that would not give the same response if the guess were the code.
38
41
  def prune(turn)
39
- @set.select! do |combination|
40
- code = Game::Code.from(combination)
42
+ @set.select! do |code|
41
43
  !(turn.guess == code) &&
42
44
  turn.exact == code.exact_matches_with(turn.guess) &&
43
45
  turn.partial == code.partial_matches_with(turn.guess)
44
46
  end
45
47
 
46
- @possible_guesses.delete(turn.guess.sequence.map(&:color))
48
+ @possible_guesses.reject! { |guess| turn.guess == guess }
47
49
  end
48
50
 
49
51
  # 4. For each possible guess, find the minimum highest match count
50
52
  def minimum_match_count
51
53
  lowest = @set.length
52
54
  @possible_guesses.each do |possible|
53
- count = highest_match_count(Game::Code.from(possible))
55
+ count = highest_match_count(possible)
54
56
  lowest = count if count < lowest
55
57
  end
56
58
  lowest
@@ -62,7 +64,7 @@ module Mastermind
62
64
  (0..@game.secret_length).each do |matches|
63
65
  # count how many possibilities in S would be retained
64
66
  count = @set.count do |combination|
65
- Game::Code.from(combination).color_matches_with(guess) == matches
67
+ combination.color_matches_with(guess) == matches
66
68
  end
67
69
  # track the highest of these
68
70
  highest = count if count > highest
@@ -74,7 +76,7 @@ module Mastermind
74
76
  def maximum_scoring_guesses
75
77
  min_matches = minimum_match_count
76
78
  @possible_guesses.select do |possible|
77
- highest_match_count(Game::Code.from(possible)) == min_matches
79
+ highest_match_count(possible) == min_matches
78
80
  end
79
81
  end
80
82
  end
@@ -7,7 +7,7 @@ module Mastermind
7
7
  end
8
8
 
9
9
  def get_code(length:)
10
- Array.new(length) { Game::Piece::COLORS.sample }
10
+ Game::Code.random(length)
11
11
  end
12
12
  end
13
13
  end
@@ -11,7 +11,7 @@ module Mastermind
11
11
  sequence << color
12
12
  end
13
13
 
14
- sequence
14
+ Game::Code.from(sequence)
15
15
  end
16
16
 
17
17
  def get_guess_for(game)
@@ -1,3 +1,3 @@
1
1
  module Mastermind
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mastermind-game
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andur Carr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-28 00:00:00.000000000 Z
11
+ date: 2017-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  version: '0'
117
117
  requirements: []
118
118
  rubyforge_project:
119
- rubygems_version: 2.5.1
119
+ rubygems_version: 2.6.10
120
120
  signing_key:
121
121
  specification_version: 4
122
122
  summary: An API for creating the game "Mastermind".