Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/kleinleinta105608/Informatik2-Praktikum-Wortsalat
This commit is contained in:
commit
9e53289322
3 files changed
+25
-43
No files matched your search
+23
-42
@@ -3,75 +3,56 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 1000
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
// TODO: Spiellogik implementieren:
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int wordCount, char words[][100], unsigned int gridSize)
|
||||||
/* * 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 searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
|
||||||
{
|
{
|
||||||
// 1. Spielfeld vollständig mit EMPTY_CHAR initialisieren
|
// 1. Komplettes Array sicherheitshalber mit EMPTY_CHAR leeren
|
||||||
for (int r = 0; r < MAX_SEARCH_FIELD_LEN; r++) {
|
memset(salad, EMPTY_CHAR, MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN);
|
||||||
for (int c = 0; c < MAX_SEARCH_FIELD_LEN; c++) {
|
|
||||||
salad[r][c] = EMPTY_CHAR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int placedWordsCount = 0;
|
int placedWordsCount = 0;
|
||||||
|
|
||||||
// 2. Jedes Wort aus der Wörterliste platzieren
|
// 2. Wörter platzieren
|
||||||
for (unsigned int i = 0; i < wordCount; i++) {
|
for (unsigned int i = 0; i < wordCount; i++) {
|
||||||
if (words[i] == NULL) continue;
|
|
||||||
|
|
||||||
int len = (int)strlen(words[i]);
|
int len = (int)strlen(words[i]);
|
||||||
if (len == 0 || len > MAX_SEARCH_FIELD_LEN) continue;
|
|
||||||
|
|
||||||
// Versuche bis zu MAX_RAND_TRIES_PER_WORD Mal, eine freie Position zu finden
|
// WICHTIG: Wenn das Wort länger ist als das eigentliche Spielfeld (gridSize), direkt überspringen!
|
||||||
|
if (len == 0 || len > gridSize) continue;
|
||||||
|
|
||||||
|
// Versuche 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++) {
|
||||||
// 0 = Horizontal, 1 = Vertikal
|
|
||||||
int dir = rand() % 2;
|
|
||||||
|
|
||||||
int row, col;
|
int is_vert = rand() % 2;
|
||||||
if (dir == 0) { // Horizontal
|
int dr = is_vert;
|
||||||
row = rand() % MAX_SEARCH_FIELD_LEN;
|
int dc = !is_vert;
|
||||||
col = rand() % (MAX_SEARCH_FIELD_LEN - len + 1);
|
|
||||||
} else { // Vertikal
|
// Zufällige Position INNERHALB der gridSize wählen (nicht mehr 100x100!)
|
||||||
row = rand() % (MAX_SEARCH_FIELD_LEN - len + 1);
|
int row = rand() % (gridSize - (is_vert ? len - 1 : 0));
|
||||||
col = rand() % MAX_SEARCH_FIELD_LEN;
|
int col = rand() % (gridSize - (is_vert ? 0 : len - 1));
|
||||||
}
|
|
||||||
|
|
||||||
// Prüfen, ob das Wort platziert werden kann (Kollisionsprüfung)
|
|
||||||
int canPlace = 1;
|
int canPlace = 1;
|
||||||
for (int k = 0; k < len; k++) {
|
for (int k = 0; k < len; k++) {
|
||||||
int r = (dir == 1) ? (row + k) : row;
|
char current = salad[row + k * dr][col + k * dc];
|
||||||
int c = (dir == 0) ? (col + k) : col;
|
if (current != EMPTY_CHAR && current != words[i][k]) {
|
||||||
|
|
||||||
// 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 (int k = 0; k < len; k++) {
|
for (int k = 0; k < len; k++) {
|
||||||
int r = (dir == 1) ? (row + k) : row;
|
salad[row + k * dr][col + k * dc] = words[i][k];
|
||||||
int c = (dir == 0) ? (col + k) : col;
|
|
||||||
salad[r][c] = words[i][k];
|
|
||||||
}
|
}
|
||||||
placedWordsCount++;
|
placedWordsCount++;
|
||||||
break; // Nächstes Wort verarbeiten
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Verbleibende leere Felder (EMPTY_CHAR) mit Zufallsbuchstaben 'A'-'Z' auffüllen
|
// 3. Verbleibende leere Felder (nur innerhalb der gridSize!) mit Zufallsbuchstaben auffüllen
|
||||||
for (int r = 0; r < MAX_SEARCH_FIELD_LEN; r++) {
|
for (unsigned int r = 0; r < gridSize; r++) {
|
||||||
for (int c = 0; c < MAX_SEARCH_FIELD_LEN; c++) {
|
for (unsigned int c = 0; c < gridSize; 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);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#define MAX_SEARCH_FIELD_LEN 100
|
#define MAX_SEARCH_FIELD_LEN 100
|
||||||
|
|
||||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
|
// 4. Parameter ist die gridSize!
|
||||||
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int wordCount, char words[][100], unsigned int gridSize);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user