Compare commits

...

8 Commits
main ... main

Author SHA1 Message Date
f637b84844 Alles fertig babakrass 2025-11-04 15:05:03 +01:00
yannick
ad5a4de563 alles bis auf makefile 2025-11-03 21:52:37 +01:00
yannick
5e77016df3 test 2025-10-30 15:17:40 +01:00
1f3236c18b input.c fertig 2025-10-28 14:43:46 +01:00
0feb82646e Wieder weg 2025-10-21 14:25:14 +02:00
yannick
cb9246caa5 test 2025-10-16 14:15:55 +02:00
5a730de455 Wieder weg 2025-10-14 15:24:29 +02:00
0b01ae2039 Test 2025-10-14 15:05:39 +02:00
4 changed files with 155 additions and 32 deletions

View File

@ -6,6 +6,7 @@
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
//TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
@ -13,11 +14,81 @@
// 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((unsigned int)time(NULL));
// Spielfeld leeren
for (unsigned int i = 0; i < searchFieldLen; i++)
for (unsigned int j = 0; j < searchFieldLen; j++)
salad[i][j] = EMPTY_CHAR;
int placedCount = 0;
for (unsigned int w = 0; w < wordCount; w++) {
const char *word = words[w];
unsigned int len = strlen(word);
int placed = 0;
if (len > searchFieldLen)
continue; // Wort passt niemals ins Feld
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD * searchFieldLen && !placed; tries++) {
int horizontal = rand() % 2;
unsigned int row = rand() % searchFieldLen;
unsigned int col = rand() % searchFieldLen;
// Stelle sicher, dass Wort im Spielfeld bleibt
if (horizontal) {
if (col + len > searchFieldLen) continue;
} else {
if (row + len > searchFieldLen) continue;
}
// Prüfen, ob Platz frei ist oder gleiche Buchstaben überlappen
int fits = 1;
for (unsigned int i = 0; i < len; i++) {
char c = horizontal ? salad[row][col + i] : salad[row + i][col];
if (c != EMPTY_CHAR && c != word[i]) {
fits = 0;
break;
}
}
if (!fits) continue;
// Wort einsetzen
for (unsigned int i = 0; i < len; i++) {
if (horizontal)
salad[row][col + i] = word[i];
else
salad[row + i][col] = word[i];
}
placed = 1;
placedCount++;
}
if (!placed) {
fprintf(stderr, "WARNUNG: Wort \"%s\" konnte nicht platziert werden.\n", word);
}
}
// Leere Felder auffüllen
for (unsigned int i = 0; i < searchFieldLen; i++)
for (unsigned int j = 0; j < searchFieldLen; j++)
if (salad[i][j] == EMPTY_CHAR)
salad[i][j] = 'A' + (rand() % 26);
return placedCount;
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
for (unsigned int i = 0; i < searchFieldLen; i++) {
for (unsigned int j = 0; j < searchFieldLen; j++) {
printf("%c ", salad[i][j]);
}
printf("\n");
}
}

View File

@ -8,5 +8,32 @@
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
if (file == NULL)
return 0;
unsigned int count = 0;
char line[512];
// Lese Datei zeilenweise
while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount)
{
// Zerlege die Zeile in Tokens (Wörter), getrennt durch gängige Delimiter
char *token = strtok(line, " ,;:.!?\"\n\r\t");
while (token != NULL && count < maxWordCount)
{
// In Großbuchstaben umwandeln
for (int i = 0; token[i]; i++)
token[i] = (char)toupper((unsigned char)token[i]);
// Wort kopieren
strncpy(words[count], token, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0';
count++;
token = strtok(NULL, " ,;:.!?\"\n\r\t");
}
}
return count;
}

View File

@ -6,49 +6,66 @@
#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20
#define WINDOW_SIZE 800 // Fenstergröße für das grafische Spiel
int main(int argc, char *argv[])
{
int exitCode = EXIT_SUCCESS;
int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided
if(argc != 2)
// Check if the correct number of arguments is provided
if(argc != 2)
{
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
exitCode = EXIT_FAILURE;
}
else
{
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)
{
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
exitCode = EXIT_FAILURE;
}
else
{
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
unsigned int wordCount = 0;
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
FILE *file = fopen(argv[1], "r");
// Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file);
if(file != NULL)
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// Check if all words were successfully placed
if (placedWords == wordCount)
{
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
printf("All %u words successfully placed!\n\n", placedWords);
// 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, wordCount);
// TODO:
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
// Show the generated word salad on console
showWordSalad(wordSalad, SALAD_SIZE);
// Start the graphical game (raylib-basierte Version)
printf("\nStarting graphical word search game ...\n");
startGame(wordSalad, SALAD_SIZE, words, wordCount, WINDOW_SIZE);
}
else
{
// Print error message if file couldn't be opened
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
fprintf(stderr,
"Error: Only %u of %u words could be placed.\n"
"Please reduce the number of words or increase the grid size.\n",
placedWords, wordCount);
exitCode = EXIT_FAILURE;
}
}
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;
}
}
return exitCode;
}
return exitCode;
}

View File

@ -1,5 +1,5 @@
CC = gcc
CFLAGS = -g -Wall -I$(raylibfolder)
CFLAGS = -g -Wall -I$(raylib_folder)
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
BINARIES = ./windows
@ -12,6 +12,14 @@ unityfolder = ./unity
wordsalad_initial:
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
# ------------------------------
# Eigene Version mit libwordsalad.a
# ------------------------------
wordsalad_myversion:
$(CC) -o wordsalad_myversion main.o game.o input.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
# .o Datein -> kompilierte .c Datein (Objektdateien)
#libraylib.a -> Graphik Bibliothek
# --------------------------
# Normales Spiel bauen
@ -23,7 +31,7 @@ main.o: main.c
$(CC) -c $(CFLAGS) main.c
input.o: input.c
$(CC) -c $(CFLAGS)input.c
$(CC) -c $(CFLAGS) input.c
game.o: game.c
$(CC) -c $(CFLAGS) game.c