diff --git a/Wordsalat/.vscode/launch.json b/Wordsalat/.vscode/launch.json new file mode 100644 index 0000000..5c7247b --- /dev/null +++ b/Wordsalat/.vscode/launch.json @@ -0,0 +1,7 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [] +} \ No newline at end of file diff --git a/Wordsalat/.vscode/tasks.json b/Wordsalat/.vscode/tasks.json new file mode 100644 index 0000000..08d9005 --- /dev/null +++ b/Wordsalat/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc build active file", + "command": "/usr/bin/gcc", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/Wordsalat/game.c b/Wordsalat/game.c index 803301f..e0f5a83 100644 --- a/Wordsalat/game.c +++ b/Wordsalat/game.c @@ -13,5 +13,65 @@ // 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) { - + // Initialize the salad with empty characters + for (unsigned int i = 0; i < searchFieldLen; i++) { + for (unsigned int j = 0; j < searchFieldLen; j++) { + salad[i][j] = EMPTY_CHAR; + } + } + + // Seed the random number generator + srand((unsigned int)time(NULL)); + + // Place each word in the salad + for (unsigned int w = 0; w < wordCount; w++) { + const char *word = words[w]; + unsigned int wordLen = strlen(word); + int placed = 0; + + for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) { + // Randomly choose orientation: 0 for horizontal, 1 for vertical + int orientation = rand() % 2; + unsigned int row, col; + + if (orientation == 0) { // Horizontal + row = rand() % searchFieldLen; + col = rand() % (searchFieldLen - wordLen + 1); + // Check if the space is free + int canPlace = 1; + for (unsigned int k = 0; k < wordLen; k++) { + if (salad[row][col + k] != EMPTY_CHAR) { + canPlace = 0; + break; + } + } + if (canPlace) { + for (unsigned int k = 0; k < wordLen; k++) { + salad[row][col + k] = word[k]; + } + placed = 1; + } + } else { // Vertical + row = rand() % (searchFieldLen - wordLen + 1); + col = rand() % searchFieldLen; + // Check if the space is free + int canPlace = 1; + for (unsigned int k = 0; k < wordLen; k++) { + if (salad[row + k][col] != EMPTY_CHAR) { + canPlace = 0; + break; + } + } + if (canPlace) { + for (unsigned int k = 0; k < wordLen; k++) { + salad[row + k][col] = word[k]; + } + placed = 1; + } + } + } + + if (!placed) { + return -1; // Failed to place the word after max tries + } } diff --git a/Wordsalat/game.o b/Wordsalat/game.o deleted file mode 100644 index 93ff4a0..0000000 Binary files a/Wordsalat/game.o and /dev/null differ diff --git a/Wordsalat/graphicalGame.o b/Wordsalat/graphicalGame.o deleted file mode 100644 index e75d53f..0000000 Binary files a/Wordsalat/graphicalGame.o and /dev/null differ diff --git a/Wordsalat/input.c b/Wordsalat/input.c index ed77805..8116178 100644 --- a/Wordsalat/input.c +++ b/Wordsalat/input.c @@ -8,5 +8,18 @@ // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + unsigned int wordCount = 0; + char buffer[MAX_WORD_LEN]; + while (wordCount < maxWordCount && fscanf(file, "%s", buffer) == 1) { + + for (int i = 0; buffer[i]; i++) { + buffer[i] = tolower(buffer[i]); + } + strncpy(words[wordCount], buffer, MAX_WORD_LEN - 1); + words[wordCount][MAX_WORD_LEN - 1] = '\0'; + wordCount++; + } + + return wordCount; } \ No newline at end of file diff --git a/Wordsalat/input.o b/Wordsalat/input.o deleted file mode 100644 index edb41b2..0000000 Binary files a/Wordsalat/input.o and /dev/null differ diff --git a/Wordsalat/main.o b/Wordsalat/main.o deleted file mode 100644 index b3e59cc..0000000 Binary files a/Wordsalat/main.o and /dev/null differ diff --git a/Wordsalat/wordsalad b/Wordsalat/wordsalad deleted file mode 100755 index d88ed8c..0000000 Binary files a/Wordsalat/wordsalad and /dev/null differ