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
|
package hacking
import (
"fmt"
"math/rand"
"strconv"
"strings"
)
var dodecahedron = [20][3]int{
{1, 4, 7},
{0, 2, 9},
{1, 3, 11},
{2, 4, 13},
{0, 3, 5},
{4, 6, 14},
{5, 7, 16},
{0, 6, 8},
{7, 9, 17},
{1, 8, 10},
{9, 11, 18},
{2, 10, 12},
{11, 13, 19},
{3, 12, 14},
{5, 13, 15},
{14, 16, 19},
{6, 15, 17},
{8, 16, 18},
{10, 17, 19},
{12, 15, 18},
}
type WumpusGame struct {
player int
wumpus int
pits [2]int
ice [2]int
probes int
gameOver bool
won bool
}
func NewWumpusGame() *WumpusGame {
return &WumpusGame{}
}
func (w *WumpusGame) Name() string {
return "Hunt the Wumpus"
}
func (w *WumpusGame) Init(level int) string {
taken := make(map[int]bool)
w.player = rand.Intn(20)
taken[w.player] = true
w.wumpus = randFree(taken)
taken[w.wumpus] = true
w.pits[0] = randFree(taken)
taken[w.pits[0]] = true
w.pits[1] = randFree(taken)
taken[w.pits[1]] = true
w.ice[0] = randFree(taken)
taken[w.ice[0]] = true
w.ice[1] = randFree(taken)
w.probes = 5
w.gameOver = false
w.won = false
var sb strings.Builder
sb.WriteString("=== HUNT THE ROGUE AI ===\n")
sb.WriteString("\n")
sb.WriteString("You've jacked into an abandoned network. Somewhere in this maze of\n")
sb.WriteString("20 nodes, a rogue AI lurks. You have 5 probes to find and neutralize it.\n")
sb.WriteString("\n")
sb.WriteString("Hazards:\n")
sb.WriteString(" - Rogue AI: Moves to an adjacent node if you enter its node. Kills you.\n")
sb.WriteString(" - Data Traps: Fall in and you're fried. 2 in the network.\n")
sb.WriteString(" - ICE: Grabs you and dumps you in a random node. 2 in the network.\n")
sb.WriteString("\n")
sb.WriteString("Commands:\n")
sb.WriteString(" move <node> - Move to an adjacent node (1-20)\n")
sb.WriteString(" shoot <node> - Launch a probe into an adjacent node (1-20)\n")
sb.WriteString(" status - Show your current status\n")
sb.WriteString(" map - Show network map\n")
sb.WriteString(" jack out - Disconnect (forfeit)\n")
sb.WriteString("\n")
sb.WriteString(w.roomDesc())
return sb.String()
}
func (w *WumpusGame) HandleInput(input string) (string, bool, bool) {
input = strings.TrimSpace(strings.ToLower(input))
parts := strings.Fields(input)
if len(parts) == 0 {
return "Unknown command.", false, false
}
cmd := parts[0]
switch cmd {
case "move", "m":
if len(parts) < 2 {
return "Move where? (e.g., move 3)", false, false
}
n, err := strconv.Atoi(parts[1])
if err != nil || n < 1 || n > 20 {
return "Invalid node. Use 1-20.", false, false
}
return w.doMove(n - 1)
case "shoot", "probe", "launch", "s":
if len(parts) < 2 {
return "Shoot where? (e.g., shoot 3)", false, false
}
n, err := strconv.Atoi(parts[1])
if err != nil || n < 1 || n > 20 {
return "Invalid node. Use 1-20.", false, false
}
return w.doShoot(n - 1)
case "status":
return w.doStatus(), false, false
case "map":
return w.doMap(), false, false
default:
n, err := strconv.Atoi(cmd)
if err == nil && n >= 1 && n <= 20 {
return w.doMove(n - 1)
}
return "Commands: move <N>, shoot <N>, status, map, jack out", false, false
}
}
func (w *WumpusGame) doMove(target int) (string, bool, bool) {
if !w.hasAdjacent(w.player, target) {
adj := w.adjStrings(w.player)
return fmt.Sprintf("Can't move there. Adjacent nodes: %s", strings.Join(adj, ", ")), false, false
}
w.player = target
return w.checkRoom()
}
func (w *WumpusGame) doShoot(target int) (string, bool, bool) {
if !w.hasAdjacent(w.player, target) {
adj := w.adjStrings(w.player)
return fmt.Sprintf("Can't shoot there. Adjacent nodes: %s", strings.Join(adj, ", ")), false, false
}
w.probes--
if target == w.wumpus {
w.gameOver = true
w.won = true
return "Your probe hits the rogue AI! It destabilizes and crashes.\n\nConnection terminated.", true, true
}
var sb strings.Builder
sb.WriteString(fmt.Sprintf("Your probe finds nothing in node %d.", target+1))
if w.probes == 0 {
sb.WriteString("\nYou're out of probes. The rogue AI detects your presence and terminates your connection.")
w.gameOver = true
w.won = false
return sb.String(), true, false
}
if rand.Float64() < 0.75 {
w.wumpus = w.randomAdjacent(w.wumpus)
sb.WriteString("\nYou hear something shift in the network...")
}
sb.WriteString("\n")
sb.WriteString(w.roomDesc())
return sb.String(), false, false
}
func (w *WumpusGame) checkRoom() (string, bool, bool) {
var sb strings.Builder
cur := w.player
if cur == w.wumpus {
if rand.Float64() < 0.75 {
newRoom := w.randomAdjacent(w.wumpus)
w.wumpus = newRoom
sb.WriteString("--- Node " + fmt.Sprint(cur+1) + " ---\n")
sb.WriteString("The rogue AI stirs and relocates to a nearby node...\n")
sb.WriteString("\n")
sb.WriteString(w.roomDesc())
return sb.String(), false, false
}
sb.WriteString("The rogue AI devours your connection!\n\nConnection terminated.")
w.gameOver = true
w.won = false
return sb.String(), true, false
}
for _, p := range w.pits {
if cur == p {
sb.WriteString("You've fallen into a data trap! Your connection is severed.")
w.gameOver = true
w.won = false
return sb.String(), true, false
}
}
for _, i := range w.ice {
if cur == i {
sb.WriteString("ICE detected! You're relocated to a random node...\n")
w.player = rand.Intn(20)
chained, _, _ := w.checkRoom()
sb.WriteString(chained)
return sb.String(), false, false
}
}
sb.WriteString(w.roomDesc())
return sb.String(), false, false
}
func (w *WumpusGame) roomDesc() string {
cur := w.player
var sb strings.Builder
sb.WriteString(fmt.Sprintf("--- Node %d ---\n", cur+1))
adj := w.adjStrings(cur)
sb.WriteString(fmt.Sprintf("Tunnels lead to: %s\n", strings.Join(adj, ", ")))
for _, a := range dodecahedron[cur] {
if a == w.wumpus {
sb.WriteString("{C4}You detect corrupted data nearby...{/}\n")
}
for _, p := range w.pits {
if a == p {
sb.WriteString("{D0}You sense a void in the network...{/}\n")
}
}
for _, i := range w.ice {
if a == i {
sb.WriteString("{E2}You hear static crackling...{/}\n")
}
}
}
return sb.String()
}
func (w *WumpusGame) doStatus() string {
return fmt.Sprintf("Node: %d | Probes: %d/5", w.player+1, w.probes)
}
func (w *WumpusGame) doMap() string {
var sb strings.Builder
sb.WriteString("=== Network Map ===\n")
for i := 0; i < 20; i++ {
marker := " "
if i == w.player {
marker = "*"
}
adj := dodecahedron[i]
sb.WriteString(fmt.Sprintf(" [%s] Node %2d -> %2d, %2d, %2d\n",
marker, i+1, adj[0]+1, adj[1]+1, adj[2]+1))
}
sb.WriteString(fmt.Sprintf("\nYou are at Node %d.\n", w.player+1))
return sb.String()
}
func (w *WumpusGame) hasAdjacent(from, to int) bool {
for _, a := range dodecahedron[from] {
if a == to {
return true
}
}
return false
}
func (w *WumpusGame) randomAdjacent(r int) int {
adj := dodecahedron[r]
return adj[rand.Intn(len(adj))]
}
func (w *WumpusGame) adjStrings(r int) []string {
var out []string
for _, a := range dodecahedron[r] {
out = append(out, fmt.Sprint(a+1))
}
return out
}
func randFree(taken map[int]bool) int {
for {
r := rand.Intn(20)
if !taken[r] {
return r
}
}
}
|