From e1352870e0d1ca3d2e6fe320cb82b10c12c24fc0 Mon Sep 17 00:00:00 2001 From: Henrik Batz Date: Thu, 29 Oct 2026 15:43:59 +0100 Subject: [PATCH] Refactor game.c --- Startcode/game.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/Startcode/game.c b/Startcode/game.c index cca6f86..5c49ee8 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -7,9 +7,9 @@ #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 -int placeWord(int len, int column, int row, - char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], - int searchFieldLen, const char* word, int axis); + +int placeWord(const char* word, int wordLength, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], + int searchFieldLen, int column, int row, int axis); // Creates the word salad by placing words randomly and filling empty spaces int createWordSalad( @@ -48,22 +48,17 @@ int createWordSalad( int row = rand() % searchFieldLen; int column = rand() % searchFieldLen; - int success = placeWord( - wordlenght, column, row, salad, - searchFieldLen, words[w], axis); + int success = placeWord( words[w],wordlenght, salad, searchFieldLen, column, row, axis); if (success) { placedWords++; break; } - - printf("Platzierung zum %d. Mal fehlgeschlagen!\n", - attempts + 1); } } - //Fill empty spaces + // Fill empty spaces for (int r = 0; r < searchFieldLen; r++) { for (int c = 0; c < searchFieldLen; c++) @@ -79,16 +74,15 @@ int createWordSalad( } -int placeWord(int len, int column, int row, - char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], - int searchFieldLen, const char* word, int axis) +int placeWord(const char* word, int wordLength, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], + int searchFieldLen, int column, int row, int axis) { // Available space check & word collision avoidance - for (int i = 0; i < len; i++) + for (int i = 0; i < wordLength; i++) { if (axis == 0) // Horizontal { - if (column + len > searchFieldLen || + if (column + wordLength > searchFieldLen || salad[row][column + i] != EMPTY_CHAR) { return 0; @@ -96,7 +90,7 @@ int placeWord(int len, int column, int row, } else // Vertical { - if (row + len > searchFieldLen || + if (row + wordLength > searchFieldLen || salad[row + i][column] != EMPTY_CHAR) { return 0; @@ -105,7 +99,7 @@ int placeWord(int len, int column, int row, } //Write Word - for (int i = 0; i < len; i++) + for (int i = 0; i < wordLength; i++) { if (axis == 0) // Horizontal {