/* * Exercise for Lesson 5 : Word Building Game * * This is a word guessing game. Words are read from a file and the player has to guess * the correct word from the shuffled letters. */ #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> const int true = 1; const int false = 0; // This structure stores one entry in the high score table struct highscore { char name[20]; int numofwords; }; // This function reads the word list from the wordlist.txt file int readwordlist(char wordlist[][20]) { // Task 1. Read the word list from the text file // // Open the word list file for reading // // Read the words using a loop // // Close the word list file // FILE *fp; char word[849]; int i; fp = fopen("wordlist.txt", "r"); if (fp == NULL) { printf("Failed to open the file!"); return false; fclose(fp); \ } for (i = 0; i < 849; i++) { fscanf(fp, "%s", word); strcpy(wordlist[i], word); } fclose(fp); return true; } // This function takes the target word and then print the letters in // a random order. void printshuffleword(char word[]) { char wordcopy[20]; int letternum; int index; // Copy the word into another variable strcpy(wordcopy, word); // Pick and print a letter from the copy randomly letternum = 0; while (letternum < strlen(word)) { // Pick a letter randomly index = rand() % strlen(word); // Print the letter if it has not been printed if (wordcopy[index] != '\0') { printf("%c", wordcopy[index]); wordcopy[index] = '\0'; letternum++; } } } // This function reads the high score table from a text file into an array. void readhighscoretable(struct highscore table[]) { // Task 2. Read the high score table from the text file // // Open the high score table file for reading // // Read the high score entries using a while loop // // Close the word list file // FILE *fp; int i = 0; char name[20]; int numofwords; fp = fopen("highscoretable.txt", "r"); while (fscanf(fp, "%s %d", name, &numofwords) != EOF) { strcpy(table[i].name, name); table[i].numofwords = numofwords; i = i + 1; } while (i < 10) { strcpy(table[i].name, ""); table[i].numofwords = 0; i = i + 1; } fclose(fp); } // This function inserts a new highscore entry into the table array. void inserthighscoretable(struct highscore table[], int numofwords) { int i, j; char name[20]; // Ask for the player's name printf("Please enter your name: "); scanf("%s", name); // Find the position of the player in the table i = 0; while (table[i].numofwords > numofwords && i < 10) { i++; } // Update the table if (i < 10) { // Push down all the rest of the entries for (j = 9; j > i; j--) { strcpy(table[j].name, table[j - 1].name); table[j].numofwords = table[j - 1].numofwords; } // Insert the new entry at the ith position strcpy(table[i].name, name); table[i].numofwords = numofwords; } } // This function saves the highscore table to the text file. void savehighscoretable(struct highscore table[]) { // Task 3. Save the high score table into the text file // // Open the high score table file for writing // // Write the high score entries using a loop // // Close the word list file // FILE *fp; int i = 0; char name[20]; int numofwords; fp = fopen("highscoretable.txt", "w"); while (i < 10) { strcpy(name, table[i].name); numofwords = table[i].numofwords; fprintf(fp, "%s %d\n", name, numofwords); i = i + 1; } fclose(fp); } // This function prints the highscore table. void printhighscoretable(struct highscore table[]) { int i; // Print the title if the highscore table contains at least one entry if (table[0].numofwords > 0) { printf("\nHighscore Table\n"); printf("--------------------------\n"); } // Print all valid entries of the highscore table i = 0; while (table[i].numofwords > 0 && i < 10) { printf("%-3d%-20s%3d\n", i + 1, table[i].name, table[i].numofwords); i++; } printf("\n"); } int main() { // This array stores the 849 words available in the game char wordlist[849][20]; // This is the current number of words the player has guessed correctly int numofwords = 0; // This stores the target word index int targetwordindex; // This stores the guessed word char guess[20]; // This stores the number of guesses the player has used for one word int numofguesses; // This is the high score table struct highscore table[10]; // Read the word list file if (!readwordlist(wordlist)) { return 0; } // Randomize the random numbers srand(time(NULL)); printf("Welcome to the word building game!\n"); printf("----------------------------------\n\n"); printf("In this game, you have to build a secret word using the letters given to you!\n\n"); printf("You have got three chances for each word. If you use up your chances,\nthe game will be over.\n\n"); do { // Pick a word randomly from the word list targetwordindex = rand() % 849; printf("Word %d - ", numofwords + 1); // Print the shuffled letters in the target word printshuffleword(wordlist[targetwordindex]); printf("\n\n"); // Get the guess from the player numofguesses = 0; do { // Give hints for second and last chance if (numofguesses == 1) { printf("Hint: the word starts with '%c'.\n", wordlist[targetwordindex][0]); } else if (numofguesses == 2) { printf("Hint: the word ends with '%c'.\n", wordlist[targetwordindex][strlen(wordlist[targetwordindex]) - 1]); } printf("Please enter your guess: "); scanf("%s", guess); // Increase the number of guesses numofguesses++; // The loop stops if either the player guesses correctly or the // player guesses three times already } while (strcmp(wordlist[targetwordindex], guess) != 0 && numofguesses < 3); // Increase the number of words if the player guesses correctly if (strcmp(wordlist[targetwordindex], guess) == 0) { numofwords++; } printf("\n"); } while (strcmp(wordlist[targetwordindex], guess) == 0); // Print the game over message printf("Game over! You have guessed %d word(s) correctly.\n\n", numofwords); // Read the high score table readhighscoretable(table); // If your score is higher than the minimum score in the table if (numofwords > 0 && numofwords >= table[9].numofwords) { // Insert your entry in the table inserthighscoretable(table, numofwords); // Save the high score table savehighscoretable(table); } // Print the high score table printhighscoretable(table); }
Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!
OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.
#include <stdio.h>
int main()
{
char name[50];
printf("Enter name:");
scanf("%s", name);
printf("Hello %s \n" , name );
return 0;
}
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.
When ever you want to perform a set of operations based on a condition if-else
is used.
if(conditional-expression) {
// code
} else {
// code
}
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to if-else-if ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type array-name[size];
data-type array-name[size][size];
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Two types of functions are present in C
Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,
User defined functions are the ones which are written by the programmer based on the requirement.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
//code
}