/* Legend Of Velda -- a sample adventure game, by Jason Allen. Consult this file and issue the command: start. NOTE: This game does not operate as intented in this browser version of SWI-prolog. Must download SWI-prolog for full functionality */ :- dynamic at/2, i_am_at/1, alive/1, player/2, enemy/3. /* Needed by SWI-Prolog. */ /* This defines my current location. */ i_am_at(meadow). /* These facts describe how areas are connected. */ path(meadow, n, hyrule_castle) :- at(mastersword, in_hand). path(meadow, n, hyrule_castle) :- write('You cannot fight Banondorf without the Master Sword'), nl, fail. path(hyrule_castle, s, meadow). path(hyrule_castle, n, ganondorf). path(ganondorf, n, throne_room) :- \+ alive(ganondorf). path(ganondorf, n, throne_room) :- write('You must defeat Banondorf to enter the throne room!'), nl, fail. path(meadow, s, holy_altar). path(meadow, e, haunted_woods). path(meadow, w, cave). path(haunted_woods, s, ice_castle). path(haunted_woods, w, meadow). path(haunted_woods, e, chest1). path(chest1, w, haunted_woods). path(ice_castle, n, haunted_woods) :- \+ alive(player), write('You are dead!'), nl, fail. path(ice_castle, n, haunted_woods) :- alive(player). path(ice_castle, s, skeleton) :- \+ alive(player), write('You are dead!'), nl, fail. path(ice_castle, s, skeleton) :- alive(player). path(skeleton, n, ice_castle) :- \+ alive(player), write('You are dead!'), nl, fail. path(skeleton, n, ice_castle) :- alive(player), alive(skeleton), write('You must defeat the Skeleton King!'), nl, fail. path(skeleton, n, ice_castle) :- \+ alive(skeleton). path(cave, e, meadow) :- \+ alive(player), write('You are dead!'), nl, fail. path(cave, e, meadow) :- alive(player). path(cave, w, spider) :- \+ alive(player), write('You are dead!'), nl, fail. path(cave, w, spider) :- alive(player). path(spider, e, cave) :- \+ alive(player), write('You are dead!'), nl, fail. path(spider, e, cave) :- alive(player), alive(spider), write('You must defeat the Spider Queen!'), nl, fail. path(spider, e, cave) :- \+ alive(spider). path(holy_altar, n, meadow). path(holy_altar, s, shrine) :- at(icestone, holy_altar), at(firestone, holy_altar). path(holy_altar, s, shrine) :- write('You must find the power stones to enter the shrine.'), nl, fail. path(shrine, n, holy_altar). /* These facts tell where the various objects in the game are located. */ at(firestone, cave). at(icestone, ice_castle). at(mastersword, shrine). at(potion, chest1). /* This fact specifies game pieces that are alive. */ alive(spider). alive(skeleton). alive(ganondorf). alive(player). /* These rules describe how to pick up an object. */ take(X) :- at(X, in_hand), write('You''re already holding it!'), nl, look. take(X) :- i_am_at(Place), at(X, Place), retract(at(X, Place)), assert(at(X, in_hand)), nl, look. take(_) :- write('I don''t see it here.'), nl, look. /* These rules describe how to put down an object. */ drop(X) :- at(X, in_hand), i_am_at(Place), retract(at(X, in_hand)), assert(at(X, Place)), nl, look. drop(_) :- write('You aren''t holding it!'), nl, look. /* These rules define the six direction letters as calls to go/1. */ n :- go(n). s :- go(s). e :- go(e). w :- go(w). u :- go(u). d :- go(d). /* This rule tells how to move in a given direction. */ go(Direction) :- i_am_at(Here), path(Here, Direction, There), retract(i_am_at(Here)), assert(i_am_at(There)), look. go(_) :- write('You can''t go that way.'), nl,nl. /* This rule tells how to look about you. */ look :- i_am_at(Place), nl, describe(Place), nl, notice_objects_at(Place), nl. /* These rules set up a loop to mention all the objects in your vicinity. */ notice_objects_at(Place) :- at(X, Place), write('There is a '), write(X), write(' here.'), nl, fail. notice_objects_at(_). /* Define rules for battle */ player(100, 50). enemy(spider, 100, 30). enemy(skeleton, 100, 30). enemy(ganondorf, 1200, 100). isAlive(X):- X > 0. isDead(X) :- X =< 0. attack(X, Y, Z) :- Z is X - Y. restoreHealth(J, K, L) :- L is J + K. playerStats:- player(X, Y), write('Player Health: '), write(X), nl, write('Attack Power: '), write(Y), nl, nl. monsterStats:- i_am_at(Monster), enemy(Monster, MonsterHealth, MonsterAttackStrength), write(Monster), write(' Health: '), write(MonsterHealth), nl, write(Monster), write(' Attack Power: '), write(MonsterAttackStrength), nl, nl. monsterStats:- write('There is no monster here.'), nl. upgrade:- retract(player(PlayerHealth, PlayerAttackStrength)), assert(player(500, 500)), playerStats. battle:- i_am_at(Monster), player(PlayerHealth, PlayerAttackStrength), isDead(PlayerHealth), retract(alive(player)), die, nl. battle:- i_am_at(Monster), enemy(Monster, MonsterHealth, MonsterAttackStrength), isDead(MonsterHealth), retract(alive(Monster)), write('The '), write(Monster), write(' is dead'), nl. battle:- i_am_at(Monster), enemy(Monster, MonsterHealth, MonsterAttackStrength), isAlive(MonsterHealth), player(PlayerHealth, PlayerAttackStrength), attack(PlayerHealth, MonsterAttackStrength, NewPlayerHealth), attack(MonsterHealth, PlayerAttackStrength, NewMonsterHealth), retract(player(PlayerHealth, PlayerAttackStrength)), assert(player(NewPlayerHealth, PlayerAttackStrength)), retract(enemy(Monster, MonsterHealth, MonsterAttackStrength)), assert(enemy(Monster, NewMonsterHealth, MonsterAttackStrength)), write('Player Health: '), write(NewPlayerHealth), nl, write(Monster), write(' Health: '), write(NewMonsterHealth), nl. battle:- write('There is no a monster here.'), nl. drink:- at(potion, in_hand), write('You drink a potion.'), nl, retract(player(PlayerHealth, PlayerAttackStrength)), restoreHealth(PlayerHealth, 100, NewPlayerHealth), assert(player(NewPlayerHealth, PlayerAttackStrength)), retract(at(potion, in_hand)), assert(at(potion, dead_zone)). drink:- write('you dont have a potion!'). /* This rule tells how to die. */ die :- finish. /* Under UNIX, the halt. command quits Prolog but does not remove the output window. On a PC, however, the window disappears before the final output can be seen. Hence this routine requests the user to perform the final halt. */ finish :- nl, write('The game is over. Please enter the halt. command.'), nl. /* This rule just writes out game instructions. */ instructions :- nl, write('Enter commands using standard Prolog syntax.'), nl, write('Available commands are:'), nl, write('start. -- to start the game.'), nl, write('n. s. e. w. -- to go in that direction.'), nl, write('take(Object). -- to pick up an object.'), nl, write('drop(Object). -- to put down an object.'), nl, write('battle. -- to attack an enemy.'), nl, write('drink. -- to drink a potion.'), nl, write('look. -- to look around you again.'), nl, write('playerStats. -- see current player stats.'), nl, write('monsterStats. -- see current monster stats.'), nl, write('instructions. -- to see this message again.'), nl, write('halt. -- to end the game and quit.'), nl, nl. /* This rule prints out instructions and tells where you are. */ start :- instructions, look, playerStats. /* These rules describe the various areas. Depending on circumstances, an area may have more than one description. */ describe(meadow) :- write('You are in a meadow. To the north is Pyrule Castle. To the east is the haunted'), nl, write('woods, and to the west is a dark cave. To the south is the holy altar which'), nl, write('is said to hold the Master Sword. You must obtain this legendary blade so that'), nl, write('you can defeat Banondorf and save Princess Velda!'), nl. describe(holy_altar) :- at(firestone, holy_altar), at(icestone, holy_altar), write('The earth starts to shake as the stones sink into the altar'), nl, write('A staircase appears leading down into the shrine of the Master Sword'), nl, write('Proceed south'), nl. describe(holy_altar) :- at(firestone, holy_altar), write('The fire stone shines brightly from the altar'), nl, write('Go seek out the ice stone to unlock the secrets'), nl, write('of the holy altar.'), nl, write('North back to meadow'), nl. describe(holy_altar) :- at(icestone, holy_altar), write('The ice stone shines brightly from the altar'), nl, write('Go seek out the fire stone to unlock the secrets'), nl, write('of the holy altar. North back to meadow.'), nl. describe(holy_altar) :- write('You are at the holy altar. The meadow is to the north.'), nl, write('There appears to be a space to insert 2 stones. The inscription'), nl, write('reads, "he who finds the stones will be granted true power."'), nl. describe(hyrule_castle) :- at(mastersword, in_hand), write('Youve entered the castle courtyard! Head north to enter'), nl, write('the throne room and defeat Banondorf!'), nl, write('south to return to meadow'), nl. describe(ganondorf) :- alive(ganondorf), write('BOSS BATTLE: defeat Banondorf!'), nl. describe(ganondorf) :- write('Banondorf is defeated! Head north to save the princess!'), nl. describe(throne_room) :- write('you saved the princess!'), nl, write('GAME OVER'), nl. describe(cave) :- alive(spider), at(firestone, in_hand), retract(alive(player)), write('The Spider Queen sees you with the fire stone and attacks!!!'), nl, write(' ...it is over in seconds....'), nl, die. describe(cave) :- alive(spider), write('You have entered the lair of the Spider Queen'), nl, write('you must defeat her if you wish to obtain the'), nl, write('fire stone....'), nl. describe(cave) :- at(firestone, in_hand), write('The Spider Queen is defeated! You obtained the fire stone. Exit e'), nl. describe(cave) :- write('The Spider Queen is defeated! Take the fire stone!.'), nl. describe(chest1) :- at(potion, chest1), write('You found a chest!'), nl. describe(chest1) :- write('The chest has already been opened.'), nl. describe(haunted_woods) :- write('You are in the haunted woods.'), nl, write('to the south is the Frozen Castle.'), nl, write('to the west is the meadow.'), nl, write('a sign reads, "those who explore will be rewarded"'), nl. describe(shrine) :- at(mastersword, in_hand), write('You obtained the master sword!'), nl, write('You feel its power flowing through you!'), nl, write('go save the princess!'), nl, write('exit north.'), nl, nl, upgrade. describe(shrine) :- write('You entered the shrine of the master sword!'), nl, write('Remove it from its pedestal to harness its'), nl, write('power!'), nl. describe(ice_castle) :- alive(skeleton), at(icestone, in_hand), retract(alive(player)), write('The evil power of the ice stone has corrupted you'), nl, write('because the Skeleton King still lives.'), nl, die. describe(ice_castle) :- alive(skeleton), write('You have entered the Frozen Castle of the Skeleton'), nl, write('King. You must defeat him to obtain the'), nl, write('ice stone....'), nl. describe(ice_castle) :- at(icestone, in_hand), write('The Skeleton King is defeated! You obtained the ice stone. Exit n'), nl. describe(ice_castle) :- write('The Skeleton King is defeated! Take the ice stone!'), nl. describe(skeleton) :- alive(skeleton), write('You encountered the Skeleton King! Defeat him and take the ice stone!'), nl, write('battle.'), nl. describe(skeleton) :- write('The Skeleton King is dead.'), nl. describe(spider) :- alive(spider), write('You encountered the Spider Queen! Defeat her and take the fire stone!'), nl, write('battle.'), nl. describe(spider) :- write('The Spider Queen is dead. Exit e'), nl.