/* Shuffle Sentences * * A game where sentences are presented with words out of order and * the player must place them in the correct order. Sentences * are taken from the file sentences.txt in a random order * and shuffled randomly * * yetimach */ #include #include #include #include #include #include #define MAX_SENTENCE_L 1000 // max number of sentences supported #define MAX_W 1000 // max number words and punctuation #define MAX_LEN_WORD 20 // max number of chars allowed in a single word #define DELIMITER '#' // symbol with in which to place sentences in // the file sentences.txt char sentence_str[MAX_SENTENCE_L]; char sentence_ordered[MAX_W][MAX_LEN_WORD]; int word_order[MAX_W]; char user_answer[MAX_SENTENCE_L]; char user_answer_words[MAX_W][MAX_LEN_WORD]; int word_p_ua[MAX_SENTENCE_L]; // the position of the words in the user answer // necessary to make the words in the wrong position // into ALL CAPS FILE * sentences_file; // file from which all the sentences will be read int gets_by_delimiter(FILE * in, char str[], char dl){ char ch; int n = 0; str[0] = '\0'; //read over white space ch = getc(in); while(ch != dl){ if(ch == EOF) return 1; ch = getc(in); } while(((ch = getc(in)) != dl) && (ch != EOF)) str[n++] = ch; str[n] = '\0'; return 0; } int gen_rand(int max, int seed){ //generate a random number from 0 to max - 1 //for various numbers, seed should increment int r; srand((unsigned)time(NULL)+seed); r=rand() % max; return r; } void shuffle_int_array(int array[], int array_size){ //shuffle an array of ints int seed = 1; int random_array[array_size], counter = 0, random_num = 1, add_num; int temp_array[array_size]; for(int i = 0; i < array_size; i++) random_array[i] = -1; while(counter < array_size){ random_num = gen_rand(array_size, seed); add_num = 1; for(int i = 0; i <= counter; i++) if(random_array[i] == random_num) add_num = 0; if(add_num == 1){ random_array[counter] = random_num; ++counter; } ++seed; } for(int i = 0; i < array_size; i++) temp_array[i] = array[i]; for(int i = 0; i < array_size; i++) array[i] = temp_array[random_array[i]]; } int count_sentences(FILE *sf, char D){ char c; int s_count = 0; while ((c = getc(sf)) != EOF){ if (c == D) ++s_count; } return s_count / 2; } void read_sentence(FILE *sf, int s_num, char D, char *sent){ int mark = 0; char c; while (mark < s_num * 2){ c = getc(sf); if (c == D) ++mark; } gets_by_delimiter(sf, sent, D); } // put the string of the sentence into two arrays of words // s = sentence; sw = sentence words (array of words) int separate_into_words(char *s, char sw[][MAX_LEN_WORD]){ int p = 0, i = 0, j = 0; char ch; while((ch = s[p++]) != '\0'){ if (ch == ' '){ sw[i][j] = '\0'; j = 0; ++i; }else sw[i][j++] = ch; } return i + 1; } // create an array to use as the index for the word order // nw = number of words void gen_order_array(int nw, int *wo){ for (int i = 0; i < nw; i++) wo[i] = i; } int read_user_answer(char *ua, int *p_ua, int max){ int count = 0; char ch; while ((ch = getchar()) != '\n'){ ua[count++] = ch; } ua[count] = '\0'; if (count != 0){ for (int i = 0; i < count; i++) p_ua[i] = i; } return (count == 0 || count > max) ? -1 : 0; } int compare_sentences(int nws, int nwus, int *p_ua, char s[][MAX_LEN_WORD], char us[][MAX_LEN_WORD]){ int correct = 1; if (nws != nwus){ correct = -1; for (int i = 0; i < nwus; i++){ if (strcmp(s[i], us[i]) != 0){ p_ua[i] = -1; } } }else{ for (int i = 0; i < nws; i++){ if (strcmp(s[i], us[i]) != 0){ correct = -1; p_ua[i] = -1; } } } return correct; } void make_word_upper(char *w){ char ch; int i = 0; while ((ch = w[i]) != '\0'){ w[i++] = toupper(ch); } } void reprint_ua(char ua[][MAX_LEN_WORD], int *p_ua, int wc){ printf("\n "); for (int i = 0; i < wc; i++){ if (p_ua[i] != -1) printf("%s ", ua[i]); else{ make_word_upper(ua[i]); printf("%s ", ua[i]); } } } int ask_to_play_again(){ printf("\n Would you like to play again? [Y/n] "); char ch = getchar(); if (ch == '\n') return 1; while (ch != '\n'){ if (ch == 'y' || ch == 'Y'){ while ((getchar()) != '\n') ; return 1; } ch = getchar(); } return 0; } void clear_user_answer(){ int i, j; for (i = 0; i < MAX_SENTENCE_L; i++) user_answer[i] = '\0'; for (i = 0; i < MAX_W; i++) for (j = 0; j < MAX_LEN_WORD; j++) user_answer_words[i][j] = '\0'; } void clear_all_variables(){ int i, j; for (i = 0; i < MAX_SENTENCE_L; i++) sentence_str[i] = '\0'; for (i = 0; i < MAX_W; i++) for (j = 0; j < MAX_LEN_WORD; j++) sentence_ordered[i][j] = '\0'; for (i = 0; i < MAX_W; i++) word_order[i] = '\0'; for (i = 0; i < MAX_SENTENCE_L; i++) user_answer[i] = '\0'; for (i = 0; i < MAX_W; i++) for (j = 0; j < MAX_LEN_WORD; j++) user_answer_words[i][j] = '\0'; for (i = 0; i < MAX_SENTENCE_L; i++) word_p_ua[MAX_SENTENCE_L] = '\0'; } int main() { if ( access("sentences.txt", F_OK) == -1){ printf("\n\n\t\tSentence Shuffle\n\n Before you " "can play, in this directory, generate\n" " a file called sentences.txt and place within it\n" " a list of the sentences you want. Place each sentence\n" " within the '#' symbol, like this:\n\n #This is an " "example sentence.#\n\n The sentences in this file " "will be shuffled\n so the words are out of order. The game " "consists\n in placing them in the correct order.\n\n"); return -1; } int repeat(int, int); sentences_file = fopen("sentences.txt", "r"); int num_of_s = count_sentences(sentences_file, DELIMITER); fclose(sentences_file); sentences_file = fopen("sentences.txt", "r"); int sent_num = gen_rand(num_of_s, 1) + 1; if (sent_num == num_of_s) --sent_num; read_sentence(sentences_file, sent_num, DELIMITER, sentence_str); int num_of_words = separate_into_words(sentence_str, sentence_ordered); gen_order_array(num_of_words, word_order); shuffle_int_array(word_order, num_of_words); printf("\n --------Sentence Shuffle--------\n\n Here is a sentence with the" " words\n in the wrong order:\n\n\n "); for (int i = 0; i < num_of_words; i++) printf("%s ", sentence_ordered[word_order[i]]); printf("\n\n\n Try to put them in the correct order:\n\n\n Answer: "); int correct_answer = 0; int value; while (correct_answer != 1){ while ((value = read_user_answer(user_answer, word_p_ua, MAX_SENTENCE_L)) == -1) printf(" Give it a try: "); int num_of_words_ua = separate_into_words(user_answer, user_answer_words); int result = compare_sentences(num_of_words, num_of_words_ua, word_p_ua, sentence_ordered, user_answer_words); if (result == 1){ printf("\n Correct! You got it!\n\n"); correct_answer = 1; }else{ reprint_ua(user_answer_words, word_p_ua, num_of_words_ua); clear_user_answer(); printf("\n\n Sorry, that's not right : (\n\n Try again." " (Words in the wrong position are shown in ALL CAPS).\n\n Answer: "); } } printf("\n"); fclose(sentences_file); while (ask_to_play_again() == 1) sent_num = repeat(sent_num, num_of_s); printf("\n Thanks for playing!\n\n Have a great day! : )\n\n"); return 0; } // to repeat the whole thing. ls is the number of the last sentence--to avoid // repeating it over and over. ns is the number of sentences from first count. int repeat(int ls, int ns){ clear_all_variables(); int nsn = 0; // new sentence number (the sentence from the file sentences.txt // that will be used this round. if (ls == ns - 1) ; else nsn = ls + 1; sentences_file = fopen("sentences.txt", "r"); int sent_num = nsn; read_sentence(sentences_file, sent_num, DELIMITER, sentence_str); int num_of_words = separate_into_words(sentence_str, sentence_ordered); gen_order_array(num_of_words, word_order); shuffle_int_array(word_order, num_of_words); printf("\n --------Sentence Shuffle--------\n\n Here is a sentence with the" " words\n in the wrong order:\n\n\n "); for (int i = 0; i < num_of_words; i++) printf("%s ", sentence_ordered[word_order[i]]); printf("\n\n\n Try to put them in the correct order:\n\n\n Answer: "); int correct_answer = 0; int value; while (correct_answer != 1){ while ((value = read_user_answer(user_answer, word_p_ua, MAX_SENTENCE_L)) == -1) printf(" Give it a try: "); int num_of_words_ua = separate_into_words(user_answer, user_answer_words); int result = compare_sentences(num_of_words, num_of_words_ua, word_p_ua, sentence_ordered, user_answer_words); if (result == 1){ printf("\n Correct!! You got it!\n\n"); correct_answer = 1; }else{ reprint_ua(user_answer_words, word_p_ua, num_of_words_ua); clear_user_answer(); printf("\n\n Sorry, that's not right : (\n\n Try again." " (Words in the wrong position are shown in ALL CAPS).\n\n Answer: "); } } printf("\n"); fclose(sentences_file); return nsn; }