creates a new game - generates a random word and stores in global variable current_word
134 {
135
136 char word[30];
137
138 FILE *fptr;
139 fptr = fopen("games/words.txt", "r");
140
141 if (fptr == NULL){
142 fprintf(stderr, "File not found.\n");
143 exit(EXIT_FAILURE);
144 }
145
146
147 int line_number = 0;
148 while (fgets(word, 30, fptr) != NULL) {
149 line_number++;
150 }
151
152 rewind(fptr);
153
154
155 int random_num;
156 srand(time(NULL));
157 random_num = rand() % line_number;
158
159
160 int s = 0;
161 while (s <= random_num){
162 fgets(word, 30, fptr);
163 s++;
164 }
165
166
167 if (strchr(word, '\n') != NULL){
168 word[strlen(word) - 1] = '\0';
169 }
170
171 fclose(fptr);
172
173
175 strcpy(current_game.current_word, word);
176 current_game.size = strlen(word);
177 for (int i = 0; i < (strlen(word)); i++) {
178 current_game.hidden[i] = '_';
179 }
180 current_game.incorrect = 0;
181 current_game.guesses_size = 0;
182
183 return current_game;
184}