| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2318 人关注过本帖
标题:请大佬帮我讲解每一个函数的意思
只看楼主 加入收藏
我是一个黑客
Rank: 1
等 级:新手上路
帖 子:69
专家分:2
注 册:2018-6-11
结帖率:81.82%
收藏
 问题点数:0 回复次数:3 
请大佬帮我讲解每一个函数的意思
这是一个游戏代码,请大佬帮我讲解一下每一段函数的意思
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <stack.h>
#include <string.h>
#include <map.h>
#include <vector.h>
#include <math.h>

using namespace std;

const int MAXX = 10;

int box[MAXX][MAXX];//定义数组

void moveLeft(int n, bool &ret){
    for (int i = 0; i < n; ++i){
        for (int j = 0; j < n; ++j){
            int k;
            for (k = j - 1; k >= 0; --k){
                if (box[i][k] != -1){
                    break;
                }
            }
            box[i][k + 1] = box[i][j];

            if ((k + 1) != j){
                box[i][j] = -1;
                ret = true;
            }
        }
    }
}

void mergeLeft(int n, bool &ret){
    for (int i = 0; i < n; ++i){
        for (int j = 0; j < n - 1; ++j){
            if (box[i][j] == -1)continue;

            if (box[i][j] == box[i][j + 1]){
                box[i][j] *= 2;
                box[i][j + 1] = -1;
                ret = true;
                ++j;
            }
        }
    }
}

bool left(int n){
    bool ret = false;

    mergeLeft(n, ret);
    moveLeft(n, ret);
    return ret;
}

void moveRight(int n, bool &ret){
    for (int i = 0; i < n; ++i){
        for (int j = n - 1; j >= 0; --j){
            if (box[i][j] == -1)continue;

            int k;
            for (k = j + 1; k < n; ++k){
                if (box[i][k] != -1){
                    break;
                }
            }

            box[i][k - 1] = box[i][j];
            if ((k - 1) != j){
                box[i][j] = -1;
                ret = true;
            }
        }
    }
}

void mergeRight(int n, bool &ret){
    for (int i = 0; i < n; ++i){
        for (int j = n - 1; j >0; --j){
            if (box[i][j] == -1)continue;

            if (box[i][j] == box[i][j - 1]){
                box[i][j] *= 2;
                box[i][j - 1] = -1;
                ret = true;
                --j;
            }
        }
    }
}

bool right(int n){
    bool ret = false;
    moveRight(n, ret);
    mergeRight(n, ret);
    moveRight(n, ret);
    return ret;
}

void moveUp(int n, bool &ret){
    for (int j = 0; j < n; ++j){
        for (int i = 0; i < n; ++i){
            int k;
            for (k = i - 1; k >= 0; --k){
                if (box[k][j] != -1){
                    break;
                }
            }
            box[k + 1][j] = box[i][j];

            if ((k + 1) != i){
                box[i][j] = -1;
                ret = true;
            }
        }
    }
}

void mergeUp(int n, bool &ret){
    for (int j = 0; j < n; ++j){
        for (int i = 0; i < n - 1; ++i){
            if (box[i][j] == -1)continue;

            if (box[i][j] == box[i + 1][j]){
                box[i][j] *= 2;
                box[i + 1][j] = -1;
                ret = true;
                ++i;
            }
        }
    }
}

bool up(int n){
    bool ret = false;
    moveUp(n, ret);
    mergeUp(n, ret);
    moveUp(n, ret);
    return ret;
}

void moveDown(int n, bool &ret){
    for (int j = 0; j < n; ++j){
        for (int i = n - 1; i >= 0; --i){
            if (box[i][j] == -1)continue;

            int k;
            for (k = i + 1; k < n; ++k){
                if (box[k][j] != -1)break;
            }

            box[k - 1][j] = box[i][j];

            if ((k - 1) != i){
                box[i][j] = -1;
                ret = true;
            }
        }
    }
}

void mergeDown(int n, bool &ret){
    for (int j = 0; j < n; ++j){
        for (int i = n - 1; i > 0; --i){
            if (box[i][j] == -1)continue;

            if (box[i][j] == box[i - 1][j]){
                box[i][j] *= 2;
                box[i - 1][j] = -1;
                ret = true;
                --i;
            }
        }
    }
}

bool down(int n){
    bool ret = false;
    moveDown(n, ret);
    mergeDown(n, ret);
    moveDown(n, ret);
    return ret;
}

void line(int n){
    for (int i = 0; i < n; ++i){
        printf("--------");
    }
    printf("-");
    printf("\n");
}

void print(int n){
    for (int i = 0; i < n; ++i){
        line(n);
        for (int j = 0; j < n; ++j){
            printf("|");
            if (box[i][j] == -1){
                printf("\t");
                continue;
            }

            printf("%2d\t", box[i][j]);
        }
        printf("|");
        printf("\n");
    }
    line(n);
}

bool isFull(int n){
    bool mark = true;
    for (int i = 0; i < n; ++i){
        for (int j = 0; j < n; ++j){
            if (box[i][j] == -1){
                mark = false;
                return mark;
            }
        }
    }
    return mark;
}

bool isOver(int n){
    if (!isFull(n)){
        return false;
    }
    for (int i = 0; i < n; ++i){
        for (int j = 0; j < n; ++j){
            if (box[i][j] == box[i][j + 1] || box[i][j] == box[i + 1][j]){
                return false;
            }
        }
    }
    return true;
}

void fillBox(int n){
    int i, j, num;

    if (isFull(n)){
        return;
    }

    while (true){
        i = rand() % n;
        j = rand() % n;
        num = rand() % 2 == 0 ? 2 : 4;

        if (box[i][j] == -1){
            box[i][j] = num;
            break;
        }
    }
}

int main(){
    //freopen("in.txt", "r", stdin);
    memset(box, -1, sizeof(box));

    int n;
    bool mark;
    scanf("%d%*c", &n);

    fillBox(n);
    fillBox(n);
    print(n);

    while (true){
        char ch;
        scanf("%c%*c", &ch);
        if (ch == 'a'){
            mark = left(n);
        }
        else if (ch == 'd'){
            mark = right(n);
        }
        else if (ch == 'w'){
            mark = up(n);
        }
        else if (ch == 's'){
            mark = down(n);
        }
        else{
            continue;
        }
        system("cls");
        printf("Move:\n");
        print(n);
        if (!mark){
            continue;
        }
        fillBox(n);
        printf("Fill:\n");
        print(n);

        if (isOver(n)){
            printf("\n\nGame Over!\n\n");
            break;
        }
    }

    return 0;
}
搜索更多相关主题的帖子: bool ret box int for 
2019-09-04 11:31
dd1161639542
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2019-9-13
收藏
得分:0 
2019-09-13 15:22
zbjzbj
Rank: 12Rank: 12Rank: 12
来 自:郑州
等 级:贵宾
威 望:52
帖 子:633
专家分:3045
注 册:2011-4-22
收藏
得分:0 
知道为什么少有人光顾吗?因为胃口和脑容量不配套。
2019-09-13 20:56
亿白
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2019-9-17
收藏
得分:0 
回复 2楼 dd1161639542
你也是新手?
2019-09-17 11:22
快速回复:请大佬帮我讲解每一个函数的意思
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025578 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved