[英]Check for line on bingo program with arrays
I have this problem of bingo, where I have to check for bingo, line, or nothing, for a given input, where I get the 3 X 3 bingo card, and next 15 numbers extracted.我有宾果游戏的这个问题,对于给定的输入,我必须检查宾果游戏、行或什么都没有,在那里我得到 3 X 3 宾果卡,然后提取接下来的 15 个数字。 I've wrote some code and I pass the tests for bingo and nothing, but not for line and I don't know why because I think my logic is good.
我写了一些代码,我通过了宾果游戏的测试,但没有通过,但我不知道为什么,因为我认为我的逻辑很好。
Here is the input:这是输入:
1 2 3 1 2 3
4 5 6 4 5 6
7 8 9 7 8 9
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
10 10
11 11
12 12
13 13
14 14
15 15
16 16
I should have the result "line" but I get "nothing" instead.我应该有结果“行”,但我得到“什么都没有”。 I think that my problem is on CheckLine method and I think this happens because somehow I don't pass correct parameters or maybe I have/or don't have to use ref keyword on some parameters.
我认为我的问题出在 CheckLine 方法上,我认为这是因为我没有传递正确的参数,或者我有/或不必在某些参数上使用 ref 关键字。 Can someone tell me what's wrong?
有人可以告诉我出了什么问题吗?
Here is my code:这是我的代码:
static void Main()
{
const int numberOfRows = 3;
const int numberOfColumnns = 3;
const int numbersExtracted = 15;
int[,] bingoCard = ReadBingoCard(numberOfRows, numberOfColumnns);
int[] numbers = ReadNumbersExtracted(numbersExtracted);
PrintResult(bingoCard, numbers);
}
static int[,] ReadBingoCard(int rowsNumber, int columnNumber)
{
int[,] card = new int[rowsNumber, columnNumber];
for (int i = 0; i < rowsNumber; i++)
{
string[] array = Console.ReadLine().Split(' ');
for (int j = 0; j < columnNumber; j++)
{
card[i, j] = Convert.ToInt32(array[j]);
}
}
return card;
}
static int[] ReadNumbersExtracted(int numbersExtracted)
{
int[] numbers = new int[numbersExtracted];
for (int i = 0; i < numbersExtracted; i++)
{
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
return numbers;
}
static void CheckLine(int[,] bingoCard, int nr, int nr2, int[] numbersExtracted,
ref int counter, int nr3, ref bool isTrue)
{
const int rowEnd = 2;
bool isLine = counter == nr3 && nr2 == rowEnd;
for (int k = 0; k < numbersExtracted.Length; k++)
{
if (bingoCard[nr, nr2] == numbersExtracted[k])
{
counter++;
if (isLine)
{
isTrue = true;
break;
}
}
}
}
static void CheckBingo(int[,] bingoCard, int nr, int nr2, int[] numbersExtracted,
ref int counter, int nr3, ref bool isTrue)
{
for (int k = 0; k < numbersExtracted.Length; k++)
{
if (bingoCard[nr, nr2] == numbersExtracted[k])
{
counter++;
if (counter == nr3)
{
isTrue = true;
}
}
}
}
static bool CheckForLine(int[,] bingoCard, int[] numbersExtracted)
{
const int length = 3;
int count = 0;
bool isLine = false;
for (int i = 0; i < bingoCard.GetLength(0); i++)
{
for (int j = 0; j < bingoCard.GetLength(0); j++)
{
CheckLine(bingoCard, i, j, numbersExtracted, ref count, length, ref isLine);
}
}
return isLine;
}
static bool CheckForBingo(int[,] bingoCard, int[] numbersExtracted)
{
const int length = 9;
int count = 0;
bool isBingo = false;
for (int i = 0; i < bingoCard.GetLength(0); i++)
{
for (int j = 0; j < bingoCard.GetLength(1); j++)
{
CheckBingo(bingoCard, i, j, numbersExtracted, ref count, length, ref isBingo);
}
}
return isBingo;
}
static void PrintResult(int[,] bingoCard, int[] numbersExtracted)
{
if (CheckForBingo(bingoCard, numbersExtracted))
{
Console.WriteLine("bingo");
}
else if (CheckForLine(bingoCard, numbersExtracted))
{
Console.WriteLine("linie");
}
else
{
Console.WriteLine("nimic");
}
}
I think part of the problem here is that you're passing program state around to the methods using ref
arguments.我认为这里的部分问题是您使用
ref
参数将程序状态传递给方法。 This can lead to problems because the state is now shared between methods and can easily get out of sync if we're not careful.这可能会导致问题,因为状态现在在方法之间共享,如果我们不小心,很容易失去同步。
Instead we should define methods that take in some information, do some calculation, and return a result.相反,我们应该定义接收一些信息、进行一些计算并返回结果的方法。 It makes tracking down problems much easier this way.
通过这种方式,它可以更轻松地跟踪问题。
For example, to determine if there is a "Bingo", we need to check if all items in the bingoCard
array are also in the numbers
array.例如,要确定是否有“宾果”,我们需要检查
bingoCard
数组中的所有项目是否也在numbers
数组中。 This can be done fairly easily with a nested loop, where we:这可以通过嵌套循环很容易地完成,我们:
row
in the bingoCard
array bingoCard
数组中的每一row
column
value.column
值。numbers
array to see if there's a match for the value.numbers
数组,看看是否有匹配的值。
bingoCard
array, then we return true
bingoCard
数组中的项目数,则返回true
false
false
For example:例如:
static bool CheckForBingo(int[,] bingoCard, int[] numbers)
{
int numMatchesFound = 0;
for (int row = 0; row < bingoCard.GetLength(0); row++)
{
for (int col = 0; col < bingoCard.GetLength(1); col++)
{
for (int numIndex = 0; numIndex < numbers.Length; numIndex++)
{
if (bingoCard[row, col] == numbers[numIndex])
{
// Match found! Increment our counter and break from this loop
numMatchesFound++;
break;
}
}
}
}
// If the number of matches equals the number of items in the card, return 'true'
return numMatchesFound == bingoCard.Length;
}
Similarly, to check for a "line" we examine each row, and for each row we look at the column value.类似地,为了检查“行”,我们检查每一行,并为每一行查看列值。 Then we loop through the
numbers
array to see if there's a match.然后我们循环遍历
numbers
数组以查看是否存在匹配项。 If there is, then we increment a counter.如果有,那么我们增加一个计数器。 If, at the end of the columns loop, our counter matches the total number of columns, then we have a "line" and return
true
.如果在列循环结束时,我们的计数器与列的总数匹配,那么我们就有了一个“行”并返回
true
。 Otherwise, if we get to the end of the loops, then we return false
:否则,如果我们到达循环的末尾,则返回
false
:
static bool CheckForLine(int[,] bingoCard, int[] numbers)
{
for (int row = 0; row < bingoCard.GetLength(0); row++)
{
// For a 'line', we only need to match all columns in a row,
// so create a counter to track that here
int colMatchesInRow = 0;
for (int col = 0; col < bingoCard.GetLength(1); col++)
{
for (int numIndex = 0; numIndex < numbers.Length; numIndex++)
{
if (bingoCard[row, col] == numbers[numIndex])
{
// Match found! Increment our counter and break from this loop
colMatchesInRow++;
break;
}
}
}
// If our counter equals the number of columns, return 'true'
if (colMatchesInRow == bingoCard.GetLength(1)) return true;
}
// If we get this far, we never found a 'line', so return 'false'
return false;
}
Hopefully this helps.希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.