prolog project report

Post on 18-Feb-2016

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

prolog project report with full code inside. The code is for turbo prolog. A sample game in prolog using NLP

TRANSCRIPT

CSE-4706 Project

Report

Text Based Advanture Game in

Turbo Prolog

Submitted in partial fulfillment ofthe requirements for the award of the degree of

Bachelorin

Computer Science and Engineering

Submitted by

Name Ratan Sunder ParaiID C-093503Section 7ACSemester 10th

Under the guidance ofDr. Mohammad Shahadat Hossain

Department of Computer Science and EngineeringInternational Islamic University Chittagong

Chittagong, Bangladesh

Spring Semester 2015

Abstract

Prolog is a backward chaining declarative language associated with ar-tificial intelligent. It is a logical programing language that make it easy todevelop logic base programs. Text Based adventure game designing withProlog is a great way to learn it’s reasoning procedure in .

Developing Text Based Adventure game with Prolog is easy than anyother language because

Contents

1 Introduction 11.1 Literature Survey . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 Prolog 22.1 Turbo Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

3 Work Done 33.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

3.1.1 Natural Language Processing . . . . . . . . . . . . . . 33.1.2 Decision Making . . . . . . . . . . . . . . . . . . . . . 10

3.2 Screenshoot . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

4 Conclusion 19

i

List of Figures

3.1 Start Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.2 go to office . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

ii

Chapter 1

Introduction

1.1 Literature Survey

Turbo Prolog Handbook[1] help me a lot to learn the Prolog language and agreat reference book. Because Turbo Prolog do not support Definite ClauseGrammars(DCG) I have used a simple NLP system from Introduction toTurbo Prolog[2] book. I learned the main game design[3]

1.2 Motivation

I love playing computer games. Text Based Adventure game open a new eraof game development. They have a good story line and the player visualizethe game world depending on the description. As a result same TBA gamehave different texture in gamers.

1

Chapter 2

Prolog

Prolog is a general purpose logic programming language associated with ar-tificial intelligence and computational linguistics.

Prolog has its roots in first-order logic, a formal logic, and unlike manyother programming languages, Prolog is declarative: the program logic isexpressed in terms of relations, represented as facts and rules. A computationis initiated by running a query over these relations.

The language was first conceived by a group around Alain Colmerauerin Marseille, France, in the early 1970s and the first Prolog system wasdeveloped in 1972 by Colmerauer with Philippe Roussel.

Prolog was one of the first logic programming languages, and remains themost popular among such languages today, with several free and commercialimplementations available. The language has been used for theorem proving,expert systems, as well as its original intended field of use, natural languageprocessing. Modern Prolog environments support creating graphical userinterfaces, as well as administrative and networked applications.

Prolog is well-suited for specific tasks that benefit from rule-based log-ical queries such as searching databases, voice control systems, and fillingtemplates.

2.1 Turbo Prolog

Turbo Prolog is a quite old Prolog system that only works with MS-DOS.This implies serious memory limitations. Also, it uses a special Prolog di-alect with typed variables and some other restrictions that does not complywith the Edinborough Standard . However, it is very fast, and contains aconvenient visual debugger.

2

Chapter 3

Work Done

The Text Based Adventure(TBA) game consists of two parts, Natural Lan-guage Processing and Decision making.

3.1 Code

The Text Based Adventure(TBA) game consists of two parts, Natural Lan-guage Processing and Decision taking.

3.1.1 Natural Language Processing

nlp.pro

domainsco l , no = integerword , text , key = s t r i n gnoun , verb = r e f e r e n c e s t r i n gw o r d l i s t = word∗

p r e d i c a t e sgogo oncetok ( co l , text , noun , verb )checknoun ( key , noun )checkverb ( key , verb )i s noun ( key )i s v e r b ( key )par s e r ( text , noun , verb )member( word , w o r d l i s t )

3

noun ( key )verb ( key )repeatsynverb ( word , word )chksyn ( word , word l i s t , word )head o f ( word l i s t , word )

do ( word , word )

