docs

您正在閱讀 v0.1.0 的文件。Nabi 0.1.0 已停止支援。 最新版本: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