-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.go
335 lines (302 loc) · 8.51 KB
/
player.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
*****************************************************
* © 2019 Stefano Peris <[email protected]> *
*****************************************************
*
* Released under the GNU/GPL 3.0 license
*
* github: <https://github.com/XenonLab-Studio/GoZork>
*
*
* :'######::::'#######::'########::'#######::'########::'##:::'##:
* ##... ##::'##.... ##:..... ##::'##.... ##: ##.... ##: ##::'##::
* ##:::..::: ##:::: ##::::: ##::: ##:::: ##: ##:::: ##: ##:'##:::
* ##::'####: ##:::: ##:::: ##:::: ##:::: ##: ########:: #####::::
* ##::: ##:: ##:::: ##::: ##::::: ##:::: ##: ##.. ##::: ##. ##:::
* ##::: ##:: ##:::: ##:: ##:::::: ##:::: ##: ##::. ##:: ##:. ##::
* . ######:::. #######:: ########:. #######:: ##:::. ##: ##::. ##:
* :......:::::.......:::........:::.......:::..:::::..::..::::..::
*
* Textual adventure written in golang inspired by "Zork I"
*/
package main
import (
"bufio"
"fmt"
"sort"
"strings"
)
type Player struct {
console *bufio.ReadWriter
room *Room
maxPoints byte
points byte
trollai TrollAI
dead bool
win bool
// a player is a object container (inventory)
ObjectContainer
}
// verbs are mapped to Player methods:
func (p *Player) Go(args []string) bool {
if newRoom := p.room.ExitDirection(args[0]); newRoom != nil {
p.room.Leave()
p.room = newRoom
p.Look(!newRoom.visited)
newRoom.Enter()
p.trollai.PlayerMove()
} else {
p.Println("You can't go in that direction.")
}
return true
}
func (p *Player) Look(printDesc bool) bool {
//p.room.Look(true)
p.Println(p.room.name)
if printDesc && p.room.desc != "" {
p.Println(p.room.desc)
}
if objstr, err := p.room.ObjectNames(); err == nil {
p.Println("There is " + objstr + " here.")
}
return true
}
func (p *Player) LookAt(args []string) bool {
if len(args) == 0 {
return p.Look(true)
}
if obj := p.FindNearObject(args); obj != nil {
p.Println(obj.GetDesc())
} else {
p.Printf("I don't see any %v here.\n", strings.Join(args, " "))
}
return true
}
func (p *Player) Take(args []string) bool {
if obj := p.room.FindObject(args); obj != nil {
if obj.carryable && !obj.fixture {
p.room.RemoveObject(obj)
p.AddObject(obj)
p.Println("Taken.")
} else {
p.Println("This can't be taken.")
}
} else {
p.Printf("I don't see any %v here.\n", strings.Join(args, " "))
}
return true
}
func (p *Player) Push(obj *Object) bool {
if obj != nil {
if cb := obj.verbs["PUSH"]; cb != nil {
cb(obj, p)
} else {
p.Println("You can't push this.")
}
} else {
p.Println("I don't know what you are referring to.")
}
return true
}
func (p *Player) Pull(obj *Object) bool {
if obj != nil {
if cb := obj.verbs["PULL"]; cb != nil {
cb(obj, p)
} else {
p.Println("You can't pull this.")
}
} else {
p.Println("I don't know what you are referring to.")
}
return true
}
func (p *Player) LookUnder(obj *Object) bool {
if obj != nil {
if cb := obj.verbs["LOOK UNDER"]; cb != nil {
cb(obj, p)
} else {
p.Println("You don't see anything out of the ordinary.")
}
} else {
p.Println("I don't know what you are referring to.")
}
return true
}
func (p *Player) Drop(args []string) bool {
if obj := p.FindObject(args); obj != nil {
p.RemoveObject(obj)
p.room.AddObject(obj)
p.Println("Dropped.")
} else {
p.Printf("I don't see any %v here.\n", strings.Join(args, " "))
}
return true
}
func (p *Player) Inventory(args []string) bool {
if objstr, err := p.ObjectNames(); err == nil {
p.Println("You are carrying " + objstr + ".")
} else {
p.Println("You are empty handed.")
}
return true
}
func (p *Player) Open(args []string) bool {
if obj := p.FindNearObject(args); obj != nil {
if obj.openable {
if !obj.open {
obj.open = true
p.Println("Opened.")
} else {
p.Println("Already open.")
}
} else {
p.Println("I can't open that.")
}
} else {
p.Printf("I don't see any %v here.\n", strings.Join(args, " "))
}
return true
}
func (p *Player) Close(args []string) bool {
if obj := p.FindNearObject(args); obj != nil {
if obj.openable {
if obj.open {
obj.open = false
p.Println("Closed.")
} else {
p.Println("Already closed.")
}
} else {
p.Println("I can't close that.")
}
} else {
p.Printf("I don't see any %v here.\n", strings.Join(args, " "))
}
return true
}
func (p *Player) Wait() bool {
p.Println("Time passes.")
return true
}
func (p *Player) Help(args []string) bool {
p.Println("\nThis is a text adventure game, the goal is to find and kill the troll.")
p.Println("\nThe game only understands very simple single-verb, single-object sentences, for instance: PICK UP HAT, or OPEN DOOR etc.")
p.Println("\nThe Verbs this game understands are: LOOK, LOOK AT, LOOK UNDER, PUSH, PULL, TAKE, DROP, WAIT, OPEN, CLOSE and INVENTORY.")
p.Println("\nDirections are: NORTH, SOUTH, EAST, WEST, UP, DOWN, IN and OUT.")
p.Println("\nThere are also many aliases for verbs and directions.")
return true
}
func (p *Player) GivePoints(off byte) {
p.points += off
p.Printf("(\nYour score increased by %d points, you now have %d/%d points.)\n", off, p.points, p.maxPoints)
}
func (p *Player) Die() {
p.dead = true
p.Println(" **** GAME OVER! You are dead.")
p.Printf("You managed to score %d out of %d possible points.\n", p.points, p.maxPoints)
}
func (p *Player) Win() {
p.win = true
p.Println(" **** CONGRATULATIONS! YOU WON THE GAME!")
p.Printf("You managed to score %d out of %d possible points.\n", p.points, p.maxPoints)
}
func (p *Player) FindNearObject(args []string) *Object {
// objects in the current room:
if obj := p.room.FindObject(args); obj != nil {
return obj
}
// objects/items in the player inventory:
if obj := p.FindObject(args); obj != nil {
return obj
}
return nil
}
func (p *Player) ExecuteCommand(command string) bool {
verbMap := map[string]func([]string) bool{
"GO": func(args []string) bool { return p.Go(args) },
"LOOK": func(args []string) bool { return p.Look(true) },
"LOOK AT": func(args []string) bool { return p.LookAt(args) },
"TAKE": func(args []string) bool { return p.Take(args) },
"PUSH": func(args []string) bool { return p.Push(p.FindNearObject(args)) },
"PULL": func(args []string) bool { return p.Pull(p.FindNearObject(args)) },
"LOOK UNDER": func(args []string) bool { return p.LookUnder(p.room.FindObject(args)) },
"DROP": func(args []string) bool { return p.Drop(args) },
"OPEN": func(args []string) bool { return p.Open(args) },
"WAIT": func(args []string) bool { return p.Wait() },
"CLOSE": func(args []string) bool { return p.Close(args) },
"INVENTORY": func(args []string) bool { return p.Inventory(args) },
"XYZZY": func(args []string) bool { return true },
"HELP": func(args []string) bool { return p.Help(args) },
}
delegated := false
// we need to make sure to sort the verbs by length first:
verbs := []string{} // make([]string, len(verbMap))
for verb := range verbMap {
verbs = append(verbs, verb)
}
sort.Sort(ByLength(verbs))
sort.Sort(sort.Reverse(sort.StringSlice(verbs)))
for _, verb := range verbs {
fn := verbMap[verb]
if verb == command {
delegated = fn(make([]string, 0))
} else if i := strings.Index(command, verb+" "); i == 0 {
command = command[len(verb)+1:]
delegated = fn(strings.Split(command, " "))
}
}
if !delegated {
p.Println("Sorry, what?")
} else {
p.trollai.Turn()
}
return delegated
}
func (p *Player) VerbAliasReplace(cmd string) string {
for verb, aliases := range verbAliasMap {
for _, alias := range aliases {
if alias == cmd {
return verb
} else if i := strings.Index(cmd, verb+" "); i == 0 {
return cmd
} else if i := strings.Index(cmd, alias+" "); i == 0 {
return strings.Replace(cmd, alias+" ", verb+" ", 1)
}
}
}
return cmd
}
func (p *Player) Println(line string) {
p.Printf(line + "\n")
}
func (p *Player) Printf(format string, args ...interface{}) {
line := fmt.Sprintf(format, args...)
p.console.WriteString(line)
p.console.Flush()
}
func (p *Player) Run() {
p.Println("Welcome to GOZORK! Type HELP for help.")
// start the player west of the house
p.room.Enter()
p.Look(true)
for {
p.Printf(">")
cmd, err := p.console.ReadString('\n')
if err != nil {
p.Println("error reading console")
break
}
cmd = strings.ToUpper(strings.Trim(cmd, "\n"))
// replace alias mapping
cmd = p.VerbAliasReplace(cmd)
//fmt.Printf("[command read as: %v]\n", cmd)
if cmd == "QUIT" {
p.Println("\nThanks for playing!")
break
}
p.ExecuteCommand(cmd)
if p.dead || p.win {
break
}
}
}