From 9d08cf3feefc1a5dc6ad423419eb24295ee4ccb9 Mon Sep 17 00:00:00 2001 From: pvtrx Date: Tue, 4 Nov 2025 11:49:18 +0100 Subject: [PATCH] game.c done -- started main.c --- Start_Windows/Test123.c | 116 ++++++++++++++++++++++++++++++++++----- Start_Windows/TestMain.c | 3 + Start_Windows/game.c | 80 ++++++++++++++++++++++++++- 3 files changed, 182 insertions(+), 17 deletions(-) create mode 100644 Start_Windows/TestMain.c diff --git a/Start_Windows/Test123.c b/Start_Windows/Test123.c index bd54964..5e71e10 100644 --- a/Start_Windows/Test123.c +++ b/Start_Windows/Test123.c @@ -1,27 +1,115 @@ -#include "input.h" +#include "game.h" +#include +#include +#include #include -#include -#include -int i = 10; -int main(void) { -// Read words from file and store in 'words' array -int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) +int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { + //testing variables + int MAX_RAND_TRIES_PER_WORD = 10; + int EMPTY_CHAR = 0; + //end of testing variables - while (i < 100) { - Sleep(5000); + memset(salad, EMPTY_CHAR, MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN * sizeof(salad[0][0])); + int line = 0; + int column = 0; + int wordsplaced = 0; - i++; + while (wordsplaced < wordCount) { + int direction = rand() % 2; // 0 = horizontal, 1 = vertical + int wordlen = strlen(words[wordsplaced]); + int placed = 0; + int tries = 0; + + while (!placed && tries < MAX_RAND_TRIES_PER_WORD) { + + if (direction == 0) { // horizontal + if (wordlen <= searchFieldLen) { + column = rand() % (searchFieldLen - wordlen + 1); + line = rand() % searchFieldLen; + int canPlace = 1; + for (int i = 0; i < wordlen; i++) { + if (salad[line][column + i] != EMPTY_CHAR && salad[line][column + i] != words[wordsplaced][i]) { + canPlace = 0; + break; + } + } + if (canPlace) { + for (int i = 0; i < wordlen; i++) { + salad[line][column + i] = words[wordsplaced][i]; + } + placed = 1; + } + } + } else { // vertical + if (wordlen <= searchFieldLen) { + line = rand() % (searchFieldLen - wordlen + 1); + column = rand() % searchFieldLen; + int canPlace = 1; + for (int i = 0; i < wordlen; i++) { + if (salad[line + i][column] != EMPTY_CHAR && salad[line + i][column] != words[wordsplaced][i]) { + canPlace = 0; + break; + } + } + if (canPlace) { + for (int i = 0; i < wordlen; i++) { + salad[line + i][column] = words[wordsplaced][i]; + } + placed = 1; + } + } + } + tries++; + } + if (placed) { + wordsplaced++; + } else { + // Could not place the word after max tries + return -1; + } + } + + for (int i = 0; i < searchFieldLen; i++) { // replaces 0 with random letters + for (int j = 0; j < searchFieldLen; j++) { + if (salad[i][j] == EMPTY_CHAR) { + salad[i][j] = 'A' + (rand() % 26); + } + } } - - //TODO: Implement function to read words from file words.txt and store them in the words array. See, that in the file words are not seperated only by , ; or space but also by new lines. + return wordsplaced; } -return 0; -} \ No newline at end of file +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 + unsigned int wordCount = 3; // Update this based on number of words + int placedWords; + + placedWords = createWordSalad(salad, searchFieldLen, words, wordCount); + + for (unsigned int i = 0; i < searchFieldLen; i++) { // code von showWordSalad + for (unsigned int j = 0; j < searchFieldLen; j++) { + printf("%c ", salad[i][j]); + } + printf("\n"); + } + + //code for main.c: + + if (placedWords == wordCount) { + printf("All words placed successfully.\n"); + } else { + printf("Could not place all words. Placed %d out of %d words.\n", placedWords, wordCount); + } + + return 0; +} diff --git a/Start_Windows/TestMain.c b/Start_Windows/TestMain.c new file mode 100644 index 0000000..0960bd9 --- /dev/null +++ b/Start_Windows/TestMain.c @@ -0,0 +1,3 @@ +int main(void) { + +} \ No newline at end of file diff --git a/Start_Windows/game.c b/Start_Windows/game.c index db49e36..b24f3e9 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -10,13 +10,87 @@ // 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) { - /* TODO: Implement Game Logic: Place words from the array char words[][MAX_WORD_LEN] randomly horizontally and vertivally - and fill empty spaces with random letters. Only cross words when the overlaping cell contains the same letter in both words. */ + memset(salad, EMPTY_CHAR, MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN * sizeof(salad[0][0])); + + int line = 0; + int column = 0; + + int wordsplaced = 0; + + while (wordsplaced < wordCount) { + int direction = rand() % 2; // 0 = horizontal, 1 = vertical + int wordlen = strlen(words[wordsplaced]); + int placed = 0; + int tries = 0; + + while (!placed && tries < MAX_RAND_TRIES_PER_WORD) { + + if (direction == 0) { // horizontal + if (wordlen <= searchFieldLen) { + column = rand() % (searchFieldLen - wordlen + 1); + line = rand() % searchFieldLen; + int canPlace = 1; + for (int i = 0; i < wordlen; i++) { + if (salad[line][column + i] != EMPTY_CHAR && salad[line][column + i] != words[wordsplaced][i]) { + canPlace = 0; + break; + } + } + if (canPlace) { + for (int i = 0; i < wordlen; i++) { + salad[line][column + i] = words[wordsplaced][i]; + } + placed = 1; + } + } + } else { // vertical + if (wordlen <= searchFieldLen) { + line = rand() % (searchFieldLen - wordlen + 1); + column = rand() % searchFieldLen; + int canPlace = 1; + for (int i = 0; i < wordlen; i++) { + if (salad[line + i][column] != EMPTY_CHAR && salad[line + i][column] != words[wordsplaced][i]) { + canPlace = 0; + break; + } + } + if (canPlace) { + for (int i = 0; i < wordlen; i++) { + salad[line + i][column] = words[wordsplaced][i]; + } + placed = 1; + } + } + } + tries++; + } + if (placed) { + wordsplaced++; + } else { + // Could not place the word after max tries + return -1; + } + } + + for (int i = 0; i < searchFieldLen; i++) { // replaces 0 with random letters + for (int j = 0; j < searchFieldLen; j++) { + if (salad[i][j] == EMPTY_CHAR) { + salad[i][j] = 'A' + (rand() % 26); + } + } + } + + return wordsplaced; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { - //TODO: print word salad to console + for (unsigned int i = 0; i < searchFieldLen; i++) { + for (unsigned int j = 0; j < searchFieldLen; j++) { + printf("%c ", salad[i][j]); + } + printf("\n"); + } }