Vous êtes sur la page 1sur 24

CSE-4706 Project

Report

Text Based Advanture Game in


Turbo Prolog
Submitted in partial fulfillment of
the requirements for the award of the degree of
Bachelor
in
Computer Science and Engineering

Submitted by

Name
Ratan Sunder Parai
ID
C-093503
Section
7AC
Semester
10th

Under the guidance of


Dr. Mohammad Shahadat Hossain

Department of Computer Science and Engineering


International Islamic University Chittagong
Chittagong, Bangladesh
Spring Semester 2015

Abstract
Prolog is a backward chaining declarative language associated with artificial intelligent. It is a logical programing language that make it easy to
develop logic base programs. Text Based adventure game designing with
Prolog is a great way to learn its reasoning procedure in .
Developing Text Based Adventure game with Prolog is easy than any
other language because

Contents
1 Introduction
1.1 Literature Survey . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1
1
1

2 Prolog
2.1 Turbo Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . .

2
2

3 Work Done
3.1 Code . . . . . . . . . . .
3.1.1 Natural Language
3.1.2 Decision Making
3.2 Screenshoot . . . . . . .

. . . . . . .
Processing
. . . . . . .
. . . . . . .

4 Conclusion

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

3
3
3
10
17
19

List of Figures
3.1
3.2

Start Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
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 a
great reference book. Because Turbo Prolog do not support Definite Clause
Grammars(DCG) I have used a simple NLP system from Introduction to
Turbo 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 era
of game development. They have a good story line and the player visualize
the game world depending on the description. As a result same TBA game
have different texture in gamers.

Chapter 2
Prolog
Prolog is a general purpose logic programming language associated with artificial intelligence and computational linguistics.
Prolog has its roots in first-order logic, a formal logic, and unlike many
other programming languages, Prolog is declarative: the program logic is
expressed in terms of relations, represented as facts and rules. A computation
is initiated by running a query over these relations.
The language was first conceived by a group around Alain Colmerauer
in Marseille, France, in the early 1970s and the first Prolog system was
developed in 1972 by Colmerauer with Philippe Roussel.
Prolog was one of the first logic programming languages, and remains the
most popular among such languages today, with several free and commercial
implementations available. The language has been used for theorem proving,
expert systems, as well as its original intended field of use, natural language
processing. Modern Prolog environments support creating graphical user
interfaces, as well as administrative and networked applications.
Prolog is well-suited for specific tasks that benefit from rule-based logical queries such as searching databases, voice control systems, and filling
templates.

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 dialect with typed variables and some other restrictions that does not comply
with the Edinborough Standard . However, it is very fast, and contains a
convenient visual debugger.

Chapter 3
Work Done
The Text Based Adventure(TBA) game consists of two parts, Natural Language Processing and Decision making.

3.1

Code

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

3.1.1

Natural Language Processing


nlp.pro

domains
c o l , no = integer
word , t e x t , key = s t r i n g
noun , verb = r e f e r e n c e s t r i n g
w o r d l i s t = word
predicates
go
go once
tok ( c o l , t e x t , noun , verb )
checknoun ( key , noun )
c h e c k v e r b ( key , verb )
i s n o u n ( key )
i s v e r b ( key )
p a r s e r ( t e x t , noun , verb )
member ( word , w o r d l i s t )
3

noun ( key )
verb ( key )
repeat
synverb ( word , word )
chksyn ( word , w o r d l i s t , word )
h e a d o f ( w o r d l i s t , word )
do ( word , word )
% p r e d i c a t e s f o r t h e main game f i l e s
% we need t o d e f i n e them h e r e so t h a t we can
% c a l l them from t h e N a t u r a l Language P r o c e s s i n g
% section .
room ( word )
look
goto ( word )
t a k e ( word )
inventory
l o o k i n ( word )
s l e e p ( word )
drop ( word )
e a t ( word )
use ( word )
database
l o c a t i o n ( symbol , symbol )
h e r e ( symbol )
have ( symbol )
t u r n e d o n ( symbol )
awake ( symbol )
s l e e p i n g ( symbol )

goal
makewindow ( 1 , 7 , 7 , SLEEPY HEAD ADVENTURE GAME , 0 , 0 , 2 5 , 8 0 ) ,
asserta ( awake ( p l a y e r ) ) ,
go .
clauses
go :
repeat ,
4

