chore: add 3.4.3
chore: add 3.4.4 chore: add 3.4.5 chore: add 3.4.5.1 chore: add 3.4.6.1 chore: add 3.4.6.2 chore: add 3.4.6.3 chore: add 3.4.6.4 chore: add 3.4.6.5
This commit is contained in:
@@ -8,19 +8,14 @@
|
||||
|
||||
玩家属性不再需要存储在单独的变量中;我们可以使用与任何其他对象相同的数据结构。所以玩家,作为一个对象必须具有以下特点:
|
||||
|
||||
- 所处位置(我在哪)
|
||||
- 所处位置(我在哪)
|
||||
- 玩家可能持有的任何物品的位置。
|
||||
|
||||
这使得某些常见操作非常容易实现:
|
||||
|
||||
```
|
||||
<strong> Action </strong> <strong>Typical</strong>
|
||||
```
|
||||
|
||||
<strong> command </strong> <strong>Example</strong>
|
||||
|
||||
| 玩家从一个位置移动到另一个位置 | go | player->location = cave; |
|
||||
|**Action**|**Typical Command**|**Example**|
|
||||
| ------------------------------ | --------- | ------------------------------------ |
|
||||
| 玩家从一个位置移动到另一个位置 | go | player->location = cave; |
|
||||
| 列出某个位置存在的项和参与者 | look | listObjectsAtLocation(cave); |
|
||||
| 玩家获取物品 | get | silver->location = player; |
|
||||
| 玩家掉落物品 | drop | silver->location = player->location; |
|
||||
@@ -35,7 +30,7 @@
|
||||
|
||||
如果你可以在不参考下面内容的情况下就写出基本内容会有很大收获的
|
||||
|
||||
# <strong>parsexec.c</strong>
|
||||
## <strong>parsexec.c</strong>
|
||||
|
||||
```c
|
||||
#include <stdbool.h>
|
||||
@@ -48,7 +43,7 @@ bool parseAndExecute(char *input)
|
||||
{
|
||||
char *verb = strtok(input, " \n");
|
||||
char *noun = strtok(NULL, " \n");
|
||||
//第二次使用strtok要用NULL传参
|
||||
//第二次使用 strtok 要用 NULL 传参
|
||||
if (verb != NULL)
|
||||
{
|
||||
if (strcmp(verb, "quit") == 0)
|
||||
@@ -94,7 +89,7 @@ bool parseAndExecute(char *input)
|
||||
|
||||
新命令由以下模块实现。
|
||||
|
||||
# <strong>inventory.h</strong>
|
||||
## <strong>inventory.h</strong>
|
||||
|
||||
```c
|
||||
extern void executeGet(const char *noun);
|
||||
@@ -104,7 +99,7 @@ extern void executeGive(const char *noun);
|
||||
extern void executeInventory(void);
|
||||
```
|
||||
|
||||
# <strong>inventory.c</strong>
|
||||
## <strong>inventory.c</strong>
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
@@ -171,13 +166,13 @@ void executeInventory(void)
|
||||
|
||||
从本质上讲,<em>get</em>, <em>drop</em>, <em>give</em> and <em>ask 这些命令</em>除了将项目从一个地方移动到另一个地方之外,什么都不做。单个函数 <em>move 对象</em>可以对所有四个命令执行该操作。
|
||||
|
||||
# move.h
|
||||
## move.h
|
||||
|
||||
```c
|
||||
extern void moveObject(OBJECT *obj, OBJECT *to);
|
||||
```
|
||||
|
||||
# move.c
|
||||
## move.c
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
@@ -231,14 +226,14 @@ void moveObject(OBJECT *obj, OBJECT *to)
|
||||
|
||||
命令“get”使用函数<em>getVisible</em>将名词转换为 object,就像命令“go”一样;请参阅上一章。但是对于对玩家(或其他一些参与者)已经持有的对象进行<em>drop</em>, <em>ask</em>, <em>give 等</em>命令时,我们需要稍微不同的东西。我们将在 <em>noun.c</em> 中添加一个函数 <em>getPossession</em>。
|
||||
|
||||
# noun.h
|
||||
## noun.h
|
||||
|
||||
```c
|
||||
extern OBJECT *getVisible(const char *intention, const char *noun);
|
||||
extern OBJECT *getPossession(OBJECT *from, const char *verb, const char *noun);
|
||||
```
|
||||
|
||||
# noun.c
|
||||
## noun.c
|
||||
|
||||
```c
|
||||
#include <stdbool.h>
|
||||
@@ -318,18 +313,18 @@ OBJECT *getPossession(OBJECT *from, const char *verb, const char *noun)
|
||||
}
|
||||
```
|
||||
|
||||
注意:新函数(45-75 行) <em>getPossession</em> 是 <em>getObject</em> 的装饰器(wrapper )(参见第 52 行),它要么返回匹配的对象,要么返回 NULL(如果没有合适的对象与名词匹配)。
|
||||
注意:新函数(45-75 行) <em>getPossession</em> 是 <em>getObject</em> 的装饰器(wrapper)(参见第 52 行),它要么返回匹配的对象,要么返回 NULL(如果没有合适的对象与名词匹配)。
|
||||
|
||||
函数 <em>actor 这里</em>用于命令 <em>give</em> 和 <em>ask</em>,但它也可能由其他命令调用。所以我们在<em>misc.c</em>中定义了它。
|
||||
|
||||
# misc.h
|
||||
## misc.h
|
||||
|
||||
```c
|
||||
extern OBJECT *actorHere(void);
|
||||
extern int listObjectsAtLocation(OBJECT *location);
|
||||
```
|
||||
|
||||
# misc.c
|
||||
## misc.c
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
Reference in New Issue
Block a user