C++ 为什么我的简单递归方法是&x27;s的最终返回值始终关闭1?

C++ 为什么我的简单递归方法是&x27;s的最终返回值始终关闭1?,c++,recursion,C++,Recursion,我正在尝试创建一个基于文本的版本 代码: #包括 #包括 #包括 类失眠症{ 公众: Clickomania(); 矢量板; int移动(int,int); bool-isSolved(); 作废打印(); 无效下推(); bool是有效的(); }; Clickomania::Clickomania() :电路板(12,标准::矢量(8,0)){ srand((未签名)时间(0)); 对于(int i=0;i您想要的>= 您要检查行>=12列>=8看起来您在错误的位置增加了totalMoves

我正在尝试创建一个基于文本的版本

代码:

#包括
#包括
#包括
类失眠症{
公众:
Clickomania();
矢量板;
int移动(int,int);
bool-isSolved();
作废打印();
无效下推();
bool是有效的();
};
Clickomania::Clickomania()
:电路板(12,标准::矢量(8,0)){
srand((未签名)时间(0));
对于(int i=0;i<12;i++){
对于(int j=0;j<8;j++){
int color=(rand()%3)+1;
板[i][j]=颜色;
}
}
}
void Clickomania::pushDown(){
对于(int i=0;i<8;i++){
对于(int j=0;j<12;j++){
如果(板[j][i]==0){
对于(int k=j;k>0;k--){
董事会[k][i]=董事会[k-1][i];
}
董事会[0][i]=0;
}
}
}
}
int Clickomania::move(int行,int列){
布尔匹配=假;
int totalMatches=0;
如果(行>12 | |行<0 | |列>8 | |列<0){
返回0;
}
int currentColor=线路板[行][col];
线路板[行][列]=0;
如果((第+1行)<12){
if(线路板[行+1][列]==currentColor)
{
匹配=真;
totalMatches++;
totalMatches+=移动(行+1,列);
}
}
如果((第1行)>=0){
if(电路板[第1行][col]==currentColor){
匹配=真;
totalMatches++;
totalMatches+=移动(第1行,列);
}
}
如果((列+1)<8){
if(线路板[行][col+1]==currentColor){
匹配=真;
totalMatches++;
totalMatches+=移动(行、列+1);
}
}
如果((列-1)>=0){
if(线路板[行][col-1]==currentColor){
匹配=真;
totalMatches++;
totalMatches+=移动(行,列-1);
}
}
返回所有匹配项;
}
void Clickomania::print(){
对于(int i=0;i<12;i++){
对于(int j=0;j<8;j++){
std::cout col;
int numDestroyed=游戏移动(行、列);
game.print();

std::cout0基于索引。您不想检查>您想要的>=


您想要检查行>=12列>=8

0的索引。您不想要检查>您想要的>=


您要检查行>=12列>=8

看起来您在错误的位置增加了totalMoves。您应该在设置
board[r][c]=0
的点上计算匹配项,并删除对
totalMoves++
的其他引用


第一次调用没有被计数是对的,它只是对递归调用进行计数。

看起来您在错误的位置增加了totalMoves。您应该在设置
board[r][c]=0
的点上对匹配进行计数,并删除对
totalMoves++
的其他引用


你是对的,第一个调用没有被计数,它只是计算递归调用。

你到底在说什么?在开头的guard子句中?我的方法是将正确的正方形标记为“0”。只有计数是关闭的。我认为您在技术上对这些边界是正确的,但这并不是使此方法在其返回值中以1关闭的原因。您到底在说什么?在开头的guard子句中?我的方法是将正确的正方形标记为“0”。只有计数是关闭的。我认为您在技术上对这些边界是正确的,但这并不是使此方法在其返回值中以1关闭的原因。@Stephen,就是这样。非常感谢!我还有一个简单的问题,我将如何修改move()因此,您不能在没有其他匹配项的情况下移动一个正方形?在这种情况下,您不能翻转一个正方形并让它返回1,而只将其自身设置为“0”。它必须至少有一个匹配项与其相邻,否则它将从一开始返回0,而不会将任何正方形设置为0。您可以提供“hasAdjacentMatches”函数,您可以用它来保护任何操作。顺便说一句,我喜欢老式的参考。@Stephen,就是这样。非常感谢!我还有一个简单的问题,我将如何修改move()因此,您不能在没有其他匹配项的情况下移动一个正方形?在这种情况下,您不能翻转一个正方形并让它返回1,而只将其自身设置为“0”。它必须至少有一个匹配项与其相邻,否则它将从一开始返回0,而不会将任何正方形设置为0。您可以提供“hasAdjacentMatches”函数,你可以用它来保护任何行为。顺便说一下,我喜欢老式的参考。
#include <iostream>
#include <vector>
#include <ctime>

class Clickomania {
    public:
        Clickomania();
        std::vector<std::vector<int> > board;
        int move(int, int);
        bool isSolved();
        void print();
        void pushDown();
        bool isValid();
};

Clickomania::Clickomania()
    : board(12, std::vector<int>(8,0)) {

    srand((unsigned)time(0));

    for(int i = 0; i < 12; i++) {
        for(int j = 0; j < 8; j++) {
            int color = (rand() % 3) + 1;
            board[i][j] = color;
        }
    }
}

void Clickomania::pushDown() {
    for(int i = 0; i < 8; i++) {
        for(int j = 0; j < 12; j++) {
            if (board[j][i] == 0) {
                for(int k = j; k > 0; k--) {
                    board[k][i] = board[k-1][i];
                }
                board[0][i] = 0;
            }
        }
    }
}

int Clickomania::move(int row, int col) {
    bool match = false;
    int totalMatches = 0;
    if (row > 12 || row < 0 || col > 8 || col < 0) {
        return 0;
    }

    int currentColor = board[row][col];
    board[row][col] = 0;

    if ((row + 1) < 12) {
        if (board[row+1][col] == currentColor)
        {
            match = true;
            totalMatches++;
            totalMatches +=  move(row+1, col);
        }
    }

    if ((row - 1) >= 0) {
        if (board[row-1][col] == currentColor) {
            match = true;
            totalMatches++;
            totalMatches += move(row-1, col);
        }
    }

    if ((col + 1) < 8) {
        if (board[row][col+1] == currentColor) {
            match = true;
            totalMatches++;
            totalMatches += move(row, col+1);
        }
    }

    if ((col - 1) >= 0) {
        if (board[row][col-1] == currentColor) {
            match = true;
            totalMatches++;
            totalMatches += move(row, col-1);
        }
    }

    return totalMatches;
}

void Clickomania::print() {
    for(int i = 0; i < 12; i++) {
        for(int j = 0; j < 8; j++) {
            std::cout << board[i][j];
        }
        std::cout << "\n";
    }
}

int main() {
    Clickomania game;
    game.print();
    int row;
    int col;
    std::cout << "Enter row: ";
    std::cin >> row;
    std::cout << "Enter col: ";
    std::cin >> col;
    int numDestroyed = game.move(row,col);
    game.print();
    std::cout << "Destroyed: " << numDestroyed << "\n";
}
int Clickomania::move(int row, int col) {
        bool match = false;
        int totalMatches = 0;

        if (row > 12 || row < 0 || col > 8 || col < 0) {
            return 0;
        }

        int currentColor = board[row][col];
        board[row][col] = 0;

        if ((row + 1) < 12) {
            if (board[row+1][col] == currentColor) {
                match = true;
                totalMatches++;
                totalMatches +=  move(row+1, col);
            }
        }

        if ((row - 1) >= 0) {
            if (board[row-1][col] == currentColor) {
                match = true;
                totalMatches++;
                totalMatches += move(row-1, col);
            }
        }

        if ((col + 1) < 8) {
            if (board[row][col+1] == currentColor)
            {
                match = true;
                totalMatches++;
                totalMatches += move(row, col+1);
            }
        }

        if ((col - 1) >= 0) {
            if (board[row][col-1] == currentColor) {
                match = true;
                totalMatches++;
                totalMatches += move(row, col-1);
            }
        }

        return totalMatches;
    }