Skip to content

Instantly share code, notes, and snippets.

@guregu
Created December 4, 2022 19:26
Show Gist options
  • Save guregu/e514a57c1eb426e4bf3d5167c5a715bc to your computer and use it in GitHub Desktop.
Save guregu/e514a57c1eb426e4bf3d5167c5a715bc to your computer and use it in GitHub Desktop.
text adventure generated by AI
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Room struct {
ID int
Title string
Description string
Exits map[string]int
Nouns map[string]string
}
func (r *Room) Look() {
// Loop through the list of important nouns and emphasize them in the description:
for noun, _ := range r.Nouns {
r.Description = strings.Replace(r.Description, noun, "\033[4m"+noun+"\033[0m", -1)
}
fmt.Println(r.Description)
fmt.Println("Exits:")
for direction := range r.Exits {
fmt.Printf(" %s\n", direction)
}
}
func (r *Room) LookAt(noun string) {
if desc, ok := r.Nouns[noun]; ok {
fmt.Println(desc)
} else {
fmt.Printf("You don't see any %s here.\n", noun)
}
}
func (r *Room) Go(dir string) int {
if id, ok := r.Exits[dir]; ok {
return id
}
fmt.Printf("You can't go %s from here.\n", dir)
return r.ID
}
func main() {
// Define the rooms in the game.
rooms := []*Room{
&Room{
ID: 1,
Title: "Asteroid Field",
Description: "You find yourself drifting through an asteroid field. The asteroids range in size from small rocks to massive boulders. You hear the sound of metal scraping against rock as your ship navigates through the field. To the east, you see a planet with a shimmering atmosphere.",
Exits: map[string]int{
"east": 2,
},
Nouns: map[string]string{
"asteroids": "The asteroids in the field range in size from small rocks to massive boulders.",
},
},
&Room{
ID: 2,
Title: "Orbiting Space Station",
Description: "You approach the planet and see a space station orbiting it. The station is massive, with multiple docking bays and a rotating section for artificial gravity. To the north, you see a shuttle dock that looks like it's open for business.",
Exits: map[string]int{
"west": 1,
"north": 3,
},
Nouns: map[string]string{
"planet": "The planet below the space station is a popular destination for travelers from all over the galaxy.",
"station": "The space station is a hub of commerce and culture, with shops, restaurants, and entertainment venues.",
"docking bays": "The space station's docking bays are constantly busy, with ships coming and going at all hours of the day.",
},
},
&Room{
ID: 3,
Title: "Shuttle Dock",
Description: "You enter the shuttle dock and see a variety of ships docked here. Most of the ships are small, single-person shuttles, but there are a few larger transports as well. To the west, you see a corridor leading to the rest of the space station.",
Exits: map[string]int{
"west": 4,
},
Nouns: map[string]string{
"ships": "The ships docked in the shuttle dock range in size and function, from small shuttles to larger transports.",
"corridor": "The corridor to the west leads to the rest of the space station, where you can find shops, restaurants, and entertainment venues.",
"space station": "The space station is a hub of commerce and culture, with many different areas to explore.",
},
},
&Room{
ID: 4,
Title: "Market District",
Description: "You enter the market district and see a bustling hub of commerce. There are shops and stalls selling all manner of goods, from food and clothing to technology and weapons. To the north, you see a large, opulent palace. To the south, you see a seedy-looking bar.",
Exits: map[string]int{
"north": 5,
"south": 6,
},
Nouns: map[string]string{
"shops": "The shops and stalls in the market district sell a wide variety of goods, from everyday necessities to exotic items.",
"palace": "The palace to the north is the seat of the planet's ruling monarch. It is a massive and impressive structure, surrounded by guards.",
"bar": "The bar to the south is a dingy, run-down establishment. It looks like the kind of place where you can find all sorts of illicit goods and services.",
},
},
&Room{
ID: 5,
Title: "Palace",
Description: "You enter the palace and are immediately struck by its opulence. The floors are made of gleaming marble, the walls are adorned with intricate tapestries, and the ceilings are painted with scenes from mythology. To the south, you see the entrance to the throne room.",
Exits: map[string]int{
"south": 4,
},
Nouns: map[string]string{
"floors": "The palace's floors are made of polished marble, giving them a smooth and luxurious finish.",
"walls": "The walls of the palace are covered with tapestries depicting scenes of battle, hunting, and other grand adventures.",
"ceilings": "The ceilings of the palace are painted with scenes of gods and heroes from mythology. The paintings are so detailed and lifelike that they seem to come to life.",
},
},
&Room{
ID: 6,
Title: "Bar",
Description: "You enter the bar and are immediately hit by the smell of smoke and alcohol. The bar is crowded and noisy, with patrons from all walks of life. To the north, you see a door leading to the back room.",
Exits: map[string]int{
"north": 4,
},
Nouns: map[string]string{
"smoke": "The air in the bar is thick with smoke from cigars and cigarettes. It stings your eyes and makes it difficult to see.",
"alcohol": "The bar is filled with the smell of alcohol. You see patrons drinking all sorts of beverages, from cheap beer to expensive spirits.",
"patrons": "The patrons of the bar come from all walks of life. You see spacers, smugglers, mercenaries, and all sorts of other colorful characters.",
},
},
&Room{
ID: 7,
Title: "Back Room",
Description: "You enter the back room and see a small group of shady-looking individuals gathered around a table. They look up at you with suspicion, but one of them gestures for you to come closer. To the east, you see a door leading to the bar's storage room.",
Exits: map[string]int{
"east": 6,
},
Nouns: map[string]string{
"individuals": "The individuals in the back room are all dressed in dark, utilitarian clothing. They look like they're involved in some sort of illegal activity.",
"table": "The table in the back room is covered in papers, money, and other items. It looks like the individuals were in the middle of a transaction.",
"door": "The door to the east leads to the bar's storage room. You see crates and barrels stacked up against the wall.",
},
},
&Room{
ID: 8,
Title: "Storage Room",
Description: "You enter the storage room and see rows of crates and barrels stacked up against the walls. The room is dimly lit, with only a few flickering fluorescent lights. To the west, you see the door back to the bar's back room.",
Exits: map[string]int{
"west": 7,
},
Nouns: map[string]string{
"crates": "The crates in the storage room are marked with various labels and symbols. You can't make out what's inside, but some of them look quite heavy.",
"barrels": "The barrels in the storage room are also marked with labels and symbols. You can hear liquid sloshing around inside some of them.",
"lights": "The lights in the storage room are flickering and dim. You can barely see what's in front of you, and you have to be careful not to trip over anything.",
},
},
&Room{
ID: 9,
Title: "Escape Pod",
Description: "You enter the escape pod and see a cramped, utilitarian space. The walls are lined with buttons, levers, and screens, and there is a small window that allows you to see outside. You hear a loud alarm blaring and realize that the space station is under attack. To the west, you see the door back to the storage room.",
Exits: map[string]int{
"west": 8,
},
Nouns: map[string]string{
"walls": "The walls of the escape pod are lined with buttons, levers, and screens. You can see various readouts and warnings flashing on the screens.",
"window": "The window in the escape pod is small, but it allows you to see outside. You see the space station under attack, with ships and lasers flashing in the darkness.",
"alarm": "The alarm in the escape pod is blaring loudly, indicating that the space station is under attack. You need to launch the escape pod and get away from here quickly.",
},
},
&Room{
ID: 10,
Title: "Outer Space",
Description: "You launch the escape pod and are immediately thrown into the void of outer space. The stars are a breathtaking spectacle, but you are also aware of the danger of being out here without a proper ship. You see the space station, now a burning wreckage, behind you. Ahead of you, you see a distant planet with a habitable atmosphere.",
Exits: map[string]int{
"planet": 11,
},
Nouns: map[string]string{
"stars": "The stars in outer space are a beautiful and awe-inspiring sight. You see thousands of them, twinkling and shining in the blackness.",
"space station": "The space station, now a burning wreckage, is a grim reminder of the dangers of space travel. You hope that you can reach the planet ahead of you before it's too late.",
"planet": "The planet ahead of you is a distant, but promising, destination. You see that it has a breathable atmosphere, which means that you may be able to land and find safety there.",
},
},
&Room{
ID: 11,
Title: "Planet Surface",
Description: "You land the escape pod on the planet's surface and are immediately struck by the sheer size and beauty of the place. The planet is covered in lush forests, towering mountains, and sparkling oceans. You see a small, primitive village in the distance. To the west, you see a path leading into the forest.",
Exits: map[string]int{
"west": 12,
"village": 13,
},
Nouns: map[string]string{
"planet": "The planet that you've landed on is a beautiful and wild place. It is covered in forests, mountains, and oceans, and is teeming with life.",
"forests": "The forests on the planet are dense and lush, filled with all manner of plants and animals. You see towering trees and vines that stretch up into the sky.",
"mountains": "The mountains on the planet are massive and imposing, with steep cliffs and snow-capped peaks. You can see the shadows of predators lurking on the slopes.",
"oceans": "The oceans on the planet are vast and deep, with crashing waves and hidden currents. You see schools of colorful fish and other marine life swimming in the waters.",
},
},
&Room{
ID: 12,
Title: "Forest Path",
Description: "You follow the path into the forest and are immediately struck by the beauty and danger of the place. The trees are tall and thick, and the underbrush is dense and tangled. You hear the calls of birds and animals, and the rustling of leaves. To the east, you see the path back to the planet's surface. To the north, you see a clearing with a small pond.",
Exits: map[string]int{
"east": 11,
"north": 13,
},
Nouns: map[string]string{
"path": "The path through the forest is narrow and winding, but it is clearly well-traveled. You see footprints and other signs that other people have been here before.",
"trees": "The trees in the forest are tall and ancient, with leaves that shimmer in the sunlight. Some of them have vines and other plants growing around their trunks.",
"underbrush": "The underbrush in the forest is dense and tangled, making it difficult to move through. You see all sorts of plants and animals hidden in the shadows.",
"clearing": "The clearing to the north is a small, peaceful oasis in the midst of the forest. You see a pond with crystal-clear water and a few logs that have been arranged in a circle.",
},
},
&Room{
ID: 13,
Title: "Forest Clearing",
Description: "You enter the forest clearing and see a small pond surrounded by logs. The water is crystal-clear and reflects the trees and sky. To the south, you see the path back to the planet's surface. To the east, you see the entrance to the village.",
Exits: map[string]int{
"south": 11,
"east": 14,
},
Nouns: map[string]string{
"pond": "The pond in the forest clearing is a small, but beautiful, body of water. The water is crystal-clear and reflects the trees and sky above.",
"logs": "The logs in the forest clearing are arranged in a circle around the pond. They look like they've been placed there by someone, and they provide a comfortable place to sit and rest.",
"village": "The village to the east is a small, primitive settlement. You see thatched huts, smoke rising from chimneys, and people going about their daily business.",
},
},
&Room{
ID: 14,
Title: "Village Entrance",
Description: "You enter the village and are immediately struck by the simplicity and warmth of the place. The people are friendly and welcoming, and they invite you to share a meal with them. You see thatched huts, smoke rising from chimneys, and children playing in the dirt. To the west, you see the entrance to the village. To the north, you see a large, communal building.",
Exits: map[string]int{
"west": 13,
"north": 15,
},
Nouns: map[string]string{
"village": "The village is a small, but thriving, community. The people are friendly and welcoming, and they seem to live simple, yet happy, lives.",
"huts": "The huts in the village are thatched and primitive, but they are well-constructed and comfortable. You see smoke rising from the chimneys, indicating that they are well-insulated and warm.",
"children": "The children in the village are curious and energetic. They run around and play games, laughing and enjoying life.",
"communal building": "The communal building to the north is the largest structure in the village. It looks like it serves as a meeting place and gathering hall for the community.",
},
},
&Room{
ID: 15,
Title: "Communal Building",
Description: "You enter the communal building and are immediately struck by the sense of community and history within. The walls are decorated with paintings and carvings, and the air is filled with the sound of people talking and laughing. You see a large fireplace in the center of the room, and tables and chairs arranged around it. To the south, you see the entrance to the village. To the east, you see a hallway leading to other rooms.",
Exits: map[string]int{
"south": 14,
"east": 16,
},
Nouns: map[string]string{
"communal building": "The communal building is the largest and most important structure in the village. It serves as a meeting place and gathering hall for the community.",
"walls": "The walls of the communal building are decorated with paintings and carvings. You see scenes of hunting, farming, and daily life, as well as symbols and patterns that are significant to the village's culture.",
"fireplace": "The fireplace in the center of the communal building is large and well-constructed. It provides warmth and light to the room, and it is a gathering place for the community.",
"hallway": "The hallway to the east leads to other rooms in the communal building. You see that it is dimly lit and quiet, indicating that it is not commonly used.",
},
},
&Room{
ID: 16,
Title: "The End",
Description: "You enter the hallway and follow it to its end, where you find a small, dark room. In the center of the room, you see a strange, alien device that is pulsing with energy. You realize that this must be the source of the planet's life-giving energy, and that it has been hidden here for eons. To the west, you see the entrance to the communal building. There are no other exits.",
Exits: map[string]int{
"west": 15,
},
Nouns: map[string]string{
"hallway": "The hallway is dimly lit and quiet, indicating that it is not commonly used. You see that it leads to a small, dark room at the end.",
"room": "The room at the end of the hallway is small and dark, with a low ceiling and bare walls. You see a strange, alien device in the center of the room, pulsing with energy.",
"device": "The alien device in the room is a marvel of technology. It is clearly not of human origin, and it seems to be the source of the planet's life-giving energy. You wonder how long it has been hidden here, and what secrets it holds.",
},
},
// Additional rooms here...
}
// Create a map of room IDs to room objects.
roomMap := make(map[int]*Room)
for _, room := range rooms {
roomMap[room.ID] = room
}
// Start the game in the first room.
currentRoom := roomMap[1]
fmt.Println("Welcome to the game!")
// Continuously read commands from the user and execute them.
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Printf("> ")
if !scanner.Scan() {
break
}
input := scanner.Text()
// Split the input into a command and an optional argument.
parts := strings.Split(input, " ")
command := parts[0]
arg := ""
if len(parts) > 1 {
arg = parts[1]
}
// Handle the different commands.
switch command {
case "look":
currentRoom.Look()
case "lookat":
currentRoom.LookAt(arg)
case "go":
currentRoom = roomMap[currentRoom.Go(arg)]
currentRoom.Look()
case "quit":
fmt.Println("Goodbye!")
return
case "help":
fmt.Println("Commands:")
fmt.Println(" look: Displays the current room description and exit information.")
fmt.Println(" lookat <thing>: Displays a description of something.")
fmt.Println(" go <direction>: Moves in the specified direction. Available directions are: north, south, east, and west.")
fmt.Println(" help: Displays this list of commands.")
fmt.Println(" quit: Exits the game.")
default:
fmt.Printf("I don't understand the command '%s'.", command)
fmt.Println()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment