Makefile überarbeitet; readWords() implementiert; createWordSalad implementiert; main.c fertiggestellt und unsere Namen zu words.txt hinzugefügt
This commit is contained in:
1 parent
f2e54529a4
commit
fb760d76fe
5 files changed
+111
-9
No files matched your search
@@ -1,5 +1,4 @@
|
||||
#include "game.h"
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -13,5 +12,77 @@
|
||||
// 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)
|
||||
{
|
||||
// 0. Grundlegende Validierung der Parameter (nur bei echten Fehlern -1)
|
||||
if (salad == NULL || words == NULL || searchFieldLen == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 1. Spielfeld mit EMPTY_CHAR (0) initialisieren
|
||||
for (unsigned int i = 0; i < searchFieldLen; i++) {
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++) {
|
||||
salad[i][j] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
int placedWordsCount = 0;
|
||||
|
||||
// 2. Jedes Wort der Liste einzeln im Spielfeld platzieren
|
||||
for (unsigned int w = 0; w < wordCount; w++) {
|
||||
const char *word = words[w];
|
||||
unsigned int len = strlen(word);
|
||||
|
||||
if (len == 0 || len > searchFieldLen) {
|
||||
continue; // Zu lange Wörter überspringen
|
||||
}
|
||||
|
||||
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++) {
|
||||
int direction = rand() % 2; // 0 = Horizontal, 1 = Vertikal
|
||||
|
||||
unsigned int startRow = 0;
|
||||
unsigned int startCol = 0;
|
||||
|
||||
if (direction == 0) { // Horizontal
|
||||
startRow = rand() % searchFieldLen;
|
||||
startCol = rand() % (searchFieldLen - len + 1);
|
||||
} else { // Vertikal
|
||||
startRow = rand() % (searchFieldLen - len + 1);
|
||||
startCol = rand() % searchFieldLen;
|
||||
}
|
||||
|
||||
// Kollisionsprüfung
|
||||
int collision = 0;
|
||||
for (unsigned int i = 0; i < len; i++) {
|
||||
unsigned int currRow = (direction == 0) ? startRow : (startRow + i);
|
||||
unsigned int currCol = (direction == 0) ? (startCol + i) : startCol;
|
||||
|
||||
char currentCell = salad[currRow][currCol];
|
||||
if (currentCell != EMPTY_CHAR && currentCell != word[i]) {
|
||||
collision = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn keine Kollision vorliegt, eintragen
|
||||
if (!collision) {
|
||||
for (unsigned int i = 0; i < len; i++) {
|
||||
unsigned int currRow = (direction == 0) ? startRow : (startRow + i);
|
||||
unsigned int currCol = (direction == 0) ? (startCol + i) : startCol;
|
||||
salad[currRow][currCol] = word[i];
|
||||
}
|
||||
placedWordsCount++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Übrige leere Felder mit zufälligen Buchstaben fü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 placedWordsCount;
|
||||
}
|
||||
Reference in new issue
Block a user