1. Prolog Programming.
Aim
The aim of thisassignmentwastocreate a searchgame inprolog.The mannerinwhichit was
tackledwasby creatingdifferentsectsof roomsalongwithobjectsetoutinthem, each objectwas
able to be dynamicandtherefore made the game more lifelike.The objectiveof thisassignmentwas
to make a character (studentA) lookandeventuallyfindhisAIbook(artificial intelligent).
Progress/outcome
Primary level
To beginwith,the firststepthatwas takento achieve adynamiccharacterwas to create a dynamic
atmosphere.Butbefore beingable todoso,it was necessarytocreate the basicstructure that
wouldeventuallycreate it. Tobeginwith,declaringthe differentsurroundingsinthe house.Tobe
specific,the five differentrooms:
Rooms(kitchen).
Rooms(bedRoomA).
Rooms(bedRoomB).
Rooms(bathroom).
Rooms(hall).
From whatcan be seeninthe above code,the simple code thatwasusedistellingprologthat
kitchenisa” Room” and“bedRoomA”isalsoa room. Thisisknownas a rule.A rule inprologiswhen
a predicate/1isdefinedandendswithafull stop.Anexampleof thisisdemonstratedinthe above
coding. Once the differentroomsare created,the nextlogical sequence wouldbe tocreate different
doorsto connecteach room.
doors(hall,bedRoomA).
doors(bedRoomB,hall).
doors(hall,kitchen).
2. doors(kitchen,bathroom)
Once the doors are made,itisclear to see whatroomsare connectedandfromwhichdirection.This
isextremelyusefulbecause once the game becomesdynamic; itwouldmake iteasieronthe userto
be able to knowwhatand howeach room isconnected. Althougheachdoorisspecifiedtowhat
room theyare connected,itdoesnotdo the trick inthiscase.In prolog,the logicof it isnot similar
to that of a human.What that meansisforexample if apersonwere totell hisfriendthatif he were
to be in the bathroomand openhisdoor,itwouldleadhimto the kitchen.Now naturallyhisfriend
wouldassume thatif he was inthe kitchenandopenedthe doorit wouldleadhimtothe bathroom.
That is because ahumanunderstandthata doorhas a two waysystem.Fromone room to another
and back to that room.Unfortunately,prologdoesnothave thissortof intelligence.Sothe manner
inresolvingthisdilemmaisbydoingthe follow:
connects(X,Y):-doors(X,Y).
connects(X,Y):-doors(Y,X).
What that doesisthat it statesthatthe value of “X” and “Y” is similartothe door value of “X” and
“Y”. So if door “hall”and “kitchen”were there forexample,thatwouldbe the value of connect“X”
“Y”. When the firstline iscompleted,the secondlinehasasimilarpiece of code toit but the orderof
the “X” “Y” is changedto“Y” “X”. What that doesisthat it allowsprologtounderstandthatfrom
one room to anotherandback to that room (basicallyeachdoorcanconnectto two differentrooms
dependingonwhatdirectionyouare headingto).Now the roomsare made the doorsare created
and connectswell the nextstepistotestif each setof rulesworks.
?-Rooms(kitchen).
True
?-Rooms(office).
False.
Nowby askingprologif the roomsare correct itwouldgive useithertrue or false (yesorno.
Dependingonwhatversionof prologisbeingused).Sowhenitwasaskedif there isa kitchen,it
confirmeditwiththe user. Whenaskedif there anoffice,it respondedwithanegative feedback
(statingthatoffice isnota room). Nowthat eachrule that wasmade is workingfine,itistime to
start settinglocationsof items. (Notethiswillstartof basichoweverasthisreportcontinuesitwill
getmore complicatedandsome of the codingwill change.Currentlythe level thatisbeingworked
on isthe primarylevel.)
location(bedA,bedRoomA).
location(bedB,bedRoomB).
3. There are multiplewaysof statingitems/equipmentwithinprolog.However,fortime andefficiency
purpose,itiswisertouse the code above.The twoexample usedisstatingtwodifferentthings. The
top one isstatingthat there isa bedcalledbedA ina room calledbedRoomA. Ashasbeenexplained
before,bedRoomAisalreadyarule whichcomesunderthe roomsectionasit had beenmentioned
before.Butas forbedA itis a newrule that we setthat islocatedinside the bedRoomA. Before this
reportcontinues, let’sdemonstrateitbywritingasimple queryinprolog.
?-location(X,bedRoomA).
X=bedA.
So whathas beendone isthatto verifyif bedA islocatedinbedRoomA,simplyaskprologaquestion.
By doingso,it will give usthe missingword.Sofromthe code above,whenprologwasaskedwhatis
X (somethingunknown)butinsidebedRoomA.Prologgave the value XasbedA.By reversingthe
orderof the questionforinstance askingprolog?-location(bedA,X) prologwouldgive the value of X
as bedRoomA.Once thatstepisoverand all the rulesare definedandtested,the nextstepin
completingthe primarylevel wouldbe todefine where studentA initiallystays.However,itwould
be more appropriate todiscussitlateron.
Middle level
The main objective withinthe middlelevel istobe able todevelopasemi dynamicsearchgame.At
thisstage it may notmake much sense butitwill soon.The firstthingtothat needstobe developed
isthe abilityforthe characterto take the object.Thisiswhenthe game becomessemi dynamic. The
firststepwouldbe to state where the characterwill be (note thatitwas statedinthe primarylevel
that where the characterwill be locatedismentionedlateron).
Here(hall).
So the above code literallyjuststatesthatthe characterisin the hall.Althoughthatisa correct
mannerindoingso, itwouldnotworkfor our code lateron as it wouldnotworkif we want to make
our character intoa movingtakingdynamicdevice.Sothe waythatit will be done isbywrite the
worddynamicbefore.
:-dynamichere/1.
Here(hall).
Right,at thisstage the character isable to move.Howeverhe cannotdue tothe fact that no rule has
beencreatedinorderto allowthe characterto be able tomove around.Thiswill be done inthis
manner.
goto(Place):-
can_go(Place),
travel(Place),
4. see.
The above codingmentionsif statement.Whatitmeansisthat if the characterscan gotoa location
fromwhere he is,it wouldautomaticallyprintoutthe statementthatisinthe travel section.
can_go(Place):-
Here(X),
Connect(X,Place).
can_go(Place):-
Write ('youcannot getthere'),nl,
Fail.
Nowthispart is where the semi dynamicpartthatwas mentionedearliercomestoplay.Bystating
can_go (Place) itwouldgothrougha set of rules.Sofor thisexample the setof rulesare;
Here(X) thisreferstothe currentlocationof the character.
Connect(X,Place) whatthisreferstoisthe connectionof the door whichisX andthe currentPlace
of the character.
Once thisset of rule iscomplete,whatwasdone nextisanerror message wascodedin.
Write ('youcan’t getthere'),nl,
Fail.
The mannerin whichitwas done isplainlystatingit.
Higher level
Thisis where the semi dynamiccode will beautifullytransferintoafullyfunctional dynamicpiece of
work. But before we explain,let’sbacktrackandlisteverythingthathasbeendone through this
report.
Roomsand connection Yes
Itemsandtheirlocation Yes
Where studentA is Yes
Movingthe character Yes
Takingobjects Comingto it
Removingobjects Comingto it
5. Nowthat a listof what isdone is completed,itiseasytosee where this piece of projectisgettingto.
Let’sstart withtakingobjects.It’sverysimilartocan go andgo to but insteadthe wordingwouldbe
can take and take item.The onlydifferenceisthattwonew terminologyneedstointroduced. The
firstof the newterminologyis“asserta”.Assertaaddsaclause for itsfirstpredicate.The second
terminologyis retracting.Whatthismeansisthatit removesthe clausesfromthe logicbase.
take_item(X):-
retract(location(X,_)),
asserta(have(X)),
write(X),write('isnowtaken'),nl.
Finallythisiswhere the code becomeswell functional anddynamic.Thispartmakesthe object/item
able to move aroundthe house bytypingthe words“take_item”. Butwhat hasbeenleftoutof this
reportis the detail of the surroundinglocation.Thisisstatedassuch.
see:-
here(Place),
write('youare in'),write(Place),nl,
write('Youcansee:'),nl,
listthings(Place),
write('Youcango to :'),nl,
listconnections(Place).
The firstpart of thiscode starts withthe word see.See if the main wordthatneedstowatch out for.
Thisis the wordthat wouldbe usedwhentypingintothe shell.Sobacktothe listener. Here is
referringtothe locationof the character. Asit has previouslybeenset,the locationscanconstantly
change.
6. Conclusion/advancedlevel
To concluded,the code above all workandhave all beenverifiedthroughoutthe code.Itprovesthat
thiscode is fullydynamic.Furthermore,the code can move aroundand interactwiththe
environment.The lastpartof the code whichwas lefttothe endof thisreportisthat once the AI
bookis found,the game declares isover.
?-take(aibook).
Well done.Youwonthe game.
Howeverif the userchoosestoquithe has to type the word quit.
?-quit.
The Ai bookisstill missing.Maybe next type.
But if the user needstogeta hint,he will needtogo andfindhisphone andsee the message from
studentA.
?-hint.
If you take the phone,there isa message onyourphone to where the AIbookis.
?-take_phone(Iphone).
StudentA has placedthe AIbookeitherinthe hall,kitchenorbathroom.
Roomsand connection Yes
Itemsandtheirlocation Yes
Where studentA is Yes
Movingthe character Yes
Takingobjects Yes
Removingobjects Yes