Compare commits

..

2 Commits

Author SHA1 Message Date
4ba1251b56
set up graphical game 2025-11-03 20:12:14 +01:00
cbf806ae1b
sync linux folder
Co-authored-by: Fabrice <fabrice.reintsch@gmail.com>
2025-11-03 20:05:08 +01:00
2 changed files with 66 additions and 29 deletions

View File

@ -13,47 +13,86 @@
// Creates the word salad by placing words randomly and filling empty spaces // 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) int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
{ {
// set seed for random number generator int row, col, placedWords=0;
srand(time(NULL));
int r, c;
//Gesamtes Feld auf 0 setzen //Gesamtes Feld auf 0 setzen
for (r=0; r < searchFieldLen; r++) { for (row=0; row < searchFieldLen; row++) {
for (c=0; c < searchFieldLen; c++) { for (col=0; col < searchFieldLen; col++) {
salad [r][c] = EMPTY_CHAR; salad [row][col] = EMPTY_CHAR;
} }
} }
//Wörter zufällig horizontal oder vertikal platzieren //Wortlänge wird für jedes Wort erfasst
enum Richtung {HORIZONTAL,VERTIKAL}; //0;1 for (int w=0; w< wordCount; w++) {
enum Richtung dir; const char *word = words[w];
dir = (rand() % 2 == 0) ? HORIZONTAL : VERTIKAL; int wordLen = strlen(word);
//Schleife, die das Setzen für alle Wörter durchführt, Zufällig horizontal(0) oder vertikal(1), an zufällige Stelle if (wordLen == 0 || wordLen > searchFieldLen) {
//Prüfen, ob das Wort von der Länge her ins Feld passt continue;
}
int placed = 0;
//Felder, in denen keine Wörter stehen, werden mit zufälligen Buchstaben befüllt for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) {
for (r=0; r < searchFieldLen; r++) { int dir = (rand() % 2); // 0 oder 1 zur Festlegung von vertikal oder horizontal
for (c=0; c < searchFieldLen; c++) {
if (salad[r][c] == EMPTY_CHAR) { row = 0;
salad [r][c] = 'A' + (rand()% 26); col = 0;
if (dir == 0) { //horizontal //Startpunkt so festlegen, dass Wort nicht aus Feld raus ragt
row = rand () % searchFieldLen;
col = rand () % (searchFieldLen - wordLen + 1);
} else { //vertikal
row = rand () % (searchFieldLen - wordLen + 1);
col = rand () % searchFieldLen;
}
int placeable = 1;
for (int i = 0; i < wordLen; i++) { //Felder ab Startpunkt werden durchgegangen und gecheckt, ob Felder auf Null sind:
// JA =>Wort wird im nächsten Schritt platziert, NEIN => Abbruch, Neuer Startpunkt wird festgelegt
if ((dir == 0 && salad[row][col + i] != EMPTY_CHAR) || (dir == 1 && salad[row + i][col] != EMPTY_CHAR)) {
placeable = 0;
break;
}
}
if (placeable) { //wenn es keine Überschneidungen gibt, wird Wort platziert
for (int i=0; i<wordLen; i++) {
if (dir == 0) {
salad [row][col+i] = word[i];
} else {
salad [row+i][col] = word[i];
}
}
placed = 1;
placedWords++;
}
}
}
//Felder, in denen keine Buchstaben stehen, werden mit zufälligen Buchstaben befüllt
for (row=0; row < searchFieldLen; row++) {
for (col=0; col < searchFieldLen; col++) {
if (salad[row][col] == EMPTY_CHAR) {
salad [row][col] = 'A' + (rand()% 26);
} }
} }
} }
//return "Anzahl platzierter Wörter", wird in Main dann mit "soll" verglichen
return 0; return placedWords;
} }
// Prints the word salad to console // Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{ {
int r, c; int row, col;
//Ausgabe des gesamten Feldes //Ausgabe des gesamten Feldes
for (r=0; r < searchFieldLen; r++) { for (row=0; row < searchFieldLen; row++) {
for (c=0; c < searchFieldLen; c++) { for (col=0; col < searchFieldLen; col++) {
printf("%c ", salad[r][c]); printf("%c ", salad[row][col]);
} }
printf("\n"); printf("\n");
} }

View File

@ -42,15 +42,13 @@ int main(int argc, char *argv[])
{ {
int notPlacedNum = wordCount - placedWords; int notPlacedNum = wordCount - placedWords;
fprintf(stderr, "%d word(s) couldn't be placed\n", notPlacedNum); fprintf(stderr, "%d word(s) couldn't be placed\n", notPlacedNum);
exitCode = EXIT_FAILURE;
} }
// TODO: // Start the game if all words were successfully placed
// correct parameters for startGame else
// Start the game if at least 1 word has been successfully placed
else if (placedWords >= 1)
{ {
startGame(wordSalad, 80, words, placedWords, 800); startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
} }
} }
else else