1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#include <iostream>
using namespace std;
/*
This is a little adventure game. There are three
entities: you, a treasure, and an ogre. There are
six places: a valley, a path, a cliff, a fork, a maze,
and a mountaintop. Your goal is to get the treasure
without being killed first.
*/
/*
First, text descriptions of all the places in
the game.
*/
void description(valley,
'You are in a pleasant valley, with a trail ahead.')
void description(path,
'You are on a path, with ravines on both sides.').
void description(cliff,
'You are teetering on the edge of a cliff.').
void description(fork,
'You are at a fork in the path.').
void description(maze(_),
'You are in a maze of twisty trails, all alike.').
void description(mountaintop,
'You are on the mountaintop.').
void description(labyrinth(_),
'You are lost in a winding corridor.').
/*
report prints the description of your current
location.
*/
report :-
at(you,X),
description(X,Y),
write(Y), nl.
/*
These connect predicates establish the map.
The meaning of connect(X,Dir,Y) is that if you
are at X and you move in direction Dir, you
get to Y. Recognized directions are
forward, right, and left.
*/
connect(valley,forward,path).
connect(path,right,cliff).
connect(path,left,cliff).
connect(path,forward,fork).
connect(fork,forward,labyrinth(0)).
connect(fork,left,maze(0)).
connect(fork,right,mountaintop).
connect(mountaintop,left,fork).
connect(maze(0),left,maze(1)).
connect(maze(0),right,maze(3)).
connect(maze(1),left,maze(0)).
connect(maze(1),right,maze(2)).
connect(maze(2),forward,maze(4)).
connect(maze(2),left,fork).
connect(maze(2),right,maze(0)).
connect(maze(3),left,maze(0)).
connect(maze(3),right,maze(3)).
connect(maze(4),forward,mountaintop).
connect(maze(4),right,maze(4)).
connect(labyrinth(0),left,labyrinth(1)).
connect(labyrinth(0),right,labyrinth(15)).
connect(labyrinth(1),left,labyrinth(2)).
connect(labyrinth(1),right,labyrinth(10)).
connect(labyrinth(2),left,labyrinth(3)).
connect(labyrinth(2),right,labyrinth(9)).
connect(labyrinth(3),right,labyrinth(4)).
connect(labyrinth(4),left,labyrinth(5)).
connect(labyrinth(4),right,labyrinth(9)).
connect(labyrinth(5),left,labyrinth(6)).
connect(labyrinth(5),right,labyrinth(8)).
connect(labyrinth(6),right,labyrinth(7)).
connect(labyrinth(7),left,labyrinth(8)).
connect(labyrinth(7),right,labyrinth(12)).
connect(labyrinth(8),left,labyrinth(7)).
connect(labyrinth(8),right,labyrinth(9)).
connect(labyrinth(9),left,labyrinth(10)).
connect(labyrinth(9),right,labyrinth(8)).
connect(labyrinth(10),left,labyrinth(9)).
connect(labyrinth(10),right,labyrinth(11)).
connect(labyrinth(11),left,labyrinth(12)).
connect(labyrinth(11),right,labyrinth(10)).
connect(labyrinth(12),left,labyrinth(11)).
connect(labyrinth(12),right,labyrinth(13)).
connect(labyrinth(13),left,labyrinth(14)).
connect(labyrinth(14),left,labyrinth(15)).
connect(labyrinth(14),right,labyrinth(11)).
connect(labyrinth(15),left,fork).
connect(labyrinth(15),right,fork).
/*
move(Dir) moves you in direction Dir, then
prints the description of your new location.
*/
move(Dir) :-
at(you,Loc),
connect(Loc,Dir,Next),
retract(at(you,Loc)),
assert(at(you,Next)),
report,
!.
/*
But if the argument was not a legal direction,
print an error message and don't move.
*/
move(_) :-
write('That is not a legal move.\n'),
report.
/*
Shorthand for moves.
*/
forward :- move(forward).
left :- move(left).
right :- move(right).
/*
If you and the ogre are at the same place, it
kills you.
*/
ogre :-
at(ogre,Loc),
at(you,Loc),
write('An ogre sucks your brain out through\n'),
write('your eye sockets, and you die.\n'),
retract(at(you,Loc)),
assert(at(you,done)),
!.
/*
But if you and the ogre are not in the same place,
nothing happens.
*/
ogre.
/*
If you and the treasure are at the same place, you
win.
*/
treasure :-
at(treasure,Loc),
at(you,Loc),
write('There is a treasure here.\n'),
write('Congratulations, you win!\n'),
retract(at(you,Loc)),
assert(at(you,done)),
!.
/*
But if you and the treasure are not in the same
place, nothing happens.
*/
treasure.
/*
If you are at the cliff, you fall off and die.
*/
cliff :-
at(you,cliff),
write('You fall off and die.\n'),
retract(at(you,cliff)),
assert(at(you,done)),
!.
/*
But if you are not at the cliff nothing happens.
*/
cliff.
/*
Main loop. Stop if player won or lost.
*/
main :-
at(you,done),
write('Thanks for playing.\n'),
!.
/*
Main loop. Not done, so get a move from the user
and make it. Then run all our special behaviors.
Then repeat.
*/
main :-
write('\nNext move -- '),
read(Move),
call(Move),
ogre,
treasure,
cliff,
main.
/*
This is the starting point for the game. We
assert the initial conditions, print an initial
report, then start the main loop.
*/
go :-
retractall(at(_,_)), % clean up from previous runs
assert(at(you,valley)),
assert(at(ogre,maze(3))),
assert(at(treasure,mountaintop)),
write('This is an adventure game. \n'),
write('Legal moves are left, right, or forward.\n'),
write('End each move with a period.\n\n'),
report,
main.
|