gute version

This commit is contained in:
Simon Moedl 2025-11-05 18:19:15 +01:00
parent 629f5ed15e
commit 992119f42c
2 changed files with 103 additions and 241 deletions

View File

@ -1,223 +1,79 @@
#include "game.h" #include "game.h"
#include <time.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#define MAX_RAND_TRIES_PER_WORD 10 int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
#define EMPTY_CHAR '.' unsigned int size, const char words[][MAX_WORD_LEN],
unsigned int wordCount) {
#define EMPTY_CHAR '.'
// TODO: Spiellogik implementieren: typedef enum { HORIZONTAL, VERTICAL } Direction;
/* * 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
{
for (int i = 0; i < searchFieldLen; i++)
{
for (int j = 0; j < searchFieldLen; j++)
{
salad[i][j] = EMPTY_CHAR;
}
}
}
static void col_row (unsigned int searchFieldLen, int *x, int *y) //Zufällige x/y Koordinate für Anfang von Wort
{
*x = rand() % searchFieldLen;
*y = rand() % searchFieldLen;
}
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
{
int x, y;
int placedWords = 0; //Counter plazierte Wörter
for(int i = 0; i < wordCount; i++)
{
int placed = 0;
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;
void initializeArray() {
for (unsigned int y = 0; y < size; y++)
for (unsigned int x = 0; x < size; x++)
salad[y][x] = EMPTY_CHAR;
} }
int canPlace(unsigned int x, unsigned int y, const char *word, Direction dir) {
unsigned int len = strlen(word);
if ((dir == HORIZONTAL && x + len > size) || (dir == VERTICAL && y + len > size))
return 0;
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;
}
void placeWord(unsigned int x, unsigned int y, const char *word, Direction dir) {
for (unsigned int i = 0; i < strlen(word); i++) {
if (dir == HORIZONTAL)
salad[y][x + i] = word[i];
else else
{ salad[y + i][x] = word[i];
}
for(int j = x; j < (x + len[i]); j++) }
{
if(salad[y][j] != EMPTY_CHAR)
{
works = 0;
break;
void fillRandomLetters() {
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);
} }
} }
} }
// 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;
}
static void fillLetters(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) //Übrige freie Felder mit Buchstaben füllen
{
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)); srand(time(NULL));
initializeArray();
initializeArray(salad, searchFieldLen); int placed = 0;
for (unsigned int i = 0; i < wordCount; i++) {
int len [wordCount]; int success = 0;
int count = 0; for (int tries = 0; tries < 1000 && !success; tries++) {
unsigned int x = rand() % size;
for(int i = 0; i < wordCount; i++) unsigned int y = rand() % size;
{ Direction dir = rand() % 2 ? HORIZONTAL : VERTICAL;
if (canPlace(x, y, words[i], dir)) {
if(words [i] [0] != '\0') placeWord(x, y, words[i], dir);
count++; success = 1;
placed++;
}
}
} }
int placeWords = place(salad, searchFieldLen, len, words, count); fillRandomLetters();
fillLetters(salad, searchFieldLen);
return placeWords;
return placed;
} }
// Ausgabe von Wortsalat auf Konsole void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int size) {
for (unsigned int y = 0; y < size; y++) {
void showWordSalad(const char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) for (unsigned int x = 0; x < size; x++) {
{ printf("%c ", salad[y][x]);
for (int i = 0; i < searchFieldLen; i++)
{
for (int j = 0; j < searchFieldLen; j++)
{
printf("%c ", salad [i][j]);
} }
printf("\n"); printf("\n");
} }
} }

View File

@ -11,44 +11,50 @@ 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)
if(argc != 2)
{ {
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]); fprintf(stderr, "Benutzung: %s <Pfad zur Wörterdatei>\n", argv[0]);
exitCode = EXIT_FAILURE; return EXIT_FAILURE;
} }
else
{ char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN];
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 wordCount = 0;
FILE *file = fopen(argv[1], "r"); FILE *file = fopen(argv[1], "r");
if (!file)
if(file != NULL)
{ {
unsigned int placedWords = 0; fprintf(stderr, "Konnte Datei %s nicht öffnen.\n", argv[1]);
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad return EXIT_FAILURE;
}
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
// Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file); 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: printf("\nWortsalat-Initialisierung\n");
// Check if all words were successfully placed printf("-------------------------\n");
// Start the game if successful printf("Gelesene Wörter: %u\n", wordCount);
// error message if some words couldn't be placed printf("Platzierte Wörter: %u\n", placedWords);
} if (placedWords < wordCount)
else printf("Hinweis: %u Wörter konnten nicht platziert werden.\n", wordCount - placedWords);
printf("\nSpielfeld (%dx%d):\n\n", SALAD_SIZE, SALAD_SIZE);
for (int i = 0; i < SALAD_SIZE; i++)
{ {
// Print error message if file couldn't be opened for (int j = 0; j < SALAD_SIZE; j++)
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]); printf("%c ", wordSalad[i][j]);
exitCode = EXIT_FAILURE; printf("\n");
}
} }
printf("\nInitialisierung abgeschlossen.\n");
// Später evtl.:
// startGraphicalGame(wordSalad, SALAD_SIZE, words, wordCount);
return exitCode; return exitCode;
} }