From 3a7cb925911181ab40d20f71b899ae34e4d51c3e Mon Sep 17 00:00:00 2001 From: richardtfe99149 Date: Wed, 5 Nov 2025 23:53:57 +0100 Subject: [PATCH] implementation of game.c with working example to generate gameboard --- Start_Windows/game.c | 134 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 130 insertions(+), 4 deletions(-) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index d8cc133..9a6ed1b 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -2,6 +2,8 @@ #include #include #include +#include + #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 @@ -10,14 +12,138 @@ /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ -// 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) -{ +//Enum for direction +typedef enum { + HORIZONTAL, + VERTICAL +} Direction; +//this function clears gameboard +static void initializeSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen){ + for (int i = 0; i < searchFieldLen; i++){ + for (int j = 0; j < searchFieldLen; j++){ + salad[i][j] = EMPTY_CHAR; + } + } +} + +// this function checks if the word is placable at the current coordinates +static int isPlacementPossible(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], const char* word, int row, int col, Direction dir){ + int len = strlen(word); + for (int k = 0; k < len; k++){ + if (dir == HORIZONTAL){ + if (salad[row][col + k] != EMPTY_CHAR) return 0; + } else { //Vertikal + if (salad[row + k][col] != EMPTY_CHAR) return 0; + } + } + return 1; +} + +//this function places the word at the current coordinate +static void placeWord (char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], const char* word, int row, int col, Direction dir){ + int len = strlen(word); + for (int k = 0; k < len; k++){ + if (dir == HORIZONTAL){ + salad[row][col + k] = word[k]; + } else { + salad[row + k][col] = word[k]; + } + } +} + +// this function fills the empty cells with random characters +static void fillEmtpyCellsRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen){ + for (int i = 0; i < searchFieldLen; i++){ + for (int j = 0; j < searchFieldLen; j++){ + if (salad[i][j] == EMPTY_CHAR){ + salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A'; + } + } + } +} + + +// 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){ + srand(time(NULL)); + int placedWords = 0; + + initializeSalad(salad, searchFieldLen); + + for (int i = 0; i < wordCount; i++){ + const char* currentWord = words[i]; + int len = strlen(currentWord); + int tries = 0; + int success = 0; + + while (tries < MAX_RAND_TRIES_PER_WORD && !success){ + Direction dir = rand() % 2; + int row, col; + + if (dir == HORIZONTAL){ + row = rand() % searchFieldLen; + col = rand() % (searchFieldLen - len + 1); + } else { //Vertikal + row = rand() % (searchFieldLen - len + 1); + col = rand() % searchFieldLen; + } + if (isPlacementPossible(salad, currentWord, row, col, dir)){ + placeWord(salad, currentWord, row, col, dir); + success = 1; + placedWords++; + } + tries++; + } + } + fillEmtpyCellsRandom(salad, searchFieldLen); + + return placedWords; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { - + for (unsigned int i = 0; i < searchFieldLen; i++) { + for (unsigned int j = 0; j < searchFieldLen; j++) { + printf("%c ", salad[i][j]); + } + printf("\n"); + } } + + +int main(void) +{ + // Spielfeldgröße + unsigned int searchFieldLen = 15; + + // Beispiel-Wortliste + const char words[][MAX_WORD_LEN] = { + "AUTO", + "BAUM", + "HAUS", + "KATZE", + "SONNE" + }; + unsigned int wordCount = sizeof(words) / sizeof(words[0]); + + // Spielfeld-Array + char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; + + // Zufall initialisieren (einmalig!) + srand((unsigned int)time(NULL)); + + // Wortsalat erzeugen + int placed = createWordSalad(salad, searchFieldLen, words, wordCount); + + printf("Platziert: %d von %u Woertern\n\n", placed, wordCount); + + // Wortsalat ausgeben + showWordSalad(salad, searchFieldLen); + + return 0; +} + + +