【发布时间】:2020-06-07 09:16:45
【问题描述】:
我有一个二维宝石数组,所有的宝石都被赋予了随机颜色。现在我想检测一行或一列中三个或更多连续的宝石是否具有相同的颜色。如果是这样,我想对这些宝石采取一些行动。
Gem gems[10][10];
for(int i=0;i<10;++i){
for(int j=0;j<10;++j){
gems[i][j].setColor(GetRandInRange(0,6));
}
}
bool detectMatch(){
for(int i=0;i<10;++i){
for(int j=0;j<10;++j){
// Code to detect three or more consecutive gems with same color
// Give some special color to the matched gems
}
}
这是我尝试的方法,但它不起作用
bool GameBoard::findMatch(){
for(int i=0;i<10;++i){
int count=0;
for(int j=0;j<10;++j){
if(j!=0){
if(gems[i][j].getColor()==gems[i][j-1].getColor()){ //Color same with previous one
int a=i, b=j;
while(gems[a][b].getColor()==gems[i][j].getColor() && (a<10 && b<10)){ // Check till the color does not match
count++;
a++;
b++;
}
if(count>=3){ //If 3 or more have matched
for(int a=i, b=j, c=0;c<count;++c){
gems[a][b].setColor(0);
}
return true;
}
}
}
}
}
return false;
}
如果您可以帮助我处理此代码
【问题讨论】:
-
有几个问题不太清楚:数组限制为10乘10?或者可以更大?如果你连续发现三个连续的宝石颜色相同,并且如果你找到的宝石的上排或下排有宝石,你是否也需要改变这些宝石的颜色?
-
@TigerYu 我希望它改变检测到的第一个匹配的颜色,因为它在主程序的 do while 循环中被调用,直到它返回 false。
-
并且数组大小限制为 10 x 10