A player guesses letters that are part of a word!
If he has not guessed the complete word before making six (6) mistakes, he has lost!
_______
|/ |
| (_)
| \|/ It is a game over!
| |
| / \
|
___|___
⚫ Your Challenge
In a language of your choice, write a program that takes as its input (in some way) the target word (with blanks) and the letters that have been guessed already. It should then output the next letter to be guessed.
For example:
$ echo -n "p...s\npds" | ./guesser
e
You may assume the word to be guessed is an English word and contains only lowercaseASCIIletters.
Provide code and instructions if usage is not obvious. Consider using a tripcode to ease conversation about your entry.
Entries will be judged on GOODITUDE (defined as its win/loss ratio over twenty random games), on SPEED, and on CLEVERNESS.
Your deadline is 23:59:59 on Sunday, May 20th. Results will be posted the following day.
>>45
Modern Operating Systems, 3rd edition - Andrew S. Tanenbaum
By reading it, you will learn to hate him. In part due to his pandering to the Uni money-grubbing book market, by inflating every sentence to fill a fucking page; mostly however, because he's stuck in the '90s and refuses to acknowledge that a micro kernel isn't THE ONE TRUE WAY IN 100% OF CASES
Name:
Anonymous2012-05-16 17:19
>>46
actually it is, it's just that he doesn't realizes that C is fucking shit.
$ echo -e "kangar..\nargnk" | ./hangman
hangman: Prelude.maximum: empty list
$ echo -e "p.ncre.s\nepsrcn" | ./hangman
hangman: Prelude.maximum: empty list
May just be the fact that I'm using GHC 6.12.1, though. I don't know enough Haskell anymore to tell.
Name:
Anonymous2012-05-16 20:03
>>37,40
You really need to take guessed letters into account. If the word isn't in your word list, it will guess wrong letters forever instead of eventually working its way to right ones.
>>49
It's a beginner's challenge. Be nice or constructive.
Name:
Anonymous2012-05-17 0:50
>>51
b-b-b-b-bbbbut how can we know what letters were already guessed if we only get the guess as input? >>49
ss-s-s-sorrrry anon-kun, i will do better next time!
Name:
Anonymous2012-05-17 1:10
>>53
OH NO
I DIDN'T READ
ahhhhh
sorry anon-kun. im slow. forgive me!
hangman: freqlist: openFile: does not exist (No such file or directory)
Name:
Anonymous2012-05-17 17:35
>>56
I only know what GHC 7.0 complains about. I could imagine a readFile function that would just return empty if the file didn't exist. But yes, I did write it with GHC 7.0.4 and I honestly don't know enough about haskell to know how to fix it.
Name:
Anonymous2012-05-17 18:01
>>57
I upgraded to GHC 7.4.1 (the most recent version), and it's still doing the same thing as in >>50. Are you sure it works for you?
Name:
Anonymous2012-05-17 18:49
>>58
Just copied it from here and pastebin and it works fine. $ echo -e "p.ncre.s\nepsrcn" | ./hangman
a
Maybe the freqlist file is improperly formatted on your end?
The first non-empty non-comment line is supposed to be the letters a-z with spaces. Followed by a line of 26 fp numbers, followed by a lot of lines like this: a 0.0002835 0.0228302 0.0369041 0.0426290 0.0012216 0.0075739 0.0171385 0.0014659 0.0372661 0.0002353 0.0110124 0.0778259 0.0260757 0.2145354 0.0005459 0.0195213 0.0001749 0.1104770 0.0934290 0.1317960 0.0098029 0.0306574 0.0088799 0.0009562 0.0233701 0.0018701
where the first column is the prefix
Name:
Anonymous2012-05-17 18:53
>>59
I removed the commented and empty lines at the start of the freqlist file, and now it seems to work. I don't know why that should matter if it doesn't on your end, but I guess the important thing is that it works now.
Name:
Anonymous2012-05-17 19:43
>>60
You know what, I bet you're on OSX. I just tried with classic Mac line endings (ie. '\r') and Windows (ie. "\r\n") and got the same error. It's probably getting the '\r' in each line and misinterpreting it as a key character.
Name:
Anonymous2012-05-17 20:01
>>61
Nope, Linux here.
I did wget the file rather than copy/paste it, so I thought I might have gotten bad line endings that way because that's happened before, but apparently not.
new entry. anon-kun, is this suitable?
import sys
validwords = filter(lambda tuplist : len(filter(lambda tup : tup[0] == '.' or tup[0] == tup[1], tuplist)) == len(tuplist), map(lambda word : zip(sys.argv[1], word), filter(lambda word : len(word) == len(sys.argv[1]), map(lambda word : word.strip(), open("words.txt", "r")))))
letters = {}
for word in validwords:
used = []
for guess, letter in word:
if letter in letters:
letters[letter] += 1
else:
letters[letter] = 1
if(len(sys.argv) == 3):
letters = dict((k, v) for k,v in letters.iteritems() if k not in sys.argv[2])
chosen = max(letters.iterkeys(), key=lambda k : letters[k] )
print chosen
sytnax - python <script name> <guess> <guessed letters in a list ie abcde>
use . to indicate unguessed letters
also, this fails if the word isn't in the word list
word list format is just 1 word per line in "words.txt" in the same directory as the script
Name:
Anonymous2012-05-19 16:37
and i forgot to remove used = [] :(((((((((
crap, anon-kun, i always screw up somehow
import sys
validwords = filter(lambda tuplist : len(filter(lambda tup : tup[0] == '.' or tup[0] == tup[1], tuplist)) == len(tuplist), map(lambda word : zip(sys.argv[1], word), filter(lambda word : len(word) == len(sys.argv[1]), map(lambda word : word.strip(), open("words.txt", "r")))))
letters = {}
for word in validwords:
for guess, letter in word:
if letter in letters:
letters[letter] += 1
else:
letters[letter] = 1
if(len(sys.argv) == 3):
letters = dict((k, v) for k,v in letters.iteritems() if k not in sys.argv[2])
chosen = max(letters.iterkeys(), key=lambda k : letters[k] )
print chosen
Name:
Anonymous2012-05-19 21:55
Against random words the best strategy is to maximize your expected chance of winning, but because the game tree grows exponentially you can't brute force every possibility and have to use a heuristic. ``Most used character'' is a very good heuristic, and ``most entropy in result for this turn'' is also pretty good but harder to compute. How many of the top heuristic choices to examine is a trade-off, but because of diminishing returns I like 2. Perhaps a dynamic choice would be best.
On another note, don't forget to filter out words that have characters that have already been guessed and found not be in the word.
#lang racket/base
;; Usage: racket -t hangman.rkt [word misses]
;; Input is taken from stdin if not given on command line
(define Hang 0)
(define Len 1)
(define File "hangman.txt")
(define Char 0)
(define Handle
(open-input-file File))
(define Word
(read-line Handle)); For time reasons i put one word in here :/
; Thought of making it read lines in a loop and the test being a random number of some sort with the last read line being the word
(while (and (if (= Hang 6)
(begin (display "LOST!\n") ; I wish i had time to make it draw a hanging man but i dont have it
#f))
(not (= (string-length Word)
Len)))
(set! Char
(read-char (current-input-port)))
(if (not (char=? Char #\nl)) ; Filter #\nl(\n) read when pressing enter
(if (not (char=? Char (string-ref Word
Len)))
(set! Hang
(+ 1 Hang))
(begin (set! Len
(+ 1 Len))
(display Char))))
A very simple and primitive implementation
Im made this sort of in a real rush, but i send it in anyway.
I wish i had the time to improve it.
Dont be hard on me since this is my first real scheme program.
It should work, i couldnt test i as much as i should.
Im glad OP took the time to make a programming contest, i missed those.
Name:
Anonymous2012-05-20 20:10
>>79
I hate to break this to you, VIPPER-kun, but that wasn't what was asked.