diff --git a/Start_Windows/game.c b/Start_Windows/game.c index d8cc133..bc86c26 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -1,11 +1,15 @@ #include "game.h" +#include #include #include #include - +// Input eine datei erstellen und einfügen sodass wie eine andere datei zu implementierne und dazu noch Ende und Wichtig ! noch main funktion einfügen // #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 +#define MAX_WORDS 50 +#define MAX_WORD_LENGTH 30 +typedef enum {HORIZONTAL, VERTIKAL} Direction; //TODO: Spiellogik implementieren: /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ @@ -13,11 +17,74 @@ // Creates the word salad by placing words randomly and filling empty spaces int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { +for (int r=0; r< serachFieldLen; r++) { + for (int c=0;c< searchFieldLen; c++) { + salad[r][c] = EMPTY_CHAR; + } +} + for (int i = 0; i < wordCount; i++) { + const char *word = words[i]; + int len = strlen(word); + int placed =0; + for (int try = 0; try < MAX_RAND_TRIES_PER_WORD && !placed; try++) { + Direction dir = (rand()%2==0) ? HORIZONTAL : VERTIKAL; + int r=rand()%searchFieldLen; + int c=rand()%searchFieldLen; + + if (dir == HORIZONTAL && (c + len > searchFieldLen)) { + continue; + } + if (dir == VERTIKAL && (r + len > searchFieldLen)) { + continue; + } + int can_place = 1; + for (int k =0; k < len; k++) { + char existting_char; + if (dir == HORIZONTAL) { + existting_char = salad[r][c+k]; + } else { + existting_char = salad[r+k][c]; + } + if (existting_char != EMPTY_CHAR && existting_char != word[k]) { + can_place=0; + break; + } + } + if (can_place) { + for (int k =0; k < len; k++) { + if (dir == HORIZONTAL) { + salad[r][c+k] = word[k]; + } else { + salad[r+k][c] = word[k]; + } + } + placed = 1; + placed_count++; + } + } + + } +//Restliche feledr ausfüllen + for (int r=0; r < searchFieldLen; r++) { + for (int c=0; c < searchFieldLen; c++) { + if (salad[r][c] == EMPTY_CHAR) { + salad[r][c] = 'A'+(rand()%26); + } + } + } + return placed_count; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { - +printf("\n--- WORTSALAT ---\n"); + for (int r=0; r < searchFieldLen; r++) { + for (int c=0; c < searchFieldLen; c++) { + printf("%c", salad[r][c] ? salad[r][c] : '.'); + } + printf("\n"); + } + printf("-----------------\n"); } diff --git a/Start_Windows/input.c b/Start_Windows/input.c index ed77805..13a99c8 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -4,9 +4,39 @@ // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. +static void trim_and_toupper(char*dest, const char*src, int max_len) { + int len = strlen(src); + int start = 0, end = len-1; + while (start < len && isspace((unsigned char)src[start])) { + start++; + } + while (end >= start && isspace((unsigned char)src[end])) { + end--; + } + int i; + for (i = 0; i <= (end - start)&& i <(max_len-1) ; i++) { + dest[i] = toupper((unsigned char)src[start+i]); + } + dest[i] = '\0'; + +} // Read words from file and store in 'words' array -int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) -{ +int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + unsigned int count = 0; + char line_buffer[MAX_LINE_LEN]; + const char* DELIMITERS=",:\n"; + while (count < maxWordCount && fgets(line_buffer, MAX_LINE_LEN, file)!= NULL) { + char* token = strtok(line_buffer, DELIMITERS); + while (token != NULL && count < maxWordCount) { + trim_and_toupper(words[count], token, MAX_WORD_LEN); + if (words[count][0] !='\n'){ + count++; + } + token = strtok(NULL, DELIMITERS); + + } + } + return count; } \ No newline at end of file diff --git a/Start_Windows/main.c b/Start_Windows/main.c index 03da755..0c13c17 100644 --- a/Start_Windows/main.c +++ b/Start_Windows/main.c @@ -1,51 +1,46 @@ #include #include +#include #include "input.h" #include "game.h" #include "graphicalGame.h" #define MAX_NUMBER_OF_WORDS 100 #define SALAD_SIZE 20 +#define WINDOW_WIDTH 800 int main(int argc, char *argv[]) { int exitCode = EXIT_SUCCESS; - - // Check if the correct number of arguments is provided - if(argc != 2) - { + srand(time(NULL)); + if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exitCode = EXIT_FAILURE; - } - else - { - char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game + } else { + char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; unsigned int wordCount = 0; FILE *file = fopen(argv[1], "r"); - - if(file != NULL) - { + if (file != NULL) { 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]; - // Read words from file and store in 'words' array wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); fclose(file); + placedWords = createWordSalad(wordSalad, SALAD_SIZE, (const char(*)[MAX_WORD_LEN])words, wordCount); + if (placedWords == wordCount) { + printf("Info: %d Wörter geladen und platziert. Starte speil...\n", placedWords); + showWordSalad(wordSalad, SALAD_SIZE); + startGame(wordSalad, SALAD_SIZE, words, wordCount,WINDOW_WIDTH); + printf("Spielbeendet.\n"); + } else { + fprintf(stderr, "Fehler: Es konnten nur %d von %d Wörtern platziert werden.\n", placedWords, wordCount, wordCount); + fprintf(stderr, "Das Spiel ist evtl. zu klein.\n"); + exitCode = EXIT_FAILURE; + } - // Create the word salad by placing words into grid - placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); - - // TODO: - // 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 open file %s for reading ...\n", argv[1]); + } else { + fprintf(stderr, "Datei konnte nicht geöffnet werden.\n"); exitCode = EXIT_FAILURE; } }