% pred i c a t e s f o r the main game f i l e s% we need to d e f i n e them here so t ha t we can% c a l l them from the Natura l Language Process ing% se c t i on .room ( word )lookgoto ( word )take ( word )inventoryl o o k i n ( word )s l e e p ( word )drop ( word )eat ( word )use ( word )

databasel o c a t i o n ( symbol , symbol )here ( symbol )have ( symbol )turned on ( symbol )awake ( symbol )s l e e p i n g ( symbol )

goa lmakewindow (1 , 7 , 7 , ”SLEEPY HEAD − ADVENTURE GAME” ,0 , 0 , 25 , 80 ) ,asserta ( awake ( p laye r ) ) ,go .

c l a u s e sgo :−

repeat ,

4

clearwindow ,write ( ”Welcome s l e epy head ! ” ) , nl ,write ( ”To win t h i s game you need to s l e e p . . ” ) , nl ,write ( ”Have fun . . ! ” ) , nl ,write ( ”====================================” ) , nl , nl ,

look ,go once ,write ( ”Another game? ” ) ,readchar ( Replay ) ,Replay = ’n ’ .

go once :−awake ( p laye r ) ,write ( ” > ” ) ,r ead ln ( Text ) ,upper lower ( Text , ModText ) ,ModText <> ” qu i t e ” , ! ,pa r s e r (ModText , , ) ,go once .

go once :−s l e e p i n g ( p laye r ) ,write ( ” Congratu lat ion ! You win ! ! ! ” ) , nl ,write ( ” Press any key to e x i t . . . ” ) ,r ead ln ( ) .

go once .

repeat .repeat :−

repeat .

pa r s e r ( Text , Noun , Verb ) :−tok (3 , Text , Noun , Verb ) ,trace ( on ) ,bound ( Verb ) ,bound (Noun ) , ! ,

5

Word = Verb ,Object = Noun ,synverb (Word , OutVerb ) ,do ( OutVerb , Object ) , ! .

pa r s e r ( , , ) :−write ( ” so r ry I do not understand . ” ) , nl .

% go to a new p lacedo ( ”go” , Place ) :−

Place <> ”” ,goto ( Place ) ,! .

do ( ”go” , Place ) :−Place = ”” ,write ( ” I didn ’ t understand the p lace you want to go . ” ) ,nl , ! .

% check inven torydo ( ”” , ” inventory ” ) :−

% wr i t e (”You are l oo k in g at my inven tory ”) , nl ,inventory ,! .

% look at a o b j e c tdo ( ” look ” , Place ) :−

Place<> ”” ,%wr i t e (”You are l oo k in g at ” , Place ) , nl ,l o o k i n ( Place ) ,! .

% genera l l ook to ge t d e t a i l s i n f o about your environmentdo ( ” look ” , ) :−

%wr i t e (”You are l oo k in g ”) , nl ,look ,! .

% take a o b j e c t i n t o your inven tory

6

do ( ” take ” , Object ) :−Object <> ”” ,%wr i t e (”Take o b j e c t p r e d i c a t e s ”) , nl ,take ( Object ) ,! .

do ( ” take ” , ) :−write ( ”What do you want to take ? ” ) , nl , ! .

% put down o b j e c tdo ( ”drop” , Object ) :−

Object <> ”” ,%wr i t e (”Put ” , Objec t ) , nl ,drop ( Object ) ,nl ,! .

do ( ”drop” , ) :−write ( ”What you want to drop ?” ) , nl , ! .

do ( ” eat ” , Object ) :−Object <> ”” ,%wr i t e ( Object , ” was too t a s t y ”) , nl ,eat ( Object ) ,! .

do ( ” eat ” , ) :−write ( ”What you want to eat ?” ) , nl .

do ( ” s l e e p ” , Place ) :−Place <> ”” ,s l e e p ( Place ) ,! .

do ( ” s l e e p ” , ) :−write ( ”Where are you want to s l e e p ? ” ) , nl , ! .

do ( ” use ” , ”bed” ) :−s l e e p ( bed ) , ! .

7

do ( ” use ” , Thing ) :−Thing <> ”” ,use ( Thing ) ,! .

do ( ” use ” , ) :−write ( ”What you want to use ?” ) , nl , ! .

do ( , ) :−write ( ” Sorry I do not understand . ” ) , nl .

