nutzlose Variable gelöscht

This commit is contained in:
Kristin 2025-11-04 10:33:36 +01:00
parent cbc48c3362
commit c288f6debf
8 changed files with 42 additions and 62 deletions

View File

@ -6,12 +6,6 @@
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
// 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]
@ -22,63 +16,59 @@ int createWordSalad(
enum Richtung { HORIZONTAL, VERTIKAL };
// wipe field
// array auf emptychar
for (int x = 0; x < searchFieldLen; x++) {
for (int y = 0; y < searchFieldLen; y++) {
salad[y][x] = EMPTY_CHAR;
salad[y][x] = EMPTY_CHAR; // Spielfeld auf EmptyChar setzen
}
}
unsigned int placedWords = 0; // variable platzierte Wörter
// place words
// for schleife mit wortzahl = 0 bis wordCount
// Wörter platzieren
// for schleife mit wortNummer = 0 bis wordCount - 1 (insgesamt wordCount)
for (int wortNummer = 0; wortNummer < wordCount; wortNummer++) {
// strlen mit Wortlänge
int wortLen = strlen(words[wortNummer]);
int platziert = 0;
int versuch = 0;
int platziert = 0; // variable, um platziertes Wort zu signalisieren
int versuch = 0; // veruche bis max rand tries
// rand % 2 vertikal oder horizontal
// rand % searchFieldLen um Ort zu generieren
// Länge überprüfen und überprüfen, ob wort sich überschneidet ist nicht
// randomtries
while (versuch < MAX_RAND_TRIES_PER_WORD &&
platziert == 0) { // wort platziert -> Schleife wird verlassen oder
// konnte nach 10 Versuchen nicht platziert werden
while (versuch < MAX_RAND_TRIES_PER_WORD && platziert == 0) {
enum Richtung r =
(rand() % 2) ? HORIZONTAL : VERTIKAL; // mit rand Richtung
enum Richtung r = (rand() % 2) ? HORIZONTAL : VERTIKAL;
int x = rand() % searchFieldLen;
int x = rand() % searchFieldLen; // mit rand Koordinaten bestimmen
int y = rand() % searchFieldLen;
int fits = 1;
int wortFrei = 1; // Wort passt ins Feld und überschneidet sich nicht
if (r == VERTIKAL) {
if ((y + wortLen) > (searchFieldLen)) {
fits = 0;
if ((y + wortLen) > (searchFieldLen)) { // Wortlänge größer als Feld
wortFrei = 0;
}
for (int i = 0; (i < wortLen) && (fits != 0); i++) {
for (int i = 0; (i < wortLen) && (wortFrei != 0); i++) {
char var = salad[y + i][x];
if (var != EMPTY_CHAR) {
fits = 0;
wortFrei = 0; // Wort überschneidet sich mit bereits platziertem
// Wort
}
}
if (fits != 0) {
if (wortFrei != 0) {
for (int i = 0; i < wortLen; i++) {
salad[y + i][x] = words[wortNummer][i];
salad[y + i][x] = words[wortNummer][i]; // Wort platzieren
}
placedWords++;
platziert = 1;
platziert = 1; // schleife wird verlassen
}
}
@ -86,17 +76,17 @@ int createWordSalad(
else if (r == HORIZONTAL) {
if ((x + wortLen) > searchFieldLen) {
fits = 0;
wortFrei = 0;
}
for (int i = 0; (i < wortLen) && (fits != 0); i++) {
for (int i = 0; (i < wortLen) && (wortFrei != 0); i++) {
char var = salad[y][x + i];
if (var != EMPTY_CHAR) {
fits = 0;
wortFrei = 0;
}
}
if (fits != 0) {
if (wortFrei != 0) {
for (int i = 0; i < wortLen; i++) {
@ -117,15 +107,13 @@ int createWordSalad(
if (salad[i][j] == EMPTY_CHAR) {
salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A';
salad[i][j] = rand() % (90 - 65 + 1) + 65;
}
}
}
return placedWords;
// emptychar rand() % ('Z' - 'A' + 1) + 'A'
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int searchFieldLen) {

Binary file not shown.

View File

@ -7,18 +7,19 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN],
unsigned int maxWordCount) {
if (file == NULL) {
perror("File couldn't be opened");
printf("File couldn't be opened...");
return 0;
}
char line[MAX_LINE_LEN];
unsigned int wordCount = 0;
char line[MAX_LINE_LEN]; // fehlerhaften String zwischenspeichern
unsigned int wordCount = 0; // eingelesene Wörter
while ((fgets(line, sizeof(line), file) != NULL) &&
(wordCount < maxWordCount)) {
(wordCount < maxWordCount)) { // gesamte Wörterliste einlesen
// Token initialisieren
char *token = strtok(line, " ,;.\n");
char *token = strtok(
line, " ,;.\n"); // Trennen der Wörter bei den angegebenen Zeichen
while (token && (wordCount < maxWordCount)) {
@ -27,16 +28,15 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN],
continue;
}
// Token in Großbuchstaben konvertieren
for (int i = 0; token[i] != '\0'; i++) {
token[i] = toupper(token[i]);
token[i] = toupper(token[i]); // Alles in Großbuchstaben
}
// In das words-Array kopieren
strcpy(words[wordCount], token);
strcpy(words[wordCount], token); // einzelnes Wort in words array
// speichern
wordCount++; // Nächstes Wort
token = strtok(NULL, " ,;.\n"); // Nächstes Token
token = strtok(NULL, " ,;.\n"); // Nächster Token
}
}

View File

@ -17,8 +17,8 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
exitCode = EXIT_FAILURE;
} else {
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN] = {
{0}}; // Array to hold the words to be used in the game
char words[MAX_NUMBER_OF_WORDS]
[MAX_WORD_LEN]; // Array to hold the words to be used in the game
unsigned int wordCount = 0;
FILE *file = fopen(argv[1], "r");
@ -41,14 +41,6 @@ int main(int argc, char *argv[]) {
// error message if some words couldn't be placed
printf("Placed Words: %d\n", placedWords);
printf("Word Count:%d\n", wordCount);
int var = 0;
for (unsigned int i = 0; i < wordCount; i++) {
printf("Word %u: %s ", i, words[i]);
if (var == 10) {
printf("\n");
var = 0;
}
}
if (placedWords == wordCount) {
@ -60,8 +52,7 @@ int main(int argc, char *argv[]) {
{
// Print error message if words couldn't be placed
// evtl noch richtigen error code einfügen
fprintf(stderr, "Could not place words...\n");
printf("Placed %d words of %d.", placedWords, wordCount);
exitCode = EXIT_FAILURE;
}
} else {

BIN
Start_Windows/runTests.exe Normal file

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +1,6 @@
Yeti,Nessie Werwolf; Vampir
Monster
Hydra;Frankenstein
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
Gespenst, Oger
Dracula;KingKong,xxxxxxxx
Max, Tobi, Kristin