• C++精简实现2048游戏逻辑


    #include 
    #include 
    
    #define BORDSIZE 4
    #define SIZE 16
    
    using namespace std;
    
    void printMap(int arr16[SIZE])
    {
        int(*arr2v)[BORDSIZE] = (int(*)[BORDSIZE])arr16;
        for (int i = 0; i < BORDSIZE; i++)
        {
            for (int j = 0; j < BORDSIZE; j++)
            {
                cout << arr2v[i][j] << " ";
            }
            cout << endl;
        }
        cout << "-----------------" << endl;
    }
    
    int findFirstNotZero(const int curX, const int curY, int operX, int operY, int *arr16, int &outX, int &outY)
    {
        int(*arr2v)[BORDSIZE] = (int(*)[BORDSIZE])arr16;
        outX = curX;
        outY = curY;
        if (outX < 0 || outY < 0 || outX >= BORDSIZE || outY >= BORDSIZE)
        {
            return -1;
        }
        while (outX >= 0 && outY >= 0 && outX < BORDSIZE && outY < BORDSIZE)
        {
            if (arr2v[outX][outY] != 0)
            {
                return 0;
            }
            outX += operX;
            outY += operY;
        }
        return -1;
    }
    
    int MoveTo(const int *arr16, int operX, int operY, int *arr16Out, int maxLevel, bool &bMaxLevelHappen)
    {
        memcpy(arr16Out, arr16, sizeof(int) * SIZE);
        int(*arr2v)[BORDSIZE] = (int(*)[BORDSIZE])arr16Out;
        int score = 0;
        bMaxLevelHappen = false;
        for (int idx1 = 0; idx1 < BORDSIZE; idx1++)
        {
            int curX = 0, curY = 0;
            if (operX == 0)
            {
                curX = idx1;
                if (operY == 1)
                {
                    curY = 0;
                }
                else if (operY == -1)
                {
                    curY = BORDSIZE - 1;
                }
                else
                {
                    return -1;
                }
            }
            else if (operY == 0)
            {
                curY = idx1;
                if (operX == 1)
                {
                    curX = 0;
                }
                else if (operX == -1)
                {
                    curX = BORDSIZE - 1;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                return -1;
            }
    
            while (curX >= 0 && curX < BORDSIZE && curY >= 0 && curY < BORDSIZE)
            {
                int findedFirstNotZeroX = 0, findedFirstNotZeroY = 0;
                // 从(curX,curY)开始在oper方向上用offset偏移寻找第一个非零放到(curX,curY),并把移动的元素位置赋为0
                int iRet = findFirstNotZero(curX, curY, operX, operY, arr16Out, findedFirstNotZeroX, findedFirstNotZeroY);
                if (iRet != 0)
                {
                    break;
                }
                cout << "find not zero:" << arr2v[findedFirstNotZeroX][findedFirstNotZeroY] << endl;
                arr2v[curX][curY] = arr2v[findedFirstNotZeroX][findedFirstNotZeroY];
                if (findedFirstNotZeroX != curX || findedFirstNotZeroY != curY)
                {
                    arr2v[findedFirstNotZeroX][findedFirstNotZeroY] = 0;
                }
    
                cout << "idx1:" << idx1 << endl;
                printMap(arr16Out);
                // 从(curX,curY)+(operX,operY)开始在oper方向上用offset偏移寻找第一个非零放到(curX,curY)+(operX,operY),并把移动的元素位置赋为0
                iRet = findFirstNotZero(curX + operX, curY + operY, operX, operY, arr16Out, findedFirstNotZeroX, findedFirstNotZeroY);
                if (iRet != 0)
                {
                    break;
                }
                cout << "find not zero:" << arr2v[findedFirstNotZeroX][findedFirstNotZeroY] << endl;
                arr2v[curX + operX][curY + operY] = arr2v[findedFirstNotZeroX][findedFirstNotZeroY];
                if (findedFirstNotZeroX != curX + operX || findedFirstNotZeroY != curY + operY)
                {
                    arr2v[findedFirstNotZeroX][findedFirstNotZeroY] = 0;
                }
                cout << "idx1:" << idx1 << endl;
                printMap(arr16Out);
                // 尝试(curX,curY)与(curX,curY)+(operX,operY)的合并
                if (arr2v[curX][curY] == arr2v[curX + operX][curY + operY] && arr2v[curX][curY] < maxLevel)
                {
                    arr2v[curX][curY] *= 2;
                    if (arr2v[curX][curY] == maxLevel)
                    {
                        bMaxLevelHappen = true;
                    }
                    score += arr2v[curX][curY];
                    arr2v[curX + operX][curY + operY] = 0;
                }
                cout << "idx1:" << idx1 << " try merge" << endl;
                printMap(arr16Out);
                //  偏移到下一个位置,进行下一次Loop
                curX += operX;
                curY += operY;
            }
        }
        return score;
    }
    
    bool gameOver(const int *arr16, int maxLevel)
    {
        // 判断棋盘有没有满
        for (int i = 0; i < SIZE; i++)
        {
            if (arr16[i] == 0)
            {
                return false;
            }
        }
        int arr16Out[SIZE] = {0};
        bool bMaxLevelHappen = false;
        // 向上探测
        int score = MoveTo(arr16, 1, 0, arr16Out, maxLevel, bMaxLevelHappen);
        if (score != 0)
        {
            return false;
        }
        // 向下探测
        score = MoveTo(arr16, -1, 0, arr16Out, maxLevel, bMaxLevelHappen);
        if (score != 0)
        {
            return false;
        }
        // 向左探测
        score = MoveTo(arr16, 0, 1, arr16Out, maxLevel, bMaxLevelHappen);
        if (score != 0)
        {
            return false;
        }
        // 向右探测
        score = MoveTo(arr16, 0, -1, arr16Out, maxLevel, bMaxLevelHappen);
        if (score != 0)
        {
            return false;
        }
        return true;
    }
    
    enum Move
    {
        TOP = 0, //(1,0)
        DOWN,    //(-1,0)
        LEFT,    //(0,1)
        RIGHT    //(0,-1)
    };
    
    int main()
    {
        int maxLevel = 12;
        int arr16[SIZE] = {1, 2, 3, 4,
                           2, 3, 4, 5,
                           3, 4, 5, 6,
                           4, 5, 6, 7};
        printMap(arr16);
        cout << "start" << endl;
        bool bMaxLevelHappen = false;
        int arr16Out[SIZE] = {0};
        int score = MoveTo(arr16, 0, -1, arr16Out, maxLevel, bMaxLevelHappen);
        printMap(arr16);
        printMap(arr16Out);
        cout << "score:" << score << endl;
        cout << "maxHappen:" << bMaxLevelHappen << endl;
        cout << "game over:" << gameOver(arr16Out, maxLevel) << endl;
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
  • 相关阅读:
    初识Java 11-2 函数式编程
    Linux yum 运行时提示编码问题错误
    spark sql(四)物理计划解析
    【更新】囚生CYの备忘录(202331014~)
    ATF(TF-A)/OPTEE之动态代码分析汇总
    这是什么是
    『忘了再学』Shell基础 — 23、其他环境变量配置文件
    25.1 MySQL SELECT语句
    Java8 时间处理
    java spring cloud 企业工程管理系统源码+二次开发+定制化服务
  • 原文地址:https://blog.csdn.net/qq_45812941/article/details/134083695