我试图创建一个简单的填字游戏。我读了一个txt文件,其中包含有关单词的所有信息(方向,起始位置,线索等)。我尝试调试,我的程序在调试模式下完美运行,即使没有任何断点。我使用Code :: Blocks 17.12。和GNU GCC编译器。我是C上的新手,所以我无法弄清楚为什么我的代码在发布模式下无效。
当我输入txt文件的名称时崩溃。
很抱歉很长的帖子,但我不知道错误在哪里
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_WORD_LENGTH 10
#define MAX_CLUES_LENGTH 50
typedef struct
{
char *word; //word and corresponding hint
char *clues;
int x; //Starting x and y positions
int y;
char direction; //H for horizontal, V for vertical
int f; //solved or not
} Word_t;
Word_t* loadTextFile(FILE* myfile, int nrWords);
void displayBoard(int rows, int cols, char** myBoard);
int isBoardFilled(int rows, int cols, char** myBoard);
char** createArray(int rows, int cols);
int findIndex(int arr[], int size, int val);
void playGame(char** myBoard, int words, Word_t *rWords, int x, int y, int
countToFinish);
char** updateBoard(char** myBoard, Word_t *nWords, int solve);
int main()
{
FILE *input_file;
char file_name[20];
int rows, cols, nrWords, count=0;
printf("\nEnter name of text file: ");
gets(file_name);
while(access( file_name, F_OK) == -1)
{
printf("Cannot open the file or file does not exist.\nPlease try again: ");
gets( file_name);
}
input_file = fopen(file_name, "r");
fscanf(input_file,"%d%d%d", &rows, &cols, &nrWords);
Word_t* words;
words=(Word_t*)malloc(sizeof(Word_t)*nrWords);
for(int i=0; i<nrWords; i++)
{
words[i].word = malloc(sizeof(MAX_WORD_LENGTH));
words[i].clues=malloc(sizeof(MAX_CLUES_LENGTH));
}
printf("Game is %d rows x %d cols with %d words\n", rows, cols, nrWords);
words=loadTextFile(input_file,nrWords);
char **myBoard=(char**)malloc(rows * sizeof(char *));
for(int i=0; i<rows; i++)
{
myBoard[i]=(char*)malloc(cols * sizeof(char));
}
myBoard=createArray(rows,cols);
/*for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
printf("%c ", myBoard[i][j]);
}
printf("\n");
}*/
for(int i=0; i<nrWords; i++)
{
myBoard=updateBoard(myBoard, words, -1);
}
playGame(myBoard, nrWords, words, rows, cols, nrWords);
fclose(input_file);
free(words);
return 0;
}
Word_t* loadTextFile(FILE* myfile, int nrWords)
{
Word_t* temp;
temp=malloc(sizeof(Word_t)*nrWords);
for(int count=0; count<nrWords; count++)
{
temp[count].word=malloc(MAX_WORD_LENGTH);
temp[count].clues=malloc(MAX_CLUES_LENGTH);
fscanf(myfile," %c %d %d %s %[^\n]%*c", &temp[count].direction, &temp[count].x, &temp[count].y, temp[count].word, temp[count].clues);
}
printf("Words and clues are read!\n");
return temp;
}
char** createArray(int rows, int cols)
{
int count=0;
char** tempBoard = (char**)malloc(rows * sizeof(char*));
for(int i=0; i<rows; i++)
{
tempBoard[i]=(char*)malloc(cols*sizeof(char*));
}
count=0;
for(int i=0; i<rows; i++)
for(int j=0; j<cols; j++)
tempBoard[i][j]='#';
return tempBoard;
}
void displayBoard(int rows, int cols, char** myBoard)
{
int i;
for(i=0; i<cols; i++)
{
printf(" %d ", i+1);
}
printf("\n ");
for(i=0; i<cols; i++)
{
printf(" --");
}
printf("\n");
for(i=0; i<rows; i++)
{
printf("%d |", i+1);
for(int k=0; k<cols; k++)
{
printf("%-3c", myBoard[i][k]);
}
printf("\n");
}
printf(" ***************\n");
}
int isBoardFilled(int rows, int cols, char** myBoard)
{
for(int i=0; i<rows; i++)
{
for(int k=0; k<cols; k++)
{
if((myBoard[i][k])=='_')
return 0;
}
}
return 1;
}
int findIndex(int arr[], int size, int val)
{
for(int i=0; i<size; i++)
{
if(arr[i]==val)
return 1;
}
return 0;
}
char** updateBoard(char** myBoard, Word_t *nWords, int solve)
{
if(solve==-1)
{
for(int k=0; k<sizeof(Word_t); k++)
{
if(nWords[k].direction=='V')
{
int len=strlen(nWords[k].word);
for(int i=0; i<len; i++)
{
myBoard[((nWords[k].x)-1)+i][(nWords[k].y)-1]='_';
}
}
else if(nWords[k].direction=='H')
{
int len=strlen(nWords[k].word);
for(int i=0; i<len; i++)
{
myBoard[(nWords[k].x)-1][((nWords[k].y)-1)+i]='_';
}
}
}
}
else if(solve!=-1)
{
if(nWords[solve].direction=='V')
{
int len=strlen(nWords[solve].word);
for(int i=0; i<len; i++)
{
myBoard[((nWords[solve].x)-1)+i][(nWords[solve].y)-1]=nWords[solve].word[i];
}
}
else if(nWords[solve].direction=='H')
{
int len=strlen(nWords[solve].word);
for(int i=0; i<len; i++)
{
myBoard[(nWords[solve].x)-1][((nWords[solve].y)-1)+i]=nWords[solve].word[i];
}
}
}
return myBoard;
}
void playGame(char** myBoard, int words, Word_t *rWords, int x, int y, int countToFinish)
{
int gameOver=0;
char answer[MAX_WORD_LENGTH];
int array[countToFinish];
int index;
int filled;
printf("\nCurrent puzzle:\n\n");
printf(" ");
for(int i=0; i<countToFinish; i++)
{
myBoard=updateBoard(myBoard, rWords, -1);
}
displayBoard(x, y, myBoard);
while(gameOver<countToFinish || filled != 1)
{
printf("\nAsk for hint:\n");
printf("# Direction row col\n");
printf("------------------------------\n");
for(int k=0; k<countToFinish; k++)
{
if(rWords[k].direction=='H')
{
printf("%d: Horizontal %d %d\n", k+1, rWords[k].x, rWords[k].y);
}
else
{
printf("%d: Vertical %d %d\n", k+1, rWords[k].x, rWords[k].y);
}
}
printf("\nEnter -1 to exit\n");
printf("Which word to solve next?: ");
scanf(" %d", &words);
if(words == -1)
{
return 0;
}
while(words>countToFinish || words<=0 )
{
printf("Invalid input\n");
printf("Please try again:");
scanf("%d", &words);
}
index=findIndex(array, countToFinish, words);
while(index == 1)
{
printf("That word has been solved before");
printf("\nEnter -1 to exit\n");
printf("Which word to solve next?: ");
scanf(" %d", &words);
index=findIndex(array, countToFinish, words);
}
printf("\nCurrent hint: %s", rWords[words-1].clues);
printf("\nEnter your solution: ");
scanf("%s", &answer);
if(strcasecmp(answer,rWords[words-1].word)==0)
{
printf("\nCorrect!\n");
gameOver++;
myBoard=updateBoard(myBoard, rWords, (words-1));
displayBoard(x, y, myBoard);
array[words]=words;
}
else
{
printf("\nWRONG ANSWER\n");
printf("Current puzzle:\n");
displayBoard(x, y, myBoard);
}
filled = isBoardFilled(x, y, myBoard);
}
}