generated from freudenreichan/info2Praktikum-Wortsalat
Merge remote-tracking branch 'origin/main'
# Conflicts: # Startcode/game.c
This commit is contained in:
commit
fdb0390b59
2 files changed
+84
-59
No files matched your search
+48
-58
@@ -11,67 +11,57 @@
|
||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||
|
||||
// Creates the word salad by placing words randomly and filling empty spaces
|
||||
|
||||
|
||||
// Erstellt den Wortsalat.
|
||||
// Die Wörter werden zufällig horizontal oder vertikal eingesetzt.
|
||||
// Freie Felder werden anschließend mit zufälligen Buchstaben gefüllt.
|
||||
|
||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||
int createWordSalad(
|
||||
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||
unsigned int searchFieldLen,
|
||||
const char words[][MAX_WORD_LEN],
|
||||
unsigned int wordCount)
|
||||
{
|
||||
unsigned int placedWords = 0;
|
||||
|
||||
/ 5. Vertikale Platzierung prüfen
|
||||
else {
|
||||
|
||||
if (row + wordLen > searchFieldLen) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (size_t j = 0; j < wordLen; j++) {
|
||||
|
||||
if (salad[row + j][col] != EMPTY_CHAR &&
|
||||
salad[row + j][col] != words[i][j]) {
|
||||
|
||||
canPlace = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Platzierung nicht möglich
|
||||
if (!canPlace) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 6. Wort tatsächlich platzieren
|
||||
for (size_t j = 0; j < wordLen; j++) {
|
||||
|
||||
if (direction == 0) {
|
||||
salad[row][col + j] = words[i][j];
|
||||
}
|
||||
else {
|
||||
salad[row + j][col] = words[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
// Erfolgreich platziertes Wort zählen
|
||||
placedWords++;
|
||||
|
||||
// Nächstes Wort bearbeiten
|
||||
break;
|
||||
}
|
||||
// 1. Spielfeld leeren
|
||||
for (unsigned int i = 0; i < searchFieldLen; i++) {
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++) {
|
||||
salad[i][j] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
// 7. Leere Felder mit Zufallsbuchstaben füllen
|
||||
for (unsigned int i = 0; i < searchFieldLen; i++) {
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++) {
|
||||
// 2. Alle Wörter durchlaufen
|
||||
for (unsigned int i = 0; i < wordCount; i++) {
|
||||
|
||||
if (salad[i][j] == EMPTY_CHAR) {
|
||||
salad[i][j] = 'A' + rand() % 26;
|
||||
}
|
||||
}
|
||||
}
|
||||
size_t wordLen = strlen(words[i]);
|
||||
|
||||
// 8. Anzahl erfolgreich platzierter Wörter zurückgeben
|
||||
return (int)placedWords;
|
||||
}
|
||||
// Leere oder zu lange Wörter überspringen
|
||||
if (wordLen == 0 || wordLen > searchFieldLen) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 3. Platzierungsversuche
|
||||
for (int attempt = 0;
|
||||
attempt < MAX_RAND_TRIES_PER_WORD;
|
||||
attempt++) {
|
||||
unsigned int row = rand() % searchFieldLen;
|
||||
unsigned int col = rand() % searchFieldLen;
|
||||
int direction = rand() % 2;
|
||||
int canPlace = 1;
|
||||
|
||||
// 4. Horizontale Platzierung prüfen
|
||||
if (direction == 0) {
|
||||
|
||||
if (col + wordLen > searchFieldLen) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (size_t j = 0; j < wordLen; j++) {
|
||||
|
||||
if (salad[row][col + j] != EMPTY_CHAR &&
|
||||
salad[row][col + j] != words[i][j]) {
|
||||
|
||||
canPlace = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 5. Vertikale Platzierung prüfen
|
||||
+36
-1
@@ -8,5 +8,40 @@
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
int c = 0;
|
||||
int count = 0;
|
||||
int letterCount = 0;
|
||||
while (count < maxWordCount)
|
||||
{
|
||||
while ((c = fgetc(file)) != EOF) //Leerzeichen prüfen
|
||||
{
|
||||
if (isspace(c) || !isalpha(c)) {
|
||||
if (letterCount > 0) {
|
||||
break; //Wort fertig eingelesen
|
||||
}
|
||||
|
||||
continue; // mehrere Leerzeichen oder Sonderzeichen überspringen
|
||||
}
|
||||
|
||||
if (letterCount < MAX_WORD_LEN - 1) //wörter Buchstabenweise einlesen
|
||||
{
|
||||
words[count][letterCount] = (char)toupper(c);
|
||||
letterCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (letterCount == 0) {
|
||||
break; //kein weiteres Wort gefunden
|
||||
}
|
||||
|
||||
words[count][letterCount] = '\0';
|
||||
letterCount = 0;
|
||||
count++;
|
||||
|
||||
if (c == EOF) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
}dhckhdkfhdjf
|
||||
Reference in new issue
Block a user