Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've heard that logic programming can serve as a general-purpose alternative to other programming paradigms such as OO or functional programming. (Since Prolog is Turing-complete, this must be so!)

However, I'm having trouble seeing how one would implement an interactive program, like a simple, graphical console game in Prolog or a similar language. You have facts, rules which can derive more facts, and queries which retrieve facts. It's easy to see how you can use those basic elements to create something like a sudoku solver. But how about Pac-man, or more simply, Pong?

PLEASE NOTE: I'm not looking for low-level details, but a conceptual overview. (For example: in high-level terms, how would you handle I/O? How would you store the game state? How would you implement something like a "main loop"? How would you measure and respond to the passing of time?)

share|improve this question
1  
I really like your question - I was thaught Prolog a long time ago and asked myself the same Question and was never able to think of a way to do this. –  Christian Sauer Aug 8 '13 at 7:53
    
I've personally run a logic programming course implementing a Frozen Bubbles clone in SWI Prolog. Worked like a charm (after the tenth day productivity went way down because just playing it was more fun than adding functionality). The uni website link is broken, but I'll try to see if I can make the code available again. –  Kilian Foth Aug 8 '13 at 10:00
2  
"Since Prolog is Turing-complete, this must be so!" – Not really. Prolog being Turing-complete means that any mathematical function on natural numbers that can be computed by a Turing Machine can be computed by Prolog. But it doesn't say anything about algorithms which aren't mathematical functions on natural numbers. For example: is an operating system a mathematical function on natural numbers? A web server? A game? Printing to the console? Driving a robot? I have no doubt that all of these can be done in Prolog, but it doesn't necessarily follow from Prolog being Turing-complete. –  Jörg W Mittag Aug 8 '13 at 10:05
    
@JörgWMittag: In other words, it may be possible, it just may not be practical? –  Robert Harvey Aug 9 '13 at 16:04
1  
@JörgWMittag - Yes, all of them, at their heart, are just math and storage on numbers. –  Bobson Aug 9 '13 at 19:04

2 Answers 2

Tracking game state is no different than tracking state in any other Prolog program. You define facts and then use them to make decisions. It's pretty old, but the article Exploring Prolog : Adventures, Objects, Animals, and Taxes does a good job of explaining how this might work in a game. Summarized from the article:

% Set up - you start in a house
location(you, house).

% Move to a new location.
goto(X) :- 
    location(you, L), % Read your current location into L
    connect(L, X), % Check if you can reach the new location X from L
    retract( location(you, L) ), % Remove the old location fact
    assert( location(you, X) ). % Add a new location fact. You are in X now.
    write($ You are in the $), write(X), nl.

Beyond that, you need a graphics and IO library. There may be commercial Prolog distributions that include them. I'm most familiar with SWI Prolog, so I'll suggest plOpenGL as a starting point. Not only does it give you access to OpenGL's rendering capabilities, it also includes bindings for mouse and keyboard events. For example, to handle a press of the Escape key, you define a keyboard rule like so:

% 27 is ASCII Code for Escape
keyboard(27,_,_) :-
    write('Escape key was pressed...'),nl.

Take a look at plOpenGL's moving light example for a few more details and an example of handling mouse movement.

If you use a graphics library, it will likely handle the game loop for you. Basically, you invert control to the library and provide rules to be executed when appropriate: set up, repainting, IO events, etc. If you want to limit FPS or run code conditionally based on time, you can track elapsed time using time/date predicates and make decisions accordingly.

There are many Prolog flavors, so this is certainly not the only way to build a game. Different distributions and related languages will use different libraries/bindings that may encourage different approaches. In addition, polyglot programmers might encourage you to use a more "graphics friendly" host language/runtime to manage rendering and IO while using Prolog to model game entity behaviors and decision making.

share|improve this answer
1  
The same site you've linked to also contains a longer tutorial, Adventure in Prolog, which I found useful, and which also ends in the development of a simple text-based adventure game. –  Josh Caswell Aug 9 '13 at 19:05
    
Great! This is what I was looking for. I do feel that including operations with side effects (like write) in a "logical predicate" seems to bend the concept rather far. –  Alex D Aug 9 '13 at 19:41

On top of Corbin's answer: In general state can be stored/retrieved in Prolog using the assert/retract predicates. But there are many non-standard options like saving into RDF, XML, relational databases, etc. If you want GUI one example is XPCE offered by SWI-Prolog.

Note that although implementing a complete game in Logic Programming is a good exercise, in practice its performance would be inadequate and that's why vendors like SWI-Prolog offer bindings to lower-level languages (eg. Java, C++, etc.). Even implementing a simple Sudoku solver requires using CLP libraries.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.