// main package main import ( "fmt" "strings" ) func main() { type Item struct { name string moveable bool openable bool } type Room struct { name, description string items map[string]*Item north, south, west, east string } type Player struct { loc *Room items map[string]string } inventory := map[string]string{} entryItems := map[string]*Item{} chestItems := map[string]*Item{ "chest": {"chest", false, true, }, } caveItems := map[string]*Item{ "House Key": {"House Key", true, false}, } /* Pointing to the struct "Room" allows us to directly modify its values */ rooms := map[string]*Room{ "entry": {"Field", "\nYou find yourself before a mansion to the (n)orth.\n" + "There is a cave to the (w)est.\n" + "In the distance, you see some sort of box to the (e)ast.", entryItems, "none", "none", "cave", "chest"}, "chest": {"Chest", "\nYou see a closed chest freezer in front of you.\n" + "There is an empty field to the (w)est.\n", chestItems, "none", "none", "entry", "none"}, "cave": {"Cave", "\n A beast of inconevialable horror dwells in this" + " cave and proceds to tear you apart.", caveItems, "none", "none", "none", "entry"}, } player := Player{rooms["entry"], inventory} player.items["syringe"] = "A syringe with a missing needle is in your pocket." var command [2]string var quit bool = false var steak_happened = false println() println() println() println() println() println("Welcome to Crush Candy DX!") println("Type \"h\" for help!") println("Enjoy!") println() println() for quit != true { println(player.loc.description, "\n") fmt.Print("Enter text: ") fmt.Scanln(&command[0], &command[1]) command[0] = strings.ToLower(command[0]) command[1] = strings.ToLower(command[1]) switch command[0] { case "q", "quit", "exit": print("Are you sure you want to quit? (y/n): ") fmt.Scanln(&command[0]) if strings.ToLower(command[0]) == "y" { println("The shame of quiting causes your liver to burst" + " as you keel over.") println("RIP") quit = true } else { println("Welcome back from the edge! ") } case "h", "help": print("Commands:\n\n" + "(h)elp\n" + "(c)lear\n" + "(q)uit\n\n" + "(n)orth\n" + "(s)outh\n" + "(e)ast\n" + "(w)est\n\n" + "(l)ook\n" + "(g)et\n" + "(u)se\n" + "(i)nventory\n\n") case "w", "west": if player.loc.west != "none" { player.loc = rooms[player.loc.west] break } println("\nYou can't go that way!\n") case "e", "east": if player.loc.east != "none" { player.loc = rooms[player.loc.east] break } println("\nYou can't go that way!\n") case "n", "north": if player.loc.north != "none" { player.loc = rooms[player.loc.north] break } println("\nYou can't go that way!\n") case "s", "south": if player.loc.south != "none" { player.loc = rooms[player.loc.south] break } println("\nYou can't go that way!\n") case "i", "inventory": println("") println("Inventory:\n") //Listing items, and looking at them, should be different. for k, v := range player.items { println(k, " ", v) } println("") case "open": if command[1] == "" { println("") println("Open what? ") break } if _, ok := player.loc.items[command[1]]; ok && player.loc.items[command[1]].openable == true { if player.loc.name == "Chest" { println("\nYou open the chest") player.loc.items["chest"].openable = false player.loc.items["steak"] = &Item{"steak", true, false} player.loc.description = "" + "\nYou see an open chest freezer in front of you.\n" + "There is a putrid steak in the freezer\n" + "There is an empty field to the (w)est.\n" break } } println("I don't know how to open a", command[1]) default: println("\nI don't know how to '", command[0], "'\n") } // Perhaps the below should somehow be attached to the room struct? // Feels silly to check this every time, but I suppose it is fine. // Perhaps something like, instead of these if statements // We run something like room.action() every loop, and stuff like this // here below will be activated. // I guess room.action() would be a seperate function for every room? // Perhaps next game. I think we'll just loop through all these things // time if player.loc.name == "Cave" { if _, ok := player.items["steak"]; ok { println("\n You see a poodle sitting in the cave. You can't stand " + "the smell of the steak anymore and decide to toss it " + "out of the cave which the dog happily chases after.") rooms["cave"].description = "You are in a dark cave.\n" + "There is a key on the floor.\n" + "There is an empty field to the (e)ast." steak_happened = true } if steak_happened == false { println("Well, it looks like you died! Good luck next time!") quit = true } } } }