diff --git a/Start_Mac/game.c b/Start_Mac/game.c index 6fb47a5..bb6cd10 100644 --- a/Start_Mac/game.c +++ b/Start_Mac/game.c @@ -1,223 +1,93 @@ #include "game.h" -#include +#include #include #include +#include -#define MAX_RAND_TRIES_PER_WORD 10 -#define EMPTY_CHAR '.' +#define EMPTY_CHAR '.' // Leeres Kästchen +typedef enum { HORIZONTAL, VERTICAL } Direction; -// TODO: Spiellogik implementieren: -/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren - * restliche Felder mit zufälligen Buchstaben füllen */ - - - -static void initializeArray(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) //Array initialisieren & mit Filler füllen +// 1. Funktion – Array initialisieren +static void initializeArray(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int size) { - - for (int i = 0; i < searchFieldLen; i++) - { - - for (int j = 0; j < searchFieldLen; j++) - { - salad[i][j] = EMPTY_CHAR; - } - } + for (unsigned int y = 0; y < size; y++) + for (unsigned int x = 0; x < size; x++) + salad[y][x] = EMPTY_CHAR; } - -static void col_row (unsigned int searchFieldLen, int *x, int *y) //Zufällige x/y Koordinate für Anfang von Wort +// 2. Funktion – prüfen, ob Wort passt +static int canPlace(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], + unsigned int size, unsigned int x, unsigned int y, + const char *word, Direction dir) { - *x = rand() % searchFieldLen; + unsigned int len = strlen(word); + if ((dir == HORIZONTAL && x + len > size) || (dir == VERTICAL && y + len > size)) + return 0; - *y = rand() % searchFieldLen; + for (unsigned int i = 0; i < len; i++) { + if ((dir == HORIZONTAL && salad[y][x + i] != EMPTY_CHAR) || + (dir == VERTICAL && salad[y + i][x] != EMPTY_CHAR)) + return 0; + } + return 1; } - -static int place(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], int searchFieldLen, int len [],const char words [] [MAX_WORD_LEN], unsigned int wordCount) //Prüfen ob plazierbar und plazieren +// 3. Funktion – Wort eintragen +static void placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], + unsigned int x, unsigned int y, const char *word, Direction dir) { - int x, y; - int placedWords = 0; //Counter plazierte Wörter + for (unsigned int i = 0; i < strlen(word); i++) { + if (dir == HORIZONTAL) + salad[y][x + i] = word[i]; + else + salad[y + i][x] = word[i]; + } +} +// 4. Funktion – leere Felder mit Zufallsbuchstaben füllen +static void fillRandomLetters(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int size) +{ + for (unsigned int y = 0; y < size; y++) + for (unsigned int x = 0; x < size; x++) + if (salad[y][x] == EMPTY_CHAR) + salad[y][x] = 'A' + (rand() % 26); +} - for(int i = 0; i < wordCount; i++) - { - int placed = 0; +// Hauptfunktion – Wortsalat erzeugen +int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], + unsigned int size, const char words[][MAX_WORD_LEN], + unsigned int wordCount) +{ + srand(time(NULL)); + initializeArray(salad, size); + int placed = 0; + for (unsigned int i = 0; i < wordCount; i++) { + int success = 0; + for (int tries = 0; tries < 1000 && !success; tries++) { + unsigned int x = rand() % size; + unsigned int y = rand() % size; + Direction dir = rand() % 2 ? HORIZONTAL : VERTICAL; - for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) - { - len[i] = (int)strlen(words[i]); - - col_row(searchFieldLen, &x, &y); - - int orient = rand() % 2; - - - if(orient == 0) //wir prüfen und füllen horizontal (in x-Richtung) - { - int works = 1; - - if(((x + len[i]) > searchFieldLen)) - { - - works = 0; - + if (canPlace(salad, size, x, y, words[i], dir)) { + placeWord(salad, x, y, words[i], dir); + success = 1; + placed++; } + } + } - - else - { - - for(int j = x; j < (x + len[i]); j++) - { - - if(salad[y][j] != EMPTY_CHAR) - { - - works = 0; - break; - - } - } - } - - // ab hier platzieren wir - - if(works) - { - - for (int k = 0; k < len[i]; k++) - { - - salad[y][x + k] = words[i][k]; - - } - - placed = 1; - placedWords++; - - } - } - - - else //wir prüfen und füllen vertikal (in y-Richtung) - { - int works = 1; - - if(((y + len[i]) > searchFieldLen)) - { - - works = 0; - - } - - else - { - - for(int j = y; j < (y + len[i]); j++) - { - - if(salad[j][x] != EMPTY_CHAR) - { - - works = 0; - break; - - } - } - } - - // ab hier platzieren wir - - if(works) - { - - for (int k = 0; k < len[i]; k++) - { - - salad[y + k][x] = words[i][k]; - - } - - placed = 1; - placedWords++; - - } - } - } - } - -return placedWords; - + fillRandomLetters(salad, size); + return placed; } - -static void fillLetters(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) //Übrige freie Felder mit Buchstaben füllen +// Ausgabe-Funktion +void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int size) { - - for (int i = 0; i < searchFieldLen; i++) - { - - for (int j = 0; j < searchFieldLen; j++) - { - - if (salad[i][j] == EMPTY_CHAR) - { - - char random_letter = 'A' + (rand() % 26); - salad[i][j] = random_letter; - - } - } - } -} - -//Erstellt Wortsalat, plaziert Wörter zufällig & füllt die Lücken - -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)); - - initializeArray(salad, searchFieldLen); - - int len [wordCount]; - int count = 0; - - for(int i = 0; i < wordCount; i++) - { - - if(words [i] [0] != '\0') - count++; - - } - - int placeWords = place(salad, searchFieldLen, len, words, count); - - - fillLetters(salad, searchFieldLen); - - return placeWords; - -} - -// Ausgabe von Wortsalat auf Konsole - -void showWordSalad(const char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) -{ - - for (int i = 0; i < searchFieldLen; i++) - { - - for (int j = 0; j < searchFieldLen; j++) - { - - printf("%c ", salad [i][j]); - - } - - printf("\n"); - - } -} + for (unsigned int y = 0; y < size; y++) { + for (unsigned int x = 0; x < size; x++) + printf("%c ", salad[y][x]); + printf("\n"); + } +} \ No newline at end of file diff --git a/Start_Mac/game.o b/Start_Mac/game.o index 1af5590..6c071c5 100644 Binary files a/Start_Mac/game.o 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..ded4dd0 Binary files /dev/null and b/Start_Mac/graphicalGame.o differ diff --git a/Start_Mac/input.c b/Start_Mac/input.c index 1794b48..b4e1fc4 100644 --- a/Start_Mac/input.c +++ b/Start_Mac/input.c @@ -1,39 +1,38 @@ +#include +#include +#include +#include #include "input.h" -#include -#include -#include + // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. -// Read words from file and store in 'words' array + int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { - - unsigned int count = 0; - char line[MAX_LINE_LEN]; - while(fgets(line, sizeof(line), file)!= NULL && count < maxWordCount) + unsigned int count = 0; // wie viele Wörter bisher gespeichert wurden + char line[MAX_LINE_LEN]; // Puffer für EINE gelesene Textzeile + + while(fgets(line, sizeof(line), file)!= NULL && count < maxWordCount) //fgets liest eine komplette Zeile wenn es noch eine Zeile gibt und noch Platz für mehr Wörter ist { - const char *delims = "\t\n\r ,;"; - char *token = strtok(line, delims); - + const char *delims = "\t\n\r ,;"; //Liste der Trenner (z.B. Komma) + char *token = strtok(line, delims); //strok sucht in line das erste Wort, indem es an Trennzeichen teilt. Wenn kein Wort gefunden, wird NULL zurück gegeben - while (token != NULL && count < maxWordCount) + + while (token != NULL && count < maxWordCount) //Solange es noch ein Wort (token) gibt und noch Platz in words ist { for(unsigned int i = 0; token[i] != '\0'; i++) { - token[i] = toupper((unsigned char)token[i]); + token[i] = toupper((unsigned char)token[i]); //toupper macht jeden Buchstaben groß. } - strncpy(words[count], token, MAX_WORD_LEN -1); + strncpy(words[count], token, MAX_WORD_LEN -1); //Kopiert das Wort in den nächsten freien Slot words[count]. words[count][MAX_WORD_LEN -1] = '\0'; - count++; - token = strtok(NULL, delims); //hier + count++; //Wir habenein Wort abgespeichert count + 1 + token = strtok(NULL, delims); //hier } - } return count; - } - diff --git a/Start_Mac/input.o b/Start_Mac/input.o index 8acf8a7..32b24e4 100644 Binary files a/Start_Mac/input.o and b/Start_Mac/input.o differ diff --git a/Start_Mac/main.c b/Start_Mac/main.c index 36cce28..7a0bfd6 100644 --- a/Start_Mac/main.c +++ b/Start_Mac/main.c @@ -10,84 +10,44 @@ int main(int argc, char *argv[]) -{ +{ int exitCode = EXIT_SUCCESS; - - // Check if the correct number of arguments is provided - - if(argc != 2) + if (argc != 2) //wenn keine zwei argumente gefunde -> alarm { - - fprintf(stderr, "Usage: %s \n", argv[0]); - exitCode = EXIT_FAILURE; - + fprintf(stderr, "Benutzung: %s \n", argv[0]); + return EXIT_FAILURE; } - else + char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; //array mit eingelesenen wörtern + unsigned int wordCount = 0; + + FILE *file = fopen(argv[1], "r"); //schaut in die words.txt datei + if (!file) //wenn keine datei -> alarm { - - char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game - unsigned int wordCount = 0; - - FILE *file = fopen(argv[1], "r"); - - if(file != NULL) - { - - unsigned int placedWords = 0; - char wordSalad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad - - - // Read words from file and store in 'words' array - - wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); - fclose(file); - - - // Create the word salad by placing words into grid - - placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, MAX_NUMBER_OF_WORDS); - - - // TODO: - // Check if all words were successfully placed - - if(placedWords == wordCount) - { - - printf("Alle %u Wörter wurden erfolgreich platziert.", placedWords); - - // Start the game if successful - - startGame(wordSalad, SALAD_SIZE, words, wordCount, 800); - - } - - // error message if some words couldn't be placed - - - else - { - - printf("Es konnten nicht alle Wörter ausgegeben werden. Nur %u von %u Wörtern wurde platziert.\n", placedWords, wordCount); - - } - } - - - else - { - - // Print error message if file couldn't be opened - - fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]); - exitCode = EXIT_FAILURE; - - } + fprintf(stderr, "Konnte Datei %s nicht öffnen.\n", argv[1]); + return EXIT_FAILURE; } + unsigned int placedWords = 0; + char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; + + wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); //holt sich aus readwords funktion die wortanzahl + fclose(file); + + placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); //holt sich anzahl der platzierten wörter von createwordsalad funktion + + printf("\nWortsalat-Initialisierung\n"); //ganz viel printen + printf("-------------------------\n"); + printf("Gelesene Woerter: %u\n", wordCount); + printf("Platzierte Woerter: %u\n", placedWords); + + if (placedWords < wordCount) //macht checker, ob alle wörter salatiert wurden + printf("Hinweis: %u Woerter konnten nicht platziert werden.\n", wordCount - placedWords); + + printf("\nSpielfeld (%dx%d):\n\n", SALAD_SIZE, SALAD_SIZE); + showWordSalad(wordSalad, SALAD_SIZE); + printf("\nInitialisierung abgeschlossen.\n"); return exitCode; - } \ No newline at end of file diff --git a/Start_Mac/main.o b/Start_Mac/main.o new file mode 100644 index 0000000..e0cf0ec Binary files /dev/null and b/Start_Mac/main.o differ diff --git a/Start_Mac/runTests b/Start_Mac/runTests deleted file mode 100755 index b936797..0000000 Binary files a/Start_Mac/runTests and /dev/null differ diff --git a/Start_Mac/words.txt b/Start_Mac/words.txt index 9ce7be2..222987c 100644 --- a/Start_Mac/words.txt +++ b/Start_Mac/words.txt @@ -1,16 +1,7 @@ -YETI -NESSIE -WERWOLF -VAMPIR -MONSTER -HYDRA -FRANKENSTEIN -DRACULA -KINGKONG -GREMLIN -KOBOLD -HEXE -POLTERGEIST -GESPENST -OGER -APFEL \ No newline at end of file +Yeti,Nessie Werwolf; Vampir +Monster +Hydra;Frankenstein; fortnite +Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist +Gespenst, Oger +tomasschwester, lababage, eckesbokesseresekes +Doener, Kebab, nacho, alejandrogarnacho, megafon, Telefon, gianluigibuffon, mehrweorter, flosschwester, gingerlatina; \ No newline at end of file diff --git a/Start_Mac/wordsalad b/Start_Mac/wordsalad new file mode 100755 index 0000000..45c6b24 Binary files /dev/null and b/Start_Mac/wordsalad differ diff --git a/Start_Mac/wordsalad.dSYM/Contents/Info.plist b/Start_Mac/wordsalad.dSYM/Contents/Info.plist new file mode 100644 index 0000000..582b74b --- /dev/null +++ b/Start_Mac/wordsalad.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.wordsalad + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Start_Mac/wordsalad.dSYM/Contents/Resources/DWARF/wordsalad b/Start_Mac/wordsalad.dSYM/Contents/Resources/DWARF/wordsalad new file mode 100644 index 0000000..5e9bffe Binary files /dev/null and b/Start_Mac/wordsalad.dSYM/Contents/Resources/DWARF/wordsalad differ diff --git a/Start_Mac/wordsalad.dSYM/Contents/Resources/Relocations/aarch64/wordsalad.yml b/Start_Mac/wordsalad.dSYM/Contents/Resources/Relocations/aarch64/wordsalad.yml new file mode 100644 index 0000000..46a8bcd --- /dev/null +++ b/Start_Mac/wordsalad.dSYM/Contents/Resources/Relocations/aarch64/wordsalad.yml @@ -0,0 +1,5 @@ +--- +triple: 'arm64-apple-darwin' +binary-path: wordsalad +relocations: [] +...