28
11
You are required to write a Hangman solver. Testing against this English word list[1], the solver that solves the most number of words wins, with the number of total incorrect guesses being the tie-breaker. All words in the word list will be tested in random order.
[1]: This word list is taken from here, then the numbers are removed, then words with length 1 or with non-alphabetical characters are removed, then the most frequent 4096 unique words are chosen as this word list.
The details:
Your program will interact with the game program, which will give you through stdin the underscores and correctly guessed letters. Your program will give to stdout your guesses, and it has to infer from the input whether the previous guess was right or wrong. After being wrong for 6 times, your program loses. Your program must be ready for the next game after each game ends (after win or loss).
Your code length must be strictly less than 2048 bytes, and your program must not use any external resources (including but not limited to accessing the wordlist on local storage or from the Internet).
Example: (Input is preceded by > here only for clarification - it is not actually present in the input)
>_______ // 7 underscores
a // Now you wait for input again
>_a___a_
e
>_a___a_ // Implies that your guess is wrong
>_____ // new round, this will be given ONLY IF you already have 6 losses
Suppose you are wrong for 6 times, you will receive a final input which implies your guess is wrong, and your program must be ready to start a new round (i.e. take another input).
If you win,
>_angman
h
>hangman
>_____ // new round
After knowing that you have won (because the input has no underscores), you must be ready to accept the next round.
Your program must terminate when it receives an input END.
If your program is not deterministic (depends on randomness, pseudorandomness, system time, ambient temperature, my mood etc.), you must explicitly state that in your submission, and your score will be taken 10 times (by me, unless otherwise instructed) and averaged.
Note: if you use languages like python, please explicitly flush your stdout after each print statement.
The game program is as follows (credit to nneonneo):
import sys, random, subprocess
proc = subprocess.Popen(sys.argv[1:], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
def p(x):
proc.stdin.write(x+'\n')
proc.stdin.flush()
wordlist=[]
f=open('wordlist.txt', 'r')
for i in f:
wordlist.append(i[:-1] if i[-1]=='\n' else i)
# wordlist=[i[:-1] for i in f]
random.shuffle(wordlist)
score=0
totalerr=0
for s in wordlist:
s2=[]
for i in s:
s2.append('_')
err=0
p(''.join(s2))
while err<6 and '_' in s2:
c=proc.stdout.readline().strip()
nomatch=True
for i in range(0, len(s)):
if s[i]==c:
s2[i]=c
nomatch=False
if nomatch:
err+=1
totalerr+=1
p(''.join(s2))
if err<6:
score+=1
p('END')
sys.stderr.write('score is '+str(score)+', totalerr is '+str(totalerr)+'\n')
Usage: python ./game.py [yoursolverprogram]
Example: python ./game.py ruby ./solver.rb
This should work like the old scoring program, but does not depend on named pipes, so it can work on other platforms. Refer to the revision history if you are interested in the old one.
What runtime environments will you have? Can I use node.js? – aebabis – 2014-04-08T18:29:09.757
If a submission fails a word having guessed no letters, and the next word is the same length, it will be impossible to determine only from the input that six guesses have elapsed. So submissions must store the number of guesses as state. – laindir – 2014-04-08T19:29:57.813
1"If your program is not deterministic (depends on randomness, pseudorandomness, system time, ambient temperature, my mood etc.), you must explicitly state that in your submission, and your score will be taken 10 times (by me, unless otherwise instructed) and averaged." Your own test program is not deterministic, so I think it would only be fair to proceed like that with all submissions? – Martin Ender – 2014-04-08T21:17:23.857
1@ace Not if my submission remembers which words have already been used. ;) I suppose that falls in the category "your program depends on pseudorandomness" ... although it's the pseudorandomness in your test program. :D – Martin Ender – 2014-04-08T21:49:29.143
Please add a trailing newline to your gist (if the gist keeps them), otherwise the python script doesn't work as it strips the
pfromhandicap. – Martin Ender – 2014-04-09T00:27:47.673Oh shoot. Is interaction with the wordlist forbidden? – skibrianski – 2014-04-09T01:07:23.583
@skibrianski well I hope not... I can't see it stated anywhere and I spent the past 4 hours under the assumption it's allowed :D – Martin Ender – 2014-04-09T01:09:57.620
@m.buettner yeah i was going that direction too, until i noticed this bit: "Your code length must be strictly less than 2048 bytes, and your program must not use any external resources." ... which reads to me like no wordlist allowed? – skibrianski – 2014-04-09T01:11:46.483
@skibrianski oh. "external sources" is a good point :-/ – Martin Ender – 2014-04-09T01:13:47.087
@m.buettner i actually find both problems interesting. i think having a good solution to the "you can look at the wordlist" is a necessary first step to the "no peeking" problem. we'll see what ace says – skibrianski – 2014-04-09T02:33:31.910
@m.buettner No. Accessing files is not allowed. – user12205 – 2014-04-09T06:50:20.857
2
@ace: May I suggest using
– nneonneo – 2014-04-12T10:31:38.517subprocessinstead of an external fifo to drive the game? This way the code will work for other OSes (for example, Cygwin on Windows). Here'sgame.pymodified to usesubprocessto start the program named on the command-line: https://gist.github.com/nneonneo/d173f8888e1ea0c6fe37. Use it aspython game.py <program> [args], e.g.python game.py python hangman.py.@nneonneo Cool, didn't know you could do that. (I'm not that much of a Python programmer.) Thanks a lot. – user12205 – 2014-04-12T11:08:49.520
@nneonneo Cool, thanks. I'll give this a go. – tttppp – 2014-04-12T11:48:49.077
I hate it when nothing changes after spending 100 rep as bounty. – user12205 – 2014-04-21T09:32:37.427