Start_Windows/game.c aktualisiert

This commit is contained in:
Simon Schuerer 2025-11-07 06:57:56 +00:00
parent 1332dc9355
commit 35ab724f53

View File

@ -1,103 +1,136 @@
#include "game.h" #include "game.h" // Einbindung einer benutzerdefinierten Header-Datei, vermutlich mit Konstanten und Deklarationen
#include <time.h> #include <time.h> // Für Zeitfunktionen wie time(), wird für Zufallszahlengenerierung verwendet
#include <stdlib.h> #include <stdlib.h> // Für Funktionen wie rand(), srand(), malloc() etc.
#include <string.h> #include <string.h> // Für Stringfunktionen wie strlen()
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren #define MAX_RAND_TRIES_PER_WORD 10 // Maximale Versuche pro Wort (nicht direkt verwendet im Code)
#define EMPTY_CHAR 0 // Definition eines leeren Zeichens (nicht direkt verwendet im Code)
/* 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 */
// Aufzählungstyp zur Richtungsprüfung
typedef enum { typedef enum {
DIR_DOWN, DIR_DOWN, // Wort wird vertikal nach unten platziert
DIR_RIGHT, DIR_RIGHT, // Wort wird horizontal nach rechts platziert
DIR_FAILURE, DIR_FAILURE, // Platzierung nicht möglich
} check_dir_e; } check_dir_e;
// Prüft, ob ein Wort an einer bestimmten Position und Richtung platziert werden kann
static check_dir_e check_field(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], static check_dir_e check_field(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int searchFieldLen, const unsigned int word_len, unsigned int searchFieldLen, const unsigned int word_len,
const int pos_x, const int pos_y) const int pos_x, const int pos_y)
{ {
const int random_direction = rand() % 2; const int random_direction = rand() % 2; // Zufällige Richtung: 0 = unten, 1 = rechts
/* Check if direction right is usable*/
// Prüft, ob Richtung rechts möglich ist
if (random_direction == 1) { if (random_direction == 1) {
for (int i = 0; i < word_len; i++) { for (int i = 0; i < word_len; i++) {
if (pos_x + i >= searchFieldLen || salad[pos_y][pos_x + i] != '\0') { if (pos_x + i >= searchFieldLen || salad[pos_y][pos_x + i] != '\0') {
break; break; // Abbruch, wenn außerhalb des Feldes oder Feld bereits belegt
} }
if (i == word_len - 1) { if (i == word_len - 1) {
return DIR_RIGHT; return DIR_RIGHT; // Richtung rechts ist möglich
} }
} }
} }
/* Check if direction down is usable*/
// Prüft, ob Richtung unten möglich ist
for (int i = 0; i < word_len; i++) { for (int i = 0; i < word_len; i++) {
if (pos_y + i > searchFieldLen - 1 || salad[pos_y + i][pos_x] != '\0') { if (pos_y + i > searchFieldLen - 1 || salad[pos_y + i][pos_x] != '\0') {
break; break; // Abbruch, wenn außerhalb des Feldes oder Feld bereits belegt
} }
if (i == word_len - 1) { if (i == word_len - 1) {
return DIR_DOWN; return DIR_DOWN; // Richtung unten ist möglich
} }
} }
return DIR_FAILURE;
return DIR_FAILURE; // Keine Richtung möglich
} }
// Erstellt das Buchstabensalat-Feld mit den gegebenen Wörtern
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN],
unsigned int wordCount) unsigned int wordCount)
{ {
srand(time(NULL)); srand(time(NULL)); // Initialisiert Zufallszahlengenerator mit aktuellem Zeitpunkt
int pos_x;
int pos_y; int pos_x; // X-Position für Wortplatzierung
int pos_y; // Y-Position für Wortplatzierung
// Initialisiert das Feld mit leeren Zeichen
for (unsigned int y = 0; y < searchFieldLen; y++) { for (unsigned int y = 0; y < searchFieldLen; y++) {
for (unsigned int x = 0; x < searchFieldLen; x++) { for (unsigned int x = 0; x < searchFieldLen; x++) {
salad[y][x] = '\0'; salad[y][x] = '\0'; // Setzt jedes Feld auf leer
} }
} }
int placedWords = 0;
int placedWords = 0; // Zähler für erfolgreich platzierte Wörter
unsigned int cnt; unsigned int cnt;
// Schleife über alle Wörter
for (cnt = 0; cnt < wordCount; cnt++) { for (cnt = 0; cnt < wordCount; cnt++) {
check_dir_e direction = DIR_FAILURE; check_dir_e direction = DIR_FAILURE; // Anfangszustand: keine Richtung möglich
int tries = 0; int tries = 0;
// Versucht bis zu 1000-mal ein Wort zu platzieren
while (direction == DIR_FAILURE && tries < 1000) { while (direction == DIR_FAILURE && tries < 1000) {
/* Generate new random position */ direction = DIR_FAILURE; // Rücksetzen der Richtung
direction = DIR_FAILURE; pos_x = rand() % searchFieldLen; // Zufällige X-Position
pos_x = rand() % searchFieldLen; pos_y = rand() % searchFieldLen; // Zufällige Y-Position
pos_y = rand() % searchFieldLen;
/* Check for valid fielddirection and create string */ // Prüft, ob das Wort an der Position platziert werden kann
direction = check_field(salad, searchFieldLen, strlen(words[cnt]), pos_x, pos_y); direction = check_field(salad, searchFieldLen, strlen(words[cnt]), pos_x, pos_y);
/* Place words either down or right */
// Platziert das Wort je nach Richtung
switch (direction) { switch (direction) {
case DIR_DOWN: case DIR_DOWN:
for (int i = 0; i < strlen(words[cnt]); i++) { for (int i = 0; i < strlen(words[cnt]); i++) {
salad[pos_y + i][pos_x] = words[cnt][i]; salad[pos_y + i][pos_x] = words[cnt][i]; // Buchstaben vertikal einfügen
// printf("%d ; %d %s\n", (pos_y + i), pos_x, words[cnt]);
} }
placedWords++; placedWords++; // Erfolgreich platziertes Wort zählen
break; break;
case DIR_RIGHT: case DIR_RIGHT:
for (int i = 0; i < strlen(words[cnt]); i++) { for (int i = 0; i < strlen(words[cnt]); i++) {
salad[pos_y][pos_x + i] = words[cnt][i]; salad[pos_y][pos_x + i] = words[cnt][i]; // Buchstaben horizontal einfügen
// printf("%d ; %d %s\n", pos_y, (pos_x + i), words[cnt]);
} }
placedWords++; placedWords++; // Erfolgreich platziertes Wort zählen
break; break;
case DIR_FAILURE: case DIR_FAILURE:
break; break; // Keine Aktion bei Fehlschlag
} }
tries++;
/* If word was successfully placed break the while loop */ tries++; // Versuchszähler erhöhen
// Wenn erfolgreich platziert, Schleife verlassen
if (direction != DIR_FAILURE) { if (direction != DIR_FAILURE) {
break; break;
} }
} }
} }
/* Fill the empty fields with random letters*/
// Füllt alle leeren Felder mit zufälligen Buchstaben
for (unsigned int y = 0; y < searchFieldLen; y++) { for (unsigned int y = 0; y < searchFieldLen; y++) {
for (unsigned int x = 0; x < searchFieldLen; x++) { for (unsigned int x = 0; x < searchFieldLen; x++) {
if (salad[y][x] == '\0') { if (salad[y][x] == '\0') {
salad[y][x] = 'A' + (rand() % 26); salad[y][x] = 'A' + (rand() % 26); // Zufälliger Buchstabe von A bis Z
} }
} }
} }
return placedWords;
return placedWords; // Gibt Anzahl der platzierten Wörter zurück
}
// Gibt das Buchstabensalat-Feld auf der Konsole aus
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int searchFieldLen)
{
for (unsigned int y = 0; y < searchFieldLen; y++) {
for (unsigned int x = 0; x < searchFieldLen; x++) {
printf("%c ", salad[y][x]); // Gibt jeden Buchstaben mit Leerzeichen aus
}
printf("\n"); // Neue Zeile nach jeder Reihe
}
} }
// Prints the word salad to console // Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],