我正在做一個非常簡單的多選控制檯遊戲。我創建了一個使用枚舉的有限狀態機,如果其他狀態在while循環中(while game = true)。如何在C#中的類之外更改變量?
問題是,在class.level1(包含關卡信息)內部,它需要將myState更改爲主程序中的新狀態,但它不會影響它。任何想法如何做到這一點?我很難過。
var game = true;
while (game = true)
{
State myState = State.intro;
//first level
if (myState == State.intro)
{
level.Intro();
}
else
{
if (myState == State.contact1)
{
Console.WriteLine("Wroked");
}
關卡類
:
Console.WriteLine(name + " What are you doing, I need your help!");
Thread.Sleep(1000);
Console.WriteLine("\nA. Who is this? \nB. How are you doing this?\nC. How do you know my name?");
var res = Console.ReadLine();
char response = res[0];
switch (response)
{
case 'a':
State myState = State.contact1;
break;
case 'b':
Console.WriteLine("\nright");
break;
case 'c':
Console.WriteLine("\nleft");
break;
}
有明顯比這更多的代碼,但是這是重要的組成部分。你可以看到完整的代碼在這裏: https://gist.github.com/MajorMojo/700936c02f1430de44b4559f0579917d https://gist.github.com/MajorMojo/5bba21cb977a4b768029e765a0d74886
完美,謝謝! – MajorMoj0