docs

你正在阅读 v0.1.2 的文档。Nabi 0.1.2 已不再提供支持。 最新版本:v0.1.3 →

本页尚未翻译,以下为英文原文。

#Text adventure

本页内容

A small text adventure with rooms, exits, items and a locked door, played to the end from a prewritten list of commands.

#Code

#Walkthrough

  • Player input is first parsed into a Command enum, and the game logic only deals with that enum. String handling and state changes are kept apart, which makes new commands easy to add, and the compiler checks that match covers every variant (Sum types, 3.12 match).
  • Or-patterns such as "n" | "north" => ... group aliases, and "go" calls parse again on the remaining words so go west and west become the same command (3.11 Patterns).
  • Dict::get returns usize | none. Splitting into next: usize => ... and none => ... handles "no exit that way" naturally (Dict).
  • The game state Game changes in place through step(ref self, ...). A room's items is a List, so taking it out with let items = self.rooms[self.here].items; and calling remove also changes the room's own list (Collections and iterators).
  • The script list is fed in one line at a time instead of standard input. To make it a real game, replace this loop with while let line = try io::input("> ") { .. }, which reads a line until the input ends (I/O).

Found a mistake? Edit this page on GitHub. Last updated