说我刚从船上新鲜是轻描淡写的。我认为对于有经验的程序员来说,这个解决方案非常简单,但我对C#和编码一般都是新手,所以如果我完全缺乏理解而冒犯任何人,我会道歉。话虽如此,我们还是会感谢那些对编码完全陌生的人。基本上,请不要事先知道。
我正在进行短文冒险作为学习经历。我正在尝试建立一个对话系统,玩家可以选择三种对话选项:
玩家说了些什么 - > NPC回应 - >玩家响应NPC的回应 - > NPC再次回应 - >选项循环回到三个初始对话选项
玩家说了些什么 - > NPC回应 - >选项循环回到三个初始对话选项
玩家结束对话 - >选项返回主对话选项(包含以下代码)
这是我到目前为止所提出的:
//Talk to Smith
if (Input == "TALK TO SMITH")
{
{
Console.Clear();
Console.WriteLine("Initial discussion and character introduction");
Console.WriteLine("(Enter the corresponding number with what you want to say)");
Console.WriteLine("What would you like to discuss with Smith?");
}
do
{
correct = 0;
Console.WriteLine("1. Dialogue Option #1");
Console.WriteLine("2. Dialogue Option #2");
Console.WriteLine("3. Dialogue Option #3");
Input = Console.ReadLine().ToUpper();
if (Input == "1")
{
Console.Clear();
dialogue = 1;
correct = 1;
Console.WriteLine("Dialogue Option #1");
Console.WriteLine("Response #1");
Console.WriteLine("1. Dialogue Option #1A");
Console.WriteLine("2. Dialogue Option #1B");
Input = Console.ReadLine().ToUpper();
do
{
if (Input == "1")
{
dialogue = 0;
Console.Clear();
Console.WriteLine("Dialogue Option #1A");
Console.WriteLine("Response #1A");
Console.ReadKey();
correct = 1;
}
if (Input == "2")
{
dialogue = 0;
Console.Clear();
Console.WriteLine("Dialogue Option #1B");
Console.WriteLine("Response #1B");
Console.ReadKey();
correct = 1;
}
} while (correct == 1 && dialogue == 0);
}
if (Input == "2" && dialogue == 0)
{
Console.Clear();
dialogue = 1;
correct = 1;
Console.WriteLine("Response #2");
Input = Console.ReadLine().ToUpper();
}
if (Input == "3")
{
Console.Clear();
dialogue = 1;
correct = 0;
Console.WriteLine("Response #3");
Input = Console.ReadLine().ToUpper();
}
} while (correct == 1 && location == 1);
}
(这只是游戏代码的一部分,而不是整个程序本身)
问题是,一旦我选择了选项#1A,#1B或#2,程序就不会循环回到与NPC的对话,而是我设置的主菜单。我尝试了多种方法,但似乎都没有。
答案 0 :(得分:7)
计算机科学实际上有一个正式的概念,可以很好地映射到你想要做的事情:Finite State Machine。
有限状态机的想法是,它是一个系统,它可以处于有限数量的状态,系统总是处于一个状态,并且每个状态包含一定数量的定义转换,系统进入一个新的状态。
对话树非常适合FSM概念。系统的初始状态是对话的开始,并且每个玩家的可能响应都会导致转换到新状态。它有助于正式模拟它:
State 0:
MessageBox(NPC001, "Is there anything else you need to know?")
Response "Where is the castle located?":
GotoState 1
Response "What sort of defenses does the castle have?":
GotoState 2
Response "Are we sure the Princess is being held in this castle?":
GotoState 3
Response "No, I think that's all.":
GotoState 4
State 1:
MessageBox(NPC001, "It is located two days' journey to the north, on the other side of the Dark Forest")
GotoState 0
State 2:
MessageBox(NPC001, "The castle is defended by monsters conjured forth by the Sorcerer King. Be sure to bring plenty of healing potions!")
GotoState 0
State 3:
MessageBox(NPC001, "Of course! What do you think this is, a Mario game?")
GotoState 0
State 4:
MessageBox(NPC001, "Farewell, heroes. May the Gods be with you on your journey!")
//no state transition here, so execution of the state machine ends at this point
将此大纲转换为实际的C#代码留给读者作为练习,但基本思想是每个State是状态机对象上的一个方法,它继续运行一个接一个的方法,直到它执行一个方法,最终不会告诉它下一个国家应该是什么。 (如果你使用Boo,你可以定义一个状态机宏,上面的大纲可以字面上是对话树的代码,但这是另一回事。)
对有限状态机如何工作的理论做一些研究,你会发现这样的实现变得容易很多。
这是为这样的事情实现FSM的一种方法。
class DialogueTree {
public void Execute()
{
int state = 0;
while (state >= 0)
{
switch (state)
{
case 0:
state = this.State0();
break;
case 1:
state = this.State1();
break;
//and so on
}
}
}
}
每种方法都会显示一些对话和选择,并根据玩家的选择返回状态机的下一个状态。要退出对话,方法应返回-1。这有帮助吗?
请记住,这是一种可行的方法。还有其他一些实现,其中一些对于State Machine概念的某些应用程序可能更好或更差。
答案 1 :(得分:4)
这需要重组代码,特别是将“线性”或“if then else”流程更改为“显示 - 响应”循环。
要显示的下一个对话框是在状态变量中捕获的。
在每个“显示 - 响应”周期中,
代码的简化草图如下所示:
/// somewhere else
enum NextDialog
{
Smith,
Anderson,
Finished
}
NextDialog nextDialog = NextDialog.Smith;
while (nextDialog != NextDialog.Finished)
{
NextDialog nextNextDialog;
switch (nextDialog)
{
case NextDialog.Smith:
// Each handler is responsible for:
// (1) printing the prompt
// (2) getting the user response
// (3) converting the user response into state (program variable) changes, as well as determine the next dialog.
// Each handler will need access to object fields;
// these typically do not appear on the arguments list because
// all instance methods can access all object fields.
nextNextDialog = ProcessDialogSmith( ... );
break;
case NextDialog.Anderson:
nextNextDialog = ProcessDialogAnderson( ... );
break;
default:
throw new UnhandledException();
}
nextDialog = nextNextDialog;
}