C++ 对于4或5的匹配,忽略3的匹配

C++ 对于4或5的匹配,忽略3的匹配,c++,C++,我正在为一个数据结构类开发一个类似宝石的游戏。自从我开始使用C++以来,我的逻辑已经很陈旧了。 我的代码将4的匹配识别为3的两个匹配。我不知道如何绕过3比4的比赛。我尝试了一个if语句,比如if(counter=3&&board[y][x]==board[y][x-1]),但它没有按照我想要的方式工作 这是我的密码: // Counts the number of matches (3 or more) // returns number of matches found int CountJe

我正在为一个数据结构类开发一个类似宝石的游戏。自从我开始使用C++以来,我的逻辑已经很陈旧了。 我的代码将4的匹配识别为3的两个匹配。我不知道如何绕过3比4的比赛。我尝试了一个if语句,比如
if(counter=3&&board[y][x]==board[y][x-1])
,但它没有按照我想要的方式工作

这是我的密码:

// Counts the number of matches (3 or more)
// returns number of matches found
int CountJewels(){
int matches = 0;

// Horizontal jewels
for (int y = 0; y < size; y++){
    int counter = 1;
    for (int x = 1; x < size; x++){
        if (board[y][x] == board[y][x-1]){
            counter++;
            if (counter >= 3){
                matches++;
            }
        } else  {
            counter = 1;
        }
    }
}

// Vertical jewels
for (int x = 0; x < size; x++){
    int counter = 1;
    for(int y = 1; y < size; y++){
        if(board[y][x] == board[y-1][x]){
            counter++;
            if (counter >= 3){
                matches++;
            }
        } else  {
            counter = 1;
        }
    }
}
cout << matches << endl;
return matches;
}
//统计匹配数(3个或更多)
//返回找到的匹配数
int CountJewels(){
int匹配=0;
//水平宝石
对于(int y=0;y=3){
匹配++;
}
}否则{
计数器=1;
}
}
}
//垂直宝石
用于(int x=0;x=3){
匹配++;
}
}否则{
计数器=1;
}
}
}

cout非常简单。只需将条件更改为
计数器==3
。3个或更多的匹配项将被识别为单个匹配项,因为当连续匹配单元格数>=3时,您只需计数一次。

非常简单。只需将条件更改为
计数器==3
。3个或更多的匹配项将被识别为单个匹配项,因为当连续匹配的单元格数>=3时,您只能计数一次。

一种方法是不直接增加匹配,而只是增加相同宝石中的计数。最后,当发现一个不相同的字符时,如果计数大于2,则将其计为一个匹配:

//Counts the number of matches (3 or more)
// returns number of matches found
int CountJewels(){
int matches = 0;

// Horizontal jewels
for (int y = 0; y < size; y++){
    int counter = 1;
    for (int x = 1; x < size; x++){
        if (board[y][x] == board[y][x-1]){
            counter++;
        } else  {
            //Check if there was a matching before the mismatch.
            if(counter > 2){
              matches++;
            }
            counter = 1;
        }
    }
    //Checking last cell matches.
    if(counter > 2){
        matches++;
    }
}

//Vertical jewels
for (int x = 0; x < size; x++){
    int counter = 1;
    for(int y = 1; y < size; y++){
        if(board[y][x] == board[y-1][x]){
            counter++;
        } else  {
            //Check if there was a matching before the mismatch.
            if(counter > 2){
              matches++;
            }
            counter = 1;
        }

    }
    //Checking last cell matches.
    if(counter > 2){
        matches++;
    }
}
//统计匹配数(3个或更多)
//返回找到的匹配数
int CountJewels(){
int匹配=0;
//水平宝石
对于(int y=0;y2){
匹配++;
}
计数器=1;
}
}
//检查最后一个单元格是否匹配。
如果(计数器>2){
匹配++;
}
}
//垂直宝石
用于(int x=0;x2){
匹配++;
}
计数器=1;
}
}
//检查最后一个单元格是否匹配。
如果(计数器>2){
匹配++;
}
}

一种方法是不直接增加匹配项,而只是增加相同珠宝中的计数。最后,当发现不相同的字符时,如果计数大于2,则将其作为一个匹配项进行计数:

//Counts the number of matches (3 or more)
// returns number of matches found
int CountJewels(){
int matches = 0;

// Horizontal jewels
for (int y = 0; y < size; y++){
    int counter = 1;
    for (int x = 1; x < size; x++){
        if (board[y][x] == board[y][x-1]){
            counter++;
        } else  {
            //Check if there was a matching before the mismatch.
            if(counter > 2){
              matches++;
            }
            counter = 1;
        }
    }
    //Checking last cell matches.
    if(counter > 2){
        matches++;
    }
}

//Vertical jewels
for (int x = 0; x < size; x++){
    int counter = 1;
    for(int y = 1; y < size; y++){
        if(board[y][x] == board[y-1][x]){
            counter++;
        } else  {
            //Check if there was a matching before the mismatch.
            if(counter > 2){
              matches++;
            }
            counter = 1;
        }

    }
    //Checking last cell matches.
    if(counter > 2){
        matches++;
    }
}
//统计匹配数(3个或更多)
//返回找到的匹配数
int CountJewels(){
int匹配=0;
//水平宝石
对于(int y=0;y2){
匹配++;
}
计数器=1;
}
}
//检查最后一个单元格是否匹配。
如果(计数器>2){
匹配++;
}
}
//垂直宝石
用于(int x=0;x2){
匹配++;
}
计数器=1;
}
}
//检查最后一个单元格是否匹配。
如果(计数器>2){
匹配++;
}
}

您可以使用以下内容:

// Horizontal jewels
for (int y = 0; y < size; y++){
    int counter = 1;
    for (int x = 1; x < size; x++){
        if (board[y][x] == board[y][x-1]){
            counter++;
        } else  {
            foundAlignmentOf(counter);
            counter = 1;
        }
    }
    foundAlignmentOf(counter); // Or use extra row/column with sentinel values
}
//水平宝石
对于(int y=0;y

其中,
foundAlignmentOf(counter)
被称为找到的每个对齐方式(因此您可以根据对齐方式的大小使用测试条件)。

您可以使用类似于:

// Horizontal jewels
for (int y = 0; y < size; y++){
    int counter = 1;
    for (int x = 1; x < size; x++){
        if (board[y][x] == board[y][x-1]){
            counter++;
        } else  {
            foundAlignmentOf(counter);
            counter = 1;
        }
    }
    foundAlignmentOf(counter); // Or use extra row/column with sentinel values
}
//水平宝石
对于(int y=0;y

其中
foundAlignmentOf(counter)
被称为找到的每个对齐方式(因此您可以根据对齐方式的大小使用测试条件)。

我知道Bejeweled的规则,我可以提供帮助

答案很简单,做一个向后的if/then或select语句

if (counter >= 5) matches++;
else if (counter == 4) matches++;
else if (counter == 3) matches++;


你可以走得更高,甚至做一个相反的for/do循环来检查和保存一些代码。这只是一个例子…

我知道Bejeweled的规则,我可以提供帮助

答案很简单,做一个向后的if/then或select语句

if (counter >= 5) matches++;
else if (counter == 4) matches++;
else if (counter == 3) matches++;


你可以走得更高,甚至做一个相反的for/do循环来检查和保存一些代码。这只是一个例子…

如果匹配在最后,你需要修正它。如果(计数器>2)匹配+++
在内部循环之后,移动最后一个
会更容易。对,不需要在第二个循环中不断检查。对不起,我的错了。(柜台是