C - 返回语句未退出void函数

时间:2017-06-30 02:44:52

标签: c return

我正在用C编写一个简单的连接四游戏来强化我在C中学习的内容。我已经在多个地方读过,void函数中的return语句应该结束函数的执行。但是,在将一个数字放入row6中的一个数组之后的下面的代码中,它还将一个数字放入row5中的同一列。

void putInBottomRow(int col, int player) {
if(row6[col] == 0) {
    row6[col] = player;
    return;
}
if(row5[col] == 0) {
    row5[col] = player;
    return;
}

return;
}

当我运行它时,这是输出: Command Window Output。 我错过了什么吗?问题可能在我的代码中的其他地方吗?感谢。

以下是其他相关代码:

#include <stdio.h>
#include <stdlib.h>

int row1[] = {0, 0, 0, 0, 0, 0, 0};
int row2[] = {0, 0, 0, 0, 0, 0, 0};
int row3[] = {0, 0, 0, 0, 0, 0, 0};
int row4[] = {0, 0, 0, 0, 0, 0, 0};
int row5[] = {0, 0, 0, 0, 0, 0, 0};
int row6[] = {0, 0, 0, 0, 0, 0, 0};

/*
  Use row six as bottom row and row 1 as top row.
*/

int main()
{
printf("\n   Connect Four\n");
printGameScreen();
printf("Player 1's turn.");
playerPlace(1);
printGameScreen();
printf("Player 2's turn.");
playerPlace(2);
printGameScreen();
return 0;
}

void playerPlace(int player) {
int rowChoice;
printf("Player 1's turn!\nWhich column (1-7)? ");
scanf(" %d", &rowChoice);
rowChoice--;
if(rowChoice > 6 || rowChoice < 0) {
    printf("That's not a row. Try again.");
    playerPlace(player);
    return;
 } else {
    switch(rowChoice) {
    case 0 :
        putInBottomRow(rowChoice, player);
    case 1 :
        putInBottomRow(rowChoice, player);
    case 2 :
        putInBottomRow(rowChoice, player);
    case 3 :
        putInBottomRow(rowChoice, player);
    case 4 :
        putInBottomRow(rowChoice, player);
    case 5 :
        putInBottomRow(rowChoice, player);
    case 6 :
        putInBottomRow(rowChoice, player);
    }

}
return;
}

1 个答案:

答案 0 :(得分:2)

你有:

if (rowChoice > 6 || rowChoice < 0) {
    printf("That's not a row. Try again.");
    playerPlace(player);
    return;
} else {
    switch (rowChoice) {
    case 0 :
        putInBottomRow(rowChoice, player);
    case 1 :
        putInBottomRow(rowChoice, player);
    case 2 :
        putInBottomRow(rowChoice, player);
    case 3 :
        putInBottomRow(rowChoice, player);
    case 4 :
        putInBottomRow(rowChoice, player);
    case 5 :
        putInBottomRow(rowChoice, player);
    case 6 :
        putInBottomRow(rowChoice, player);
    }
}

目前,如果选择的行为0,则行执行7次,第1行为6次,等等。表面上,每次函数调用后需要break; - 执行流程除非你添加打破。所以乍一看你需要:

if (rowChoice > 6 || rowChoice < 0) {
    printf("That's not a row. Try again.");
    playerPlace(player);
    return;
} else {
    switch (rowChoice) {
    case 0 :
        putInBottomRow(rowChoice, player);
        break;
    case 1 :
        putInBottomRow(rowChoice, player);
        break;
    case 2 :
        putInBottomRow(rowChoice, player);
        break;
    case 3 :
        putInBottomRow(rowChoice, player);
        break;
    case 4 :
        putInBottomRow(rowChoice, player);
        break;
    case 5 :
        putInBottomRow(rowChoice, player);
        break;
    case 6 :
        putInBottomRow(rowChoice, player);
        break;
    }
}

但进一步的研究表明,无论替代方案如何,您都在进行相同的函数调用。您已经检测并报告超出范围的值(这很好),因此整个开关应该成为一个简单的函数调用,所以您真的只需要:

if (rowChoice > 6 || rowChoice < 0) {
    printf("That's not a row. Try again.");
    playerPlace(player);
} else {
    putInBottomRow(rowChoice, player);
}

请注意,return正文中的if是不必要的; else正文之后的陈述也是return。实际上,由于函数没有返回任何值,因此return也不是必需的,因此从函数的底部掉下来也没问题。

在使用scanf()之前,您应该检查rowChoice代码是否成功。您应该始终检查输入是否成功。