fertige debuggte Version

This commit is contained in:
Selina Buhlheller 2025-11-06 11:41:19 +01:00
parent 765879d085
commit b4cc13d96a
6 changed files with 52 additions and 38 deletions

View File

@ -2,6 +2,7 @@
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
@ -13,96 +14,107 @@
// 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)
{
//prüfen, ob Wörter vorhanden sind mit isalpha
srand(time(NULL));
int placed_words = 0;
// Array mit 0 füllen
for (int r = 0; r < searchFieldLen; r++){
for(int s = 0; s < searchFieldLen; s++){
salad[r][s] = '.';
}
}
// -> erst wenn die ganze Länge des Wortes frei ist, array hinzufügen
// MAX Versuche das Wort zu platzieren == 10
// isalpha == 0 -> kein Buchstabe
// Wörter in salad einsortieren
for (int i = 0; i < wordCount; i++){
char einzelwort[] = words[i]
int einzelwort_laenge = strlen(einzelwort);
int laenge = strlen(words[i]);
// muss noch prüfen, ob in der Zeile schon was ist
// 1 ist horizontal, 0 ist vertikal
// frei == 1 -> Zeile nicht frei
// isalpha == 0 -> kein Buchstabe
for(int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++){
int richtung = rand()% 2;
int frei = 0;
// horizontale Eingabe
if (richtung == 1){
int zeile = rand()% MAX_SEARCH_FIELD_LEN;
int spalte = rand()% MAX_SEARCH_FIELD_LEN- einzelwort_laenge + 1;
int zeile = rand()% searchFieldLen;
int spalte = rand()% (searchFieldLen- laenge + 1);
//prüft, ob die herausgesuchte Zeile noch frei ist
for (int o = spalte; o < spalte + einzelwort_laenge; o++){
if(isalpha(salad[zeile][o]) == 0){
for (int o = spalte; o < spalte + laenge; o++){
if(salad[zeile][o] != '.'){
frei = 1;
break;
}
}
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
if (frei = 0){
for(int k = 0; k < einzelwort_laenge; k++){
salad[zeile][spalte + k] = einzelwort[k];
versuch = MAX_RAND_TRIES_PER_WORD;
if (frei == 0){
for(int k = 0; k < laenge; k++){
salad[zeile][spalte + k] = words[i][k];
}
placed_words++;
break;
}
}
// vertikale Eingabe
else if (richtung == 0){
int zeile = rand()% MAX_SEARCH_FIELD_LEN - einzelwort_laenge + 1;
int spalte = rand()% MAX_SEARCH_FIELD_LEN;
int zeile = rand()% (searchFieldLen - laenge + 1);
int spalte = rand()% searchFieldLen;
//prüft, ob die herausgesuchte Zeile noch frei ist
for (int n = zeile; n < zeile + einzelwort_laenge; n++){
if(isalpha(salad[n][spalte]) == 0){
for (int n = zeile; n < zeile + laenge; n++){
if(salad[n][spalte] != '.'){
frei = 1;
break;
}
}
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
if (frei = 0){
for(int j = 0; j < einzelwort_laenge; j++){
salad[zeile + j][spalte] = einzelwort[j];
versuch = MAX_RAND_TRIES_PER_WORD;
if (frei == 0){
for(int j = 0; j < laenge; j++){
salad[zeile + j][spalte] = words[i][j];
}
placed_words++;
break;
}
}
// gibt über searchFieldLen zurück, dass ein Wort nicht einsortiert wurde
if(versuch = MAX_RAND_TRIES_PER_WORD-1){
searchFieldLen = 1;
}
}
}
// fügt zufällige Buchstaben ein
for(int l = 0; l < MAX_SEARCH_FIELD_LEN; l++){
for(int m = 0; m < MAX_SEARCH_FIELD_LEN; m++ ){
for(int l = 0; l < searchFieldLen; l++){
for(int m = 0; m < searchFieldLen; m++ ){
if(isalpha(salad[l][m]) == 0 ){
// zufällige Buchstaben erzeugen
srand(time(NULL));
char alphabet [] = "abcdefghijklmnopqrstuvwxyz";
int laenge = strlen(alphabet);
int zufallszahl = rand()% laenge;
char zufallsbuchstabe = alphabet[zufallszahl];
// zufällige Buchstaben einfügen
salad[m][n] == zufallsbuchstabe;
salad[l][m] = zufallsbuchstabe;
}
}
}
return searchFieldLen
return placed_words;
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
for(int p = 0; p < MAX_SEARCH_FIELD_LEN; p++){
for(int q = 0; q < MAX_SEARCH_FIELD_LEN; q++){
printf("%s", salad[p][q]);
}
for(int p = 0; p < searchFieldLen; p++){
for(int q = 0; q < searchFieldLen; q++){
printf("%c", salad[p][q]);
}
printf("\n");
}
}

BIN
Start_Windows/game.o Normal file

Binary file not shown.

Binary file not shown.

View File

@ -40,12 +40,12 @@ int main(int argc, char *argv[])
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
int placed = showWordSalad(salad, searchFieldLen)
if ( placed != 1){
showWordSalad(salad, searchFieldLen);
if (placedWords == wordCount){
showWordSalad(wordSalad, placedWords);
}
else{
printf("Error. The words couldn't be placed.");
printf("Error. The words couldn't be placed.\n");
return -1;
}

Binary file not shown.

View File

@ -44,3 +44,5 @@ test: input.o game.o unit_tests.c
clean:
del /f *.o *.exe
wordsalad_myversion: main.o input.o game.o graphicalGame.o $(BINARIES)/libwordsalad.a
$(CC) $(CFLAGS) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libwordsalad.a $(LDFLAGS)