This commit is contained in:
zidanmo99094 committed 2026-10-30 22:43:27 +01:00
1 parent f732b3edf9
commit 49132b1e47
1 file changed
+44 -35
+44 -35
View File
@@ -1,68 +1,77 @@
#include "game.h" #include "game.h"
#include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <ctype.h>
#define MAX_RAND_TRIES_PER_WORD 1000 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int wordCount, char words[][MAX_WORD_LEN], unsigned int gridSize) // TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* 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 wordCount, char **words)
{ {
// 1. Spielfeld zurücksetzen // 1. Spielfeld vollständig mit EMPTY_CHAR initialisieren
memset(salad, EMPTY_CHAR, MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN); for (int r = 0; r < MAX_SEARCH_FIELD_LEN; r++) {
for (int c = 0; c < MAX_SEARCH_FIELD_LEN; c++) {
salad[r][c] = EMPTY_CHAR;
}
}
int placedWordsCount = 0; int placedWordsCount = 0;
// 2. Wörter platzieren // 2. Jedes Wort aus der Wörterliste platzieren
for (unsigned int i = 0; i < wordCount; i++) { for (unsigned int i = 0; i < wordCount; i++) {
size_t len = strlen(words[i]); if (words[i] == NULL) continue;
// Steuerzeichen am Ende entfernen int len = (int)strlen(words[i]);
while (len > 0 && (words[i][len - 1] == '\n' || words[i][len - 1] == '\r' || words[i][len - 1] == ' ' || words[i][len - 1] == '\t')) { if (len == 0 || len > MAX_SEARCH_FIELD_LEN) continue;
words[i][--len] = '\0';
}
if (len == 0 || len > gridSize) {
continue;
}
// Versuche bis zu MAX_RAND_TRIES_PER_WORD Mal, eine freie Position zu finden
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++) { for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++) {
Direction dir = (rand() % 2 == 0) ? DIR_HORIZONTAL : DIR_VERTICAL; // 0 = Horizontal, 1 = Vertikal
int dir = rand() % 2;
int dr = (dir == DIR_VERTICAL) ? 1 : 0; int row, col;
int dc = (dir == DIR_HORIZONTAL) ? 1 : 0; if (dir == 0) { // Horizontal
row = rand() % MAX_SEARCH_FIELD_LEN;
unsigned int maxRow = (dir == DIR_VERTICAL) ? (gridSize - (unsigned int)len + 1) : gridSize; col = rand() % (MAX_SEARCH_FIELD_LEN - len + 1);
unsigned int maxCol = (dir == DIR_HORIZONTAL) ? (gridSize - (unsigned int)len + 1) : gridSize; } else { // Vertikal
row = rand() % (MAX_SEARCH_FIELD_LEN - len + 1);
unsigned int row = (unsigned int)(rand() % maxRow); col = rand() % MAX_SEARCH_FIELD_LEN;
unsigned int col = (unsigned int)(rand() % maxCol); }
// Prüfen, ob das Wort platziert werden kann (Kollisionsprüfung)
int canPlace = 1; int canPlace = 1;
for (unsigned int k = 0; k < len; k++) { for (int k = 0; k < len; k++) {
char current = salad[row + k * dr][col + k * dc]; int r = (dir == 1) ? (row + k) : row;
char wordChar = (char)toupper((unsigned char)words[i][k]); int c = (dir == 0) ? (col + k) : col;
if (current != EMPTY_CHAR && current != wordChar) { // Feld muss noch leer sein oder exakt den gleichen Buchstaben enthalten
if (salad[r][c] != EMPTY_CHAR && salad[r][c] != words[i][k]) {
canPlace = 0; canPlace = 0;
break; break;
} }
} }
// Wenn die Position gültig ist, Buchstaben in das Spielfeld schreiben
if (canPlace) { if (canPlace) {
for (unsigned int k = 0; k < len; k++) { for (int k = 0; k < len; k++) {
salad[row + k * dr][col + k * dc] = (char)toupper((unsigned char)words[i][k]); int r = (dir == 1) ? (row + k) : row;
int c = (dir == 0) ? (col + k) : col;
salad[r][c] = words[i][k];
} }
placedWordsCount++; placedWordsCount++;
break; break; // Nächstes Wort verarbeiten
} }
} }
} }
// 3. Leere Felder mit Zufallsbuchstaben ('A'-'Z') füllen // 3. Verbleibende leere Felder (EMPTY_CHAR) mit Zufallsbuchstaben 'A'-'Z' auffüllen
for (unsigned int r = 0; r < gridSize; r++) { for (int r = 0; r < MAX_SEARCH_FIELD_LEN; r++) {
for (unsigned int c = 0; c < gridSize; c++) { for (int c = 0; c < MAX_SEARCH_FIELD_LEN; c++) {
if (salad[r][c] == EMPTY_CHAR) { if (salad[r][c] == EMPTY_CHAR) {
salad[r][c] = 'A' + (rand() % 26); salad[r][c] = 'A' + (rand() % 26);
} }