hangman 0.0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hangman.rb +2 -63
- data/lib/hangman/clue.rb +63 -0
- data/lib/hangman/game.rb +48 -0
- metadata +5 -3
data/lib/hangman.rb
CHANGED
@@ -1,63 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# A list of letters in the order you should try them
|
4
|
-
# Note that the order may change based on the next letter so the
|
5
|
-
# only signinficant item, in ths list is the first one
|
6
|
-
attr_reader :guesses
|
7
|
-
# A list of words that could be the solution
|
8
|
-
attr_reader :possible_words
|
9
|
-
|
10
|
-
# Create a new clue
|
11
|
-
# the clue format is a string with underscores for unknown letters
|
12
|
-
# Example: "he__o"
|
13
|
-
# The wrong letters is a list of letters that were guessed but aren't correct
|
14
|
-
def initialize(clue, wrong_letters)
|
15
|
-
@clue = clue.to_s.downcase
|
16
|
-
@wrong_letters = wrong_letters
|
17
|
-
@letter_counts = {}
|
18
|
-
@possible_words = []
|
19
|
-
@used_letters = @clue.split(//).reject { |c| c == "_" }
|
20
|
-
end
|
21
|
-
|
22
|
-
# Pass a list of words to this method to come up wth the best next guess
|
23
|
-
# It will return true if there are any potential guesses
|
24
|
-
# You can call this method multiple times on a single Clue (if you have multiple wordlists this may be helpful)
|
25
|
-
def solve(words)
|
26
|
-
length = @clue.length
|
27
|
-
|
28
|
-
words.each do |w|
|
29
|
-
w = w.chomp.downcase
|
30
|
-
next if w.size != length
|
31
|
-
next if @wrong_letters.any? { |c| w.include? c }
|
32
|
-
|
33
|
-
if match_word w
|
34
|
-
@possible_words << w
|
35
|
-
end
|
36
|
-
end
|
37
|
-
@guesses = @letter_counts.sort{ |a,b| b[1] <=> a[1] }.map{ |e| e[0] }
|
38
|
-
|
39
|
-
return !@guesses.empty?
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def match_word(w)
|
45
|
-
wcount = {}
|
46
|
-
@clue.each_char.with_index do |c, i|
|
47
|
-
if c == '_'
|
48
|
-
if @used_letters.include? w[i]
|
49
|
-
return false
|
50
|
-
end
|
51
|
-
wcount[w[i]] = 1 # equals instead of += because we want unique per word
|
52
|
-
elsif w[i] != c
|
53
|
-
return false
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
@letter_counts.merge! wcount do |k, o, n|
|
58
|
-
o + n
|
59
|
-
end
|
60
|
-
true
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
1
|
+
require_relative 'hangman/clue'
|
2
|
+
require_relative 'hangman/game'
|
data/lib/hangman/clue.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
module Hangman
|
2
|
+
class Clue
|
3
|
+
# A list of letters in the order you should try them
|
4
|
+
# Note that the order may change based on the next letter so the
|
5
|
+
# only signinficant item, in ths list is the first one
|
6
|
+
attr_reader :guesses
|
7
|
+
# A list of words that could be the solution
|
8
|
+
attr_reader :possible_words
|
9
|
+
|
10
|
+
# Create a new clue
|
11
|
+
# the clue format is a string with underscores for unknown letters
|
12
|
+
# Example: "he__o"
|
13
|
+
# The wrong letters is a list of letters that were guessed but aren't correct
|
14
|
+
def initialize(clue, wrong_letters)
|
15
|
+
@clue = clue.to_s.downcase
|
16
|
+
@wrong_letters = wrong_letters
|
17
|
+
@letter_counts = {}
|
18
|
+
@possible_words = []
|
19
|
+
@used_letters = @clue.split(//).reject { |c| c == "_" }
|
20
|
+
end
|
21
|
+
|
22
|
+
# Pass a list of words to this method to come up wth the best next guess
|
23
|
+
# It will return true if there are any potential guesses
|
24
|
+
# You can call this method multiple times on a single Clue (if you have multiple wordlists this may be helpful)
|
25
|
+
def solve(words)
|
26
|
+
length = @clue.length
|
27
|
+
|
28
|
+
words.each do |w|
|
29
|
+
w = w.chomp.downcase
|
30
|
+
next if w.size != length
|
31
|
+
next if @wrong_letters.any? { |c| w.include? c }
|
32
|
+
|
33
|
+
if match_word w
|
34
|
+
@possible_words << w
|
35
|
+
end
|
36
|
+
end
|
37
|
+
@guesses = @letter_counts.sort{ |a,b| b[1] <=> a[1] }.map{ |e| e[0] }
|
38
|
+
|
39
|
+
return !@guesses.empty?
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def match_word(w)
|
45
|
+
wcount = {}
|
46
|
+
@clue.each_char.with_index do |c, i|
|
47
|
+
if c == '_'
|
48
|
+
if @used_letters.include? w[i]
|
49
|
+
return false
|
50
|
+
end
|
51
|
+
wcount[w[i]] = 1 # equals instead of += because we want unique per word
|
52
|
+
elsif w[i] != c
|
53
|
+
return false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
@letter_counts.merge! wcount do |k, o, n|
|
58
|
+
o + n
|
59
|
+
end
|
60
|
+
true
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/hangman/game.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module Hangman
|
2
|
+
class Game
|
3
|
+
# The word to guess
|
4
|
+
attr_reader :word
|
5
|
+
# A list of wrong characters guessed
|
6
|
+
attr_reader :wrong
|
7
|
+
# A list of correct characters guessed
|
8
|
+
attr_reader :correct
|
9
|
+
# The number of incorrect guesses
|
10
|
+
attr_reader :wrong_guesses
|
11
|
+
|
12
|
+
# Set the word this puzzle wil be for
|
13
|
+
def initialize(word)
|
14
|
+
@word = word
|
15
|
+
@wrong = []
|
16
|
+
@correct = []
|
17
|
+
@wrong_guesses = 0
|
18
|
+
end
|
19
|
+
|
20
|
+
# Guess a letter for the puzzle
|
21
|
+
def guess(c)
|
22
|
+
c = c.to_s.chr.upcase
|
23
|
+
if @word.upcase.include? c
|
24
|
+
if not @correct.include? c
|
25
|
+
@correct << c
|
26
|
+
end
|
27
|
+
true
|
28
|
+
else
|
29
|
+
if not @wrong.include? c
|
30
|
+
@wrong << c
|
31
|
+
@wrong_guesses += 1
|
32
|
+
end
|
33
|
+
false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Whether or not the puzzle is completed
|
38
|
+
def solved?
|
39
|
+
@word.upcase.each_char.all? { |c| @correct.include? c }
|
40
|
+
end
|
41
|
+
|
42
|
+
# The word with only the correct guesses given. Others will be shown as '_'
|
43
|
+
def to_s
|
44
|
+
# There is probably a better way to do this...
|
45
|
+
@word.upcase.each_char.map { |c| @correct.include?(c) ? c : '_' }.join
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
- 0
|
8
7
|
- 1
|
9
|
-
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ben Olive
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-27 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -28,6 +28,8 @@ extra_rdoc_files: []
|
|
28
28
|
|
29
29
|
files:
|
30
30
|
- bin/hangman
|
31
|
+
- lib/hangman/clue.rb
|
32
|
+
- lib/hangman/game.rb
|
31
33
|
- lib/hangman.rb
|
32
34
|
has_rdoc: true
|
33
35
|
homepage: http://github.com/sionide21/Hangman
|