tok ( Col , Text , Noun , Verb ) :−f r ont token ( Text , Key , Strx ) ,checknoun (Key , Noun ) ,checkverb (Key , Verb ) ,! ,s t r l e n (Key , Len ) ,Pos l = Col + Len + 1 ,tok ( Posl , Strx , Noun , Verb ) .

tok ( , , , Verb ) :−bound ( Verb ) ; Verb = ”” .

tok ( , , Noun , ) :−bound (Noun ) ; Noun = ”” .

tok ( , , , ) .

checknoun (Key , Noun ) :−i s noun (Key ) ,Key = Noun , ! .

checknoun ( , ) .

checkverb (Key , Verb ) :−i s v e r b (Key ) ,Key = Verb , ! .

checkverb ( , ) .

i s noun (Key) :−

8

noun (Key ) .

i s v e r b (Key) :−verb (Key ) .

% Dic t i ona l r ynoun ( ” inventory ” ) .noun ( ” h a l l ” ) .noun ( ” d in ing ” ) .noun ( ” k i t chen ” ) .noun ( ”bedroom” ) .noun ( ” o f f i c e ” ) .noun ( ” apple ” ) .noun ( ”banana” ) .noun ( ” peps i ” ) .noun ( ” a lmira ” ) .noun ( ” desk ” ) .noun ( ”computer” ) .noun ( ” cha i r ” ) .noun ( ” busket ” ) .noun ( ” tab l e ” ) .noun ( ” f l a s h l i g h t ” ) .noun ( ”bed” ) .noun ( ” s o f f a ” ) .noun ( ” p l a t e s ” ) .noun ( ”key” ) .

verb ( ”go” ) .verb ( ”run” ) .verb ( ”walk” ) .verb ( ” turn ” ) .verb ( ” goto ” ) .verb ( ” take ” ) .verb ( ” look ” ) .verb ( ”put” ) .verb ( ” eat ” ) .verb ( ” use ” ) .verb ( ” s l e e p ” ) .verb ( ”drop” ) .

synverb (Word , Outword ) :−

9

Y = [ ”go” , ”run” , ”walk” , ” turn ” , ” goto ” ] ,chksyn (Word , Y , Outword ) .

synverb (Word , Outword ) :−Y = [ ”drop” , ”put” ] ,chksyn (Word , Y, Outword ) .

chksyn (Word , Y , Outword ) :−member(Word , Y ) ,head o f (Y , Outword ) , ! .

chksyn (Word , , Outword ) :−Outword = Word .

member(X , [X | ] ) .member(X , [ | Y ] ) :−

member(X , Y ) .

head o f ( [ Head | ] , Head ) .

3.1.2 Decision Making

game.pro

inc lude ” nlp . pro”

domainsmpuz = goto ( s t r i n g )

p r e d i c a t e sdoor ( symbol , symbol )e d i b l e ( symbol )connect ( symbol , symbol )l i s t c o n n e c t i o n s ( symbol )l i s t t h i n g s ( symbol )can go ( symbol )move( symbol )t a k e o b j e c t ( symbol )puzz l e (mpuz)i s h e r e ( symbol )i s t a k e a b l e ( symbol )conta in s ( symbol , symbol )f u r n i t u r e ( symbol )

10

remove ( symbol )

c l a u s e s

% rooms t ha t are in the gameroom ( h a l l ) .room ( d in ing ) .room ( k i tchen ) .room ( bedroom ) .room ( o f f i c e ) .

% l i s t o f doors in the gamedoor ( ha l l , o f f i c e ) .door ( ha l l , d in ing ) .door ( dining , k i t chen ) .door ( dining , bedroom ) .

% ob j e c t s o f the rooml o c a t i o n ( almira , h a l l ) .l o c a t i o n ( s o f f a , h a l l ) .l o c a t i o n ( desk , o f f i c e ) .l o c a t i o n ( key , desk ) .l o c a t i o n ( computer , desk ) .l o c a t i o n ( cha i r , o f f i c e ) .l o c a t i o n ( tab le , d in ing ) .l o c a t i o n ( busket , t ab l e ) .l o c a t i o n ( peps i , t ab l e ) .l o c a t i o n ( apple , busket ) .l o c a t i o n ( banana , busket ) .l o c a t i o n ( p la t e s , k i t chen ) .l o c a t i o n ( bed , bedroom ) .l o c a t i o n ( f l a s h l i g h t , busket ) .