clearwindow ,
write ( Welcome s l e e p y head ! ) , nl ,
write ( To win t h i s game you need t o s l e e p . . ) , nl ,
write ( Have fun . . ! ) , nl ,
write ( ==================================== ) , nl , nl ,
look ,
go once ,
write ( Another game? ) ,
r e a d c h a r ( Replay ) ,
Replay = n .
g o o n c e :
awake ( p l a y e r ) ,
write ( > ) ,
r e a d l n ( Text ) ,
u p p e r l o w e r ( Text , ModText ) ,
ModText <> q u i t e , ! ,
p a r s e r ( ModText ,
,
),
go once .
g o o n c e :
sleeping ( player ) ,
write ( C o n g r a t u l a t i o n ! You win ! ! ! ) , nl ,
write ( P r e s s any key t o e x i t . . . ) ,
readln ( ) .

go once .

repeat .
repeat :
repeat .
p a 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 ) , ! .
,
,
) :
parser (
write ( s o r r y I do not understand . ) , nl .

% go t o a new p l a c e
do ( go , P l a c e ) :
P l a c e <> ,
goto ( P l a c e ) ,
!.
do ( go , P l a c e ) :
P l a c e = ,
write ( I didn t understand t he p l a c e you want t o go . ) ,
nl , ! .
% check inventory
do ( , i n v e n t o r y ) :
% w r i t e (You are l o o k i n g a t my i n v e n t o r y ) , nl ,
inventory ,
!.
% look at a object
do ( l o o k , P l a c e ) :
Place<> ,
%w r i t e (You are l o o k i n g a t , P l a c e ) , nl ,
l o o k i n ( Place ) ,
!.
% g e n e r a l l o o k t o g e t d e t a i l s i n f o a b o u t your environment
do ( l o o k ,
) :
%w r i t e (You are l o o k i n g ) , nl ,
look ,
!.
% t a k e a o b j e c t i n t o your i n v e n t o r y
6

do ( t a k e , Object ) :
Object <> ,
%w r i t e ( Take o b j e c t p r e d i c a t e s ) , nl ,
t a k e ( Object ) ,
!.
) :
do ( t a k e ,
write ( What do you want t o t a k e ? ) , nl , ! .

% p u t down o b j e c t
do ( drop , Object ) :
Object <> ,
%w r i t e ( Put , O b j e c t ) , nl ,
drop ( Object ) ,
nl ,
!.
) :
do ( drop ,
write ( What you want t o drop ? ) , nl , ! .
do ( e a t , Object ) :
Object <> ,
%w r i t e ( O b j e c t , was t o o t a s t y ) , nl ,
e a t ( Object ) ,
!.
) :
do ( e a t ,
write ( What you want t o e a t ? ) , nl .
do ( s l e e p , P l a c e ) :
P l a c e <> ,
s l e e p ( Place ) ,
!.
do ( s l e e p ,
) :
write ( Where a r e you want t o s l e e p ? ) , nl , ! .
do ( use , bed ) :
s l e e p ( bed ) , ! .

do ( use , Thing ) :
Thing <> ,
use ( Thing ) ,
!.
do ( use , ) :
write ( What you want t o use ? ) , nl , ! .
do ( , ) :
write ( S o r r y I do not understand . ) , nl .
tok ( Col , Text , Noun , Verb ) :
f r o n t t o k e n ( Text , Key , S t r x ) ,
checknoun ( Key , Noun ) ,
c h e c k v e r b ( Key , Verb ) ,
! ,
s t r l e n ( Key , Len ) ,
P o s l = Col + Len + 1 ,
tok ( P o s l , S t r x , Noun , Verb ) .
tok (

,
,
, Verb ) :
bound ( Verb ) ; Verb = .

tok (

,
, Noun ,
) :
bound ( Noun ) ; Noun = .

tok (

).

checknoun ( Key , Noun ) :


i s n o u n ( Key ) ,
Key = Noun , ! .
checknoun (

).

c h e c k v e r b ( Key , Verb ) :
i s v e r b ( Key ) ,
Key = Verb , ! .
checkverb ( , ) .
i s n o u n ( Key ) :
8

noun ( Key ) .
i s v e r b ( Key ) :
verb ( Key ) .
% Dictionalry
noun ( i n v e n t o r y ) .
noun ( h a l l ) .
noun ( d i n i n g ) .
noun ( k i t c h e n ) .
noun ( bedroom ) .
noun ( o f f i c e ) .
noun ( a p p l e ) .
noun ( banana ) .
noun ( p e p s i ) .
noun ( a l m i r a ) .
noun ( desk ) .
noun ( computer ) .
noun ( c h a i r ) .
noun ( b u s k e t ) .
noun ( t a b 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 ( t a k e ) .
verb ( l o o k ) .
verb ( put ) .
verb ( e a t ) .
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 ) ,
h e a d o f (Y , Outword ) , ! .
, Outword ) :
chksyn (Word ,
Outword = Word .
member (X , [X |
]).
| Y ] ) :
member (X , [
member (X , Y ) .
h e a d o f ( [ Head |

3.1.2

] , Head ) .

Decision Making
game.pro

i n c l u d e nl p . pro
domains
mpuz = goto ( s t r i n g )
predicates
door ( symbol , symbol )
e d i b l e ( symbol )
c o n n e c t ( 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 )
c a n g o ( symbol )
move ( symbol )
t a k e o b j e c t ( symbol )
p u z z l e ( mpuz )
i s h e r e ( symbol )
i s t a k e a b l e ( symbol )
c o n t a i n s ( symbol , symbol )
f u r n i t u r e ( symbol )
10

remove ( symbol )
clauses
% rooms t h a t are i n t h e game
room ( h a l l ) .
room ( d i n i n g ) .
room ( k i t c h e n ) .
room ( bedroom ) .
room ( o f f i c e ) .
% l i s t o f d o o r s i n t h e game
door ( h a l l , o f f i c e ) .
door ( h a l l , d i n i n g ) .
door ( d i n i n g , k i t c h e n ) .
door ( d i n i n g , bedroom ) .
% o b j e c t s o f t h e room
l o c a t i o n ( a lm i ra , h a l l ) .
location ( soffa , hall ).
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 ) .
location ( chair , o f f i c e ) .
location ( table , dining ) .
l o c a t i o n ( busket , t a b l e ) .
l o c a t i o n ( pepsi , table ) .
l o c a t i o n ( apple , b u s k e t ) .
l o c a t i o n ( banana , b u s k e t ) .
location ( plates , kitchen ) .
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 ) .
% edible objects
e d i b l e ( apple ) .
e d i b l e ( banana ) .
edible ( pepsi ) .
% f u r n i t u r e t h a t can t be t a k e n
f u r n i t u r e ( bed ) .
furniture ( table ).
11

