[英]Simple console lotto game c# match arrays
我正在尝试在 c# 中制作一个简单的乐透/宾果游戏。
试图做这样的事情:
用户输入 10 个数字并存储到数组中的游戏。
然后游戏制作一张“乐透卡”,它是一个带有随机数的二维数组,如下所示:
现在我想比较两个 arrays 的内容。 我似乎找不到用 1 维和 2 维 arrays 做到这一点的方法? 现在我只设法检查其中一个数字是否匹配。
已尝试以几种方式使用 Linq 以及可枚举和不同的循环,但没有成功。
如果我水平、垂直和对角匹配数字,我希望游戏注册宾果游戏。
到目前为止,这是我的代码:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.