【发布时间】:2021-12-02 11:53:15
【问题描述】:
我正在处理这个任务,但我无法让它运行。我尝试了很多不同的方法,例如切换函数isValid的位置,但是当我在函数getGuess之前声明它时,代码将在函数populateColorArray被调用后立即停止并关闭程序。
#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};
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);
for(i = 0; i < TEN; i++)
{
for(j = 0; j < FOUR; j++)
{
printf("%c", guesses[i][j]);
}
}
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");
}
// clears the screen for the next step of the program
void clearScreen()
{
printf(" #########################\n");
printf("\n");
printf(" <Hit enter to continue>\n");
printf("\n");
printf(" #########################\n");
char key;
scanf("%c", &key);
system("cls");
}
// board of the game
void displayBoard()
{
printf("+---------------------------------------+\n");
printf("| SECRET CODE |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| PLAYER GUESS | CLUES |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
printf("| ? ? ? ? | ? ? ? ? |\n");
printf("+---------------------------------------+\n");
}
//function has as an argument codeArray[] that later on it's used when calling the function convertColor
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]);
}
}
// function used to get the random colors
int getColor ()
{
int colorRandom = rand() %COLORS;
return colorRandom;
}
// function used to convert the numbers into an actual color string
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;
}
}
// function that receives the array color[] and prints the char equivalent to each color
void populateColorArray(char color[])
{
int i;
printf("\nCharacter colors are:\n");
for(i = 0; i < COLORS; ++i)
{
printf("%c ", color[i]);
}
printf("\n");
}
// used to set all row and columns of the array as -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;
}
}
}
/* function that is going to use isValid function to check if the user's
guess is valid and continue the game from there*/
void getGuess(int guesses[TEN][FOUR], char colors[])
{
static int row = 0;
int col = 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]);
}
if(strlen(guess[TEN]) == 4)
{
for (i = 0; i < FOUR; ++i)
{
guess[i] = toupper(guess[i]);
if (isalpha(guess[i]) == TRUE)
{
if(isValid(colors, guess[i]) == TRUE)
{
row = guess[i];
col = guess[i];
if(col == THREE)
{
valid = TRUE;
}
}
else
{
printf("The value you entered is invalid");
valid = FALSE;
break;
}
}
else
{
printf("Invalid value entered %c, try again", guess[i]);
valid = FALSE;
break;
}
}
}
else
{
printf("Incorrect number of letters entered");
valid = FALSE;
}
}
row = row + 1;
}
// actually validates the user guess
// giving an error "conflicting types for 'isValid'
int isValid(char colors[COLORS], char color)
{
int c;
int valid = FALSE;
for(c = 0; c < COLORS; ++c)
{
if(colors[c] == color)
{
return TRUE;
}
}
return FALSE;
}
如果有人知道我还能做些什么来让它运行,我将不胜感激。
【问题讨论】:
-
去掉所有的定义并用X宏替换它们。可以声明静态大小的对象,eg
char colors[] = .... -
贴出的代码无法编译!当通过编译器运行时,启用警告(即
gcc -c -Wall -Wextra -Wconversion -pedantic -std=gnu11)会输出许多警告(和一些错误消息)。