AuthorTopic: Everyone loves prolog! Don't they?  (Read 639 times)

0 Members and 1 Guest are viewing this topic.

Offline RukeyTopic starter

Everyone loves prolog! Don't they?
« on: December 03, 2009, 07:11:13 PM »
I will be showing you some basics of prolog with the 'amzi' listener which helps you run the code once written by yourself. I will post some screenshots along with some code snippets once I get the program installed on my laptop!

---------------------------------------------------------------------------------------

Don't worry I'll be giving you all the facts you need to start your own text-based adventure game that you can use to keep yourself entertained for hours! (Well...)

---------------------------------------------------------------------------------------

The adventure starts off by you establishing some facts about the environment in which the game takes place. This will include details of the rooms, objects in the rooms, and connecting doors.

In prolog these facts can be easily asserted with a command like:

    room(office).

The symbols or words that we have chosen have no special meaning to Prolog but the structure implies to us that the office is a room, please remember that the office is of something else so it must be in "()" or the Prolog won't be understood. Please also note that a " . " is also needed to end the line in each case.

We can also use facts with more than one argument for example:

    location(coffee, office).

To prolog this fact relates the first argument to the second, and it must be in that order.  We use the order to imply that the location of the coffee is in the office, and not the office is in the coffee.

---------------------------------------------------------------------------------------

We can use various other facts which I will show you quickly so, as most will get the basic idea by now:

description(entrance,' a very large corridor ').

This tell gives the game a sense of realism in terms of giving a discription of the 'entrance' or place which is going to be involved in your game.

door(office, cr4).
[cr4, will be used as control room 4).

This tells us that there is a door between the office and cr4.

edible(coffee).


This statement shows that the coffee is 'edible' or drinkable in this case.

have(pen).


This tells the program that you literally 'have' a pen and it can be used.

here(cr4).


This marks where you 'are' in the game and the statement is needed for the game to be initialised in different ways.

---------------------------------------------------------------------------------------

Here could be the set of database style information you could add to get your game started until we go into further detail. You can choose whatever you wish to add in rooms and such, so have a play around!

room(office).
room(cr4).
room(cr3).
room(entrance).
room(toilet).

description(office,'a very small room that smells strongly of coffee').
description(entrance,'a cold a drafty area with lots of noise').
description(toilet,'a very small and smelly room').

door(office, cr4).
door(office, toilet).
door(toilet, cr3).
door(entrance, cr3).
door(entrance, cr4).

location(coffee, office).
location(toastie,office).
location(book, office).
location(projector, cr4).
location(muffin,office).
location(whiteboard,cr3).
location(mug, cr3).
location(computer, office).
electric(computer).
electric(projector).
edible(muffin).
edible(coffee).
edible(chocolate).

have(pen).
have(chocolate).

here(cr4).


Linkback: http://www.ikorp.net/index.php?topic=89.msg203#msg203
« Last Edit: December 03, 2009, 07:21:40 PM by Rukey »

Offline RukeyTopic starter

Re: Everyone loves prolog! Don't they?
« Reply #1 on: December 03, 2009, 07:39:36 PM »
You can download various applications which help you monitor and run your prolog programming but I'd say a good free version although quite old is the amzi listener running enviroment which can be found here:

Where do I download the amzi listener?

Linkback: http://www.ikorp.net/index.php?topic=89.msg205#msg205
« Last Edit: December 05, 2009, 10:49:25 AM by Rukey »

Offline RukeyTopic starter

Re: Everyone loves prolog! Don't they?
« Reply #2 on: December 03, 2009, 08:43:47 PM »
Step Two of your adventure game:

Understanding how to use the enviroment you will have downloaded if you are willing to follow this tutorial, I will go through the basics of how to write and run the code you have written in the various sections of this tutorial in using prolog.

----------------------------------------------------------------------------------

To run the Prolog program you should:

    * save the Prolog source file as something like "Game.pro"
    * start the 'Listener' from the Listener menu.
    * consult the prolog source file from the Listener menu. (Game.pro)

If your program contains no errors then you should see a screen like the one below.



If you do have errors then the most likely causes at this stage are:

    * you have missed the full stop at the end of a line.
    * you have used more than one word for an object but not put it in single quotes eg. 'dirty mug'
    * you have used capital letters

Linkback: http://www.ikorp.net/index.php?topic=89.msg209#msg209
« Last Edit: December 05, 2009, 11:31:31 AM by Rukey »

