-1

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?

Community
  • 1
  • 1
user12205
  • 2,684
  • 1
  • 20
  • 40
  • It is waiting, because you keep writing `raw_input`; this will wait for input – jonrsharpe Apr 07 '14 at 21:51
  • @jonrsharpe I'm trying to implement a way such that the output of game.py will be piped into solver.py, and then the output of solver.py will be piped to game.py - that is, the `raw_input()` in solver.py should accept the input from the output of game.py – user12205 Apr 07 '14 at 22:08
  • Are you determined to run this over the pipe? It might be easier to set it up as a pure Python script, then adapt it to command line use later. – jonrsharpe Apr 08 '14 at 10:10
  • @jonsharpe I do not know how to do this in a pure python script, which is why I am resolving to pipes. Also, since this will be used in a language agnostic contest, the redirection must be done either on the game side or the shell, but not the solver side. Besides, I have finally solved it. It would be nice if you can give me some pointers on how to do it in pure python though, just so I can understand this language better. :) – user12205 Apr 08 '14 at 10:22

1 Answers1

0

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

user12205
  • 2,684
  • 1
  • 20
  • 40