[英]C# Bingo Algorithm for Checking BIngo
I have a 5x5 2D array of type Boolean and I want to check for vertical, horizontal, and diagonal bingo.我有一个布尔类型的 5x5 2D 数组,我想检查垂直、水平和对角宾果游戏。
If values at those specific cells are set to "true", I get a bingo.如果这些特定单元格的值设置为“true”,我会得到一个宾果游戏。 Otherwise, I continue to play the game.
否则,我继续玩游戏。 I was looking for help to improve, or get feedback to completely re-haul this method for checking for a Bingo.
我一直在寻求帮助以改进,或获得反馈以完全重新使用此方法来检查宾果游戏。
public int checkForBingo(int rowID, int colID)
{
bool winner = false;
for (int i = 0; i < 4; i++)
{
//checking verts and horiz
if (winner = (boardState[i, 0]) == true && (boardState[i, 1] == true)
&& (boardState[i, 2] == true) && (boardState[i, 3] == true)
&& (boardState[i, 4] == true))
break;
else if (winner = (boardState[0, i] == true) && (boardState[1, i] == true)
&& (boardState[2, i] == true) && (boardState[3, i] == true) &&
(boardState[4, i] == true))
break;
//checking diagonals
if (!winner)
{
if (boardState[2, 2] == true)
if ((winner = (boardState[0, 0] == true) && (boardState[1, 1] == true) &&
(boardState[3, 3] == true) && (boardState[4, 4] == true)))
break;
else if (!(winner = (boardState[0, 4] == true) && (boardState[1, 3] == true) &&
(boardState[3, 1] == true) && (boardState[4, 0] == true)))
break;
}
}
return 0;
}
For loop run from i=0 to 3 That means it's checking 4 rows only and in if condition you are checking for all 5 columns which is right. For 循环从 i=0 运行到 3 这意味着它只检查 4 行,并且在 if 条件下您正在检查所有 5 列是正确的。 Change your for loop condition to
将您的 for 循环条件更改为
i <= 4
A few thing to note:有几点需要注意:
for (int i = 0; i < 5; i++)
less than 5 ( arrays are 0 based)for (int i = 0; i < 5; i++)
小于 5(数组是基于 0 的) You dont need to check for == true, a boolean value can be simple encapsulated in a if statement such as:您不需要检查 == true,布尔值可以简单地封装在 if 语句中,例如:
if (boardState[0, 4] && boardState[1, 3] && boardState[3, 1] && boardState[4, 0]) if (boardState[0, 4] && boardState[1, 3] && boardState[3, 1] && boardState[4, 0])
if
could lead to unwanted behavior(side effects) easy to forget is there. if
的赋值可能会导致容易忘记的不良行为(副作用)。CheckForBingo
CheckForBingo
For comparison and learning purpose(myself included) I have written the method like this:为了比较和学习的目的(包括我自己),我写了这样的方法:
public bool CheckForBingo(bool[,] boardState)
{
bool haveWeWon = false;
//check horizotal
for (int i = 0; i < 5; i++)
{
haveWeWon = true;
for (int y = 0; y < 5; y++)
{
if (!boardState[i, y])
{
haveWeWon = false;
break;
}
}
if(haveWeWon)
{
return haveWeWon;
}
}
if (!haveWeWon)
{
//check vertical
for (int i = 0; i < 5; i++)
{
haveWeWon = true;
for (int y = 0; y < 5; y++)
{
if (boardState[y, i])
{
haveWeWon = false;
break;
}
}
if (haveWeWon)
{
return haveWeWon;
}
}
}
//check the middle - if false dont bother checking diagonal
if (boardState[2, 2])
{
if (!haveWeWon)
{
//check top left diagonal
for (int i = 0; i < 5; i++)
{
haveWeWon = true;
if (!boardState[i, i])
{
haveWeWon = false;
break;
}
}
if (haveWeWon)
{
return haveWeWon;
}
}
if (!haveWeWon)
{
//check top right diagonal
for (int i = 4; i >= 0; i--)
{
haveWeWon = true;
if (!boardState[i, i])
{
haveWeWon = false;
break;
}
}
if (haveWeWon)
{
return haveWeWon;
}
}
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.