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