diff --git a/Start_Mac/.DS_Store b/Start_Mac/.DS_Store new file mode 100644 index 0000000..08a8281 Binary files /dev/null and b/Start_Mac/.DS_Store differ diff --git a/Start_Mac/game.c b/Start_Mac/game.c index d8cc133..a1fb95e 100644 --- a/Start_Mac/game.c +++ b/Start_Mac/game.c @@ -10,14 +10,92 @@ /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ +//edited + typedef enum { HORIZONTAL, VERTICAL } Direction; + +//edited: bestimmt zufällige richtung + static Direction randomDirection(void) +{ + return (rand() % 2 == 0) ? HORIZONTAL : VERTICAL; +} + +//edited // 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)); // seed bei jedem funktionsaufruf neu setzen + // spielfeld initialisieren (leer) + for (int y = 0; y < searchFieldLen; y++) { + for (int x = 0; x < searchFieldLen; x++) { + salad[y][x] = EMPTY_CHAR; + } + } + + //wörter platzieren, 1 schleife = 1 wort + for(int w = 0; w < wordCount; w++){ + const char *word = words[w]; // *word ist immer ein wort von "words" (zeigt auf eine zeile) + int len = strlen(word); + + if(len > searchFieldLen) // wenn wort zu lang, nimm nächstes wort + continue; + + int placed = 0; + + // versuche mehrere random positionen, 1 schleife = 1 position + for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++){ + Direction dir = randomDirection(); + int startX = 0; + int startY = 0; + + if(dir == HORIZONTAL){ + startX = rand() % (searchFieldLen - len + 1); //zeile ist 10 (0-9) breit, wort 5 lang -> höchstens in 5-9 -> 10 - 5 + 1 = 6 (startet random iwo von 0 bis 5)✅ + startY = rand() % searchFieldLen; //egal + }else{ + startX = rand() % searchFieldLen; + startY = rand() % (searchFieldLen - len + 1); + } + + //passt wort rein? (komplett leer/hat einen überschneidenen buchstaben) --> geht ganze wort platz durch (vert oder horiz), 1 schleife = 1 buchstabe im wort + int fits = 1; + for (int i = 0; i < len; i++) { + int x = startX + (dir == HORIZONTAL ? i : 0); + int y = startY + (dir == VERTICAL ? i : 0); + if (salad[y][x] != EMPTY_CHAR && salad[y][x] != word[i]) { // wenn die reihe mit einem buchstaben befüllt der sich nd mit wort überschneidet -> fitted nd + fits = 0; + break; + } + } + if(!fits){ + continue; // versucht nächste position + } + + //place word wenn posi passt + for(int i = 0; i < len; i++){ + int x = startX + (dir == HORIZONTAL ? i : 0); + int y = startY + (dir == VERTICAL ? i : 0); + salad[y][x] = word[i]; + } + placed = 1; + } + } + //leere felder mit random buchstaben füllen + for(int y = 0; y < searchFieldLen; y++){ + for(int x = 0; x < searchFieldLen; x++){ + if(salad[y][x] == EMPTY_CHAR) + salad[y][x] = (rand() % 26) + 'A'; + } + } + return 1; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { - + for(int y = 0; y < searchFieldLen; y++){ + for(int x = 0; x < searchFieldLen; x++){ + printf("%c", salad[y][x]); + } + printf("\n"); + } } diff --git a/Start_Mac/game.o b/Start_Mac/game.o new file mode 100644 index 0000000..921cd20 Binary files /dev/null and b/Start_Mac/game.o differ diff --git a/Start_Mac/graphicalGame.o b/Start_Mac/graphicalGame.o new file mode 100644 index 0000000..17d7707 Binary files /dev/null and b/Start_Mac/graphicalGame.o differ diff --git a/Start_Mac/input.c b/Start_Mac/input.c index 16a8cac..bf7c601 100644 --- a/Start_Mac/input.c +++ b/Start_Mac/input.c @@ -1,5 +1,5 @@ #include "input.h" -#include +#include #include #define MAX_WORD_LEN 100 @@ -10,20 +10,19 @@ int anzahlWoerter = 0; char zeile[500]; -int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) +//edited +int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){ - char *wort[] = strtok(zeile, ",; \n"); + char *wort = strtok(zeile, ",; \n"); while(wort != NULL && anzahlWoerter <= maxWordCount){ - strncpy(words[anzahlWoerter], wort, anzahlWoerter -1); + strncpy(words[anzahlWoerter], wort, MAX_WORD_LEN -1); words[anzahlWoerter][MAX_WORD_LEN - 1]= '\0'; anzahlWoerter++; - *wort = strtok(NULL, ",; \n"); - - } - + wort = strtok(NULL, ",; \n"); + } } return anzahlWoerter; } \ No newline at end of file diff --git a/Start_Mac/input.o b/Start_Mac/input.o new file mode 100644 index 0000000..22c9595 Binary files /dev/null and b/Start_Mac/input.o differ diff --git a/Start_Mac/main.c b/Start_Mac/main.c index 03da755..f9c7c3b 100644 --- a/Start_Mac/main.c +++ b/Start_Mac/main.c @@ -40,7 +40,13 @@ int main(int argc, char *argv[]) // Check if all words were successfully placed // Start the game if successful // error message if some words couldn't be placed - + int windowWidth = 880; + + if(placedWords == 1){ + startGame(wordSalad, SALAD_SIZE, words, wordCount, windowWidth); + }else{ + printf("Fehler: Nicht alle Wörter konnten platziert werden.\n"); + } } else { diff --git a/Start_Mac/main.o b/Start_Mac/main.o new file mode 100644 index 0000000..8c767f4 Binary files /dev/null and b/Start_Mac/main.o differ diff --git a/Start_Mac/makefile b/Start_Mac/makefile index 1d3b4a6..12fc525 100644 --- a/Start_Mac/makefile +++ b/Start_Mac/makefile @@ -13,6 +13,12 @@ unityfolder = ./unity wordsalad_initial: $(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) +# -------------------------- +# eigene Version +# -------------------------- +wordsalad_myversion: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a + $(CC) $(CFLAGS) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS) + # -------------------------- # Normales Spiel bauen # -------------------------- diff --git a/Start_Mac/wordsalad_initial b/Start_Mac/wordsalad_initial new file mode 100755 index 0000000..b1d7dec Binary files /dev/null and b/Start_Mac/wordsalad_initial differ diff --git a/Start_Mac/wordsalad_myversion b/Start_Mac/wordsalad_myversion new file mode 100755 index 0000000..461f298 Binary files /dev/null and b/Start_Mac/wordsalad_myversion differ