hangman 0.0.1
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.
- data/bin/hangman +31 -0
- data/lib/hangman.rb +63 -0
- metadata +66 -0
data/bin/hangman
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'hangman'
|
4
|
+
|
5
|
+
# This works on OSX and Ubuntu
|
6
|
+
# TODO add option to specify
|
7
|
+
wordfile = "/usr/share/dict/words"
|
8
|
+
|
9
|
+
if ARGV.size != 1 and ARGV.size != 2
|
10
|
+
$stderr.puts "Usage: #{$0} <clue> [wrong letters]"
|
11
|
+
$stderr.puts "\t Where clue is the clue with unknown letters replaced by _ (example: pa__word)"
|
12
|
+
exit -1
|
13
|
+
end
|
14
|
+
|
15
|
+
guess = ARGV[0]
|
16
|
+
exc = []
|
17
|
+
exc = ARGV[1].split // if ARGV.size == 2
|
18
|
+
|
19
|
+
puzzle = Hangman::Clue.new guess, exc
|
20
|
+
|
21
|
+
File.open wordfile, 'r' do |file|
|
22
|
+
if puzzle.solve file.each_line
|
23
|
+
puts "#{puzzle.possible_words.size} potential words"
|
24
|
+
if puzzle.possible_words.size < 10
|
25
|
+
puts puzzle.possible_words
|
26
|
+
end
|
27
|
+
puts "Guess: #{puzzle.guesses.first}"
|
28
|
+
else
|
29
|
+
puts "No guesses!"
|
30
|
+
end
|
31
|
+
end
|
data/lib/hangman.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
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hangman
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Ben Olive
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-04 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: " Comes up with guesses to hangman puzzles.\n"
|
22
|
+
email: ben.olive@gatech.edu
|
23
|
+
executables:
|
24
|
+
- hangman
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- bin/hangman
|
31
|
+
- lib/hangman.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/sionide21/Hangman
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 9
|
49
|
+
version: "1.9"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.3.7
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Solve hangman puzzles
|
65
|
+
test_files: []
|
66
|
+
|