Question

This question on the codegolf.SE sandbox is about writing hangman solvers. I want to write a main "game" program that outputs underscores, and takes single alphabets as input; and another program, the "solver", will take the underscores as input and outputs the letters.

I have no previous knowledge on how to do this. I tried the method described here and here, but still no luck.

My sample game program in python is as follows:

import sys
s="HANGMAN"
s2=[]
for i in s:
    s2.append('_')
err=0
print ''.join(s2)
sys.stderr.write('debug _______\n')
while err<6 and '_' in s2:
    c=raw_input()
    nomatch=True
    for i in range(0, len(s)):
        if s[i]==c:
            s2[i]=c
            nomatch=False
    if nomatch:
        err+=1
    print ''.join(s2)

And my sample solver program is as follows:

import sys
raw_input()
sys.stderr.write('debug H\n')
print 'H'
raw_input()
print 'A'
raw_input()
print 'N'
raw_input()
print 'G'
raw_input()
print 'M'
raw_input()

Then in my terminal I tried the following:

mkfifo fifo
python game.py <fifo | python solver.py >fifo

And

coproc python game.py
python solver.py <&${COPROC[0]} >&${COPROC[1]}

And

{ python game.py | python solver.py; } >/dev/fd/0

All of these give only the first debug message from game.py, and then seems like both programs are waiting for input. What did I do wrong and how do I fix this?

Was it helpful?

Solution

I managed to solve this using the mkfifo fifo method by adding sys.stdout.flush() after every print statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top