C++ 为类似connect-4的游戏赢得条件
我有一个5x10数组,其中填充了随机值1-5。我希望能够检查3个数字(水平或垂直)是否匹配。如果不写大量的if语句,我想不出一种方法来实现这一点 以下是随机填充数组的代码C++ 为类似connect-4的游戏赢得条件,c++,arrays,C++,Arrays,我有一个5x10数组,其中填充了随机值1-5。我希望能够检查3个数字(水平或垂直)是否匹配。如果不写大量的if语句,我想不出一种方法来实现这一点 以下是随机填充数组的代码 int i; int rowincrement = 10; int row = 0; int col = 5; int board[10][5]; int randomnum = 5; int main(int argc, char * argv[]) { srand(time(NULL)); cout
int i;
int rowincrement = 10;
int row = 0;
int col = 5;
int board[10][5];
int randomnum = 5;
int main(int argc, char * argv[])
{
srand(time(NULL));
cout << "============\n";
while(row < rowincrement)
{
for(i = 0; i < 5; i++)
{
board[row][col] = rand()%5 + 1;
cout << board[row][col] << " ";
}
cout << endl;
cout << "============\n";
row++;
}
cout << endl;
return 0;
}
记录在案:
我想你是说
for(i = 0; i < 5; i++)
{
board[row][i] = rand()%5 + 1;
cout << board[row][i] << " ";
}
(i=0;i<5;i++)的
{
板[行][i]=rand()%5+1;
CUT假设你有一些特定的起点(x,y),你很好奇,如果在这一点上有三个相等的数,那么让我们考虑一下你在水平方向上看到的情况。那么,这样做的一个方法(忽略边界检查)会是这样的:
bool IsHorizontalMatch(int x, int y) {
/* Get the value of the start position. */
const int startValue = board[x][y];
/* Confirm the two values after it match. */
for (int i = 1; i < 3; ++i)
if (board[x + i][y] != startValue)
return false;
/* If we got here, then they all match! */
return true;
}
给定这个原语,您可以通过迭代所有可能的起点来检查所有可能的匹配
希望这有帮助
编辑:感谢评论员帮助修复我的愚蠢错误。:-我认为这应该有效;如果有人指出错误,我很乐意纠正
for( int row = 0; row<8 ; ++row )
{
bool outerLoopBreakFlag = false ;
for( int col=0 ; col<3; ++col )
{
// check for the winning conditions
// i.e., board[row][col] == board[row][col+1] == board[row][col+2]
// board[row][col] == board[row+1][col] == board[row+2][col]
// if any one is satisfied, set the outerLoopBreakFlag to true
else
break ;
}
if( outerLoopBreakFlag == true )
break ;
}
for(int row=0;row这是家庭作业吗?你试过什么了?这不是家庭作业,但基本上我有50多条if语句说if row[0]==row[1]&&row[2],然后…但我仍然没有所有的组合,我知道一定有更好的方法。如果数字是随机生成的,我怎么知道起始位置呢?这个想法是你把它放在一个循环中,尝试所有的起始位置。当这个函数返回真时,你知道你有起始位置,可以停止循环。事实上在这里,数字是随机的应该不会有太大的区别。这些对角线不是都检查相同的方向吗?我的意思是,从左下到右上与从右上到左下相同,只是从不同的起点开始。我认为你需要修正x步,使用相反的y步(反之亦然)。
bool IsVerticalMatch(int x, int y) {
/* Get the value of the start position. */
const int startValue = board[x][y];
/* Confirm the two values after it match. */
for (int i = 1; i < 3; ++i)
if (board[x][y + i] != startValue)
return false;
/* If we got here, then they all match! */
return true;
}
bool IsDiagonalDownMatch(int x, int y) {
/* Get the value of the start position. */
const int startValue = board[x][y];
/* Confirm the two values after it match. */
for (int i = 1; i < 3; ++i)
if (board[x + i][y + i] != startValue)
return false;
/* If we got here, then they all match! */
return true;
}
bool IsDiagonalUpMatch(int x, int y) {
/* Get the value of the start position. */
const int startValue = board[x][y];
/* Confirm the two values after it match. */
for (int i = 1; i < 3; ++i)
if (board[x + i][y - i] != startValue)
return false;
/* If we got here, then they all match! */
return true;
}
bool IsLinearMatch(int x, int y, int stepX, int stepY) {
/* Get the value of the start position. */
const int startValue = board[x][y];
/* Confirm the two values after it match. */
for (int i = 1; i < 3; ++i)
if (board[x + i * stepX][y + i * stepY] != startValue)
return false;
/* If we got here, then they all match! */
return true;
}
bool IsLineStartingAt(int x, int y) {
return (IsLinearMatch(x, y, 1, 0) || // Horizontal
IsLinearMatch(x, y, 0, 1) || // Vertical
IsLinearMatch(x, y, 1, 1) || // Diagonal Down
IsLinearMatch(x, y, 1, -1)); // Diagonal Up
}
for( int row = 0; row<8 ; ++row )
{
bool outerLoopBreakFlag = false ;
for( int col=0 ; col<3; ++col )
{
// check for the winning conditions
// i.e., board[row][col] == board[row][col+1] == board[row][col+2]
// board[row][col] == board[row+1][col] == board[row+2][col]
// if any one is satisfied, set the outerLoopBreakFlag to true
else
break ;
}
if( outerLoopBreakFlag == true )
break ;
}