如何将这些重复的代码压缩为一个函数?

时间:2019-07-07 00:12:24

标签: c++

我正在以新程序员的身份进行控制台乒乓游戏,但我不知道如何将非常重复的代码压缩为一个函数。

我试图将这段代码变成一个函数,但是我无法弄清楚代码将返回什么。

case 1:
            currentX--;
            delay = 75;
            direction = BumperBounce(currentX, currentY, char1PosTop, char1PosMid, char1PosMidBot, char1PosBot, char2PosTop, char2PosMid, char2PosMidBot, char2PosBot, direction);
            for (int i = 0; i < 12; i++) {
                if ((matrix[i][0] == 'O') || (matrix[0][0] == 'O') || (matrix[12][0] == 'O')) {
                    matrix[currentY][currentX] = ' ';
                    currentX = 14;
                    currentY = 6;
                    char2Points++;
                    direction = (rand() % 8);
                    this_thread::sleep_for(chrono::milliseconds(500));

                }
            }
            for (int i = 0; i < 12; i++) {
                if ((matrix[i][28] == 'O') || (matrix[0][28] == 'O') || (matrix[12][28] == 'O')) {
                    matrix[currentY][currentX] = ' ';
                    currentX = 14;
                    currentY = 6;
                    char1Points++;
                    direction = (rand() % 8);
                    this_thread::sleep_for(chrono::milliseconds(500));
                }
            }
            this_thread::sleep_for(chrono::milliseconds(delay));
            if (direction == oldDirection) {
                currentX--;
                currentY++;
            }
            break;

因此,我有四种情况使用这两种for循环。这些for循环的作用是确定“球”是位于屏幕的最右侧还是最左侧。如果是这样,它将使适当的玩家点数增加1。现在,我想将这两个for循环放入一个或两个函数中,但是我不知道该在返回中放入什么。我不知道此功能将如何在整体上将适当的玩家得分提高1。

2 个答案:

答案 0 :(得分:2)

将游戏参数组织到一个结构中(以避免将其声明为全局参数),如下所示:

struct para_t {
    int currentX, currentY, char1PosTop, char1PosMid, char1PosMidBot, char1PosBot, char2PosTop, char2PosMid, char2PosMidBot, char2PosBot, char1Points, char2Points, direction;
};

然后BumperBounce的声明将类似于以下内容:

int BumperBounce(struct para_t para);

现在,您的函数原型将如下所示:

void process(int matrix[100][100], struct para_t& para, int oldDirection);

您现在可以这样称呼它:

 /* your code*/
 case 1:
    process( matrix, para, oldDirection);
    break;    
/* more code*/

process定义可以像这样压缩:

void process(int matrix[100][100], struct para_t& para, int oldDirection) {
    para.currentX--;
    int delay = 75;
    para.direction = BumperBounce(para);

    for (int loop = 0; loop < 2; loop++)
    {
        for (int i = 0; i < 12; i++) {
            if ((matrix[i][loop == 0 ? 0 : 28] == 'O') || (matrix[0][loop == 0 ? 0 : 28] == 'O') || (matrix[12][loop == 0 ? 0 : 28] == 'O')) {
                matrix[para.currentY][para.currentX] = ' ';
                para.currentX = 14;
                para.currentY = 6;
                para.char2Points++;
                para.direction = (rand() % 8);
                this_thread::sleep_for(chrono::milliseconds(500));

            }
        }
    }

    this_thread::sleep_for(chrono::milliseconds(delay));

    if (para.direction == oldDirection) {
        para.currentX--;
        para.currentY++;
    }

}

答案 1 :(得分:-1)

不是所有函数都必须返回什么? 我会考虑使用带有1个参数的void函数。 像这样:

假设char1Points和char2Points是整数,而我没有编译这段代码,它只是作为有用的提示而编写的

void foo(int charpoint){
    matrix[currentY][currentX] = ' ';
    currentX = 14;
    currentY = 6;
    charpoint++;
    direction = (rand() % 8);
    this_thread::sleep_for(chrono::milliseconds(500));
}