C程序提示isValid函数类型冲突,调用populateColorArray后闪退如何解决
C语言Mastermind程序修复方案
核心错误与修复点
- 函数隐式声明错误:C语言要求函数调用前必须有声明/定义,你遇到的
isValid类型冲突、其他函数编译告警都是因为没有加前置声明。解决方法是在main函数前补充所有自定义函数的声明:
void welcomeScreen(); void clearScreen(); void displayBoard(); void setCode (int codeArray[]); int getColor (); void convertColor (int color); void populateColorArray(char color[]); void initializeArray(int guesses[TEN][FOUR]); void getGuess(int guesses[TEN][FOUR], char colors[]); int isValid(char colors[COLORS], char color);
- while循环条件写错:
getGuess里的while(valid = FALSE)是赋值操作,会永远返回假,循环完全不会执行,程序直接走到结束。修改为while(valid == FALSE)用双等号做相等判断。 - strlen使用错误:
strlen(guess[TEN])存在两个问题:1. 数组下标越界,guess长度为10,最大合法下标是9;2. strlen要求传入字符串首地址,你传入的是单个char值会触发崩溃。且你读取4个字符后没有加字符串结束符\0,strlen无法正常计数。因为你是固定要求输入4位,直接去掉冗余的strlen判断即可。 - 输入缓冲区残留换行问题:之前的
scanf("%c")会读取缓冲区里残留的换行符,导致你还没输入就拿到了空值。修改读取用户输入的代码,在%c前加空格跳过空白字符:scanf(" %c", &guess[i]);,每次输入前清空残留缓冲区。 - 逻辑赋值错误:
getGuess里row = guess[i]; col = guess[i];逻辑完全错误,你需要把合法的输入存入guesses数组,改为guesses[row][i] = guess[i];,不需要单独维护col变量,循环变量i就是当前列索引。 - isalpha判断逻辑错误:isalpha返回非零值表示输入是字母,不会固定返回你定义的TRUE(1),把
if (isalpha(guess[i]) == TRUE)改为if (isalpha(guess[i]))即可。
修复后完整可运行代码
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define COLORS 8 #define THREE 3 #define FOUR 4 #define TEN 10 #define TRUE 1 #define FALSE 0 enum color {BLACK, GREEN, NAVY, ORANGE, PINK, RED, VIOLET, WHITE}; // 前置函数声明 void welcomeScreen(); void clearScreen(); void displayBoard(); void setCode (int codeArray[]); int getColor (); void convertColor (int color); void populateColorArray(char color[]); void initializeArray(int guesses[TEN][FOUR]); void getGuess(int guesses[TEN][FOUR], char colors[]); int isValid(char colors[COLORS], char color); int main() { srand((time(0))); int i; int j; char colors[COLORS] = {'B', 'G', 'N', 'O', 'P', 'R', 'V', 'W'}; int secretCode[FOUR]; int guesses[TEN][FOUR]; int clues[TEN][FOUR]; welcomeScreen(); clearScreen(); setCode(secretCode); printf("\n"); clearScreen(); displayBoard(); populateColorArray(colors); initializeArray(guesses); getGuess(guesses, colors); printf("\n你输入的猜测:"); for(i = 0; i < TEN; i++) { for(j = 0; j < FOUR; j++) { if(guesses[i][j] != -1) printf("%c", guesses[i][j]); } } printf("\n"); return 0; } void welcomeScreen() { printf(" ###################################################################\n"); printf(" ###################################################################\n"); printf(" ######################## #####################\n"); printf(" ######################## Mastermind #####################\n"); printf(" ######################## #####################\n"); printf(" ###################################################################\n"); printf(" ###################################################################\n"); printf("Rules:\n"); printf("1. The CodeMaker sets a secret code\n"); printf("2. The CodeBreaker tries to match the code using logic and deduction\n"); printf("3. After each move, the CodeMaker gives clues to the CodeBreaker\n"); printf("4. The CodeBreaker has 10 attempts to guess the secret code\n"); printf("\n"); } // 清屏并等待用户按回车 void clearScreen() { printf(" #########################\n"); printf("\n"); printf(" <Hit enter to continue>\n"); printf("\n"); printf(" #########################\n"); // 清空缓冲区避免读入残留换行 while(getchar() != '\n'); getchar(); system("cls"); } // 打印游戏面板 void displayBoard() { printf("+---------------------------------------+\n"); printf("| SECRET CODE |\n"); printf("+---------------------------------------+\n"); printf("| ? ? ? ? |\n"); printf("+---------------------------------------+\n"); printf("| PLAYER GUESS | CLUES |\n"); printf("+---------------------------------------+\n"); for(int i=0;i<TEN;i++){ printf("| ? ? ? ? | ? ? ? ? |\n"); printf("+---------------------------------------+\n"); } } // 生成随机密码 void setCode (int codeArray[]) { int i; printf("Integer Secret Code\n"); for(i = 0; i < 4; ++i) { codeArray[i] = getColor(); printf("%d ", codeArray[i]); } printf("\n"); printf("Color Secret Code\n"); for(i = 0; i < 4; ++i) { convertColor(codeArray[i]); } } // 生成随机颜色值 int getColor () { int colorRandom = rand() %COLORS; return colorRandom; } // 数字转颜色名称 void convertColor (int color) { switch (color) { case 0: printf("Black "); break; case 1: printf("Green "); break; case 2: printf("Navy "); break; case 3: printf("Orange "); break; case 4: printf("Pink "); break; case 5: printf("Red "); break; case 6: printf("Violet "); break; case 7: printf("White "); break; } } // 打印所有可选颜色字符 void populateColorArray(char color[]) { int i; printf("\nCharacter colors are:\n"); for(i = 0; i < COLORS; ++i) { printf("%c ", color[i]); } printf("\n"); } // 初始化猜测数组为-1 void initializeArray(int guesses[TEN][FOUR]) { int i; int j; for (i = 0; i < TEN; ++i) { for (j = 0; j < FOUR; ++j) { guesses[i][j] = -1; } } } // 获取用户输入的猜测 void getGuess(int guesses[TEN][FOUR], char colors[]) { static int row = 0; int valid = FALSE; char guess[TEN]; int i; while(valid == FALSE) { printf("Enter your guess of four colors (no spaces):\n"); for(i = 0; i < FOUR; ++i) { scanf(" %c", &guess[i]); } printf("You entered: "); for(i = 0; i < FOUR; ++i) { printf("%c", guess[i]); } printf("\n"); valid = TRUE; for (i = 0; i < FOUR; ++i) { guess[i] = toupper(guess[i]); if (!isalpha(guess[i])) { printf("Invalid value entered %c, try again\n", guess[i]); valid = FALSE; break; } if(!isValid(colors, guess[i])) { printf("The value %c is not a valid color\n", guess[i]); valid = FALSE; break; } guesses[row][i] = guess[i]; } } row = row + 1; } // 验证输入字符是否为合法颜色 int isValid(char colors[COLORS], char color) { int c; for(c = 0; c < COLORS; ++c) { if(colors[c] == color) { return TRUE; } } return FALSE; }
内容的提问来源于stack exchange,提问作者joazeiro
相关产品推荐
相关产品推荐