f u r n i t u r e ( almira ) .
furniture ( chair ).
f u r n i t u r e ( computer ) .
furniture ( plates ).
furniture ( soffa ).
% c u r r e n t l y where t h e p l a y e r i s
here ( h a l l ) .
% c o n n e c t two room
c o n n e c t (X,Y): door (X,Y ) .
c o n n e c t (X,Y): door (Y,X ) .
% L i s t t h i n g s in a place
l i s t t h i n g s ( P l a c e ) :
l o c a t i o n (X, P l a c e ) ,
write (X) , write ( \ t ) ,
fail .
list things ( ).
% l i s t o f rooms t h a t are c o n n e c t e d t o t h e c u r r e n t room
l i s t c o n n e c t i o n s ( P l a c e ) :
c o n n e c t ( Place ,X) ,
write (X) , write ( \ t ) ,
fail .
list connections ( ).
l o o k :
here ( Place ) ,
write ( You a r e i n t he , P l a c e ) , nl ,
write ( You can s e e th e f o l l w i n g t h i n g s : ) , nl ,
l i s t t h i n g s ( P l a c e ) , nl ,
write ( You can go t o th e 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 ( P l a c e ) , nl .
l o o k i n ( Thing ):
l o c a t i o n ( , Thing ) ,
write ( The , Thing , c o n t a i n 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 n o t h i n g i n t he , Thing ) , nl .
goto ( P l a c e ):
p u z z l e ( go to ( P l a c e ) ) ,
can go ( Place ) ,
move ( P l a c e ) ,
look , ! .
goto ( ) .

c a n g o ( P l a c e ) :
h e r e (X) ,
c o n n e c t (X, P l a c e ) .
c a n g o ( P l a c e ):
write ( You can not go t o , P l a c e ) , nl , ! ,
fail .

move ( P l a c e ):
retract ( h e r e ( ) ) ,
asserta ( h e r e ( P l a c e ) ) .

t a k e (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 ) :
h e r e ( Here ) ,
c o n t a i n s ( Thing , Here ) , ! .
i s h e r e ( Thing ) :
write ( There i s no , Thing , h e r e . ) , nl ,
13

fail .

c o n t a i n s ( Thing , Here ) :
l o c a t i o n ( Thing , Here ) .
c o n t a i n s ( Thing , Here ) :
l o c a t i o n (X, Here ) ,
c o n t a i n 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 i c k up a , Thing ) , nl , ! ,
fail .
is takeable ( ).

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 t o your i n v e n t o r y ) , nl .