Offline RukeyTopic starter

Re: Everyone loves prolog! Don't they?
« Reply #3 on: December 05, 2009, 11:42:55 AM »
Using simple queries and using basic variables to get results:

- In the listener window the database can be checked by entering one of the facts, for example  room(office).  Prolog will then check the database that is has just consulted and answer yes or no.



------------------------------------------------------------------------------------

A more useful version of this is to enter a fact but with a variable in it.  In prolog a variable is anything that starts with a capital letter so 'X', 'Place' and 'Thing' will all be treated as variables.

If we enter  room(X). then prolog will try and find all values for X which will make this true.



------------------------------------------------------------------------------------

You can do various others to find out facts from your involved database:

1. List the all the edible items. - edible(X).
2. List all the electric items. - electric(X).
3. List all the items located in the office. - location(office, X).
4. List all the doors in the office. - doors(office, X).
5. Find the location of the mug. - location(mug).
6. Find the description of the toilet. - description(toilet).

------------------------------------------------------------------------------------

More complex queries can be built by combining two queries on the same line separated by a comma.  In this case the comma acts as an 'AND' operator.

So to find the edible things that are located in the office we would use a statement like:

   location(X,office), edible(X).



Try these out yourself!:

Use complex queries in the listener to answer the following questions.

1. List the all the edible items in the office.
2. List all the electric items in cr4.
3. Find which room you go through to get from the office to cr3.

Linkback: http://www.ikorp.net/index.php?topic=89.msg231#msg231
« Last Edit: December 05, 2009, 11:48:03 AM by Rukey »

Offline RukeyTopic starter

Re: Everyone loves prolog! Don't they?
« Reply #4 on: December 05, 2009, 12:32:02 PM »
Rules in prolog - Connecting the right things together!

Finding doors between rooms provides us with a problem because the basic facts leave our doors as 'one-way'.  From this example the fact: door(office,cr4) means I can get from the office to cr4, but unfortunately notfrom cr4 to the office.

We can make a rule in prolog to state whether two rooms are connected. Rules have this syntax:

    connection(X,Y) :- door(X,Y).

which reads 'there is a connection between X and Y if there is a door from X to Y.

Each rule can have more than one definition so we can also say:

    connection(X,Y) :- door(Y,X).

If we now test this with the statement connection(cr4,office) the prolog will try the first definition of the rule and fail, then it will try the next definition of the rule and succeed.



------------------------------------------------------------------------------------

Further rules can be defined to test what we can and cannot do in the game.  For example a rule to test whether we 'cango' to a room from where we are now would look like this:

    cango(X) :- here(Y), connection(Y,X).

which reads 'we can go to X if we are at Y and there is a connection from Y to X.



------------------------------------------------------------------------------------

Also see if you can add these statements yourself without any help from me! :) These are very similar to the cango(X). rule so have a little try yourself!

cantake(X) - you can take an object if it is located in the room you are in.

caneat(X) - you can eat an object if it is edible and you have it.

canturnon(X) - you can turn on an object if you have it and it is electric

Linkback: http://www.ikorp.net/index.php?topic=89.msg233#msg233
« Last Edit: December 05, 2009, 12:38:14 PM by Rukey »

Offline RukeyTopic starter

Re: Everyone loves prolog! Don't they?
« Reply #5 on: December 05, 2009, 12:45:30 PM »
Recursion in Prolog.

To demonstrate the power of the language we can also define a recursive rule that will tell us if there is a path from one room to another.  The rule will look like this.

     path(X,Y) :- connection(X,Y).

     path(X,Y) :- connection(X,Z), path(Z,Y).


Note that the first rule definition just says there is a path between two rooms if they are connected.

The second definition says there is a path from X to Y if there is a third room Z which is connected to X and there is a path from Z to Y.

Consider the test path(office,entrance).

The first rule fails.

The second rule may find a connection from the office(X) to the toilet(Z) and then test if there is a path from the toilet to the entrance.

Linkback: http://www.ikorp.net/index.php?topic=89.msg234#msg234
« Last Edit: December 05, 2009, 12:46:53 PM by Rukey »

Offline RukeyTopic starter

Re: Everyone loves prolog! Don't they?
« Reply #6 on: December 05, 2009, 12:50:03 PM »
User Ouput - producing lists of objects and connections.

One requirement of the game will be to list the inventory of things that you are carrying. We can do this in Prolog by defining a rule which includes a 'write' statement.

