【问题标题】:How do I create a loop for a c# hangman game?如何为 c# hangman 游戏创建循环?
【发布时间】:2015-03-22 17:54:44
【问题描述】:

我已经制作了这个刽子手游戏,但唯一的问题是我不知道我应该使用哪些简单的循环(For、While 或 Do While)来让程序给用户三个机会来正确回答或保持如果他们确实回答正确,请重复。我想知道是否有人可以指导我正确的方向?

        Console.WriteLine("Do you want to play a game?");
        string answer = Console.ReadLine();

        if (answer == "no")
        {
            Console.WriteLine("Please Exit Program");
        }


        System.Threading.Thread.Sleep(1000);
        if (answer == "yes")
        {   
            Console.WriteLine(" ");
            Console.WriteLine("Welcome to Hangman");

            string[] array1 = new string[10];
            array1[0] = "hello";
            array1[1] = "swami";
            array1[2] = "zebra";
            array1[3] = "rainbow";
            array1[4] = "camp";
            array1[5] = "unicycle";
            array1[6] = "trivia";
            array1[7] = "hockey";
            array1[8] = "charlie";
            array1[9] = "canada";

            Random word = new Random();

            int mynumber = word.Next(0, 10);

            char[] array2 = array1[mynumber].ToCharArray();


            char[] array3 = new char[array2.Length];

            Console.WriteLine(array2);

            for (int i = 0; i < array2.Length; i++)
            {
                array3[i] = '*';
            }

            Console.WriteLine(" ");
            Console.WriteLine(array3);

            System.Threading.Thread.Sleep(1500);

            Console.WriteLine(" ");
            int number = 1;
            number = number + 1;

            Console.WriteLine(" Guess a letter");
            char letter = Char.Parse(Console.ReadLine());
            Console.WriteLine(" ");

            for (int i = 0; i < array2.Length; i++)
            {
                if (letter == array2[i])
                {
                    Console.Write(letter);
                }
                else
                {
                    Console.Write("*");
                }                    
            }               

            Console.WriteLine(" ");
        }        
    }
}

【问题讨论】:

  • for 循环运行在一组数字上,每个循环增加一个变量。 只要(感谢@TyCobb)循环内的某些条件得到满足,while 循环就会运行。 do while 与 while 的作用相同,但在循环结束而不是开始时测试标准。你使用哪一种取决于你想做什么。
  • 只有字母错了才需要加数;一个while循环应该没问题(do-while也可以;在这种情况下没有区别)。 For 也可以,但令人困惑。
  • 没有什么是你不能用其他人做的,所以这基本上是一个风格问题,什么最容易理解。

标签: c# loops if-statement for-loop while-loop


【解决方案1】:

暂时从代码细节退一步,考虑一下游戏的逻辑以及循环将如何用人类语言表达。

你基本上有两个循环:

  1. 在游戏中反复请求输入。
  2. 反复玩游戏。

如果我们用人类语言的伪代码将这些循环相互嵌套,它可能看起来像这样:

while (player wishes to play again)
  playGame()

在playGame()内:

while (game isn't over)
  playTurn()

如果您已经拥有进行游戏“转身”的代码(玩家猜测,猜测被确定为正确或不正确等),那么您真正需要做的就是定义这些条件。

game isn't over

您可以为此维护一个简单的布尔标志。比如:

bool gameIsOver = false;

在任何给定的回合中,检查游戏的逻辑。玩家赢了吗?显示一条消息并将标志设置为true。玩家输了吗?相同的反应,只是不同的信息。所以循环最终是这样的:

bool gameIsOver = false;
while (!gameIsOver)
{
    // perform turn logic
    // when a turn ends the game, set gameIsOver to true
}

然后可以将相同的概念应用于外部循环。那需要定义:

player wished to play again

因此,您可以将其结构如下:

bool continueWithAnotherGame = true;
while (continueWithAnotherGame)
{
    // play a game

    // ask the user if they would like to play again
    // set continueWithAnotherGame based on the user's reply
}

一旦定义了循环的语义结构,用于构造循环的物理关键字就变得更加简单明了。从那里您可以更专注于细节。例如,您说一场比赛有 3 次“罢工”。这不需要在for 循环中使用计数器,您可以使用单独的计数器。比如:

bool gameIsOver = false;
int strikes = 0;
while (!gameIsOver)
{
    // perform turn logic

    // if the turn is a fail, increment strikes

    //   if strikes is greater than or equal to 3, set gameIsOver to true

    // else if the turn is a pass, continue

    //   if the game is solved, set gameIsOver to true
}

【讨论】:

  • 正确,尽管我要指出 do-while 和 for 实现同样可能(如果更难阅读)。
  • @emodendroket:任何循环结构都可以。 for 也可以工作,可能没有步骤操作和不同的条件检查。 (当然,这也更难阅读。)
  • 对,对。我之所以提到它,是因为 OP 似乎想知道他必须使用哪个,我认为了解它们在形式上都是等效的会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多