Implement main.c

This commit is contained in:
Tobias Linz committed 2026-10-29 09:59:50 +01:00
1 parent d2ccf7d956
commit fd77c3bf45
1 file changed
+21 -19
+21 -19
View File
@@ -6,26 +6,28 @@
#define MAX_NUMBER_OF_WORDS 100 #define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20 #define SALAD_SIZE 20
#define WINDOW_WIDTH 480
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided // Check if the correct number of arguments is provided
if(argc != 2) if(argc != 2)
{ {
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]); fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
exitCode = EXIT_FAILURE; return EXIT_FAILURE;
} }
else
{
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
unsigned int wordCount = 0; unsigned int wordCount = 0;
FILE *file = fopen(argv[1], "r"); FILE *file = fopen(argv[1], "r");
if(file == NULL)
if(file != NULL)
{ {
// Print error message if file couldn't be opened
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
return EXIT_FAILURE;
}
unsigned int placedWords = 0; unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
@@ -33,22 +35,22 @@ int main(int argc, char *argv[])
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file); fclose(file);
if(wordCount == 0)
{
fprintf(stderr, "No words found...\n");
return EXIT_FAILURE;
}
// Create the word salad by placing words into grid // Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// TODO: if(placedWords != wordCount)
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
}
else
{ {
// Print error message if file couldn't be opened fprintf(stderr, "Could not place %d out of %d words ...\n", wordCount - placedWords, wordCount);
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]); return EXIT_FAILURE;
exitCode = EXIT_FAILURE;
}
} }
return exitCode; startGame(wordSalad, SALAD_SIZE, words, placedWords, WINDOW_WIDTH);
return EXIT_SUCCESS;
} }