summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2014-03-22 01:02:47 +1100
committerJed Barber <jjbarber@y7mail.com>2014-03-22 01:02:47 +1100
commitf59e957d741814459a0dca2993a4d3d4a2325688 (patch)
tree31ce30650735fa50de0aba9268dfbdf797bfe3cc
parent410fc38f935b552a58af2e716d2fe814eee432c9 (diff)
Reorganising code
-rw-r--r--agent.prolog54
-rw-r--r--item.prolog22
-rw-r--r--main.prolog15
-rw-r--r--map.prolog10
-rw-r--r--parser.prolog (renamed from read.prolog)24
5 files changed, 74 insertions, 51 deletions
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/read.prolog b/parser.prolog
index 63d3ae7..c4e0fff 100644
--- a/read.prolog
+++ b/parser.prolog
@@ -1,12 +1,15 @@
-:- module(read, [prompt/1, readList/1]).
+:- module(parser, [prompt/1, readList/1, readLine/1, split/2, join/2]).
-% functions for obtaining a line of input text and parsing
-% it into a list of words
+% 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 as well
+% 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) :-
@@ -16,7 +19,7 @@ prompt(L) :-
readList(L) :-
readLine(X),
- wordList(L,X,[]), !.
+ split(X,L).
readLine(L) :-
@@ -30,6 +33,17 @@ readLine_tail(C,[C|X]) :-
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.