inventory:-         The first inventory rule
     have(X),        finds an X that you have
     write(X),        writes out what X is
     write(', '),       writes a comma to format the list
      fail.                 and then fails so it will try again

inventory.       The second inventory does nothing but
                  will always succeed and so give a 'yes'
                  answer.



-----------------------------------------------------------------------------------

A similar rule is required to list the objects that you can see at a particular location:

listthings(Place)              the first definition of listthings
   location(X,Place),         finds an X at this place
   write(X),                      writes out what X is
   write(', '),                     writes a comma to format the list
   fail.                                and fails so that it will try again

listthings(Place).              the second definition of listthings
                                      does nothing but will always succeed
                                      and so give a 'yes' answer.

Write your own definition 'listconnections' that tells you where you can go to from a place (immediate connections only)

look :-
  here(X), description(X,Y),    finds a place X and its description Y
  write( 'You are in the '),    writes a heading
  write(X),write(' '),write(Y), writes where X is and the description Y
  nl,write('You can see '),       takes a new line and
  listthings(X),nl.             lists the things you can see in place X


Linkback: http://www.ikorp.net/index.php?topic=89.msg235#msg235

Offline RukeyTopic starter

Re: Everyone loves prolog! Don't they?
« Reply #7 on: December 05, 2009, 12:59:23 PM »
Assert and Retract - altering the knowledge base.

To play the game we need to be able to move around and pick up objects.

Prolog knows where we are because of the fact:

here(cr4).

To change location we need to retract this statement and assert a new one, but this needs to be done as the program is running, ie. from the listener environment.



-----------------------------------------------------------------------------------

This of course would allow us to 'teleport' to any location we wanted so for the game we will need to write a rule 'goto' that will check that we can go to that room and then do the move for us.

goto(X):-                  for the 'goto' rule
  cango(X),              check you can go to X
  retract(here(Y)),        retract all existing 'here' facts
  assert(here(X)),         assert the new 'here' fact.
  write('you walk into '),      and display a message
  write(X),nl.              saying where you are

Enter the goto rule above and test it to make sure that you can move to valid rooms.

