【发布时间】:2022-01-02 19:09:30
【问题描述】:
我正在尝试用 c# 制作一个简单的乐透/宾果游戏。
尝试做这样的事情:
用户输入 10 个数字并存储到数组中的游戏。
然后游戏制作一张“乐透卡”,它是一个带有随机数的二维数组,如下所示:
- 10 | 13 | 14 | 17 | 16
- 18 | 24 | 21 | 23 | 8
- 1 | 3 | 6 | 25 | 9
- 7 | 22 | 15 | 12 | 2
- 4 | 5 | 11 | 19 | 20
现在我想比较两个数组的内容。我似乎无法找到一种方法来使用 1 维和 2 维数组来做到这一点?现在我只能检查其中一个数字是否匹配。
已尝试以多种方式使用 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");
}
以上不起作用,非常感谢一些帮助!
【问题讨论】:
-
“用户输入 10 个数字并存储到数组中的游戏。”您能否进一步解释一下这 10 个数字如何对应于 5(宽)x 4(高)矩阵中的“胜利”?您是否只想枚举所有行(5 个数字)、列(4 个数字)、对角线(4 个数字)并查看这些序列中的值是否都存在于用户输入的 10 个数字中?
-
您的代码似乎正在制作 5x5 宾果卡,而您的描述显示的是 5x4 宾果卡。是哪个?
-
哦,对不起,我的错!应该是 5x5 宾果卡。如果 userNmr 中的数字与二维“bingoCard”数组匹配 - 连续 5 个。问题是我不知道如何比较一维数组和二维数组
-
您当前的宾果卡生成器可以生成重复数字的卡吗?
-
嗯...这实际上是另一个问题。但我认为解决方案可能是让宾果卡保存 1 到 25 的数字,然后洗牌?
标签: c# arrays multidimensional-array console-application