% ed i b l e o b j e c t se d i b l e ( apple ) .e d i b l e ( banana ) .e d i b l e ( peps i ) .

% fu rn i t u r e t ha t can ’ t be takenf u r n i t u r e ( ”bed” ) .f u r n i t u r e ( ” t ab l e ” ) .

11

f u r n i t u r e ( ” a lmira ” ) .f u r n i t u r e ( ” cha i r ” ) .f u r n i t u r e ( ”computer” ) .f u r n i t u r e ( ” p l a t e s ” ) .f u r n i t u r e ( ” s o f f a ” ) .

% cur r en t l y where the p l aye r i shere ( h a l l ) .

% connect two roomconnect (X,Y):− door (X,Y) .connect (X,Y):− door (Y,X) .

% Li s t t h i n g s in a p l acel i s t t h i n g s ( Place ) :−

l o c a t i o n (X, Place ) ,write (X) , write ( ”\ t ” ) ,f a i l .

l i s t t h i n g s ( ) .

% l i s t o f rooms t ha t are connected to the current rooml i s t c o n n e c t i o n s ( Place ) :−

connect ( Place ,X) ,write (X) , write ( ”\ t ” ) ,f a i l .

l i s t c o n n e c t i o n s ( ) .

look :−here ( Place ) ,write ( ”You are in the ” , Place ) , nl ,write ( ”You can see the f o l l w i n g th ing s : ” ) , nl ,l i s t t h i n g s ( Place ) , nl ,write ( ”You can go to the f o l l o w i n g l o c a t i o n s : ” ) , nl ,l i s t c o n n e c t i o n s ( Place ) , nl .

l o o k i n ( Thing ):−l o c a t i o n ( , Thing ) ,write ( ”The ” , Thing , ” conta in s : ” ) , nl ,l i s t t h i n g s ( Thing ) , nl .

12

l o o k i n ( Thing ):−write ( ”There i s nothing in the ” , Thing ) , nl .

goto ( Place ):−puzz l e ( goto ( Place ) ) ,can go ( Place ) ,move( Place ) ,look , ! .

goto ( ) .

can go ( Place ) :−here (X) ,connect (X, Place ) .

can go ( Place ):−write ( ”You can not go to ” , Place ) , nl , ! ,f a i l .

move( Place ):−retract ( here ( ) ) ,asserta ( here ( Place ) ) .

take (X) :−i s h e r e (X) ,i s t a k e a b l e (X) ,t a k e o b j e c t (X) .

take ( ) .

i s h e r e ( Thing ) :−here ( Here ) ,conta in s ( Thing , Here ) , ! .

i s h e r e ( Thing ) :−write ( ”There i s no ” , Thing , ” here . ” ) , nl ,

13

f a i l .

c onta in s ( Thing , Here ) :−l o c a t i o n ( Thing , Here ) .

conta in s ( Thing , Here ) :−l o c a t i o n (X, Here ) ,conta in s ( Thing , X) .

i s t a k e a b l e ( Thing ) :−f u r n i t u r e ( Thing ) ,write ( ”You can not p ick up a ” , Thing ) , nl , ! ,f a i l .

i s t a k e a b l e ( ) .

t a k e o b j e c t (X):−retract ( l o c a t i o n (X, ) ) ,asserta ( have (X) ) ,write ( X , ” i s added to your inventory ” ) , nl .

drop ( Thing ) :−have ( Thing ) ,here ( Here ) ,retract ( have ( Thing ) ) ,asserta ( l o c a t i o n ( Thing , Here ) ) ,write ( ”You put down ” , Thing , ” on the ” , Here , ” ’ s f l o o r ” ) ,nl , ! .

drop ( Thing ) :−write ( ”You don ’ t have ” , Thing ) , nl , f a i l .

eat ( Thing ) :−have ( Thing ) ,e d i b l e ( Thing ) ,

