summaryrefslogtreecommitdiff
path: root/agent.prolog
blob: ba59a43f6c6868be5a3c81c1b39a40df64ea25c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

:- module(agent, [init, look/0, move/1, take/1]).


:- consult('parser.prolog').
:- consult('map.prolog').




% initialisation

init :-
    initPos,
    initPits,
    initWumpus,
    initGold,
    initBats, !.


initPos :-
    retractall(here(_)),
    roomList(Rooms),
    filter(Rooms, somethingAt, Emptyrooms),
    random_member(X,Emptyrooms),
    asserta(here(X)).


initPits :- true.


initWumpus :-
    retractall(wumpusAt(_)),
    roomList(Rooms),
    filter(Rooms, somethingAt, Emptyrooms),
    random_member(X,Emptyrooms),
    asserta(wumpusAt(X)).


initGold :- true.


initBats :- true.




% command functions

look :-
    here(Location),
    writeSenses(Location),
    writeExits(Location).


move(Direction) :-
    here(Location),
    connects(Location, New, Direction),
    retract(here(Location)),
    asserta(here(New)),
    join(['You move to the ',Direction,' .\n\n'],W),
    write(W), !.


take(Item) :-
    here(Location),
    itemAt(Item,Location),
    canTake(Item),
    retract(itemAt(Item,Location)),
    asserta(holding(Item)),
    write('Taken.\n\n'), !.




% letting the player know what's going on

writeSenses(Location) :-
    glitter(Location),
    breeze(Location),
    bats(Location),
    stench(Location).


glitter(Location) :-
    current_predicate(goldAt/1),
    goldAt(Location),
    write('You see a glitter along the sandy floor of the cave.\n').
glitter(_) :- true.


breeze(Location) :-
    current_predicate(pitAt/1),
    connects(Location, ConnectedRoom, _),
    pitAt(ConnectedRoom),
    write('A cold breeze blows through the room, making you shiver slightly.\n').
breeze(_) :- true.


bats(Location) :-
    current_predicate(batsAt/1),
    connects(Location, ConnectedRoom, _),
    batsAt(ConnectedRoom),
    write('All available surfaces are covered in guano. How unsanitary.\n').
bats(_) :- true.


stench(Location) :-
    current_predicate(wumpusAt/1),
    connects(Location, ConnectedRoom, _),
    wumpusAt(ConnectedRoom),
    write('An overpowering stench fills your nose.\n').
stench(_) :- true.


writeExits(Location) :-
    findall(X, connects(Location,_,X), Exits),
    intercalate(Exits, ', ', O),
    join(['There are exits to the ',O,'.\n'],W),
    write(W).




% miscellaneous clauses

somethingAt(X) :- current_predicate(here/1), here(X).
somethingAt(X) :- current_predicate(wumpusAt/1), wumpusAt(X).
somethingAt(X) :- current_predicate(pitAt/1), pitAt(X).
somethingAt(X) :- current_predicate(goldAt/1), goldAt(X).
somethingAt(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).