It is possible to write an entire Adventure game in Prolog. An important part of such a game is the map, which we now know how to write. In a real game, we would also need to describe the caves or rooms in more detail, saying for instance that room a was ``a long low room half-a-meter long full of mud'' and that room b contained ``the magic infra-red radio-therapeutic healing portal''. Prolog makes it beautifully easy to do this, and you might consider it as your project: building a complete game from the skeletal version presented here.
To provide that essential element of fear, most Adventure games contain, as well as the player, a number of adversaries simulated by the game program. A game called Wumpus, once popular at Cambridge, involved a world of caves and pits inhabited by you (the player) and a Wumpus, who would eat you if you got into the same cave. As well as Wumpuses, we can borrow from Tolkien (why not? everyone else does) and populate our world with dragons, orcs, narcs and other menaces.
One thing we must do is to keep track of everyone's location. This is simple; in DD, you will find the facts
is_at( dragon, b ). is_at( fred, a ). is_at( wumpus, c ).One difference between these facts and the ones making up the map is that in a real game, the map stays the same, but the contents of the room can change. In a few turns' time, the dragon, player and Wumpus might have moved:
is_at( dragon, f ). is_at( fred, k ). is_at( wumpus, a ).It is perfectly possible to write programs that change facts in this way. You don't yet know how: so far, the only way a fact can be changed is if you replace or delete it. But such changes can be made under program control, and you will eventually find out how.
In the game of Wumpus, the Wumpus was simulated by the computer, which
therefore needed some representation of how it would behave towards
the other inhabitants. In Prolog, this is easy to state using rules.
assume that the player, who we have called
be eaten if he's in the same cave as
the Wumpus. Can you translate that statement into Prolog? That is, can
you concoct a fact that begins something like
will_be_eaten( fred ) :-and continues with a translation of ``fred is in the same cave as the Wumpus''?
This on its own might not seem very exciting. However,
combining it with techniques from later lessons, we could write a
Wumpus-simulator that would repeatedly move the Wumpus from one cave to
fred will_be eaten after each move. If this call ever
succeeded, the simulator could output a suitable message, display a few flowers and
a tombstone, and then quit the game.
In outline, such a program would be something like this:
To run the game: Do one game step. Run the game. To do one game step: A is the player's next action. Perform A, and update the world accordingly. To perform A, and update the world accordingly: If A is 'move to X' Then Remove the old fact about the player's location. Replace it by one saying he's at X. Check the dangers. To check the dangers: If the player will be eaten Then Say so, and quit. To check the dangers: If the player will be burnt alive (see next section) Then Say so, and quit.
In this outline sketch, the predicates concerned with writing out messages, removing and replacing facts, and quitting, are called ``command predicates''. They don't make inferences, but actually change things. The other predicates - getting the player's next action, and checking whether he'll be eaten or burnt alive - do make inferences, and you can write them with what you now know.
Most Prolog programs follow this kind of pattern. On the outside sit
anumber of ``command predicates'' which perform various actions. Which
action they select is decided by the result of inference-making
predicates, such as