Questions about cursor manipulation/keyboard input/FFI

Hello everyone. This is my first post. I’ve been programming for over 30 years, but I finished my first ever (trivial) Prolog program last night. It took me two nights to make it work, which is a little embarrassing given that it only has one simple rule, but I am undeterred. I can now roll a six sided die in Prolog, and format/2 the result! This is the program I usually write in lieu of Hello World when learning a new programming language.

I came to consider Prolog three nights ago, when I wanted a way to decouple the rules of a game I was writing in Smalltalk from the rest of the business logic. I’ve been aware of what Prolog is for about 15 years, but hadn’t had a good excuse to learn it until now. Given that I expect Prolog – being a fundamentally different computational model from any that I’ve learned before – to be difficult to learn, I’ve decided to shelve Smalltalk for now, and aim to write a small game in (hopefully) pure Prolog.

The game I’d like to write is a “roguelike.” If you aren’t familiar with roguelikes, see:
https://en.wikipedia.org/wiki/Roguelike

This type of game requires a few of things from the implementation:

  • I need to be able to disable echoing of keystrokes to the output
  • I need to be able to recognize individual keystrokes, not full lines of input (i.e., no return at the end)
  • I need to be able to move the cursor to an arbitrary screen location
  • I need to be able to replace the character at the cursor with another character

From searching the forum’s posts, it does seem that SWI-Prolog makes use of ncurses, is this correct? If so, is this functionality exposed from within Prolog, either directly or via a library? If not, is FFI my best option?

Thanks in advance for any help you can offer.

1 Like

SWI-Prolog isn’t really build for that and I’d typically write the application as web server or in older days using xpce’s graphics. Most stuff is there though. There is with_tty_raw/1 to read raw input and stuff such as tty_goto/2 to move the cursor. You may need to use some hardcoded ansi tty sequences. These days every terminal seems to implement the ansi sequences.

Hi Jan, thanks for your advice!

I think I’ve realized that I was thinking about the problem outside in instead of inside out. I didn’t realize that it’s possible to embed SWI. Maybe I just need to evert my brain.

Experience with the Squeak FFI makes me wary of FFI in general. Tends to require a lot of knowledge of the VM, can crash an otherwise safe language, etc. Seems like embedding SWI in a C program that does ncurses might be just what the doctor ordered.

Please let me know if I’ve got it all wrong, and thanks again for your help!

Well, I would turn it around and use the FFI API to make as much of ncurses available as you need (given your initial description you may be fine without anything extra if you are prepared to use a couple of hard coded ANSI sequences). Keeping the toplevel in Prolog makes your development a lot easier.