Hangman solver

Did this as a programing challenge on macshadows.com/forums


set que to "Yes"
repeat while que is not "No"
	set str to text returned of (display dialog "Enter string (_ for unknown character):" default answer "blah")
	set newt to "[^"
	repeat with i from 1 to number of characters in str
		if item i of str is not equal to "_" then
			set newt to newt & character i of str & ","
		end if
	end repeat
	set newt to newt & "]"
	set nstr to (do shell script "echo \"" & str & "\" | sed s/_/" & newt & "/g")
	display dialog "==possible answers==
" & (do shell script "cat /usr/share/dict/words | grep -i -x \"" & nstr & "\"")
	set que to button returned of (display dialog "Have any more?" buttons {"Yes", "No"})
end repeat

Pretty ghetto totally open for suggestions

And how are we supposed to use it?

It pops up and the dialog tells you how
dialog:
enter word(_ for missing letters):
[text thingy]

then it spit out a list of words it could be.

I like this. Its good as a crossword/Scrabble helper (cheater if you’re a purist) too.

I’d reduce the number of dialogs - add a continue button to “==possible answers==” for example.

j

EDIT:
no significant changes but…

set que to "Next Word"
repeat while que is "Next Word"
	set str to text returned of (display dialog "Enter string." & return & return & "Use underscores ( _ ) for unknown characters." default answer "h_ngm_n")
	set newt to "[^"
	repeat with i from 1 to number of characters in str
		if item i of str is not equal to "_" then
			set newt to newt & character i of str & ","
		end if
	end repeat
	set newt to newt & "]"
	set nstr to (do shell script "echo \"" & str & "\" | sed s/_/" & newt & "/g")
	
	set que to button returned of (display alert "Possible Answers:" message (do shell script "cat /usr/share/dict/words | grep -i -x \"" & nstr & "\"") buttons {"Next Word", "Done"})
		
end repeat

I like this, Nice one,

One thing I found was if there are Lots and Lots of words the dialog is too long for the screen.
So I changed it to a list view. this gives you a scroll bar.

set que to "Next Word"
repeat while que is "Next Word"
	set str to text returned of (display dialog "Enter string." & return & return & "Use underscores ( _ ) for unknown characters." default answer "h_ngm_n")
	set newt to "[^"
	repeat with i from 1 to number of characters in str
		if item i of str is not equal to "_" then
			set newt to newt & character i of str & ","
		end if
	end repeat
	set newt to newt & "]"
	set nstr to (do shell script "echo \"" & str & "\" | sed s/_/" & newt & "/g")
	
	set nchex to (do shell script "cat /usr/share/dict/words | grep -i -x \"" & nstr & "\"")
	set nstr to words in nchex as list
	set que to choose from list nstr with title "Hang Man:" with prompt "Possible Answers" OK button name "Next Word" cancel button name "Done" with empty selection allowed

	
end repeat

Good idea. I missed that.

Thanks, only thing just found I broke the repeat.
Trying to figure that one out, bit odd??

That is odd. I hadn’t noticed it. I was looking at error checking - if it’s used for a crossword and an intersecting word is wrong, there may be no possible answers.

Not so odd when I thought about it - the “Next Word” button returns an empty list.


set que to {}
repeat while que is {}
	set str to text returned of (display dialog "Enter string." & return & return & "Use underscores ( _ ) for unknown characters." default answer "h_ngm_n")
	set newt to "[^"
	repeat with i from 1 to number of characters in str
		if item i of str is not equal to "_" then
			set newt to newt & character i of str & ","
		end if
	end repeat
	set newt to newt & "]"
	set nstr to (do shell script "echo \"" & str & "\" | sed s/_/" & newt & "/g")
	
	set nchex to (do shell script "cat /usr/share/dict/words | grep -i -x \"" & nstr & "\"")
	set nstr to words in nchex as list
	set que to choose from list nstr with title "Hang Man:" with prompt "Possible Answers" OK button name "Next Word" cancel button name "Done" with empty selection allowed
	que
	
	
