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