Compare commits

...
2 Commits
Author SHA1 Message Date
poppni99154 bcc2fc3883 habe game und main eingefügt 2025-11-09 17:51:45 +01:00
poppni99154 3060c9f3dc Input wurde geändert mit passenden Erklärungen 2025-11-09 16:14:04 +01:00
3 changed files with 176 additions and 8 deletions

No files matched your search

+115 -5
View File
@@ -2,6 +2,7 @@
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
@@ -10,14 +11,123 @@
/* * 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][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
// einmalige Initialisierung des RNG
static void initRandom(void)
{
static int seeded = 0;
if (!seeded)
{
srand((unsigned int)time(NULL));
seeded = 1;
}
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
static int placeWordHorizontal(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int fieldLen,
const char *word)
{
unsigned int wordLen = (unsigned int)strlen(word);
if (wordLen > fieldLen)
return 0;
// zufällige Zeile, passende Startspalte
unsigned int row = (unsigned int)(rand() % fieldLen);
unsigned int maxStartCol = fieldLen - wordLen;
unsigned int col = (unsigned int)(rand() % (maxStartCol + 1));
// Kollisionen prüfen
for (unsigned int i = 0; i < wordLen; i++)
{
if (salad[row][col + i] != EMPTY_CHAR)
return 0;
}
// setzen
for (unsigned int i = 0; i < wordLen; i++)
salad[row][col + i] = word[i];
return 1;
}
static int placeWordVertical(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int fieldLen,
const char *word)
{
unsigned int wordLen = (unsigned int)strlen(word);
if (wordLen > fieldLen)
return 0;
// zufällige Spalte, passende Startzeile
unsigned int col = (unsigned int)(rand() % fieldLen);
unsigned int maxStartRow = fieldLen - wordLen;
unsigned int row = (unsigned int)(rand() % (maxStartRow + 1));
// Kollisionen prüfen
for (unsigned int i = 0; i < wordLen; i++)
{
if (salad[row + i][col] != EMPTY_CHAR)
return 0;
}
// setzen
for (unsigned int i = 0; i < wordLen; i++)
salad[row + i][col] = word[i];
return 1;
}
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int searchFieldLen,
const char words[][MAX_WORD_LEN],
unsigned int wordCount)
{
initRandom();
// 1) Feld leeren
for (unsigned int r = 0; r < searchFieldLen; r++)
for (unsigned int c = 0; c < searchFieldLen; c++)
salad[r][c] = EMPTY_CHAR;
unsigned int placedWords = 0;
// 2) Wörter platzieren (horizontal oder vertikal, max. 10 Versuche/Wort)
for (unsigned int w = 0; w < wordCount; w++)
{
const char *word = words[w];
int placed = 0;
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++)
{
int dir = rand() % 2; // 0: horizontal, 1: vertikal
if (dir == 0)
placed = placeWordHorizontal(salad, searchFieldLen, word);
else
placed = placeWordVertical(salad, searchFieldLen, word);
}
if (placed)
placedWords++;
}
// 3) restliche Felder mit A–Z füllen
for (unsigned int r = 0; r < searchFieldLen; r++)
for (unsigned int c = 0; c < searchFieldLen; c++)
if (salad[r][c] == EMPTY_CHAR)
salad[r][c] = (char)('A' + (rand() % 26));
return (int)placedWords;
}
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int searchFieldLen)
{
for (unsigned int r = 0; r < searchFieldLen; r++)
{
for (unsigned int c = 0; c < searchFieldLen; c++)
{
putchar(salad[r][c]);
putchar(' ');
}
putchar('\n');
}
}
+48 -3
View File
@@ -1,13 +1,58 @@
#include "input.h"
#include <string.h>
#include <ctype.h>
#include <stdio.h> //fürs öffnen von Textdateien
// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
//test ob es funktioniert
if (0 == maxWordCount){printf("hello")}
}
FILE *quelle; //Zeiger auf quelle
quelle = fopen("words.txt", "r");
if (quelle ==0) { //Dateiöffnen geklappt?
printf("Could not open file words.txt\n");
return -1;
}
char speicher [MAX_LINE_LEN]; //speichern einer Zeile
int anzahlwörter =0;
if (fgets(speicher, sizeof(speicher), quelle)==0) {
return 0; //falls nichts gelesen wird
}
char *token = strtok(speicher, "\t\n,;. ");
while (token != NULL && anzahlwörter < maxWordCount) {
strncpy(words[anzahlwörter], token, MAX_WORD_LEN-1); //kopiert wort von ab Zeiger *token in Zielarray words
words[anzahlwörter][MAX_WORD_LEN-1] = '\0'; //Sicherheit, dass es auch ein String ist
for (int i =0; words [anzahlwörter][i] =='\0' ;i++) { //alle Buchstaben im array words groß machen für optik
words [anzahlwörter][i] = toupper((unsigned char)words[anzahlwörter][i]);
}
anzahlwörter ++;
token = strtok(NULL, "\t\n,;. "); //NULL, damit nicht mehr Anfangsadresse, sucht das nächste Wort in der Zeile
}
fclose(quelle);
return anzahlwörter; //wie viele Wörter wurden gespeichert im Array words
}
//fgets liest eine ganze zeile ein und speicher alles im Array puffer/speicher.
//Dann implementieren wir einen Zeiger, der auf die Funktion strtok zeigt,
//die einen zeiger auf den ersten Buchstaben eines Worts im puffer zurückgibt.
//Dann kopieren wir alles in das Array words [Count]
+13
View File
@@ -6,6 +6,7 @@
#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20
#define WINDOW_SIZE 800 // Fenstergröße
int main(int argc, char *argv[])
{
@@ -41,6 +42,18 @@ int main(int argc, char *argv[])
// Start the game if successful
// error message if some words couldn't be placed
if (placedWords == wordCount && wordCount > 0)
{
startGame(wordSalad, SALAD_SIZE, words, wordCount, WINDOW_SIZE);
}
else
{
// error message if some words couldn't be placed
fprintf(stderr,
"Es konnten nicht alle Worter plaziert werden (%u von %u placed).\n",
placedWords, wordCount);
exitCode = EXIT_FAILURE;
}
}
else
{