RCRPG/Go: Difference between revisions
library changes
m (Typo) |
(library changes) |
||
Line 2:
import (
"fmt"
"io"
"math/rand"▼
"os"
▲ "rand"
"sort"
"strings"
Line 482 ⟶ 481:
name := strings.ToLower(args[0])
switch name {
case "north", "south", "east", "west", "up", "down", "attack",
"drop", "take", "inventory", "name", "equip", "alias":
fmt.Printf("Can't overwrite the %q command.\n", name)
return
Line 544:
// Resolves aliases and dispatches to the correct handler
func (g *Game) Dispatch(args []string) {
seen := make(map[string]bool)
name :=
▲ for ok {
if !ok {
break
}
args = append(prefix, args[1:]...)
name =
if seen[name] {
fmt.Print("Recursive alias.\n")
return
}
}
cb := g.Commands[name]
Line 566 ⟶ 564:
return
}
cb(
return
}
Line 627 ⟶ 625:
// Read a single line of input
func readLine(in io.Reader) (string, bool) {
var line []string
buf := []byte{0}
_, err := in.Read(buf)
for err == nil && buf[0] != '\n' && buf[0] != '\r' {
line
_, err = in.Read(buf)
}
Line 637 ⟶ 635:
in.Read(buf)
}
return strings.Join(line
}
// Returns a sorted array of command and alias names
func (g *Game) GetCommands() []string {
var commandNames []string
for name := range g.Commands {
commandNames
}
for name := range g.Aliases {
commandNames
}
sort.
return commandNames
}
// Main game loop
func (g *Game) Run() {
fmt.Printf("Welcome to RC Minimalist RPG, Go version %d.%d.\n",
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(), ", "))
Line 666 ⟶ 665:
}
for ok {
tokens := strings.Split(line, " "
g.Dispatch(tokens)
fmt.Print("\n> ")
|