summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2014-04-03 21:07:06 +1100
committerJed Barber <jjbarber@y7mail.com>2014-04-03 21:07:06 +1100
commit0b65dd864a804a6b96ca1b8aeacd947eeb361538 (patch)
tree288974d3d6f9b9ddf6d1cd7f03c0a8215f4069e4
parent133d71983ea4e39e6e971ca052b2eb0bf7437cc6 (diff)
Moving some clauses around to more appropriate modules
-rw-r--r--agent.prolog41
-rw-r--r--map.prolog25
-rw-r--r--misc.prolog20
-rw-r--r--parser.prolog9
4 files changed, 53 insertions, 42 deletions
diff --git a/agent.prolog b/agent.prolog
index 8adbe3e..41167c4 100644
--- a/agent.prolog
+++ b/agent.prolog
@@ -2,6 +2,7 @@
:- module(agent, [init, look/0, move/1, take/1, shoot/1]).
+:- consult('misc.prolog').
:- consult('parser.prolog').
:- consult('map.prolog').
@@ -275,43 +276,3 @@ hazardAt(X) :- current_predicate(wumpusAt/1), wumpusAt(X).
hazardAt(X) :- current_predicate(pitAt/1), pitAt(X).
hazardAt(X) :- current_predicate(batsAt/1), batsAt(X).
-
-intercalate([X|Y], Spacer, Result) :-
- intercalate(Y, Spacer, Tail),
- atom_concat(X, Spacer, Head),
- atom_concat(Head, Tail, Result).
-intercalate([X], _, Result) :- Result = X.
-
-
-filter(List, Predicate, Result) :-
- Test =.. [Predicate,X],
- findall(X, Test, No),
- subtract(List, No, Result).
-
-
-head(X,[Y]) :- X = Y.
-head(X,[Y|_]) :- X = Y.
-
-
-tail(X,[]) :- X = [].
-tail(X,[_]) :- X = [].
-tail(X,[_|Y]) :- X = Y.
-
-
-isConnected([]) :- true.
-isConnected(X) :-
- head(Y,X),
- tail(Z,X),
- connectTail([Y],Z).
-
-connectTail(_,[]) :- true.
-connectTail([],_) :- false.
-connectTail(Open,G) :-
- head(X,Open),
- tail(Y,Open),
- findall(N, connects(X,N,_), Ns),
- subtract(G,Ns,Gx),
- subtract(G,Gx,Nx),
- append(Y,Nx,Yx),
- connectTail(Yx,Gx).
-
diff --git a/map.prolog b/map.prolog
index 84ec8c4..f5bb863 100644
--- a/map.prolog
+++ b/map.prolog
@@ -1,5 +1,10 @@
-:- module(map, [roomList/1, connects/3]).
+:- module(map, [roomList/1, isConnected/1, connects/3]).
+
+
+:- consult('misc.prolog').
+
+
% map has 20 rooms, labelled a through t
@@ -11,6 +16,24 @@
roomList(X) :- X = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t].
+isConnected([]) :- true.
+isConnected(X) :-
+ head(Y,X),
+ tail(Z,X),
+ connectTail([Y],Z).
+
+connectTail(_,[]) :- true.
+connectTail([],_) :- false.
+connectTail(Open,G) :-
+ head(X,Open),
+ tail(Y,Open),
+ findall(N, connects(X,N,_), Ns),
+ subtract(G,Ns,Gx),
+ subtract(G,Gx,Nx),
+ append(Y,Nx,Yx),
+ connectTail(Yx,Gx).
+
+
% loop around the middle
connects(a,b,northeast).
diff --git a/misc.prolog b/misc.prolog
new file mode 100644
index 0000000..6d8ad68
--- /dev/null
+++ b/misc.prolog
@@ -0,0 +1,20 @@
+
+:- module(misc, [filter/3, head/2, tail/2]).
+
+
+
+
+filter(List, Predicate, Result) :-
+ Test =.. [Predicate,X],
+ findall(X, Test, No),
+ subtract(List, No, Result).
+
+
+head(X,[Y]) :- X = Y.
+head(X,[Y|_]) :- X = Y.
+
+
+tail(X,[]) :- X = [].
+tail(X,[_]) :- X = [].
+tail(X,[_|Y]) :- X = Y.
+
diff --git a/parser.prolog b/parser.prolog
index c4e0fff..c9f9771 100644
--- a/parser.prolog
+++ b/parser.prolog
@@ -1,5 +1,5 @@
-:- module(parser, [prompt/1, readList/1, readLine/1, split/2, join/2]).
+:- module(parser, [prompt/1, readList/1, readLine/1, split/2, join/2, intercalate/3]).
% functions for dealing with lines of text
@@ -44,6 +44,13 @@ join([X],R) :-
R = X.
+intercalate([X|Y], Spacer, Result) :-
+ intercalate(Y, Spacer, Tail),
+ atom_concat(X, Spacer, Head),
+ atom_concat(Head, Tail, Result).
+intercalate([X], _, Result) :- Result = X.
+
+
wordList(X) --> whitespace, wordList(X).
wordList([X]) --> word(X).
wordList([X]) --> word(X), whitespace.