C语言牙签游戏代码异常:函数被跳过、回合与输入无响应
问题排查:牙签游戏回合逻辑与输入环节被跳过
我调整了代码风格并更新了代码内容,但程序运行时所有回合逻辑和用户输入环节均被跳过。推测问题可能源于返回值格式错误或函数嵌套不当,附上代码寻求排查帮助:
#define ROUNDS 3 #include<stdio.h> #include<stdlib.h> #include<ctype.h> int main() { //Greets the user at the start of the round void greeting();{ printf("Welcome to the Toothpick Game! "); printf("Here are the rules. "); printf("There are currently 31 toothpicks on the table. "); printf("You and I will each get a turn to pick either 1, 2, or 3 toothpick off the table. "); printf("The player that gets to puck the last toothpicks looses the game! "); printf("Sounds easy right? Well lets see if you can beat me! "); printf("Ready to play?... Here we go! "); } //display welcome message to user for(int x = 0; x < ROUNDS; ++x) { int result = playRound(x + 1); //call playRound and assign result the value function returns void winnerAnnouncment(int user);{//overall winner of round announcement } } printf("******************************************************** "); printf("Thank you for playing! "); return 0; } int playRound(int round) { int toothpicks = 31; //number of toothpicks to start with int winner, taken, choice, leftover; printf("Welcome to a new round %d! ", round); printf("You may go first! "); int leftOnTable(int toothpicks, int taken);{ //calculate number of toothpicks left toothpicks = toothpicks - taken; while(toothpicks != 0)//loop to control playing of the game { int humanPick();{ //retrieve the user's guess int userchoice; printf("How many toothpicks do you want to take? "); scanf("%d", &taken); printf("Okay... You took %d off the table", taken); if (toothpicks = 1) winner = 0; int computerPick(int choice, int leftover);{ //computer makes its pick if (toothpicks > 4){//Caculates what the computer will take based off of the users choice choice = 4 - userchoice; toothpicks = toothpicks - choice; leftover = choice; printf("I am taking %d toothpicks off the table.", choice); } if(toothpicks = 2 || 3 || 4)//calculates how many toothpicks the computer will take to leave one left on the table if (toothpicks == 2){ choice = toothpicks - 1; leftover = 1; printf("I am taking %d toothpicks off the table.", choice); } if (toothpicks == 3){ choice = toothpicks - 1; printf("I am taking %d toothpicks off the table.", choice); } if (toothpicks == 4){ choice = toothpicks - 1; leftover = 1; printf("I am taking %d toothpicks off the table.", choice); } if (toothpicks = 1){ choice = 1; leftover = 0; toothpicks = leftover; winner = 1; printf("I will take the last toothpick."); } } } return toothpicks; } } return round; }
核心问题分析
- 函数定义语法彻底错误:你用
void greeting();{...}这种写法,分号会让编译器把它当成函数声明,后面的{}只是普通代码块,而且这些“函数”从未被调用,逻辑完全没执行。 - C语言不支持函数嵌套:你在
main、playRound内部定义其他函数的写法是非法的,编译器会直接忽略这些内部定义,代码块也不会运行。 - 逻辑执行顺序混乱:
playRound里刚写完leftOnTable的定义就直接return round,根本没进入游戏循环,直接跳过了所有回合逻辑。 - 基础语法错误:
if (toothpicks = 1)是赋值而非判断,printf里的换行应该用\n而非直接换行,这些错误会导致程序行为异常。
修复后的完整代码
#define ROUNDS 3 #include<stdio.h> #include<stdlib.h> #include<ctype.h> // 全局声明所有函数 void greeting(); int playRound(int round); void winnerAnnouncment(int winner); int humanPick(int toothpicks); int computerPick(int toothpicks, int humanTaken); int main() { greeting(); for(int x = 0; x < ROUNDS; ++x) { int result = playRound(x + 1); winnerAnnouncment(result); } printf("********************************************************\n"); printf("Thank you for playing!\n"); return 0; } void greeting() { printf("Welcome to the Toothpick Game!\n"); printf("Here are the rules.\n"); printf("There are currently 31 toothpicks on the table.\n"); printf("You and I will each pick 1, 2, or 3 toothpicks per turn.\n"); printf("The player who picks the last toothpick loses!\n"); printf("Sounds easy? Let's see if you can beat me!\n"); printf("Ready to play?... Here we go!\n"); } void winnerAnnouncment(int winner) { if(winner == 1) printf("You won this round!\n"); else printf("I won this round!\n"); } int playRound(int round) { int toothpicks = 31; int winner = 0; int humanTaken, compTaken; printf("\n=== Round %d ===\n", round); printf("You go first!\n"); while(toothpicks > 0) { // 玩家回合 humanTaken = humanPick(toothpicks); toothpicks -= humanTaken; if(toothpicks == 0) { winner = 0; // 玩家拿最后一根,输了 break; } // 电脑回合 compTaken = computerPick(toothpicks, humanTaken); toothpicks -= compTaken; printf("I took %d toothpicks. %d left on the table.\n", compTaken, toothpicks); if(toothpicks == 0) { winner = 1; // 电脑拿最后一根,玩家赢了 break; } } return winner; } int humanPick(int toothpicks) { int choice; do{ printf("How many toothpicks do you want to take? (1-3) "); scanf("%d", &choice); if(choice < 1 || choice > 3 || choice > toothpicks) printf("Invalid input! Choose 1-3, no more than remaining toothpicks.\n"); }while(choice < 1 || choice > 3 || choice > toothpicks); printf("You took %d. %d left.\n", choice, toothpicks - choice); return choice; } int computerPick(int toothpicks, int humanTaken) { int choice; // 策略:每次拿完后让剩余数量为4的倍数,最后剩1根留给玩家 if(toothpicks > 1) { choice = 4 - humanTaken; if(toothpicks - choice <= 0) choice = toothpicks - 1; } else { choice = 1; // 只剩1根,电脑拿了,玩家获胜 } return choice; }
关键修复点
- 把所有函数移到全局作用域,遵循C语言“先声明后使用”的规则
- 移除非法的函数嵌套,拆分逻辑为独立函数并主动调用
- 修复赋值/判断混淆的错误(将
=改为==) - 补全
printf的\n换行符,让输出正常显示 - 添加玩家输入合法性校验,避免无效操作
- 梳理游戏逻辑顺序,确保回合循环正常执行
内容的提问来源于stack exchange,提问作者traftrac
相关产品推荐
相关产品推荐

