diff --git a/Start_Windows/Test123.c b/Start_Windows/Test123.c index 5e71e10..787a197 100644 --- a/Start_Windows/Test123.c +++ b/Start_Windows/Test123.c @@ -20,6 +20,8 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi int wordsplaced = 0; + int wordsaborted = 0; + while (wordsplaced < wordCount) { int direction = rand() % 2; // 0 = horizontal, 1 = vertical int wordlen = strlen(words[wordsplaced]); @@ -69,9 +71,11 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi } if (placed) { wordsplaced++; - } else { - // Could not place the word after max tries - return -1; + } + else if (tries == MAX_RAND_TRIES_PER_WORD) { + printf("Abort trying to place the word -%s-\n", words[wordsplaced]); + wordsplaced++; // skip this word and move to the next + wordsaborted++; } } @@ -90,7 +94,7 @@ int main(void) { int SALAD_SIZE = 10; char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; unsigned int searchFieldLen = SALAD_SIZE; - const char words[][MAX_WORD_LEN] = {"TEST", "WORD", "DEINEMUDDA"}; // Add your words here + const char words[][MAX_WORD_LEN] = {"TEST", "WORD", "DEINEMUDDAAAA"}; // Add your words here unsigned int wordCount = 3; // Update this based on number of words int placedWords; @@ -106,7 +110,7 @@ int main(void) { //code for main.c: if (placedWords == wordCount) { - printf("All words placed successfully.\n"); + printf("All valid words placed successfully.\n"); } else { printf("Could not place all words. Placed %d out of %d words.\n", placedWords, wordCount); } diff --git a/Start_Windows/game.c b/Start_Windows/game.c index b24f3e9..50c5f16 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -7,7 +7,7 @@ #define EMPTY_CHAR 0 -// Creates the word salad by placing words randomly and filling empty spaces +// Fills array with EMPTY_CHAR, tries to place words into the salad and fills empty spaces with random letters. returns number of placed words or -1 if not all words could be placed. int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { @@ -17,6 +17,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi int column = 0; int wordsplaced = 0; + int wordsaborted = 0; while (wordsplaced < wordCount) { int direction = rand() % 2; // 0 = horizontal, 1 = vertical @@ -67,9 +68,11 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi } if (placed) { wordsplaced++; - } else { - // Could not place the word after max tries - return -1; + } + else if (tries == MAX_RAND_TRIES_PER_WORD) { + printf("Abort trying to place the word -%s-\n", words[wordsplaced]); + wordsplaced++; // skip this word and move to the next + wordsaborted++; } } diff --git a/Start_Windows/main.c b/Start_Windows/main.c index 03da755..94a69d9 100644 --- a/Start_Windows/main.c +++ b/Start_Windows/main.c @@ -35,11 +35,13 @@ int main(int argc, char *argv[]) // 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 + + // Start game if all valid words were placed + if (placedWords == wordCount) { + startGame(wordSalad, SALAD_SIZE, words, wordCount); + } else { + printf("Could not place all valid words. Placed %d out of %d words.\n", placedWords, wordCount); + } } else