mastermind-game 0.0.1 → 0.0.2
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/README.md +5 -3
- data/lib/mastermind/console/controller.rb +4 -3
- data/lib/mastermind/console/view.rb +1 -1
- data/lib/mastermind/game.rb +2 -3
- data/lib/mastermind/game/piece.rb +1 -1
- data/lib/mastermind/knuth.rb +12 -10
- data/lib/mastermind/player/computer.rb +1 -1
- data/lib/mastermind/player/human.rb +1 -1
- data/lib/mastermind/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d8f958732f5aa1360b35da4a723f4cc61e7bb0a
|
4
|
+
data.tar.gz: 2c28c828f418f0d2c31717af3886cd9d42a80f6e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
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:
|
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.
|
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
|
|
data/lib/mastermind/game.rb
CHANGED
@@ -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 =
|
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(
|
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
|
|
data/lib/mastermind/knuth.rb
CHANGED
@@ -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
|
-
|
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.
|
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 |
|
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.
|
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(
|
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
|
-
|
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(
|
79
|
+
highest_match_count(possible) == min_matches
|
78
80
|
end
|
79
81
|
end
|
80
82
|
end
|
data/lib/mastermind/version.rb
CHANGED
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.
|
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:
|
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.
|
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".
|