This commit is contained in:
Walter Schenk 2025-11-02 23:26:45 +01:00
parent 9a7d3d3766
commit 85c2c57e7f
8 changed files with 142 additions and 9 deletions

View File

@ -6,18 +6,107 @@
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
//TODO: Spiellogik implementieren:
// TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
* restliche Felder mit zufälligen Buchstaben füllen */
// 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));
const char buchstaben[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int placedWords = 0;
for (int i = 0; i < wordCount; i++)
{
int vertikal = rand() % 2;
size_t länge = strlen(words[i]);
int positionX;
int positionY;
int tries = 0;
int belegt;
while (tries <= MAX_RAND_TRIES_PER_WORD)
{
if (vertikal)
{
positionX = rand() % (searchFieldLen);
positionY = rand() % (searchFieldLen - länge);
for (int y = positionY; y < (länge + positionY); y++)
{
if (salad[y][positionX] != '0')
{
tries++;
belegt = 1;
break;
}
}
if(belegt){
continue;
belegt = 0;
}
for (int y = positionY, j = 0; y < (länge + positionY); y++, j++)
{
salad[y][positionX] = words[i][j];
}
placedWords++;
break; // Wort wurde hinzugefügt, while Schleife wird verlassen
}
else
{
positionX = rand() % (searchFieldLen - länge);
positionY = rand() % (searchFieldLen);
for (int x = positionX; x < (länge + positionX); x++)
{
if (salad[positionY][x] != '0')
{
tries++;
break;
}
}
if(belegt){
continue;
belegt = 0;
}
for (int x = positionX, j = 0; x < (länge + positionX); x++, j++)
{
salad[positionY][x] = words[i][j];
}
placedWords++;
break; // Wort wurde hinzugefügt, while Schleife wird verlassen
}
}
}
for(int i = 0; i< searchFieldLen;i++){
for(int j = 0; j< searchFieldLen;j++){
if(salad[i][j] == '0'){
salad[i][j] = buchstaben[rand() % 26];
}
}
}
return placedWords;
}
// Prints the word salad to console
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");
}
}

BIN
Start_Windows/game.o Normal file

Binary file not shown.

View File

@ -8,5 +8,39 @@
// 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];
char *teiler = " ,;\n\t";
char *token;
while(fgets(line, sizeof(line), file)){
//Yeti,Nessie Werwolf; Vampir
// token = Yeti
token = strtok(line, teiler);
while (token != NULL)
{
if (strlen(token) >= MAX_WORD_LEN) {
printf("Fehler: eigelesenes Wort ist zu lang\n");
return count;
}
if (count >= maxWordCount){
printf("Fehler: zu viele Wörter\n");
return count;
}
strncpy(words[count], token, MAX_WORD_LEN-1);
words[count][MAX_WORD_LEN-1] = '\0';
//Nessie Werwolf; Vampir
//token= Nessie
token = strtok(NULL, teiler);
count++;
}
}
return count;
}

BIN
Start_Windows/input.o Normal file

Binary file not shown.

View File

@ -12,7 +12,7 @@ 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)
{
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
exitCode = EXIT_FAILURE;
@ -24,11 +24,19 @@ int main(int argc, char *argv[])
FILE *file = fopen(argv[1], "r");
if(file != NULL)
if (file != NULL)
{
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
{
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
{
wordSalad[i][j] = '0';
}
}
// Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file);
@ -38,15 +46,17 @@ int main(int argc, char *argv[])
// TODO:
// Check if all words were successfully placed
if (placedWords == wordCount) {
if (placedWords == wordCount)
{
// Start the game if successful
showWordSalad(wordSalad, SALAD_SIZE);
} else {
}
else
{
// error message if some words couldn't be placed
printf("Some words could not be placed");
exitCode = EXIT_FAILURE;
}
}
else
{

Binary file not shown.

View File

@ -24,7 +24,7 @@ wordsalad_my: main.o graphicalGame.o $(BINARIES)/libraylib.a
# --------------------------
# Normales Spiel bauen
# --------------------------
all: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
wordsalad: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
main.o: main.c

BIN
Start_Windows/wordsalad.exe Normal file

Binary file not shown.