【问题标题】:Constructing a MasterMind game without using Arrays in c#?在 c# 中不使用数组构建 MasterMind 游戏?
【发布时间】:2017-01-30 04:25:33
【问题描述】:

我正在尝试创建一个不使用生成 5 个随机数 1-9 的数组的策划游戏,你有 15 次尝试猜测它们。*=正确 -=错误 +=位置不正确,但数字是正确的。

我创建了它的第一部分,它用于尝试猜测 1-9 的第一个随机数字。我不确定如何转到第二个数字让玩家猜测第二个 1-9 数字,以及如何使代码继续使用相同的整数/继续添加到我已经设置的猜测中。我尝试了所有我知道的方法,但我无法弄清楚。如果我能在哪里出错以及如何正确设置它得到一些帮助,我将不胜感激。干杯

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Decisions
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Guess the 5 Digit code, Under 15 tries!");

            Random myRandom = new Random();
            Int32 one = myRandom.Next(1, 10);
            Int32 two = myRandom.Next(1, 10);
            Int32 three = myRandom.Next(1, 10);
            Int32 four = myRandom.Next(1, 10);
            Int32 five = myRandom.Next(1, 10);
            int guesses = 0;
            bool incorrect = true;

            do
            {

                if (guesses < 15)
                    Console.WriteLine("Guess a number between 1 and 9");
                string result = Console.ReadLine();
                guesses++;

                if (guesses > 15)
                    Console.WriteLine("You went over 15 tries! Better luck next time");


                if (result == one.ToString())
                    incorrect = false;
                else if (result == two.ToString())
                    Console.WriteLine("+");
                else if (result == three.ToString())
                    Console.WriteLine("+");
                else if (result == four.ToString())
                    Console.WriteLine("+");
                else if (result == five.ToString())
                    Console.WriteLine("+");
                else
                    Console.WriteLine("-");
            } while (incorrect);

            if (guesses < 15)
                Console.WriteLine("*Correct! It took {0} guesses.", guesses);
            if (guesses > 15)
                Console.WriteLine("You took to many tries! Better luck next time! Total Guesses: {0}", guesses);

            Console.ReadLine();
        }
    }
}

【问题讨论】:

  • 为什么不想使用数组?
  • 为什么不使用数组?你也用指甲刀修剪草坪吗?
  • 这是一个你应该使用列表而不是数组的家庭作业吗?
  • @chadnt 是的,而且我的教授在给出指示方面几乎没有用处,只是在尝试过渡到猜测下一个数字时我出错的地方需要一些帮助。
  • @Yukon,他们必须按顺序猜吗?

标签: c#


【解决方案1】:

我认为您要使用的是列表。但首先 -

请记住,您需要注意程序中的排序方式,尤其是在用户体验方面。例如:

if (guesses < 15)
    Console.WriteLine("Guess a number between 1 and 9");
string result = Console.ReadLine();
guesses++;

if (guesses > 15)
    Console.WriteLine("You went over 15 tries! Better luck next time");

所以,当guesses 为 14 时,它将显示文本,让用户猜测并增加“猜测” - 但是,当它迭代回到开头时,您的程序将跳过提示:“猜一个数字在 1 到 9 之间” - 但它仍会等待用户输入。在他们输入输入后,它会提示他们超过了尝试次数,但它仍会检查该数字是否匹配! 如何订购程序对用户体验非常重要。

