diff --git a/Wordsalat/game.c b/Wordsalat/game.c index 803301f..faa7e59 100644 --- a/Wordsalat/game.c +++ b/Wordsalat/game.c @@ -1,7 +1,7 @@ #include "game.h" -#include #include #include +#include #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 @@ -13,5 +13,92 @@ // 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) { + unsigned int placedWords = 0; + emptyField(searchFieldLen, salad); + + for(int i = 0; i < wordCount; i++) + { + placedWords += placement(salad, searchFieldLen, words[i]); + } + + fillRandAlpha(searchFieldLen, salad); + + return placedWords; +} + + + + +char alphaRand() +{ + int letter = (rand() % 26) + 'A'; + + return (char) letter; +} + +int placement(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word) +{ + int saladX = rand() % ( searchFieldLen - strlen(word) ); + int saladY = rand() % ( searchFieldLen - strlen(word) ); + + if(rand() % 2) + { + for(int i = 0; i < strlen(word); i++) + { + if(isalpha(salad[saladX][saladY + i])) + { + return 0; + } + + } + for(int i = 0; i < strlen(word); i++) + { + salad[saladX][saladY + i] = word[i]; + } + + } + else + { + for(int i = 0; i < strlen(word); i++) + { + if(isalpha(salad[saladX + i][saladY])) + { + return 0; + } + + } + for(int i = 0; i < strlen(word); i++) + { + salad[saladX + i][saladY] = word[i]; + } + } + + return 1; +} + +void emptyField(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]) +{ + for(size_t i = 0; i < searchFieldLen; i++) + { + for(size_t j = 0; j < searchFieldLen; j++) + { + salad[i][j] = '\0'; + } + } +} + +char fillRandAlpha(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]) +{ + for(size_t i = 0; i < searchFieldLen; i++) + { + for(size_t j = 0; j < searchFieldLen; j++) + { + if(!isalpha(salad[i][j])) + { + salad[i][j] = alphaRand(); + } + } + } + return 0; } diff --git a/Wordsalat/game.h b/Wordsalat/game.h index cc66572..0e0d71d 100644 --- a/Wordsalat/game.h +++ b/Wordsalat/game.h @@ -7,4 +7,12 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount); +void emptyField(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]); + +char fillRandAlpha(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]); + +int placement(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word); + +char alphaRand(); + #endif diff --git a/Wordsalat/game.o b/Wordsalat/game.o index c167e05..a0597bd 100644 Binary files a/Wordsalat/game.o and b/Wordsalat/game.o differ diff --git a/Wordsalat/graphicalGame.o b/Wordsalat/graphicalGame.o new file mode 100644 index 0000000..fe00176 Binary files /dev/null and b/Wordsalat/graphicalGame.o differ diff --git a/Wordsalat/input.c b/Wordsalat/input.c index ed77805..7c29ab2 100644 --- a/Wordsalat/input.c +++ b/Wordsalat/input.c @@ -2,11 +2,47 @@ #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) { + int wordcount = 0; + char buffer[256]; + char *line = fgets(buffer, sizeof(buffer), file); -} \ No newline at end of file + while(line != NULL) + { + char* word = strtok(line, " ,;"); + + while (word != NULL) + { + strncpy(words[wordcount], word, strlen(word)); + + wordcount += 1; + + word = strtok(NULL, " ,;\t\n"); + } + + line = fgets(buffer, sizeof(buffer), file); + } + + for(int i = 0; i < wordcount; i++) + { + for(int j = 0; j < strlen(words[i]); j++) + { + words[i][j] = toupper(words[i][j]); + + if(words[i][j] == '\n') + { + words[i][j] = '\0'; + } + } + } + + return wordcount; +} diff --git a/Wordsalat/input.o b/Wordsalat/input.o index f018c43..d3942b7 100644 Binary files a/Wordsalat/input.o and b/Wordsalat/input.o differ diff --git a/Wordsalat/main.c b/Wordsalat/main.c index 03da755..381a7ae 100644 --- a/Wordsalat/main.c +++ b/Wordsalat/main.c @@ -3,6 +3,9 @@ #include "input.h" #include "game.h" #include "graphicalGame.h" +#include +#include + #define MAX_NUMBER_OF_WORDS 100 #define SALAD_SIZE 20 @@ -10,6 +13,8 @@ int main(int argc, char *argv[]) { int exitCode = EXIT_SUCCESS; + + srand(time(NULL)); // Check if the correct number of arguments is provided if(argc != 2) @@ -34,13 +39,30 @@ int main(int argc, char *argv[]) fclose(file); // Create the word salad by placing words into grid - placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); + //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 + for(int i = 0; i < 10000; i++) + { + placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); + + if(placedWords == wordCount) + { + + // Start the game if successful + startGame(wordSalad, SALAD_SIZE, words, wordCount, 800); + return exitCode; + } + } + + + // error message if some words couldn't be placed + fprintf(stderr, "Could not start wordsalad. "); + printf("Out of %d words only %d could be placed.\n", wordCount, placedWords); + puts("GOOD LUCK for getting this game started :)"); + exitCode = EXIT_FAILURE; } else { @@ -51,4 +73,4 @@ int main(int argc, char *argv[]) } return exitCode; -} \ No newline at end of file +} diff --git a/Wordsalat/main.o b/Wordsalat/main.o new file mode 100644 index 0000000..addcd95 Binary files /dev/null and b/Wordsalat/main.o differ diff --git a/Wordsalat/runTests b/Wordsalat/runTests index f84fff7..00cfe74 100755 Binary files a/Wordsalat/runTests and b/Wordsalat/runTests differ diff --git a/Wordsalat/testwords_delims.txt b/Wordsalat/testwords_delims.txt new file mode 100644 index 0000000..e721fce --- /dev/null +++ b/Wordsalat/testwords_delims.txt @@ -0,0 +1 @@ +Hund,Katze; Maus diff --git a/Wordsalat/testwords_empty.txt b/Wordsalat/testwords_empty.txt new file mode 100644 index 0000000..e69de29 diff --git a/Wordsalat/testwords_simple.txt b/Wordsalat/testwords_simple.txt new file mode 100644 index 0000000..37d7524 --- /dev/null +++ b/Wordsalat/testwords_simple.txt @@ -0,0 +1,3 @@ +Apfel +Banane +Kiwi \ No newline at end of file diff --git a/Wordsalat/unit_tests.c b/Wordsalat/unit_tests.c index 45bc627..f385be2 100644 --- a/Wordsalat/unit_tests.c +++ b/Wordsalat/unit_tests.c @@ -32,51 +32,29 @@ void print_test_result(int result) { void test_readWords_simple(void) { FILE *f = fopen("testwords_simple.txt", "r"); - char words[14][MAX_WORD_LEN]; - int count = readWords(f, words, 14); + char words[10][MAX_WORD_LEN]; + int count = readWords(f, words, 10); fclose(f); - TEST_ASSERT_EQUAL_INT(15, count); - TEST_ASSERT_EQUAL_STRING("Yeti", words[0]); - TEST_ASSERT_EQUAL_STRING("Nessie", words[1]); - TEST_ASSERT_EQUAL_STRING("Werwolf", words[2]); - TEST_ASSERT_EQUAL_STRING("Vampir", words[3]); - TEST_ASSERT_EQUAL_STRING("Monster", words[4]); - TEST_ASSERT_EQUAL_STRING("Hydra", words[5]); - TEST_ASSERT_EQUAL_STRING("Frankenstein", words[6]); - TEST_ASSERT_EQUAL_STRING("Dracula", words[7]); - TEST_ASSERT_EQUAL_STRING("KingKong", words[8]); - TEST_ASSERT_EQUAL_STRING("Gremlin", words[9]); - TEST_ASSERT_EQUAL_STRING("Kobold", words[10]); - TEST_ASSERT_EQUAL_STRING("Hexe", words[11]); - TEST_ASSERT_EQUAL_STRING("Poltergeist", words[12]); - TEST_ASSERT_EQUAL_STRING("Gespenst", words[13]); - TEST_ASSERT_EQUAL_STRING("Oger", words[14]); + TEST_ASSERT_EQUAL_INT(3, count); + TEST_ASSERT_EQUAL_STRING("APFEL", words[0]); + TEST_ASSERT_EQUAL_STRING("BANANE", words[1]); + TEST_ASSERT_EQUAL_STRING("KIWI", words[2]); } void test_readWords_with_delimiters(void) { FILE *f = fopen("testwords_delims.txt", "r"); - char words[14][MAX_WORD_LEN]; - int count = readWords(f, words, 14); + char words[10][MAX_WORD_LEN]; + int count = readWords(f, words, 10); fclose(f); - TEST_ASSERT_EQUAL_INT(15, count); - TEST_ASSERT_EQUAL_STRING("Yeti", words[0]); - TEST_ASSERT_EQUAL_STRING("Nessie", words[1]); - TEST_ASSERT_EQUAL_STRING("Werwolf", words[2]); - TEST_ASSERT_EQUAL_STRING("Vampir", words[3]); - TEST_ASSERT_EQUAL_STRING("Monster", words[4]); - TEST_ASSERT_EQUAL_STRING("Hydra", words[5]); - TEST_ASSERT_EQUAL_STRING("Frankenstein", words[6]); - TEST_ASSERT_EQUAL_STRING("Dracula", words[7]); - TEST_ASSERT_EQUAL_STRING("KingKong", words[8]); - TEST_ASSERT_EQUAL_STRING("Gremlin", words[9]); - TEST_ASSERT_EQUAL_STRING("Kobold", words[10]); - TEST_ASSERT_EQUAL_STRING("Hexe", words[11]); - TEST_ASSERT_EQUAL_STRING("Poltergeist", words[12]); - TEST_ASSERT_EQUAL_STRING("Gespenst", words[13]); - TEST_ASSERT_EQUAL_STRING("Oger", words[14]); + char cwd[PATH_MAX]; + + TEST_ASSERT_EQUAL_INT(3, count); + TEST_ASSERT_EQUAL_STRING("HUND", words[0]); + TEST_ASSERT_EQUAL_STRING("KATZE", words[1]); + TEST_ASSERT_EQUAL_STRING("MAUS", words[2]); } void test_readWords_empty_file(void) { @@ -195,3 +173,4 @@ int main(void) { return result; } + diff --git a/Wordsalat/words.txt b/Wordsalat/words.txt index 31ee099..3f6c4c1 100644 --- a/Wordsalat/words.txt +++ b/Wordsalat/words.txt @@ -2,4 +2,4 @@ Yeti,Nessie Werwolf; Vampir Monster Hydra;Frankenstein Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist -Gespenst, Oger \ No newline at end of file +Gespenst, Oger diff --git a/Wordsalat/wordsalad b/Wordsalat/wordsalad new file mode 100755 index 0000000..93ea713 Binary files /dev/null and b/Wordsalat/wordsalad differ diff --git a/Wordsalat/wordsalad_initial b/Wordsalat/wordsalad_initial new file mode 100755 index 0000000..08a5a74 Binary files /dev/null and b/Wordsalat/wordsalad_initial differ