0
我有一个随机生成的10x5阵列,我想检查3个数字是否水平和垂直匹配。我无法弄清楚检查数字是否匹配的好方法。我现在这样做的话,如果我的陈述会超过70,我知道还有更好的办法。我不认为我可以使用for循环来检查,因为我需要确切地知道哪3个数字(及其位置)是相同的。赢得第3场比赛的条件
这是我到目前为止的代码,对不起,如果它有点长。我只包括一行检查以节省空间。
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <cstdio>
using namespace std;
int col = 5;
int row = 0;
int board[9][4];
int i;
int main(int argc, char * argv[])
{
srand(time(NULL));
// generate the random board
cout << "==========\n";
while (row < 1)
{
for(i = 0; i < 5; i++)
{
board[row][i] = rand()%5 + 1;
cout << board[row][i] << " ";
}
cout << endl;
cout << "==========\n";
row++;
}
//----check for matches-----
// row 1
if (board[0][0] == (board[0][1] && board[0][2]))
{
cout << "Balls 1,2,3 match\n";
}
if (board[0][2] == (board[0][3] && board[0][4]))
{
cout << "Balls 3,4,5 match\n";
}
if (board[0][1] == (board[0][2] && board[0][3]))
{
cout << "Balls 2,3,4 match\n";
}
if (board[0][0] == (board[0][1] && board[0][2] && board[0][3]))
{
cout << "Balls 1,2,3,4 match\n";
}
if (board[0][1] == (board[0][2] && board[0][3] && board[0][4]))
{
cout << "Balls 2,3,4,5 match\n";
}
if (board[0][0] == (board[0][1] && board[0][2] && board[0][3] && board[0][4]))
{
cout << "Balls 1,2,3,4,5 match\n";
}
else
{
cout << "No balls match\n";
}
return 0;
}
[Win-condition for a connect-4 like game]可能的重复(http://stackoverflow.com/questions/4636575/win-conditions-for-a-connect-4-like-game) – templatetypedef 2011-01-10 22:41:12
这也是我的帖子,但它是不一样的 – FrozenWasteland 2011-01-10 22:46:01