简单的控制台乐透游戏 c# 匹配数组

Simple console lotto game c# match arrays

我正在尝试用 c# 制作一个简单的 lotto/bingo 游戏。

尝试制作这样的东西:

用户输入 10 个数字并存储到数组中的游戏。

然后游戏制作一张“乐透卡”,这是一个带有随机数的二维数组,如下所示:

现在我想比较两个数组的内容。我似乎无法找到一种方法来对一维和二维数组执行此操作?现在我只能检查其中一个数字是否匹配。

已尝试以多种方式使用 Linq 和 enumerabl 以及不同的循环,但没有成功。

如果我水平、垂直和对角匹配数字,我希望游戏注册宾果游戏。

到目前为止,这是我的代码:

static void Main(string[] args)
    {
        // Greet and explain the rules
        Console.WriteLine("Welcome to bingo!");

        
        string input; // Variabel for input by user
        int inputnmr; // Variabel for nmrinput by user
        int low = 1;
        int high = 25;

        int[] userNmr = new int[7]; // Creates a new array for the player


        // Loop - asks user to type in their lotto numbers and stores them in variable "userNmr"
        for (int i = 0; i < userNmr.Length; i++)
        {
            Console.Write("Type in a number between {0} - {1}:", low, high);
            input = Console.ReadLine();
            inputnmr = int.Parse(input);
            userNmr[i] = inputnmr;

        }

        //Prints your lotto numbers:
        Console.Write("These are your numbers :");
        foreach (int i in userNmr)
        {
            Console.Write(i);
            Console.Write(", ");
        }

        //Asks if continue:
        Console.WriteLine("Are you sure about your numbers?");
        Console.WriteLine("Do you want a bingo card?");


        int x = 5; // Variable for size of x-axis
        int y = 5; //Variable for the size of y-axis

        //Variable for 2 dimensional array:
        int[,] lottoCard = new int[x, y];
                 
        //Create random
        Random randomnmr = new Random();

        //Prints the lotto cards x-axis
        for (int i = 0; i < 5; i++)
        {


            Console.WriteLine(" |------------------------|");
            Console.Write(" | ");

            //Prints the lotto card y-axis
            for (int j = 0; j < 5; j++)
            {
                //Fills lotto card with random ints:
                lottoCard[i, j] = randomnmr.Next(1, 26);
                Console.Write(lottoCard[i, j] + " | ");

            }

            Console.WriteLine(" ");
        }
        Console.WriteLine(" |------------------------|");

        //  --- This is where im stuck --- 

        bool oneMatch = false;
        while (oneMatch == false)
        {
            foreach (var numberA in userNmr)
            {
                foreach (var numberB in lottoCard)
                {
                    if (numberA == numberB)
                    {
                        oneMatch = true;
                    }
                }
            }
        }

        if (oneMatch == true)
        {
            Console.WriteLine("BINGO!");
        }
        else
        {
            Console.WriteLine("No win . . .");
        }

我尝试过的事情:

1:

bool equal = lottoCard.Rank == userNmr.Rank && Enumerable.Range(0, lottoCard.Rank).All(dimension => lottoCard.GetLength(dimension) == lottoCard.GetLength(dimension)) && lottoCard.Cast<double>().SequenceEqual(lottoCard.Cast<double>());
if (equal == true)
{
    Console.WriteLine("Bingo");
}
else
{
    Console.WriteLine("no win");
}


2: 

bool IsContain(int[][] lottoCard, int[] userNmr)
{
    foreach (int[] row in lottoCard)
    {
        int curlIndex = 0;
        foreach (int item in row)
        {
            if (item == userNmr[curlIndex])
            {

                if (curlIndex == userNmr.Length - 1)
                {
                    return true;
                }

                curlIndex++;
            }
            else
            {
                curlIndex = 0;
            }
            return false;
        }
    }
}

if (IsContain(lottoCard, userNmr))
{
    Console.WriteLine("BINGO");
}
else
{
    Console.WriteLine("No win");
}


以上内容无效,非常感谢您的帮助!

我会这样做:

class Program
{        

    public static void Main(string[] args)
    {            
        Console.WriteLine("Welcome to Bingo!");

        int[] userNmr = getUserInput();
        int[,] bingoCard = genBingoCard();

        Console.WriteLine();
        Console.WriteLine("Your number selections are:");
        Console.WriteLine(String.Join(", ", userNmr));

        Console.WriteLine();
        Console.WriteLine("Here is your Bingo Card!");
        displayBingoCard(bingoCard);

        bool bingo = checkForBingo(userNmr, bingoCard);
        if (bingo)
        {
            Console.WriteLine("Congratulations! You had a Bingo!");
        }
        else
        {
            Console.WriteLine("Sorry, no Bingos. Better luck next time!");
        }

        Console.WriteLine();
        Console.WriteLine("Press Enter to Quit.");
        Console.ReadLine();
    }

    public static int[] getUserInput()
    {
        int low = 1;
        int high = 25;
        string input;
        int inputnmr;
        int[] userNmr = new int[7];

        // Loop - asks user to type in their lotto numbers and stores them in variable "userNmr"
        bool valid;
        for (int i = 0; i < userNmr.Length; i++)
        {
            valid = false;
            while (!valid)
            {
                Console.WriteLine();
                Console.WriteLine("User Selection #" + (i + 1));
                Console.Write("Select a number between {0} - {1}: ", low, high);
                input = Console.ReadLine();
                if (int.TryParse(input, out inputnmr))
                {
                    if (inputnmr >= low && inputnmr <= high)
                    {
                        if (!userNmr.Contains(inputnmr))
                        {
                            userNmr[i] = inputnmr;
                            valid = true;
                        }
                        else
                        {
                            Console.WriteLine("You already picked that number!");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Number must be between {0} and {1}!", low, high);
                    }
                }
                else
                {
                    Console.WriteLine("Invalid Number.");
                }
            }
        }
        return userNmr;
    }

    public static int[,] genBingoCard()
    {
        Random randomnmr = new Random();

        int x = 5; // Variable for size of x-axis
        int y = 5; //Variable for the size of y-axis

        //Variable for 2 dimensional array:
        int[,] bingoCard = new int[y, x];
        List<int> numbers = Enumerable.Range(1, x * y).ToList();
        numbers = numbers.OrderBy(z => randomnmr.Next()).ToList();

        int counter = 0;
        for (int r = 0; r < y; r++)
        {
            for (int c = 0; c < x; c++)
            {
                bingoCard[r, c] = numbers[counter++];
            }
        }
        return bingoCard;
    }

    public static void displayBingoCard(int[,] bingoCard)
    {
        int x = bingoCard.GetUpperBound(1) + 1; // Variable for size of x-axis
        int y = bingoCard.GetUpperBound(0) + 1; //Variable for the size of y-axis
        string line = "";
        for (int c = 0; c < x; c++)
        {
            line = line + "+----";
        }
        line = line + "+";

        Console.WriteLine(line);
        for (int r = 0; r < y; r++)
        {
            Console.Write("|");
            for (int c = 0; c < x; c++)
            {
                Console.Write(" " + bingoCard[r, c].ToString("00") + " |");
            }
            Console.WriteLine();
            Console.WriteLine(line);
        }
    }

    public static bool checkForBingo(int[] userNmr, int[,] bingoCard)
    {
        int x = bingoCard.GetUpperBound(1) + 1; // Variable for size of x-axis
        int y = bingoCard.GetUpperBound(0) + 1; //Variable for the size of y-axis

        bool bingo;
        // check each row
        for (int r = 0; r < y; r++)
        {
            bingo = true; // until proven otherwise
            for (int c = 0; c < x && bingo; c++)
            {
                int number = bingoCard[r, c];
                bingo = userNmr.Contains(number);
            }
            if (bingo)
            {           
                Console.WriteLine("Horizontal Bingo at Row: " + (r+1));         
                return true;
            }
        }

        // check each column
        for (int c = 0; c < x; c++)
        {
            bingo = true; // until proven otherwise
            for (int r = 0; r < y && bingo; r++)
            {
                int number = bingoCard[r, c];
                bingo = userNmr.Contains(number);
            }
            if (bingo)
            {
                Console.WriteLine("Vertical Bingo at Column: " + (c+1));         
                return true;
            }
        }

        // check diagonals
        // top left to bottom right
        bingo = true; // until proven otherwise
        for (int c = 0; c < x && bingo; c++)
        {                
            for (int r = 0; r < y && bingo; r++)
            {
                int number = bingoCard[r, c];
                bingo = userNmr.Contains(number);
            }                
        }
        if (bingo)
        {
            Console.WriteLine("Diagonal Bingo from Top Left to Bottom Right.");         
            return true;
        }
        // top right to bottom left
        bingo = true; // until proven otherwise
        for (int c = (x-1); c >= 0 && bingo; c--)
        {
            for (int r = 0; r < y && bingo; r++)
            {
                int number = bingoCard[r, c];
                bingo = userNmr.Contains(number);
            }
        }
        if (bingo)
        {
            Console.WriteLine("Diagonal Bingo from Top Right to Bottom Left.");         
            return true;
        }

        // no bingos were found!
        return false; 
    }

}