drop ( Thing ) :
have ( Thing ) ,
h e r e ( Here ) ,
retract ( have ( Thing ) ) ,
asserta ( l o c a t i o n ( Thing , Here ) ) ,
write ( You put down , Thing , on th e , Here , s f l o o r ) ,
nl , ! .

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

e a t ( Thing ) :
have ( Thing ) ,
e d i b l e ( Thing ) ,
14

remove ( Thing ) .
e a t ( Thing ) :
e d i b l e ( Thing ) ,
write ( You do not have th e , Thing , i n your ) ,
write ( i n v e n t o r y t o e a t ) ,
nl , ! .
e a t ( Thing ) :
write ( You can not e a t , Thing ) , nl , ! .

remove ( Thing ) :
have ( Thing ) ,
retract ( have ( Thing ) ) ,
write ( The , Thing , was ve ry t a s t y . ) ,
write ( You a r e f e e l i n g f r e s h . ) , nl .

i n v e n t o r y :
write ( You have : ) , nl ,
have (X) ,
write (X) , nl ,
fail .
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 ( t u r n e d o n ( f l a s h l i g h t ) ) ,
write ( You turned o f f t he 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 ( t u r n e d o n ( f l a s h l i g h t ) ) ,
write ( You turned on th e 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 th e f l a s h l i g h t ) , nl , ! .

15

use ( Thing ) :
write ( You do not know how t o use th e , Thing ) , nl , ! .
use ( ) .

s l e e p ( bed ) :
h e r e ( Here ) ,
c o n t a i n s ( bed , Here ) ,
retract ( awake ( p l a y e r ) ) ,
asserta ( s l e e p i n g ( p l a y e r ) ) ,
write ( You can t keep your e y e s anymore . As soon ) , nl ,
write ( as you l a y down i n t he bed you f a l l i n deep ) , nl ,
write ( s l e e p ! ) , nl .
s l e e p ( bed ) :
write ( You can not s e e s e e any bed h e r e . ) , nl , ! .
s l e e p ( P l a c e ) :
write ( You can not s l e e p i n , P l a c e ) , nl , ! .

% p u z z l e t o s o l v e b e f o r e advance f u r t h u r
p u z z l e ( go to ( 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 ) ,
!.
p u z z l e ( go to ( o f f i c e ) ) :
write ( I t s dark and you a r e a f r a i d o f dark . ) , nl ,
! , fail .
p u z z l e ( go to ( bedroom ) ) :
have ( key ) , ! .
p u z z l e ( go to ( bedroom ) ) :
write ( The door t o t he bedroom i s l o c k e d . ) ,
write ( You need a key t o e n t e r t h e r e . ) ,
nl , ! , f a i l .
puzzle ( ) .
16

3.2

Screenshoot

When you start the game, it will present a welcome screen with goal of the
game 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, go
to 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 room
without 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 more
rooms to the game by declaring new room(room name) and door(room f rom, room to)
clauses.
We can also use the code for totally new game story by changing only a
few line of codes. Future improvement can be made to this code by adding
capability 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 Natural
Language Processing.

19

References
[1] Turbo Prolog Owners Handbook. Scotts Valley, CA, USA: Borland Intl,
Jun. 1986.
[2] C. Townsend, Introduction to Turbo Prolog.
1987.

Berkeley: Sybex Inc, Jan.

[3] M. Dennis, Adventure in Prolog - Amzi! inc. [Online]. Available:


http://www.amzi.com/AdventureInProlog/index.php

20

Vous aimerez peut-être aussi