C语言多文件骰子游戏输出异常,请求排查main函数问题
骰子游戏程序修复求助
我使用两个C源文件(Dicegame.c、main.c)和一个头文件(dicegame.h)开发一款骰子游戏,但执行程序时输出不符合预期。经测试,除main函数外的其他功能函数工作正常,怀疑是main函数中的函数调用逻辑错误导致异常输出。以下是我的代码、当前错误输出及期望输出,希望得到帮助以修复程序使其正常运行:
Dicegame.c 文件
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include "dicegame.h" // 生成指定范围的伪随机数 int getRandomNumber(int min, int max) { int rndmNum = (rand() % (max-min+1)) + min; return rndmNum; } // 确定当前轮次类型 enum ROUNDTYPE getRoundType() { enum ROUNDTYPE roundtype; int rT = getRandomNumber(1, 10); if (rT <= 2) { roundtype = Bonus; printf("\nType : Bonus"); } else if (rT >= 5) { roundtype = Regular; printf("\nType : Regular"); } else { roundtype = Double; printf("\nType : Double"); } return roundtype; } // 根据轮次类型计算分数 int getRoundPoints(enum ROUNDTYPE roundtype) { int points; if (roundtype == Bonus) { points = 200; } else if (roundtype == Double) { points = getRandomNumber(1, 10) * 20; } else { points = getRandomNumber(1, 10) * 10; } return points; } // 打印玩家分数 void printPlayerPoints( int p1, int p2) { printf("%d\n",p1); printf("%d\n\n",p2); } // 打印轮次信息 void printRoundInfo(enum ROUNDTYPE roundtype, int dice, int points) { printf("%d\n",roundtype); printf("Dice : %d\n",dice); printf("Points : %d\n",points); }
dicegame.h 文件
#ifndef DICEGAME_H #define DICEGAME_H enum ROUNDTYPE{ Bonus, Double, Regular}; int getRandomNumber(int min, int max); enum ROUNDTYPE getRoundType(); int getRoundPoints(enum ROUNDTYPE roundtype); void printPlayerPoints( int p1, int p2); void printRoundInfo(enum ROUNDTYPE roundtype, int dice, int points); #endif // DICEGAME
main.c 文件
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include "dicegame.h" int main() { srand(time(0)); int p1 = 0; int p2 = 0; enum ROUNDTYPE roundtype; int pTurn = getRandomNumber(1, 2); int round = 0; int totalRnd; int dice = ((rand() % 6)+1); int points; printf("How many rounds would you like to play:\t"); scanf("%d\n", &totalRnd); printPlayerPoints(p1, p2); for (int i = 0; i == totalRnd; i++ && round++) { getRoundType(); getRandomNumber(1, 6); getRoundPoints(roundtype); printf("Round %d of %d\n", round, totalRnd); printf("----------------\n"); if (pTurn % 2 == 1) { printf("Player : 1"); if (dice % 2 == 1) { p1 = (points += getRoundPoints(roundtype)); } else if(dice % 2 == 0) { p1 = (points -= getRoundPoints(roundtype)); pTurn++; } } else { printf("Player : 2"); if (dice % 2 == 0) { p2 = (points += getRoundPoints(roundtype)); } else if(dice % 2 == 1) { p2 = (points -= getRoundPoints(roundtype)); pTurn++; } } printRoundInfo(roundtype, dice, points); printPlayerPoints(p1, p2); } printf("\nGAME OVER!!\n"); printf("Player 1 had: %d points\n", p1); printf("Player 1 had: %d points\n", p2); if (p1 > p2) { printf("Player 1 wins!!!!\n"); } else if(p2 > p1) { printf("Player 2 wins!!!!\n"); } else { printf("It's a tie!!!!\n"); } return 0; }
当前错误输出
Type : RegularHow many rounds would you like to play:5 5 0 0 GAME OVER!! Player 1 had: 0 points Player 1 had: 0 points It's a tie!!!!
期望输出
Enter the number of rounds: 5 P1 : 0 P2 : 0 ROUND 1 -------- Player : 1 Type : REGULAR Dice : 5 Points : 70 P1 : 70 P2 : 0 ROUND 2 -------- Player : 1 Type : REGULAR Dice : 2 Points : 50 P1 : 20 P2 : 0 ROUND 3 -------- Player : 2 Type : DOUBLE Dice : 4 Points : 180 P1 : 20 P2 : 180 GAME OVER!! P2 Won
修复方案及修改后的代码
主要问题点及修复:
- scanf格式错误:移除
scanf("%d\n", &totalRnd);中的\n,改为scanf("%d", &totalRnd);,否则输入后需要额外回车才会继续执行。 - for循环条件错误:原循环条件
i == totalRnd永远不成立,循环未执行。改为for (int i = 0; i < totalRnd; i++),并在循环内将轮次设为i+1。 - 未接收函数返回值:调用
getRoundType()、getRandomNumber(1,6)、getRoundPoints(roundtype)时,必须将返回值赋值给对应变量,否则变量为未初始化的垃圾值。 - 积分计算逻辑错误:原代码重复调用
getRoundPoints且错误覆盖玩家分数,应直接使用已计算好的points变量进行加减。 - 打印格式不符期望:修改
printPlayerPoints和printRoundInfo函数的输出格式,匹配期望输出的样式。 - 枚举值打印错误:
printRoundInfo中直接打印枚举数值,改为根据枚举值输出对应的字符串。 - 最终输出笔误:将重复的
Player 1改为Player 2。
修改后的代码:
修改后的 Dicegame.c(仅修改两个打印函数)
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include "dicegame.h" // 生成指定范围的伪随机数 int getRandomNumber(int min, int max) { int rndmNum = (rand() % (max-min+1)) + min; return rndmNum; } // 确定当前轮次类型 enum ROUNDTYPE getRoundType() { enum ROUNDTYPE roundtype; int rT = getRandomNumber(1, 10); if (rT <= 2) { roundtype = Bonus; printf("\nType : Bonus"); } else if (rT >= 5) { roundtype = Regular; printf("\nType : Regular"); } else { roundtype = Double; printf("\nType : Double"); } return roundtype; } // 根据轮次类型计算分数 int getRoundPoints(enum ROUNDTYPE roundtype) { int points; if (roundtype == Bonus) { points = 200; } else if (roundtype == Double) { points = getRandomNumber(1, 10) * 20; } else { points = getRandomNumber(1, 10) * 10; } return points; } // 打印玩家分数(匹配期望格式) void printPlayerPoints( int p1, int p2) { printf("P1 : %d\n", p1); printf("P2 : %d\n\n", p2); } // 打印轮次信息(修复枚举值打印) void printRoundInfo(enum ROUNDTYPE roundtype, int dice, int points) { const char* typeStr; switch(roundtype) { case Bonus: typeStr = "Bonus"; break; case Double: typeStr = "Double"; break; case Regular: typeStr = "Regular"; break; default: typeStr = "Unknown"; } printf("Type : %s\n", typeStr); printf("Dice : %d\n", dice); printf("Points : %d\n", points); }
修改后的 main.c
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include "dicegame.h" int main() { srand(time(0)); int p1 = 0; int p2 = 0; enum ROUNDTYPE roundtype; int pTurn = getRandomNumber(1, 2); int totalRnd; // 修正输入提示和scanf格式 printf("Enter the number of rounds: "); scanf("%d", &totalRnd); // 打印初始分数 printPlayerPoints(p1, p2); // 修正循环条件,轮次从1开始计数 for (int i = 0; i < totalRnd; i++) { int round = i + 1; int dice; int points; // 获取当前轮次的所有信息并赋值给变量 roundtype = getRoundType(); dice = getRandomNumber(1, 6); points = getRoundPoints(roundtype); // 打印轮次标题 printf("\nROUND %d\n", round); printf("--------\n"); // 当前玩家逻辑 if (pTurn % 2 == 1) { printf("Player : 1"); // 玩家1掷奇数骰子:加分 if (dice % 2 == 1) { p1 += points; } // 玩家1掷偶数骰子:扣分,切换玩家 else { p1 -= points; pTurn++; } } else { printf("Player : 2"); // 玩家2掷偶数骰子:加分 if (dice % 2 == 0) { p2 += points; } // 玩家2掷奇数骰子:扣分,切换玩家 else { p2 -= points; pTurn++; } } // 打印轮次信息和当前分数 printRoundInfo(roundtype, dice, points); printPlayerPoints(p1, p2); } printf("\nGAME OVER!!\n"); printf("Player 1 had: %d points\n", p1); printf("Player 2 had: %d points\n", p2); // 判断胜负 if (p1 > p2) { printf("Player 1 wins!!!!\n"); } else if(p2 > p1) { printf("Player 2 wins!!!!\n"); } else { printf("It's a tie!!!!\n"); } return 0; }
内容的提问来源于stack exchange,提问作者Juggs
相关产品推荐
相关产品推荐