Write your own rules for the actions take(X) and (drop(X) using the notes below to help you and test them to see if they work.


Try these yourself:

take(X)
   check that you can take the object (you have a rule for this)
   retract the location statement that tells where the object is
   assert that you now have the object

drop(X)
  check that you have the object
  use retract to remove it from your inventory
  use a new variable to note where you are now
  assert that the object is now in this location

Linkback: http://www.ikorp.net/index.php?topic=89.msg236#msg236

Offline RukeyTopic starter

Re: Everyone loves prolog! Don't they?
« Reply #8 on: December 05, 2009, 01:02:05 PM »
User input and loops - completing the game.

The final part of the game is to make a command loop that will allow the user to input a command and then will carry that command out for them. The code for this is shown below.

-----------------------------------------------------------------------------------

start :-              the name of the starting rule
  repeat,              it has a 'repeat' loop
    write('Enter command: '),   prompts for a command
    read(X),               inputs the command
    do(X), nl,               carries out the command
  terminate(X).              and tests for a termination

-----------------------------------------------------------------------------------

do(goto(X)):- goto(X).
do(take(X)):- take(X).              the various rules for doing
do(look):- look.                    things must also be written
do(inventory):- inventory.
do(X):- write('unknown command'),nl.

-----------------------------------------------------------------------------------

terminate(end).
termiate(X):-
   have(coffee),      and the different rules for the
   have(muffin),      terminiate condition
   have(mug),
   write('congratulations').



-----------------------------------------------------------------------------------

:- (op(50,fx,goto)).        define goto as a prefix operator
:-(op(50,fx,take)).        define take as a prefix operator

Linkback: http://www.ikorp.net/index.php?topic=89.msg237#msg237

Offline RukeyTopic starter

Re: Everyone loves prolog! Don't they?
« Reply #9 on: December 05, 2009, 05:10:43 PM »
almost finished now! I'll have it completed redone and completed by the end of this weekend. I've just got to tidy it up now and make it look like something you could actually read!

Rukey

Linkback: http://www.ikorp.net/index.php?topic=89.msg246#msg246

Offline Nukegaming

Re: Everyone loves prolog! Don't they?
« Reply #10 on: December 08, 2009, 02:41:22 PM »
Rukey i have see alll and it is well explained,nice 1 m8.

Linkback: http://www.ikorp.net/index.php?topic=89.msg288#msg288



Be trusted with yoda and breakdance yes,may the force be with you


Offline simluo

Re: Everyone loves prolog! Don't they?
« Reply #12 on: August 27, 2010, 04:00:26 AM »
Hewlett-Packard Co.'s luna gold,Mark Hurd joins a long list of corporate leaders felled by personal ethical lapses in recent years.Robert Moffat, a former senior vice president at International Business Machines Corp., left the company last year after becoming ensnared in a big insider trading case involving the hedge fund group Galleon and other corporate executives and traders on Wall Street luna gold.
                 wow cd keys,
John Browne, the former chief of oil giant BP PLC, resigned in 2007 after he admitted lying to a judge while trying to prevent a British newspaper from exposing details about his personal life.Concerns about ethical lapses put corporate boards in a difficult spot. If the breach isn't something that is automatic grounds for firing, directors need to make judgment calls about what to do and how much to disclose wow cd keys.
                             World of Warcraft power leveling,
If it acts too swiftly to eject an executive, a board risks unfairly and unnecessarily getting rid of a leader who may be running the business well. But if it fails to act with haste, rumors may leak out-possibly giving the appearance that the company doesn't have control of the situation.Disclosure of ethical lapses is tricky too. The less a board discloses, the more the public and employees wonder what really happened, and it is hard to move on from a scandal without sufficiently satiating that appetite World of Warcraft power leveling.
                         replica sunglasses,
Both alleged and admitted stumbles have brought down top executives who have been under increased scrutiny in the post Enron, post-Sarbanes-Oxley world. It is also harder to hide unethical behavior partly because 'there are so many news outlets that anyone can now pick up a story and run with it,' said Ronald Sims, a business-ethics professor at the Mason School of Business at The College of William & Mary. 'In the past, it was more hush hush.'Steven J. Heyer was ousted as chief executive of Starwood Hotels & Resorts Worldwide Inc. in 2007, after the board of directors received an anonymous letter accusing him of creating a hostile work environment. The letter alleged that Mr. Heyer made inappropriate physical contact with a female employee outside a restaurant bathroom, on at least one occasion. Mr. Heyer denied engaging in any impropriety replica sunglasses.
            world of warcraft power leveling,
Chris Albrecht, the CEO of Time Warner Inc.'s Home Box Office unit, left the company in 2007 over an incident for which he pleaded no contest to battery against his girlfriend. Mr. Albrecht at the time said he was leaving because he did not want his 'personal circumstances' to distract the company. Mark W. Everson was also ousted that year as president and CEO of the American Red Cross which said at the time that he had a 'personal relationship' with a subordinate employee. Mr. Everson said he was leaving 'for personal and family reasons.'In 2005, Boeing Co. replaced then-CEO Harry Stonecipher after emails revealed a relationship with a female executive at the company.Boeing said at the time that Mr. Stonecipher, who was then married, was fired not for having an affair, but for violating Boeing's code of conduct. Mr. Stonecipher acknowledged his conduct  world of warcraft power leveling.
                  2moons dil,
Also in 2005, Thomas M. Coughlin resigned his post as vice chairman of Wal-Mart Stores Inc. amid allegations that he abused expense accounts and fabricated invoices to obtain reimbursements totaling as much as $500,000.A year later, he pleaded guilty to federal wire-fraud and tax-evasion charges 2moons dil.

Linkback: http://www.ikorp.net/index.php?topic=89.msg543#msg543

Tags:
 

+ Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Note: this post will not display until it's been approved by a moderator.
Name: Email:
Verification:


* User

Welcome, Guest. Please login or register.
Did you miss your activation email?

September 09, 2010, 03:40:00 PM

Login with username, password and session length

* Stats


  • *Total Posts: 486
  • *Total Topics: 203
  • *Online Today: 17
  • *Most Online: 126
(March 17, 2010, 10:42:34 AM)
  • *Users: 0
  • *Guests: 15
  • *Spiders: 6
  • *Total: 21

  • *Yahoo!
  • *MSN (4)
  • *Google

* Premium Membership

Premium Membership: Inactive
Join Partnership

* Users Online

Online Users: 15
0 members and 15 guests
Yahoo!, MSN (4), Google

* Arcade


Arcade

Jewels
Latest High Score by
Rukey
with 3390 on
Jewels
------------------
The Top Players

1 - Rukey
Number Of Wins : 1

Portal Management Extension PortaMx v0.971 | PortaMx © 2008-2009 by PortaMx corp.