puzzles.c - The function getPuzzle of playHangman. | Lecture 1 - slide 3 : 22 Program 7 |
puzzle getPuzzle(int n){ FILE *puzzleFile; puzzle result; int i, ch, num; char *puzzleLine, *catStr, *puzzleStr; char *puzzleLineQ1, *puzzleLineQ2, *puzzleLineQ3, *puzzleLineQ4; puzzleLine = (char*)malloc(PUZZLEMAXCOUNT); catStr = (char*)calloc(PUZZLEMAXCOUNT,1); puzzleStr = (char*)calloc(PUZZLEMAXCOUNT,1); puzzleFile = fopen(puzzleFileName, "r"); for(i = 0; i < n*4; i++) matchDoubleQuoteFile(puzzleFile); /* read through n*4 double quotes */ while (isspace(ch = getc(puzzleFile))); /* skip white space */ ungetc(ch,puzzleFile); /* put double quote back */ fgets(puzzleLine, PUZZLEMAXCOUNT, puzzleFile); /* read a line from puzzle file */ puzzleLineQ1 = matchDoubleQuoteStr(puzzleLine); /* identify quotes in string */ puzzleLineQ2 = matchDoubleQuoteStr(puzzleLineQ1+1); puzzleLineQ3 = matchDoubleQuoteStr(puzzleLineQ2+1); puzzleLineQ4 = matchDoubleQuoteStr(puzzleLineQ3+1); strncpy(catStr, puzzleLineQ1+1, puzzleLineQ2 - puzzleLineQ1 - 1); strncpy(puzzleStr, puzzleLineQ3+1, puzzleLineQ4 - puzzleLineQ3 - 1); num = 0; for(i = 0; i < strlen(puzzleStr); i++) if (isalpha(puzzleStr[i])) num++; result.category = catStr, result.phrase = puzzleStr; result.numberOfCharsToGuess = num; fclose(puzzleFile); return result; }