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 MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #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( int createWordSalad(
char salad[MAX_SEARCH_FIELD_LEN] char salad[MAX_SEARCH_FIELD_LEN]
@ -22,63 +16,59 @@ int createWordSalad(
enum Richtung { HORIZONTAL, VERTIKAL }; enum Richtung { HORIZONTAL, VERTIKAL };
// wipe field
// array auf emptychar
for (int x = 0; x < searchFieldLen; x++) { for (int x = 0; x < searchFieldLen; x++) {
for (int y = 0; y < searchFieldLen; y++) { 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 unsigned int placedWords = 0; // variable platzierte Wörter
// place words // Wörter platzieren
// for schleife mit wortzahl = 0 bis wordCount // for schleife mit wortNummer = 0 bis wordCount - 1 (insgesamt wordCount)
for (int wortNummer = 0; wortNummer < wordCount; wortNummer++) { for (int wortNummer = 0; wortNummer < wordCount; wortNummer++) {
// strlen mit Wortlänge // strlen mit Wortlänge
int wortLen = strlen(words[wortNummer]); int wortLen = strlen(words[wortNummer]);
int platziert = 0; int platziert = 0; // variable, um platziertes Wort zu signalisieren
int versuch = 0; int versuch = 0; // veruche bis max rand tries
// rand % 2 vertikal oder horizontal while (versuch < MAX_RAND_TRIES_PER_WORD &&
// rand % searchFieldLen um Ort zu generieren platziert == 0) { // wort platziert -> Schleife wird verlassen oder
// Länge überprüfen und überprüfen, ob wort sich überschneidet ist nicht // konnte nach 10 Versuchen nicht platziert werden
// randomtries
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; // mit rand Koordinaten bestimmen
int x = rand() % searchFieldLen;
int y = rand() % searchFieldLen; int y = rand() % searchFieldLen;
int fits = 1; int wortFrei = 1; // Wort passt ins Feld und überschneidet sich nicht
if (r == VERTIKAL) { if (r == VERTIKAL) {
if ((y + wortLen) > (searchFieldLen)) { if ((y + wortLen) > (searchFieldLen)) { // Wortlänge größer als Feld
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 + i][x]; char var = salad[y + i][x];
if (var != EMPTY_CHAR) { 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++) { for (int i = 0; i < wortLen; i++) {
salad[y + i][x] = words[wortNummer][i]; salad[y + i][x] = words[wortNummer][i]; // Wort platzieren
} }
placedWords++; placedWords++;
platziert = 1; platziert = 1; // schleife wird verlassen
} }
} }
@ -86,17 +76,17 @@ int createWordSalad(
else if (r == HORIZONTAL) { else if (r == HORIZONTAL) {
if ((x + wortLen) > searchFieldLen) { 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]; char var = salad[y][x + i];
if (var != EMPTY_CHAR) { if (var != EMPTY_CHAR) {
fits = 0; wortFrei = 0;
} }
} }
if (fits != 0) { if (wortFrei != 0) {
for (int i = 0; i < wortLen; i++) { for (int i = 0; i < wortLen; i++) {
@ -117,15 +107,13 @@ int createWordSalad(
if (salad[i][j] == EMPTY_CHAR) { if (salad[i][j] == EMPTY_CHAR) {
salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A'; salad[i][j] = rand() % (90 - 65 + 1) + 65;
} }
} }
} }
return placedWords; 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], void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int searchFieldLen) { 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) { unsigned int maxWordCount) {
if (file == NULL) { if (file == NULL) {
perror("File couldn't be opened"); printf("File couldn't be opened...");
return 0; return 0;
} }
char line[MAX_LINE_LEN]; char line[MAX_LINE_LEN]; // fehlerhaften String zwischenspeichern
unsigned int wordCount = 0; unsigned int wordCount = 0; // eingelesene Wörter
while ((fgets(line, sizeof(line), file) != NULL) && while ((fgets(line, sizeof(line), file) != NULL) &&
(wordCount < maxWordCount)) { (wordCount < maxWordCount)) { // gesamte Wörterliste einlesen
// Token initialisieren // Token initialisieren
char *token = strtok(line, " ,;.\n"); char *token = strtok(
line, " ,;.\n"); // Trennen der Wörter bei den angegebenen Zeichen
while (token && (wordCount < maxWordCount)) { while (token && (wordCount < maxWordCount)) {
@ -27,16 +28,15 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN],
continue; continue;
} }
// Token in Großbuchstaben konvertieren
for (int i = 0; token[i] != '\0'; i++) { 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); // einzelnes Wort in words array
strcpy(words[wordCount], token); // speichern
wordCount++; // Nächstes Wort 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]); fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
exitCode = EXIT_FAILURE; exitCode = EXIT_FAILURE;
} else { } else {
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN] = { char words[MAX_NUMBER_OF_WORDS]
{0}}; // Array to hold the words to be used in the game [MAX_WORD_LEN]; // Array to hold the words to be used in the game
unsigned int wordCount = 0; unsigned int wordCount = 0;
FILE *file = fopen(argv[1], "r"); 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 // error message if some words couldn't be placed
printf("Placed Words: %d\n", placedWords); printf("Placed Words: %d\n", placedWords);
printf("Word Count:%d\n", wordCount); 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) { if (placedWords == wordCount) {
@ -60,8 +52,7 @@ int main(int argc, char *argv[]) {
{ {
// Print error message if words couldn't be placed // Print error message if words couldn't be placed
// evtl noch richtigen error code einfügen printf("Placed %d words of %d.", placedWords, wordCount);
fprintf(stderr, "Could not place words...\n");
exitCode = EXIT_FAILURE; exitCode = EXIT_FAILURE;
} }
} else { } 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 Yeti,Nessie Werwolf; Vampir
Monster Monster
Hydra;Frankenstein Hydra;Frankenstein
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist Dracula;KingKong,xxxxxxxx
Gespenst, Oger Max, Tobi, Kristin