Files
info2Praktikum-Wortsalat/Startcode/main.c
T
2026-10-29 16:00:21 +01:00

58 lines
1.6 KiB
C

#include "game.h"
#include "graphicalGame.h"
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20
#define WINDOW_WIDTH 480
int main(int argc, char *argv[])
{
// Check if the correct number of arguments is provided
if (argc != 2)
{
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
return EXIT_FAILURE;
}
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
unsigned int wordCount = 0;
FILE *file = fopen(argv[1], "r");
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;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
// Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file);
if (wordCount == 0)
{
fprintf(stderr, "No words found...\n");
return EXIT_FAILURE;
}
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
if (placedWords != wordCount)
{
fprintf(stderr, "Could not place %u out of %u words ...\n", wordCount - placedWords, wordCount);
return EXIT_FAILURE;
}
startGame(wordSalad, SALAD_SIZE, words, placedWords, WINDOW_WIDTH);
return EXIT_SUCCESS;
}