RCRPG/Go: Difference between revisions
m
Fixed syntax highlighting.
(Created) |
m (Fixed syntax highlighting.) |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 1:
{{collection|RCRPG}}
<syntaxhighlight lang="go">package main
import (
"fmt"
"math/rand"
"strings"
)
const (
)
Line 19:
const (
)
func (d Direction) Opposite() Direction {
}
func (d Direction) String() string {
case WEST:
return "west"
case UP:
return "up"
case DOWN:
return "down"
}
panic("Invalid direction!")
}
Line 47 ⟶ 53:
const (
)
func (item Item) String() string {
case LADDER:
return "Ladder"
case SLEDGE:
return "Sledge hammer"
}
panic("Invalid item!")
}
type Room struct {
Name
Items
}
// Creates a new room
func GenerateRoom(X, Y, Z int) *Room {
}
// Connects this room to another
func (room *Room) Connect(dir Direction, other *Room) {
}
// The room name
func (room *Room) String() string {
}
// Counts the number of items are laying in this room
func (room *Room) NumItems() int {
}
// Counts the number of exits out of this room
func (room *Room) NumExits() int {
}
}
// Prints a description of the current room
func (room *Room) Describe() {
continue
}
}
first = false
if count == 1 {
fmt.Printf("a %s", item)
} else {
fmt.Printf("%d %ss", count, item)
}
}
fmt.Println()
}
if room.NumExits() == 0 {
fmt.Print("There are no exits.\n")
} else {
for dir := Direction(0); dir < NUM_DIRECTIONS; dir++ {
other := room.Passages[dir]
if other != nil {
fmt.Printf(" %s: %s\n", dir, other)
}
}
}
}
type Game struct {
}
// Counts the number of items you have in your inventory
func (g *Game) NumItems() int {
}
// Creates or returns the room at specified coordinates.
func (g *Game) GetRoomAt(X, Y, Z int) *Room {
}
// north, south, east, west, up, down: Moves around
func (g *Game) Move(args []string) {
case "west":
dir = WEST
case "up":
dir = UP
case "down":
dir = DOWN
}
other
if other == nil {
fmt.Printf("You can't move %s. There is a wall in the way.\n", dir)
} else if dir == UP && g.Room.Items[LADDER] == 0 {
fmt.Printf("You can't move %s. There are no %s.\n", dir, LADDER)
} else {
g.Room = other
other.Describe()
}
}
// attack: Breaks a wall.
func (g *Game) Sledge(args []string) {
dir = NORTH
Y--
Y++
case "east":
dir = EAST
X++
case "west":
dir = WEST
X--
case "up":
dir = UP
Z++
case "down":
dir = DOWN
Z--
}
other := g.Room.Passages[dir]
if other != nil {
fmt.Print("There is already a passage there.\n")
return
}
other = g.GetRoomAt(X, Y, Z)
g.Room.Connect(dir, other)
fmt.Printf("Made a passage %s to %s\n", dir, other)
}
// drop: Puts items down
func (g *Game) DropItem(args []string) {
}
continue
}
}
first = false
if count == 1 {
fmt.Printf("a %s", item)
} else {
fmt.Printf("%d %ss", count, item)
}
g.Inventory[item] = 0
}
fmt.Println()
return
case "gold":
item = GOLD
case "ladder":
item = LADDER
case "sledge":
item = SLEDGE
}
total := g.Inventory[item]
if total == 0 {
fmt.Printf("You don't have any %ss.\n", item)
return
}
g.Inventory[item]--
g.Room.Items[item]++
fmt.Printf("You drop a %s.\n", item)
}
// take: Picks up items
func (g *Game) TakeItem(args []string) {
}
continue
}
}
first = false
if count == 1 {
fmt.Printf("a %s", item)
} else {
fmt.Printf("%d %ss", count, item)
}
g.Room.Items[item] = 0
}
fmt.Println()
return
case "gold":
item = GOLD
case "ladder":
item = LADDER
case "sledge":
item = SLEDGE
}
total := g.Room.Items[item]
if total == 0 {
fmt.Printf("You don't see any %ss.\n", item)
return
}
g.Room.Items[item]--
g.Inventory[item]++
fmt.Printf("You take a %s.\n", item)
}
// inventory: Shows what you are carrying
func (g *Game) ShowInventory(args []string) {
continue
}
}
first = false
if count == 1
fmt.Printf("a %s", item)
} else {
fmt.Printf("%d %ss", count, item)
}
}
fmt.Println()
}
// name: Renames the current room
func (g *Game) RenameRoom(args []string) {
}
// equip: Equips an item
func (g *Game) EquipItem(args []string) {
item = LADDER
case "sledge":
item = SLEDGE
}
count := g.Inventory[item]
} else if item == g.Equipped {
fmt.Printf("You already have a %s equipped.\n", item)
return
}
if g.Equipped != NIL_ITEM {
}
fmt.Printf("You equip a %s.\n", item)
g.Inventory[item]--
g.Equipped = item
}
// alias: Creats an alias
func (g *Game) CreateAlias(args []string) {
"drop", "take", "inventory", "name", "equip", "alias":
fmt.Printf("Can't overwrite the %q command.\n", name)
return
}
if len(args) == 1 {
g.Commands[name] = nil
fmt.Printf("Removed the %q alias.\n", name)
return
}
g.Aliases[name] = args[1:]
fmt.Printf("Created the %q alias.\n", name)
}
// help: Shows information about commands
func (g *Game) Help(args []string) {
}
}
}
}
}
// look: Looks around
func (g *Game) Look(args []string) {
}
// quit: Ends the game. The End. Final.
func (g *Game) Quit(args []string) {
}
// Resolves aliases and dispatches to the correct handler
func (g *Game) Dispatch(args []string) {
seen := make(map[string]bool)
name := args[0]
for {
if
break
}
args = append(prefix, args[1:]...)
}
}
cb
if cb == nil {
fmt.Printf("Invalid command: %q\n", name)
return
}
cb(args[1:])
return
}
// Set everything up
func (g *Game) Initialize() *Game {
"move":
"drop":
"take":
"name":
"equip":
"alias":
"quit":
"look":
"help":
"n":
"s":
"e":
"w":
"u":
"d":
"i":
"a":
"move":
"drop":
"take":
"name":
"equip":
"alias":
"quit":
"look":
"help":
}
// Read a single line of input
func readLine(in io.Reader) (string, bool) {
var line []string
line = append(line,
}
// Returns a sorted array of command and alias names
func (g *Game) GetCommands() []string {
commandNames = append(commandNames, name)
commandNames = append(commandNames, name)
}
// Main game loop
func (g *Game) Run() {
VERSION_MAJOR, VERSION_MINOR)
fmt.Print("You start in room 0,0,0. Your goal is at 1,1,5. Good luck!\n")
fmt.Printf("Commands: %s\n", strings.Join(g.GetCommands(), ", "))
g.Room.Describe()
fmt.Print("\n> ")
line, ok := readLine(os.Stdin)
for ok && len(line) == 0 {
fmt.Print("\n> ")
line, ok = readLine(os.Stdin)
}
for ok {
tokens := strings.Split(line, " ")
g.Dispatch(tokens)
fmt.Print("\n> ")
line, ok = readLine(os.Stdin)
for ok && len(line) == 0 {
fmt.Print("\n> ")
line, ok = readLine(os.Stdin)
}
}
func main() {
}</
|