您的程序可能如下所示:

    const int iMaxGuesses = 15;
    const int iListSize = 5;
    static void Main(string[] args)
    {
        Random myRandom = new Random();

        // Hold a list of integers
        List<int> numbers = new List<int>();
        int guesses = 0;

        // Value where we will rebuild the code
        string result = string.Empty;

        // Placeholder for user's guess
        string strGuess;

        // Converted guess
        int guess;

        // Generate random list of numbers
        for (int i = 0; i < iListSize; i++)
            numbers.Add(myRandom.Next(1, 10));

        // Prompt user
        Console.WriteLine("Guess the {0} Digit code, Under {1} tries!", iListSize, iMaxGuesses);

        // Open the game loop
        do
        {
            // Check if we have exceeded the max amount of times we get to guess
            if (guesses > iMaxGuesses)
                Console.WriteLine("You went over 15 tries! Better luck next time");
            else
            {
                // Prompt user
                Console.WriteLine("Guess a number between 1 and 9");
                // Get input
                strGuess = Console.ReadLine();
                // Check if input is in fact an integer, if so - put the value in our variable 'guess'
                if (int.TryParse(strGuess, out guess))
                {
                    // We have a proper guess, increment
                    guesses++;
                    // Checks if the number is the next in our list
                    if (numbers[0] == guess)
                    {
                        Console.WriteLine("Congratulations, {0} was a match!", guess);
                        // Remove that from the List
                        numbers.Remove(numbers[0]);
                        // Add it to our sequence (result)
                        result += guess;
                    }
                    else if(numbers.Contains(guess))
                        Console.WriteLine("Right number, wrong spot!");
                    else
                        Console.WriteLine("Incorrect, please try again!");
                }
                else // Inform user we will only accept numbered input
                    Console.WriteLine("That was not a number.  Please try again!");
            }

        } while (guesses <= iMaxGuesses && numbers.Count > 0);

        if (guesses < iMaxGuesses)
            Console.WriteLine("Correct! It took {0} guesses to come up with the pattern {1}", guesses, result);
        else
            Console.WriteLine("You took to many tries! Better luck next time! Total Guesses: {0}", guesses);

        Console.ReadLine();
    }

需要注意的几件事:我们有用于列表大小的类级别常量(我们会谈到它)以及有多少猜测,以防这些需要即时更改。你可以忽略这个。

您可能会注意到的第一件事是整数列表List&lt;int&gt; numbers = new List&lt;int&gt;() 这是一些Reading (what is difference between string array and list of string in c#) 关于数组和列表(集合)之间差异的内容。

我们通过创建一个最大大小为列表的循环来初始化随机数列表,并向该列表添加一个随机整数。

在 Do/While 循环中,我们正在检查我们是否仍然小于 iMaxGuesses 并且我们的列表中还有对象。所以我们接受用户的输入。首先我们使用int.TryParse来确定我们是否真的有一个数字。如果我们确实有号码,我们将对照列表中出现的第一个号码numbers[0] 进行检查,如果有,我们将其从列表中删除:numbers.Remove(numbers[0]); 很酷的部分关于列表,当您删除一个项目时,它会调整列表的大小,这与具有固定大小的数组不同。所以如果你的列表是12345,而你去掉了1,那么下次你检查numbers[0]的值,就会是2!

我很确定其余的应该是不言自明的。让我知道是否遗漏了任何内容或您需要帮助理解任何内容。

【讨论】:

  • 我发现“ if (numbers[0] !=guess) Console.WriteLine("Incorrect, Try Again!");"它有效,现在我只需要一个告诉你它是否正确,但只是在错误的数字位置。那么我如何能够检查您的答案输入是否等于 number[0],但不包括您当前尝试猜测的数字,或者您之前已经猜到的数字?
  • 好吧,如果“if (numbers [0] == guess)”返回false,您可以使用“else if (numbers.Contains (guess))”查看该数字是否出现在任何地方其他在列表中。用 else 包裹 if 语句以显示“不正确,再试一次”消息,而不是检查数字 [0] != guess。
  • 如果 (numbers.Contains (guess)) WriteLine("Incorrect, but correct position");否则 WriteLine("不正确,再试一次");我尝试将“else if”语句放在“else”语句之上,告诉用户他们输入的值不是正确的值,但无论如何它每次都返回“不正确,但正确的位置”。
  • 稍后我会更新我的答案。在路上。待命
  • 答案已更新,但请理解:您关于 else if/else 应该去哪里的问题非常简单:遵循逻辑。请记住,您想在检查它是否是下一个数字(并且失败)之后检查该数字是否存在于列表中 - 这就是为什么它是“else if”。希望这是有道理的,并且编程愉快。
猜你喜欢
  • 2014-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-14
相关资源
最近更新 更多