13 Commits
Author SHA1 Message Date
hoangto105860 284cc80e9f comple merge 2026-10-27 17:11:11 +01:00
hoangto105860 6ea1ec75e0 Merge branch 'wortsalat_tommy' 2026-10-27 17:02:39 +01:00
hoangto105860 596e0a5519 finish wordsalad
- remove printf for debugging and add startGame to main.c
2026-10-27 16:59:33 +01:00
hoangto105860 cbf5f85caf fix seed generator for game 2026-10-24 21:28:35 +02:00
hoangto105860 ab558de8b3 first implementation of game.c 2026-10-24 20:38:13 +02:00
hoangto105860 187e626191 second implementation for input and fix unit_tests 2026-10-24 15:49:59 +02:00
hoangto105860 aef73683bc first implementation for input 2026-10-24 02:20:56 +02:00
hoangto105860 dc58f1e4a9 initial commit 2026-10-23 14:30:39 +02:00
hoangto105860 74360620be edit test_readWords_with_delimiters 2026-10-20 16:58:25 +02:00
hoangto105860 1cc06c0e2e change test_readWords_simple 2026-10-20 16:46:35 +02:00
hoangto105860 23c1ae2059 Merge branch 'tommy' 2026-10-17 20:14:48 +02:00
hoangto105860 4cbd8d51c6 Revert "commit"
This reverts commit 8ae62a2f90.
2026-10-17 19:30:13 +02:00
hoangto105860 8ae62a2f90 commit 2026-10-17 19:20:57 +02:00
16 changed files with 183 additions and 4 deletions

No files matched your search

+19
View File
@@ -273,4 +273,23 @@ $RECYCLE.BIN/
# Windows shortcuts # Windows shortcuts
*.lnk *.lnk
### Executable ###
*.app
*.bat
*.cgi
*.com
*.exe
*.gadget
*.jar
*.pif
*.vb
*.wsf
### Kate ###
# Swap Files #
.*.kate-swp
.swp.*
# End of https://www.toptal.com/developers/gitignore/api/linux,windows,macos,visualstudiocode,clion,eclipse # End of https://www.toptal.com/developers/gitignore/api/linux,windows,macos,visualstudiocode,clion,eclipse
+88 -1
View File
@@ -1,7 +1,7 @@
#include "game.h" #include "game.h"
#include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
@@ -13,5 +13,92 @@
// Creates the word salad by placing words randomly and filling empty spaces // 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) 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;
} }
+8
View File
@@ -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); 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 #endif
BIN
View File
Binary file not shown.
Binary file not shown.
+36
View File
@@ -2,11 +2,47 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
// TODO: // TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. // 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 // Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) 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);
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;
} }
BIN
View File
Binary file not shown.
+25 -3
View File
@@ -3,6 +3,9 @@
#include "input.h" #include "input.h"
#include "game.h" #include "game.h"
#include "graphicalGame.h" #include "graphicalGame.h"
#include <errno.h>
#include <time.h>
#define MAX_NUMBER_OF_WORDS 100 #define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20 #define SALAD_SIZE 20
@@ -11,6 +14,8 @@ int main(int argc, char *argv[])
{ {
int exitCode = EXIT_SUCCESS; int exitCode = EXIT_SUCCESS;
srand(time(NULL));
// Check if the correct number of arguments is provided // Check if the correct number of arguments is provided
if(argc != 2) if(argc != 2)
{ {
@@ -34,13 +39,30 @@ int main(int argc, char *argv[])
fclose(file); fclose(file);
// Create the word salad by placing words into grid // Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); //placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// TODO: // TODO:
// Check if all words were successfully placed // Check if all words were successfully placed
// Start the game if successful for(int i = 0; i < 10000; i++)
// error message if some words couldn't be placed {
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 else
{ {
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+1
View File
@@ -0,0 +1 @@
Hund,Katze; Maus
View File
Whitespace-only changes.
+3
View File
@@ -0,0 +1,3 @@
Apfel
Banane
Kiwi
+3
View File
@@ -49,6 +49,8 @@ void test_readWords_with_delimiters(void) {
int count = readWords(f, words, 10); int count = readWords(f, words, 10);
fclose(f); fclose(f);
char cwd[PATH_MAX];
TEST_ASSERT_EQUAL_INT(3, count); TEST_ASSERT_EQUAL_INT(3, count);
TEST_ASSERT_EQUAL_STRING("HUND", words[0]); TEST_ASSERT_EQUAL_STRING("HUND", words[0]);
TEST_ASSERT_EQUAL_STRING("KATZE", words[1]); TEST_ASSERT_EQUAL_STRING("KATZE", words[1]);
@@ -171,3 +173,4 @@ int main(void) {
return result; return result;
} }
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.