View previous topic :: View next topic |
Author |
Message |
jgaz n00b

Joined: 14 Feb 2021 Posts: 49
|
Posted: Sat Jul 05, 2025 4:16 pm Post subject: Interacting with a non-terminating program expecting input |
|
|
Okay, this is a weird one I admit...
I got the itch to mess around with BASIC games that are older than I am! Why? I don't know. A lot of these are fairly simple simulation games with some randomized element to them. They're purely text-based, give or take some ASCII art. Besides their simulationist nature, the other thing they have in common is a game loop that requires human input. If you grew up in the US in the 1980's, think Lemonade Stand on the Apple II. If you grew up across the pond during the same period, I think it was Granny's Garden on the Beeb for you.
My goal here is for me to try to map out optimal choices for all sets of conditions within certain BASIC games. I think I'll start with Hamurabi as it's actually kind of interesting. The major variables are population count, acres of land, bushels of grain, and if some disaster was triggered during the previous round. No, this isn't a homework assignment... this is 100% ADHD-driven curiosity, to be perfectly honest. So, normally I'd just use grep, awk or some Perl to extract values and dump that to CSV for further analysis. The problem is, I can't really leverage them with a program that is by design in a loop awaiting input forever.
I very dimly recall dev-tcltk/expect was written to solve this problem. I think I knew some BBS guys who used it to manage their modem pool back in the day. I've read the documentation and I follow a lot of it, but not all of it. I get how I can look for a string, such as a password prompt, and insert the anticipated value. I don't understand how to extract some value and throw that into a CSV, and then make a decision. Also, I don't know TCL so it's probably not an optimal choice. I suspect there is probably now a BASH-y or Perl-ish way to solve this same set of problems. At the risk of sounding dim-witted, I'm not connecting the dots. Can someone please point me in the right direction(s)?
For technical context, I'm compiling these to amd64 executables with guru::dev-lang/fbc. Some compile without issues, a few need a small patch to compile in FreeBASIC's QuickBASIC emulation mode. So, what's the fix? If I can't just use a pipe, how do I capture output? I assume I'll have to do some virtual terminal tricks here for simulated human interactions, what's the best way to do that? I get that once I get this working, I'll have to patch the BASIC code further to allow passing forced combinations of values at runtime for efficiency. That part, at least, I understand. |
|
Back to top |
|
 |
halcon l33t


Joined: 15 Dec 2019 Posts: 686
|
Posted: Sat Jul 05, 2025 11:01 pm Post subject: |
|
|
Hello jqaz,
jgaz wrote: | purely text-based, give or take some ASCII art... a game loop that requires human input... normally I'd just use grep, awk or some Perl to extract values and dump that to CSV for further analysis. The problem is, I can't really leverage them with a program that is by design in a loop awaiting input forever.
I very dimly recall dev-tcltk/expect was written to solve this problem... I suspect there is probably now a BASH-y or Perl-ish way to solve this same set of problems. |
Yes, there is Perl::Expect.
If your task is just to catch the text output the game gives you for a set of values from min to max for some variable, that task can be achieved by a perl script that opens connection to the game process, feeds a value from the set, matches the game output against prepared patterns, saves the extracted value, closes the connection, re-opens a new connection, feeds the next value... in a loop. Matching the output should be performed according patterns, as it is described in Perl::Expect documentation.
Code: | $exp->expect($timeout, @match_patterns); | (or with callbacks, as described)
For one step (the value is typed just after launching the game) it would be straightforward; the more steps - the more code, you'll have to prepare more patterns for matching at each step. _________________ A wife asks her husband, a programmer:
- Could you please go shopping for me and buy one carton of milk, and if they have eggs, get 6?
He comes back with 6 cartons of milk.
- Why did you buy 6 cartons of milk?
- They had eggs. |
|
Back to top |
|
 |
sublogic Guru


Joined: 21 Mar 2022 Posts: 349 Location: Pennsylvania, USA
|
Posted: Sun Jul 06, 2025 12:35 am Post subject: |
|
|
For what it's worth, there is also a dev-python/pexpect . https://pexpect.readthedocs.io/en/stable/. I have no experience with it. _________________ The practical unit of "Learning Experience" is the milli-Gentoo. |
|
Back to top |
|
 |
Genone Retired Dev


Joined: 14 Mar 2003 Posts: 9640 Location: beyond the rim
|
Posted: Sun Jul 06, 2025 9:26 am Post subject: |
|
|
Key question for this is if those games (and/or the fbc compiler) use the standard UNIX IO interface (stdout, stderr and stdin) or less abstract interfaces like termio. Regular IO manipulation tools only support the first case, the second case is more tricky as most likely you'll have to simulate individual keypresses.
If they use the standard IO interfaces and the issue is only the interactive nature I'd look into using something like pythons POpen interface or fifo nodes (aka named pipes) and redirect stdin, e.g.
Code: |
mkfifo game-input
launch-game < game-input |
(while read line; do
if [[ $line == "value1" ]]; then
echo "command1" > game-input
elif [[ $line == "value2" ]]; then
echo "command2" > game-input
fi
done)
rm game-input
|
|
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|