2048-如何阻止我的图块合并两次?

时间:2019-01-09 03:19:55

标签: c++

我正尝试制定一种合并算法,但除了一件事外,它工作正常:单个图块可以一步移动两次,因此当我向左移动时,4224变为8400,而不是4440。 我试过在else if块的末尾添加一个break语句,如果磁贴已合并,则添加一个标志,但是这些都不起作用。对于可能犯的任何错误,我深表歉意,但这是我关于stackoverflow的第一篇文章。

#define TILES_X 4
#define TILES_Y 4
//Moving UP with SDL library
for (int i = 0; i < TILES_X; i++) {
    for (int j = 1; j < TILES_Y; j++) {
        if (gameBoard[i][j] != 0) {
            for (int k = 0; k < j; k++) {
                if (gameBoard[i][k] == 0) {
                    gameBoard[i][k] = gameBoard[i][j];
                    gameBoard[i][j] = 0;
                }
                else if (gameBoard[i][k] == gameBoard[i][j] && (gameBoard[i][k+1]==0||gameBoard[i][k+1]==gameBoard[i][k])) {
                    gameBoard[i][k] = 2 * gameBoard[i][j];
                    gameBoard[i][j] = 0;

                }
            }
        }
    }
}

0 个答案:

没有答案