14

remove ( Thing ) .eat ( Thing ) :−

e d i b l e ( Thing ) ,write ( ”You do not have the ” , Thing , ” in your ” ) ,write ( ” inventory to eat ” ) ,nl , ! .

eat ( Thing ) :−write ( ”You can not eat ” , Thing ) , nl , ! .

remove ( Thing ) :−have ( Thing ) ,retract ( have ( Thing ) ) ,write ( ”The ” , Thing , ” was very ta s ty . ” ) ,write ( ” You are f e e l i n g f r e s h . ” ) , nl .

inventory :−write ( ”You have : ” ) , nl ,have (X) ,write (X) , nl ,f a i l .

inventory .

use ( f l a s h l i g h t ) :−have ( f l a s h l i g h t ) ,turned on ( f l a s h l i g h t ) ,retract ( turned on ( f l a s h l i g h t ) ) ,write ( ”You turned o f f the f l a s h l i g h t ” ) , nl .

use ( f l a s h l i g h t ) :−have ( f l a s h l i g h t ) ,asserta ( turned on ( f l a s h l i g h t ) ) ,write ( ”You turned on the f l a s h l i g h t ” ) , nl , ! .

use ( f l a s h l i g h t ) :−write ( ”You don ’ t have the f l a s h l i g h t ” ) , nl , ! .

15

use ( Thing ) :−write ( ”You do not know how to use the ” , Thing ) , nl , ! .

use ( ) .

s l e e p ( bed ) :−here ( Here ) ,conta in s ( bed , Here ) ,retract ( awake ( p laye r ) ) ,asserta ( s l e e p i n g ( p laye r ) ) ,write ( ”You can ’ t keep your eyes anymore . As soon ” ) , nl ,write ( ” as you lay down in the bed you f a l l in deep” ) , nl ,write ( ” s l e e p ! ” ) , nl .

s l e e p ( bed ) :−write ( ”You can not see s ee any bed here . ” ) , nl , ! .

s l e e p ( Place ) :−write ( ”You can not s l e e p in ” , Place ) , nl , ! .

% puz z l e to s o l v e b e f o r e advance f u r t hu rpuzz l e ( goto ( o f f i c e ) ) :−

have ( f l a s h l i g h t ) ,turned on ( f l a s h l i g h t ) ,! .

puzz l e ( goto ( o f f i c e ) ) :−write ( ” I t ’ s dark and you are a f r a i d o f dark . ” ) , nl ,! , f a i l .

puzz l e ( goto ( bedroom ) ) :−have ( key ) , ! .

puzz l e ( goto ( bedroom ) ) :−write ( ”The door to the bedroom i s locked . ” ) ,write ( ” You need a key to ente r the re . ” ) ,nl , ! , f a i l .

puzz l e ( ) .

16

3.2 Screenshoot

When you start the game, it will present a welcome screen with goal of thegame shown in figure-3.1.

Figure 3.1: Start Screen

You can move between rooms using go, run, walk, turn or goto command.Suppose the player want to go to the office room. Then he/she can type, ”goto the office room” or ”I want to go to the office” or simply ”goto office” etc.The output is shown in figure-3.2.

As the output show is figure-3.2 , the player can not go to the office roomwithout having the flashlight and it is in turned on state.

17

Figure 3.2: go to office

18

Chapter 4

Conclusion

Because most of the function needed for the game is already implemented,creating a new story or modifying the story is easy. Like- we can add morerooms to the game by declaring new room(room name) and door(room from, room to)clauses.

We can also use the code for totally new game story by changing only afew line of codes. Future improvement can be made to this code by addingcapability of reading story from external text file to make the code reusable.Moreover if we use SWI-Prolog then we can use DCG for better NaturalLanguage Processing.

19

References

[1] Turbo Prolog Owner’s Handbook. Scotts Valley, CA, USA: Borland Intl,Jun. 1986.

[2] C. Townsend, Introduction to Turbo Prolog. Berkeley: Sybex Inc, Jan.1987.

[3] M. Dennis, “Adventure in Prolog - Amzi! inc.” [Online]. Available:http://www.amzi.com/AdventureInProlog/index.php

20

top related