简单的控制台乐透游戏 c# 匹配数组
Posted
技术标签:
【中文标题】简单的控制台乐透游戏 c# 匹配数组【英文标题】:Simple console lotto game c# match arrays 【发布时间】: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 的数字,然后洗牌? 【参考方案1】:我会这样做:
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;
【讨论】:
非常感谢,终于搞定了!以上是关于简单的控制台乐透游戏 c# 匹配数组的主要内容,如果未能解决你的问题,请参考以下文章