在c#中不使用数组构建MasterMind游戏?

时间:2017-01-27 18:09:16

标签: c#

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

我创建了它的第一部分,它适用于尝试猜测1-9的第一个随机数字。我不确定如何为播放器输入第二个数字来猜测第二个1-9位数,以及如何使代码继续使用相同的int /继续添加到我已经设置的猜测上。我一直尝试,我知道如何,我无法弄明白。如果我可以得到一些我出错的地方的帮助,以及如何正确设置它将非常感激。干杯

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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

请记住,您需要小心如何在程序中订购,特别是在用户体验方面。例如:

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<int> numbers = new List<int>()以下是关于数组和列表(集合)之间差异的Reading (what is difference between string array and list of string in c#)

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

在Do / While循环中,我们正在检查我们是否仍然比iMaxGuesses 还要少,我们的列表中还有对象。所以我们采取用户输入。首先,我们使用int.TryParse来确定我们是否确实有一个数字。如果我们确实有一个,我们会针对列表中显示的第一个数字numbers[0]进行检查,如果是,我们会从列表中删除它:{{1}关于列表的一个很酷的部分是,当你删除一个项目时,它会调整列表的大小,这与具有固定大小的数组不同。因此,如果您的列表是 12345 ,并且您删除了1,则下次检查numbers.Remove(numbers[0]);的值时,它将为2!

我很确定其余部分应该是自我解释的。如果遗漏了任何内容或者您需要帮助了解任何事情,请告诉我。