All tests passed
This commit is contained in:
parent
35d7a0822f
commit
a75c51c13a
177
game.c
177
game.c
@ -2,6 +2,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
@ -22,108 +23,124 @@
|
|||||||
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Funktion, die einen zufälligen Großbuchstaben generiert
|
// Funktion, die einen zufälligen Großbuchstaben (A–Z) erzeugt
|
||||||
char randomLetter()
|
char randomLetter()
|
||||||
{
|
{
|
||||||
return 'A' + rand() % 26;
|
return 'A' + rand() % 26;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hauptfunktion: Erstellt das Wortsalat-Spiel
|
// Erstellt das Wortsalat-Spielfeld und platziert die Wörter zufällig
|
||||||
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,
|
unsigned int searchFieldLen,
|
||||||
const char words[][MAX_WORD_LEN],
|
const char words[][MAX_WORD_LEN],
|
||||||
unsigned int wordCount)
|
unsigned int wordCount)
|
||||||
{
|
{
|
||||||
// 1. Leeres Spielfeld initialisieren
|
// 1. Spielfeld mit leeren Zeichen initialisieren
|
||||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||||
{
|
{
|
||||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||||
{
|
{
|
||||||
salad[i][j] = EMPTY_CHAR;
|
salad[i][j] = EMPTY_CHAR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
srand((unsigned int)time(NULL));
|
srand((unsigned int)time(NULL));
|
||||||
|
int placedWords = 0;
|
||||||
|
|
||||||
// 2. Jedes Wort zufällig im Spielfeld platzieren
|
// 2. Jedes Wort zufällig auf dem Spielfeld platzieren
|
||||||
for (unsigned int w = 0; w < wordCount; w++)
|
for (unsigned int w = 0; w < wordCount; w++)
|
||||||
{
|
{
|
||||||
const char *word = words[w];
|
char word[MAX_WORD_LEN];
|
||||||
size_t len = strlen(word);
|
strncpy(word, words[w], MAX_WORD_LEN);
|
||||||
if (len > searchFieldLen)
|
word[MAX_WORD_LEN - 1] = '\0';
|
||||||
continue; // Überspringen, falls das Wort zu lang ist
|
|
||||||
|
|
||||||
int placed = 0;
|
// Alle Buchstaben des Wortes in Großbuchstaben umwandeln
|
||||||
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
|
for (size_t k = 0; k < strlen(word); k++)
|
||||||
{
|
{
|
||||||
int dir = rand() % 2; // 0 = horizontal, 1 = vertikal
|
word[k] = (char)toupper((unsigned char)word[k]);
|
||||||
int row = rand() % searchFieldLen;
|
}
|
||||||
int col = rand() % searchFieldLen;
|
|
||||||
|
|
||||||
// Prüfen, ob das Wort an die Position passt
|
size_t len = strlen(word);
|
||||||
if (dir == 0 && col + len <= searchFieldLen)
|
if (len > searchFieldLen)
|
||||||
{
|
continue; // Wort ist zu lang → überspringen
|
||||||
// Prüfen, ob keine Überschneidungen mit anderen Buchstaben auftreten
|
|
||||||
int ok = 1;
|
int placed = 0;
|
||||||
for (size_t i = 0; i < len; i++)
|
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
|
||||||
|
{
|
||||||
|
int dir = rand() % 2; // 0 = horizontal, 1 = vertikal
|
||||||
|
int row = rand() % searchFieldLen;
|
||||||
|
int col = rand() % searchFieldLen;
|
||||||
|
|
||||||
|
if (dir == 0 && col + len <= searchFieldLen)
|
||||||
{
|
{
|
||||||
if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != word[i])
|
int ok = 1;
|
||||||
{
|
// Prüfen, ob der Platz frei ist oder die Buchstaben passen
|
||||||
ok = 0;
|
for (size_t i = 0; i < len; i++)
|
||||||
break;
|
{
|
||||||
}
|
if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != word[i])
|
||||||
|
{
|
||||||
|
ok = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ok)
|
||||||
|
{
|
||||||
|
// Wort horizontal einfügen
|
||||||
|
for (size_t i = 0; i < len; i++)
|
||||||
|
salad[row][col + i] = word[i];
|
||||||
|
placed = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (ok)
|
else if (dir == 1 && row + len <= searchFieldLen)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < len; i++)
|
int ok = 1;
|
||||||
salad[row][col + i] = word[i];
|
// Prüfen, ob der Platz frei ist oder die Buchstaben passen
|
||||||
placed = 1;
|
for (size_t i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != word[i])
|
||||||
|
{
|
||||||
|
ok = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ok)
|
||||||
|
{
|
||||||
|
// Wort vertikal einfügen
|
||||||
|
for (size_t i = 0; i < len; i++)
|
||||||
|
salad[row + i][col] = word[i];
|
||||||
|
placed = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (dir == 1 && row + len <= searchFieldLen)
|
|
||||||
{
|
|
||||||
int ok = 1;
|
|
||||||
for (size_t i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != word[i])
|
|
||||||
{
|
|
||||||
ok = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (ok)
|
|
||||||
{
|
|
||||||
for (size_t i = 0; i < len; i++)
|
|
||||||
salad[row + i][col] = word[i];
|
|
||||||
placed = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Leere Felder mit zufälligen Buchstaben füllen
|
if (placed)
|
||||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
placedWords++; // Anzahl der erfolgreich platzierten Wörter erhöhen
|
||||||
{
|
}
|
||||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
|
||||||
{
|
|
||||||
if (salad[i][j] == EMPTY_CHAR)
|
|
||||||
salad[i][j] = randomLetter();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0; // 0 = erfolgreich
|
// 3. Alle leeren 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] = randomLetter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rückgabewert: Anzahl der platzierten Wörter
|
||||||
|
return placedWords;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Funktion zur Anzeige des Spielfelds in der Konsole
|
// Gibt das Spielfeld in der Konsole aus
|
||||||
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],
|
||||||
unsigned int searchFieldLen)
|
unsigned int searchFieldLen)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||||
{
|
{
|
||||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||||
{
|
{
|
||||||
printf("%c ", salad[i][j]);
|
printf("%c ", salad[i][j]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
54
input.c
54
input.c
@ -23,35 +23,41 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsigned int count = 0;
|
unsigned int count = 0;
|
||||||
char line[MAX_WORD_LEN + 5]; // etwas Puffer, um Überlauf zu vermeiden
|
char buffer[512];
|
||||||
|
|
||||||
// Zeilen einzeln lesen, bis EOF erreicht oder das Limit erreicht ist
|
// Ganze Datei zeilenweise lesen
|
||||||
while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount)
|
while (fgets(buffer, sizeof(buffer), file) != NULL && count < maxWordCount)
|
||||||
{
|
{
|
||||||
// Zeilenumbruch '\n' oder '\r\n' entfernen
|
// Entfernt Zeilenumbruch '\n' oder '\r\n'
|
||||||
line[strcspn(line, "\r\n")] = '\0';
|
buffer[strcspn(buffer, "\r\n")] = '\0';
|
||||||
|
|
||||||
// Leere Zeilen überspringen
|
// Zerlegt die Zeile in Wörter mit den Trennzeichen: Leerzeichen, Komma, Semikolon
|
||||||
if (strlen(line) == 0)
|
char *token = strtok(buffer, " ,;");
|
||||||
continue;
|
while (token != NULL && count < maxWordCount)
|
||||||
|
|
||||||
// Leerzeichen am Anfang und Ende entfernen
|
|
||||||
char *start = line;
|
|
||||||
while (isspace((unsigned char)*start))
|
|
||||||
start++;
|
|
||||||
|
|
||||||
char *end = start + strlen(start) - 1;
|
|
||||||
while (end > start && isspace((unsigned char)*end))
|
|
||||||
{
|
{
|
||||||
*end = '\0';
|
// Leerzeichen am Anfang und Ende entfernen
|
||||||
end--;
|
while (isspace((unsigned char)*token))
|
||||||
|
token++;
|
||||||
|
char *end = token + strlen(token) - 1;
|
||||||
|
while (end > token && isspace((unsigned char)*end))
|
||||||
|
{
|
||||||
|
*end = '\0';
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// In Großbuchstaben umwandeln
|
||||||
|
for (unsigned int i = 0; i < strlen(token); i++)
|
||||||
|
{
|
||||||
|
token[i] = (char)toupper((unsigned char)token[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// In das Array 'words' kopieren
|
||||||
|
strncpy(words[count], token, MAX_WORD_LEN - 1);
|
||||||
|
words[count][MAX_WORD_LEN - 1] = '\0';
|
||||||
|
|
||||||
|
count++;
|
||||||
|
token = strtok(NULL, " ,;");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sichere Kopie in das Array 'words'
|
|
||||||
strncpy(words[count], start, MAX_WORD_LEN - 1);
|
|
||||||
words[count][MAX_WORD_LEN - 1] = '\0'; // Nullterminierung sicherstellen
|
|
||||||
|
|
||||||
count++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
|
|||||||
BIN
runTests.exe
BIN
runTests.exe
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user