From f59e957d741814459a0dca2993a4d3d4a2325688 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Sat, 22 Mar 2014 01:02:47 +1100 Subject: Reorganising code --- agent.prolog | 54 ++++++++++++++++++++++++++++++++++++++++++++- item.prolog | 22 ------------------- main.prolog | 15 +------------ map.prolog | 10 +-------- parser.prolog | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ read.prolog | 56 ----------------------------------------------- 6 files changed, 125 insertions(+), 102 deletions(-) delete mode 100644 item.prolog create mode 100644 parser.prolog delete mode 100644 read.prolog diff --git a/agent.prolog b/agent.prolog index 994b77d..0c1d445 100644 --- a/agent.prolog +++ b/agent.prolog @@ -1,7 +1,24 @@ -:- module(agent, [look/0, move/1, take/1]). +:- module(agent, [init, look/0, move/1, take/1]). +:- consult('parser.prolog'). +:- consult('map.prolog'). + + + + +% initialisation + +init :- + retractall(here(_)), + asserta(here(a)), !. + + + + +% command functions + look :- here(L), writeItemsAt(L), @@ -23,3 +40,38 @@ take(T) :- asserta(holding(T)), write('Taken.'), !. + + + +% letting the player know what's going on + +writeItemsAt(L) :- + itemAt(X,L), + description(X,D), + write(D), + fail. +writeItemsAt(_) :- true. + + +writeExits(L) :- + connects(L,_,D), + join(['You see an exit to the ',D,'.\n'],M), + write(M), + fail. +writeExits(_) :- true. + + + + +% basic facts about the maze + +itemAt(ladder,a). + + +canTake(gold). +canTake(deadWumpus). + + +description(ladder, 'There is a rope ladder hanging from the ceiling here.\n'). +description(gold, 'You see a glitter along the sandy floor of the cave.\n'). + diff --git a/item.prolog b/item.prolog deleted file mode 100644 index 11e469c..0000000 --- a/item.prolog +++ /dev/null @@ -1,22 +0,0 @@ - -:- module(item, [itemAt/2, writeItemsAt/1, canTake/1, description/2]). - - -itemAt(ladder,a). - - -writeItemsAt(L) :- - itemAt(X,L), - description(X,D), - write(D), - fail. -writeItemsAt(_) :- true. - - -canTake(gold). -canTake(deadWumpus). - - -description(ladder, 'There is a rope ladder hanging from the ceiling here.\n'). -description(gold, 'You see a glitter along the sandy floor of the cave.\n'). - diff --git a/main.prolog b/main.prolog index ddf7baa..aa25bc5 100644 --- a/main.prolog +++ b/main.prolog @@ -1,19 +1,6 @@ -:- consult('map.prolog'). +:- consult('parser.prolog'). :- consult('agent.prolog'). -:- consult('read.prolog'). -:- consult('item.prolog'). -init :- - retractall(here(_)), - asserta(here(a)), !. - - -join([X|Y],R) :- - join(Y,Rx), - atom_concat(X,Rx,R). -join([X],R) :- - R = X. - diff --git a/map.prolog b/map.prolog index 5c432d9..2c5613a 100644 --- a/map.prolog +++ b/map.prolog @@ -1,13 +1,5 @@ -:- module(map, [connects/3, writeExits/1]). - - -writeExits(L) :- - connects(L,_,D), - join(['You see an exit to the ',D,'.\n'],M), - write(M), - fail. -writeExits(_) :- true. +:- module(map, [connects/3]). % map has 20 rooms, labelled a through t diff --git a/parser.prolog b/parser.prolog new file mode 100644 index 0000000..c4e0fff --- /dev/null +++ b/parser.prolog @@ -0,0 +1,70 @@ + +:- module(parser, [prompt/1, readList/1, readLine/1, split/2, join/2]). + + +% functions for dealing with lines of text +% eg obtaining one, parsing it into a list of words, and rejoining it + +% prompt provides the user with a '>' prompt +% readList merely obtains and parses the input +% readLine obtains a raw line of input +% split parses a line of text into words +% join takes a list of words and joins them into one string + + +prompt(L) :- + write('> '), + readList(L). + + +readList(L) :- + readLine(X), + split(X,L). + + +readLine(L) :- + get_char(C), + readLine_tail(C,L). + + +readLine_tail('\n',[]) :- !. +readLine_tail(C,[C|X]) :- + get_char(C2), + readLine_tail(C2,X). + + +split(X,R) :- + wordList(R,X,[]), !. + + +join([X|Y],R) :- + join(Y,Rx), + atom_concat(X,Rx,R). +join([X],R) :- + R = X. + + +wordList(X) --> whitespace, wordList(X). +wordList([X]) --> word(X). +wordList([X]) --> word(X), whitespace. +wordList([X|Y]) --> word(X), whitespace, wordList(Y). + + +word(W) --> charList(X), {atom_chars(W,X)}. + + +charList([X|Xs]) --> char(X), charList(Xs). +charList([X]) --> char(X). + + +whitespace --> whsp, whitespace. +whitespace --> whsp. + + +whsp --> oneOf(_,[' ', '\r', '\n', '\t']). +char(X) --> noneOf(X,[' ', '\r', '\n', '\t']). + + +oneOf(X,L) --> [X], {member(X,L)}. +noneOf(X,L) --> [X], {not(member(X,L))}. + diff --git a/read.prolog b/read.prolog deleted file mode 100644 index 63d3ae7..0000000 --- a/read.prolog +++ /dev/null @@ -1,56 +0,0 @@ - -:- module(read, [prompt/1, readList/1]). - - -% functions for obtaining a line of input text and parsing -% it into a list of words - -% prompt provides the user with a '>' prompt as well -% readList merely obtains and parses the input - - -prompt(L) :- - write('> '), - readList(L). - - -readList(L) :- - readLine(X), - wordList(L,X,[]), !. - - -readLine(L) :- - get_char(C), - readLine_tail(C,L). - - -readLine_tail('\n',[]) :- !. -readLine_tail(C,[C|X]) :- - get_char(C2), - readLine_tail(C2,X). - - -wordList(X) --> whitespace, wordList(X). -wordList([X]) --> word(X). -wordList([X]) --> word(X), whitespace. -wordList([X|Y]) --> word(X), whitespace, wordList(Y). - - -word(W) --> charList(X), {atom_chars(W,X)}. - - -charList([X|Xs]) --> char(X), charList(Xs). -charList([X]) --> char(X). - - -whitespace --> whsp, whitespace. -whitespace --> whsp. - - -whsp --> oneOf(_,[' ', '\r', '\n', '\t']). -char(X) --> noneOf(X,[' ', '\r', '\n', '\t']). - - -oneOf(X,L) --> [X], {member(X,L)}. -noneOf(X,L) --> [X], {not(member(X,L))}. - -- cgit