-
Notifications
You must be signed in to change notification settings - Fork 1
/
people.grace
149 lines (131 loc) · 4.95 KB
/
people.grace
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
146
147
148
149
// vi:set ai sm nu ts=4 sw=4 expandtab:
//
// Interactive Fiction demo program -- player object
// CS 420 term project to demonstrate an object-oriented parser design
// Steve Willoughby, Portland State University, Spring 2016
//
// ©2016 Steven L. Willoughby, All Rights Reserved. May be distributed and used
// under the terms of the Simplified (2-clause) BSD open-source license (See the
// LICENSE file for details.)
//
import "rooms" as rooms
import "parser" as parser
import "sys" as sys
def player = object {
var currentLocation := rooms.lobby
var inventory := list []
var scores := dictionary []
method isCarrying (obj) { inventory.contains(obj) }
method complain (message) {
print(message)
}
method quit {
takeAction "score"
sys.exit
}
method look {
currentLocation.fullLook
}
method moveDirection (direction) {
moveTo(currentLocation.destinationFrom(direction))
}
method addToScore (points) for (reason) {
if (scores.containsKey(reason).not) then {
scores.at(reason)put(points)
print "You hear the faint sound of a chime in the distance. (+{points} points)"
}
}
method totalScore { scores.fold({a, b → a + b}) startingWith (0) }
method printScore {
def total = totalScore
def maxScore = 50
print "You scored {total} out of a possible {maxScore} points."
printRank(total) of (maxScore)
scores.keysAndValuesDo({reason, points →
print " {reason}: {points} points"
})
}
method printRank (total) of (maxScore) {
def pct = (total*100) / maxScore
if (pct == 0) then {print "You are a complete loser."}
elseif (pct < 40) then {print "That gives you the rank of Amateur Adventurer."}
elseif (pct < 60) then {print "That gives you the rank of Intermediate Adventurer."}
elseif (pct < 99) then {print "That gives you the rank of Advanced Adventurer."}
else {print "That gives you the rank of Super Adventuring Genius!"}
}
method moveTo (location) {
if (currentLocation.canLeaveHere(self)) then {
if (location.canMoveHere(self)) then {
currentLocation := location
currentLocation.look
currentLocation.visitedHere
setParserContext
}
}
}
method setParserContext {
parser.vocabulary.clear
currentLocation.updateVocabulary(parser.vocabulary)
for (inventory) do { item → item.updateVocabulary(parser.vocabulary) }
}
method takeObject (obj) {
if (obj.pickUp) then {
currentLocation.yieldObject(obj)
inventory.add(obj)
if (obj.takeScore > 0) then {
addToScore (obj.takeScore) for ("picked up {obj.shortDescription}")
}
}
}
method dropObject (obj) {
if (giveUpObject(obj)) then {
currentLocation.acceptObject(obj)
}
}
method giveUpObject (obj) {
if (inventory.contains(obj)) then {
if (obj.drop) then {
inventory.remove(obj)
return true
}
}
return false
}
method showInventory {
if (inventory.size == 0) then {
print "You are empty-handed."
}
else {
print "You are carrying:"
for (inventory) do { item →
print " {item.shortDescription}"
}
}
}
method takeAction (verb) {
match (verb)
case {"inventory" → showInventory }
case {"score" → printScore }
case {_ → print ‹I'm not sure how to just “{verb}”.
Do you perhaps need a noun in that sentence somewhere?›}
}
method takeAction (verb) on (anObject) {
match (verb)
case { "take" → takeObject(anObject) }
case { "drop" → dropObject(anObject) }
case { _ → anObject.doAction(verb) forPerson (self) }
}
method takeAction (verb) on (directObject) indirect (preposition) with (indirectObject) {
try { indirectObject.doIndirectAction (verb) forPerson (self) with (directObject) andPreposition (preposition) }
catch { parser.ActionNotResolved →
try {directObject.doAction (verb) forPerson (self) withIndirectObject (indirectObject) andPreposition (preposition) }
catch { parser.ActionNotResolved →
print "I don't know how to use “{verb} {preposition}” in that manner."
}
}
}
method takeAction (verb) indirect (preposition) with (indirectObject) {
try { indirectObject.doIndirectAction (verb) forPerson (self) withPreposition (preposition) }
catch { parser.ActionNotResolved → print "I don't know how to {verb} {preposition} that." }
}
}