这是“使用 C# 开发智能手机软件:推箱子”系列文章的第七篇。在这篇文章中,介绍 Common/Step.cs 源程序文件。
枚举 Direction 用来表示工人和箱子移动的方向,包含五个成员:None, East, South, West, North。
枚举 Action 用来表示在“设计”对话框中选取的动作,包含四个成员:None, Create, Edit, Delete。
结构 Step 用来记录走法步骤,记录的要素有:前进的方向,是否推着箱子一起前进,“撤销”时是否停留。
我们还是来看两幅图吧:


在左边这幅图中,假设鼠标先点击红色圆圈的位置,因此工人就经过“1”和“2”两步到达该位置。然后鼠标再点击绿色圆圈的位置,因此箱子被推到该位置,而工人经过“3”、“4”、“5”和“6”四步到达箱子右边一个单元格的位置,如右图所示。这六步在程序中作为结构 Step 的六个实例被压入堆栈中,如果用户以后点击了“后退”或“撤销”按钮(如右图中红色圆圈如示),则要从堆栈中弹出这些 Step 的实例。这六个 Step 的实例的值请看下表:
如果用户点击了“后退”按钮,每点击一下工人就后退(往 Step.Direct 的相反方向)一步,如果 Step.IsBox 的值是 true 的话,箱子也要跟着后退一步。如果用户点击了“撤销”按钮,工人就要一直后退到 Step.IsStop 为 true 的地方。也就是说,如果在右图的状态下,如果用户先点击一下“后退”按钮,工人就带着箱子后退一步(第6步)。如果用户又点击了“撤销”按钮的话,工人就会连续后退三步(第5步、第4步、第3步),到达左图红色圆圈的位置,而其中前两步是带着箱子后退的。这时如果再点击一次“撤销”按钮的话,工人又会继续后退二步(第2步、第1步),回到左图工人所在位置。
结构 Step 的转换操作符 char 是用来将保存通关步骤到配置文件(PushBox.cfg)和通关步骤文件(steps/*.bxs)中用的。结构 Step 的转换操作符 Step 用来从配置文件或通关步骤文件中“回放”推箱子的步骤。
上一篇:使用 C# 开发智能手机软件:推箱子(六)
下一篇:使用 C# 开发智能手机软件:推箱子(八)
返回目录
1
namespace Skyiv.Ben.PushBox.Common
2
{
3
enum Direction { None, East, South, West, North } // 方向: 无 东 南 西 北
4
public enum Action { None, Create, Edit, Delete } // 设计: 无 创建 编辑 删除
5
6
/// <summary>
7
/// 走法步骤
8
/// </summary>
9
struct Step
10
{
11
Direction direct; // 前进方向
12
bool isBox; // 是否推着箱子一起前进
13
bool isStop; // “撤销”时是否停留
14
15
public Direction Direct { get { return direct; } }
16
public bool IsBox { get { return isBox; } }
17
public bool IsStop { get { return isStop; } }
18
19
public Step(Direction direct, bool isBox, bool isStop)
20
{
21
this.direct = direct;
22
this.isBox = isBox;
23
this.isStop = isStop;
24
}
25
26
// isBox isStop None East South West North
27
// A B C D E
28
// x F G H I J
29
// x K L M N O
30
// x x P Q R S T
31
32
public static implicit operator char(Step step)
33
{
34
char c = "ABCDE"[step.direct - Direction.None];
35
if (step.isBox) c = (char)(c + 5);
36
if (step.isStop) c = (char)(c + 10);
37
return c;
38
}
39
40
public static implicit operator Step(char c)
41
{
42
int n = c - 'A';
43
return new Step((Direction)(n % 5), (n % 10 >= 5), (n >= 10));
44
}
45
}
46
}
47
这个源程序文件中包含两个枚举(Direction 和 Action) 和一个结构(Step)的定义。
namespace Skyiv.Ben.PushBox.Common2
{3
enum Direction { None, East, South, West, North } // 方向: 无 东 南 西 北4
public enum Action { None, Create, Edit, Delete } // 设计: 无 创建 编辑 删除5

6
/// <summary>7
/// 走法步骤8
/// </summary>9
struct Step10
{11
Direction direct; // 前进方向12
bool isBox; // 是否推着箱子一起前进13
bool isStop; // “撤销”时是否停留14

15
public Direction Direct { get { return direct; } }16
public bool IsBox { get { return isBox; } }17
public bool IsStop { get { return isStop; } }18

19
public Step(Direction direct, bool isBox, bool isStop)20
{21
this.direct = direct;22
this.isBox = isBox;23
this.isStop = isStop;24
}25

26
// isBox isStop None East South West North27
// A B C D E28
// x F G H I J29
// x K L M N O30
// x x P Q R S T31

32
public static implicit operator char(Step step)33
{34
char c = "ABCDE"[step.direct - Direction.None];35
if (step.isBox) c = (char)(c + 5);36
if (step.isStop) c = (char)(c + 10);37
return c;38
}39

40
public static implicit operator Step(char c)41
{42
int n = c - 'A';43
return new Step((Direction)(n % 5), (n % 10 >= 5), (n >= 10));44
}45
}46
}47

枚举 Direction 用来表示工人和箱子移动的方向,包含五个成员:None, East, South, West, North。
枚举 Action 用来表示在“设计”对话框中选取的动作,包含四个成员:None, Create, Edit, Delete。
结构 Step 用来记录走法步骤,记录的要素有:前进的方向,是否推着箱子一起前进,“撤销”时是否停留。
我们还是来看两幅图吧:


在左边这幅图中,假设鼠标先点击红色圆圈的位置,因此工人就经过“1”和“2”两步到达该位置。然后鼠标再点击绿色圆圈的位置,因此箱子被推到该位置,而工人经过“3”、“4”、“5”和“6”四步到达箱子右边一个单元格的位置,如右图所示。这六步在程序中作为结构 Step 的六个实例被压入堆栈中,如果用户以后点击了“后退”或“撤销”按钮(如右图中红色圆圈如示),则要从堆栈中弹出这些 Step 的实例。这六个 Step 的实例的值请看下表:
| 第1步 | 第2步 | 第3步 | 第4步 | 第5步 | 第6步 | |
| Direct | West | South | West | West | West | West |
| IsBox | false | false | false | true | true | true |
| IsStop | true | false | true | false | false | false |
| operator char | N | C | N | I | I | I |
结构 Step 的转换操作符 char 是用来将保存通关步骤到配置文件(PushBox.cfg)和通关步骤文件(steps/*.bxs)中用的。结构 Step 的转换操作符 Step 用来从配置文件或通关步骤文件中“回放”推箱子的步骤。
上一篇:使用 C# 开发智能手机软件:推箱子(六)
下一篇:使用 C# 开发智能手机软件:推箱子(八)
返回目录



浙公网安备 33010602011771号