end repeat

Nice one, that was doing my head in, :rolleyes:

Ya, wow thanks for adding all that to it :slight_smile: i didnt implement any error checking or anything i know it was just a quick hacked together thing for a programing challenge but thanks for all the feed back :smiley:

This checks for typos/misspellings:


set que to {}

repeat while que is {}
	set str to text returned of (display dialog "Enter string." & return & return & "Use underscores ( _ ) for unknown characters." default answer "h_ngm_n")
	set newt to "[^"
	
	repeat with i from 1 to number of characters in str
		if item i of str is not equal to "_" then
			set newt to newt & character i of str & ","
		end if
	end repeat
	
	set newt to newt & "]"
	set nstr to (do shell script "echo \"" & str & "\" | sed s/_/" & newt & "/g")
	
	try
		set nchex to (do shell script "cat /usr/share/dict/words | grep -i -x \"" & nstr & "\"")
		set nstr to words in nchex as list
	end try
	
	if nstr contains "[" then
		if button returned of (display alert "Error - there are no possible answers." message "Check for typos and try again?" buttons {"Cancel", "Try Again"}) is "Cancel" then
			exit repeat
		end if
	else
		set que to choose from list nstr with title "Hang Man:" with prompt "Possible Answers" OK button name "Next Word" cancel button name "Done" with empty selection allowed
	end if
end repeat

Edited to correct the spelling of "misspelling." :rolleyes:

Hope none of you mind i posted the original with out all of your very helpful add ons on my site, i’m gonna post it again and i will mention that you guys added some stuff to make it better. Once again thanks for all the improvements :smiley: .

Here is yet another change:

-- Preferences:
property blankChar : "_"
property wordsPath : "/usr/share/dict/words"
-- Initial pattern:
property patternInput : {"h", "ngm", "n"}

repeat
	-- Note: patternInput is coerced from a list every time in the event of blankChar being given the ability to be changed during execution.
	set text item delimiters to blankChar
	set patternInput to text items of text returned of (display dialog "Enter incomplete word. " & return & "Use " & blankChar & " for unknown letters." default answer (patternInput as Unicode text))
	set text item delimiters to ""
	set text item delimiters to "[^" & (patternInput as Unicode text) & "]"
	set patternExpr to patternInput as Unicode text
	set text item delimiters to ""
	
	try
		set matchedWords to paragraphs of (do shell script "/usr/bin/grep -xi " & quoted form of patternExpr & space & wordsPath)
		if (choose from list matchedWords with prompt "Matched Words:" OK button name "Again" with empty selection allowed) is false then exit repeat
	on error
		display dialog "No words found matching " & quoted form of patternInput & "." & return & return & "Try Again?"
	end try
end repeat

And yet another version: :slight_smile:

set que to missing value
repeat until (que is false)
	set str to text returned of (display dialog "Enter string." & return & return & "Use underscores ( _ ) for unknown characters." default answer "h_ngm_n")
	
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "_"
	set knownBits to str's text items
	set AppleScript's text item delimiters to ""
	set knownChrs to characters of (knownBits as Unicode text)
	set AppleScript's text item delimiters to ","
	set AppleScript's text item delimiters to ("[^" as Unicode text) & knownChrs & "]"
	set nstr to knownBits as Unicode text
	set astid to AppleScript's text item delimiters
	
	try
		set nchex to (do shell script "cat /usr/share/dict/words | grep -i -x \"" & nstr & "\"")
		set nstr to nchex's words
	end try
	if (nstr contains "[") then
		display alert "Error - there are no possible answers." message "Check for typos and try again?" buttons {"Cancel", "Try Again"} cancel button 1 default button 2
	else
		set que to (choose from list nstr with title "Hang Man:" with prompt "Possible Answers" OK button name "Next Word" cancel button name "Done" with empty selection allowed)
	end if
end repeat