/* word_mill: user provides a word (shorter words are better) and the program compares against * valid_word.txt (the list is not perfect, missing some words) to find all the possible words * that can be made with the letters in the word. If the user cannot find all the words, she * can give up and see the ones she missed. * by yetimach */ #include #include #include #include #define MAX_WORD 20 // max length of word played #define MWL 200 // max number of words in word_list.txt #define MP 10000 char word[MAX_WORD]; // the word the user provides in form to be processed (prime word) char w_print[MAX_WORD]; // same, in form to print char poss_w[MAX_WORD]; // possible word (user guess) char wmi[400]; //word mill image (figlet) struct good_word{ char g_word[MWL]; }; struct good_word good_words[MP]; struct user_word{ char u_word[MWL]; }; struct user_word user_words[MP]; FILE *dw; // dictionary words FILE *wif; //figlet image file int main() { void play_game(); void clear_variables(); int repeat(); int again = 1; while (again){ clear_variables(); play_game(); again = repeat(); } return 0; } void clear(){ system("clear"); } int is_valid(char *word, FILE *dw) { int status = 0, done = 0; char d_word[100]; dw = fopen("valid_words.txt", "r"); while (status == 0 && (fscanf(dw, "%s", d_word)) && done == 0){ if (strcmp(d_word, "THE_END") == 0) done = 1; if (strcmp(word, d_word) == 0) status = 1; } return status; } int word_length(char *word){ int length = 0; char c; while ((c = word[length++] != '\0')) ; --length; return length; } void read_image(FILE *wif, char wmi[300]){ int i = 0; wif = fopen("word_mill.txt", "r"); while ((wmi[i++] = getc(wif)) != EOF) ; wmi[i-1] = '\0'; fclose(wif); } void remove_letter(char *word, int lp){ int wl = word_length(word); char temp[wl]; int j = 0; for (int i = 0; i <= wl; i++){ if (i != lp) temp[j++] = word[i]; } for (int i = 0; i < wl; i++) word[i] = temp[i]; } int check_for_letter(char *word, char letter){ int wl = word_length(word); for (int i = 0; i < wl; i++){ if (word[i] == letter) return i; } return -1; } int check_word_for_letters(char *word, char *poss_w){ int wl = word_length(poss_w); int lp; for (int i = 0; i < wl; i++){ lp = check_for_letter(word, poss_w[i]); if (lp != -1) remove_letter(word, lp); else return -1; } return 1; } // read poss_w, if problem return -1, if good return 1, if quit, return 0 int read_poss_w(char *poss_w){ int p = 0; char c; while ((c = getchar()) != '\n'){ c = tolower(c); poss_w[p++] = c; } poss_w[p] = '\0'; if (poss_w[0] == '|') return -1; return 1; } int find_all_possible(FILE *dw, char *word){ int done = 0, wc = 0; char dict_word[MWL], temp_dw[MWL], temp_w[MWL]; dw = fopen("valid_words.txt", "r"); do{ strcpy(temp_w, word); fscanf(dw, "%s", dict_word); strcpy(temp_dw, dict_word); if (check_word_for_letters(temp_w, temp_dw) == 1){ int size = word_length(dict_word); if (size > 2 && strcmp(word, dict_word) != 0) strcpy(good_words[wc++].g_word, dict_word); } if ((strcmp(dict_word, "THE_END") == 0)) done = 1; }while(done == 0); return wc; } int read_prime_word(char *word, char *w_print){ printf("\n\tEnter a word (at least three letters): "); char c; int p = 0, pp = 0; while ((c = getchar()) != '\n'){ if (!isalpha(c) && c != ' ') return -1; else{ w_print[p++] = c; } } w_print[p] = '\0'; for (int i = 0; i <= p; i++){ c = w_print[i]; if (c != ' '){ c = tolower(c); word[pp++] = c; } } if (word_length(word) < 3) return -1; return 1; } void play_game(){ read_image(wif, wmi); clear(); int pwc = 0; // possible word count (that can be made from prime word) printf("%s", wmi); while (read_prime_word(word, w_print) == -1) ; pwc = find_all_possible(dw, word); int quit = 0, already_guessed = 0, nwf = 0, result; int valid_word = 0, first_time = 1; clear(); if (pwc == 0){ printf("\n\n\tSorry, no other words with three letters\n\tor more can be made from \"%s\".\n\n", w_print); return; } while (!quit){ clear(); printf("%s", wmi); printf("\n\n\t%d words with three letters or more can be made from \"%s\".\n\n\t", pwc, w_print); printf("How many of them can you find? ('|' to quit)\n\t"); printf("\n\t"); if (result != -1){ for (int i = 0; i < pwc; i++){ if (strcmp(poss_w, good_words[i].g_word) == 0) valid_word = 1; } if (valid_word){ for (int i = 0; i < nwf; i++){ if (strcmp(poss_w, user_words[i].u_word) == 0){ already_guessed = 1; } } } if (valid_word == 1 && already_guessed == 0){ strcpy(user_words[nwf++].u_word, poss_w); } if (nwf > 0) printf("\n\t%d left to find.\n\n\t", pwc - nwf); for (int j = 0; j <= nwf; j++){ printf("%s ", user_words[j].u_word); if (j % 10 == 0 && j != 0) printf("\n\t"); } printf("\n\t"); if (nwf == pwc && nwf != 0){ printf("\n\n\tWow! You found all the words!\n\tGreat job!\n\n"); return; } if (valid_word == 0 && first_time == 0){ printf("\n\tNot a valid word.\n\t"); } if (already_guessed){ printf("\n\tYou already found that word.\n\t"); already_guessed = 0; } }else quit = 1; result = read_poss_w(poss_w); if (result == -1) quit = 1; valid_word = 0; first_time = 0; } printf("\n\tNumber of words found: %d\n\n\tYou missed:\n\n\t", nwf); int print, nwp = 0; for (int i = 0; i < pwc; i++){ print = 1; for (int j = 0; j < nwf; j++) if (strcmp(user_words[j].u_word, good_words[i].g_word) == 0) print = 0; if (print){ printf("%s ", good_words[i].g_word); ++nwp; } if (nwp % 10 == 0 && nwp != 0) printf("\n\t"); first_time = 0; } printf("\n\n"); } int repeat(){ char again; printf("\n\tPlay again? [Y/n] "); again = getchar(); if (again == '\n') return 1; else if (again == 'y' || again == 'Y'){ while ((again = getchar())!= '\n') ; return 1; } else return 0; } void clear_variables(){ for (int i = 0; i < MP; i++){ strcpy(good_words[i].g_word, ""); strcpy(user_words[i].u_word, ""); } }