Compare commits

17 Commits
Author SHA1 Message Date
Björn 0c086b9607 input verbessert 2025-10-26 18:41:44 +01:00
Björn b88cf94072 miau 2025-10-26 18:30:29 +01:00
Björn 5162e9890d input überarbeitet 2025-10-26 16:59:21 +01:00
Björn fbef3806db placedwwordCount gefixt 2025-10-26 16:20:41 +01:00
Björn 8844495411 placedWordCounter eingefügt 2025-10-26 16:17:28 +01:00
Björn 288693c76f Startet Game oder gibt Fehlermeldung 2025-10-26 15:45:09 +01:00
Björn b1d7cbc74b Wortcheck mit Game-Start integriert und Fehlerausgabe hinzugefügt 2025-10-26 15:21:03 +01:00
Bastian 207d93b317 Alle Wörter werden erfolgreich in den Wortsalat gepackt und der Salat kann über die Console ausgegeben werden. Es müssen nur noch alle leeren '#' Felder mit zufälligen Buchstaben gefüllt werden. Aber das wird am ende Gemacht wenn alles passt um einfacher zu schauen was klappt 2025-10-25 18:14:53 +02:00
Bastian cf584df1b6 Wörter werden in Gitter eingefügt und Gitter ist auf console ausgebbar 2025-10-25 15:38:37 +02:00
Bastian c5501c1bd3 start der createWordSalat funktion 2025-10-25 14:18:34 +02:00
Bastian 3c68ced8ab Fileöffner verbessert 2025-10-23 11:24:50 +02:00
Björn eb15c18771 ToDo 2D char Array 2025-10-22 18:11:46 +02:00
Björn 02b4cb3b9b Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/turtschinba100320/info2Praktikum-WortsalatBasti 2025-10-22 17:59:52 +02:00
turtschinba100320 83de6ef49e merge upstream 2025-10-22 15:59:23 +00:00
Björn 69a78a0776 test 2025-10-22 17:37:45 +02:00
Bastian 3c89c51854 Pointer auf Textdokument erzeugt 2025-10-22 15:07:46 +02:00
Bastian~ 684ed5e821 Test 2025-10-14 15:07:46 +02:00
7 changed files with 139 additions and 11 deletions
+1 -1
View File
@@ -40,7 +40,7 @@ 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
printf("HAllo");
}
else
{
Binary file not shown.
+81 -1
View File
@@ -2,6 +2,8 @@
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
@@ -10,14 +12,92 @@
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
int checkforOverlap(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], short x, short y, short richtung, const char wort[])
{
for(size_t i = 0 ; i < strlen(wort); i++ )
{
short xoffset = (richtung) ? i : 0;
short yoffset = (richtung) ? 0 : i;
if (!(salad[x+xoffset][y+yoffset]=='#' || salad[x+xoffset][y+yoffset]==wort[i]))
{
return(-1);
}
}
return(1);
}
// 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)
{
unsigned int placedCount = 0;
for(short i = 0; i < searchFieldLen; i++)
{
for(short j = 0; j < searchFieldLen; j++)
{
salad[i][j] = '#';
}
}
srand(time(NULL));
for(short i = wordCount;i>0;i--)
{
size_t platzbedarf = strlen(words[i-1]);
if (platzbedarf > searchFieldLen)
{
printf("%s konnte nicht eingefuegt werden da es groesser als das Feld ist\n",words[i-1]);
continue;
}
short x,y,waagrecht;
int zuoft = enoughTries(searchFieldLen,0.99);
int versuche = 0;
do
{
versuche++;
short max = searchFieldLen - platzbedarf;
short position1 = rand() % (max +1);
short position2 = rand() % (searchFieldLen);
waagrecht = rand()%2;
x = (waagrecht) ? position1:position2;
y = (waagrecht) ? position2:position1;
}while(checkforOverlap(salad,x,y,waagrecht,words[i-1])!=1 && versuche<=zuoft);
if(versuche > zuoft)
{
printf("%s passt nicht mehr ins Gitter\n",words[i-1]);
continue;
}
for(size_t k = 0 ; k < strlen(words[i-1]); k++ )
{
short xoffset = (waagrecht) ? k : 0;
short yoffset = (waagrecht) ? 0 : k;
salad[x+xoffset][y+yoffset]=words[i-1][k];
}
placedCount++;
}
return placedCount;
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
for (unsigned int y = 0; y < searchFieldLen; y++) {
for (unsigned int x = 0; x < searchFieldLen; x++) {
printf("%c ", salad[x][y]); // oder printf("%c", salad[x][y]); ohne Leerzeichen
}
printf("\n"); // neue Zeile nach jeder Zeile
}
}
int enoughTries(double feldgroesse, double sicherheit) {
double moeglichkeiten = feldgroesse * feldgroesse; // Anzahl der Möglichkeiten
double c = -log(-log(sicherheit)); // Zusatzterm aus Zielwahrscheinlichkeit
double versuche = moeglichkeiten * (log(moeglichkeiten) + c); // Coupon-Collector-Formel
return (int)ceil(versuche);
}
+2 -2
View File
@@ -4,8 +4,8 @@
#include "input.h"
#define MAX_SEARCH_FIELD_LEN 100
int checkforOverlap(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], short x, short y, short richtung, const char wort[]);
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
int enoughTries(double feldgroesse, double sicherheit);
#endif
+24
View File
@@ -8,5 +8,29 @@
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
char line[1024]; // Puffer für eine Zeile
unsigned int count = 0;
// Alle gewünschten Trennzeichen
const char *delimiters = " \t\n,.;:!?\"'()[]{}";
while (fgets(line, sizeof(line), file) && count < maxWordCount) {
char *token = strtok(line, delimiters);
while (token != NULL && count < maxWordCount) {
// Alles in Großbuchstaben umwandeln
for (int i = 0; token[i]; i++) {
token[i] = toupper(token[i]);
}
strncpy(words[count], token, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0'; // Nullterminierung
count++;
token = strtok(NULL, delimiters);
}
}
fclose(file);
return count;
}
+30 -2
View File
@@ -9,7 +9,24 @@
int main(int argc, char *argv[])
{
int exitCode = EXIT_SUCCESS;
/*char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
char words[MAX_NUMBER_OF_WORDS][100] =
{
"TESTN",
"KATZE",
"ENTE",
"BEERE",
"FEE",
"KLEE",
"Feheler",
"SEE",
};
int wordCount = 8;
createWordSalad(salad,5, words,wordCount);
showWordSalad(salad,5);
*/
int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided
if(argc != 2)
@@ -39,7 +56,17 @@ int main(int argc, char *argv[])
// TODO:
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
// error message if some words couldn't be placed
if(placedWords == wordCount)
{
// Start the game with the created word salad
startGame(wordSalad, SALAD_SIZE, words, wordCount, placedWords);
}
else
{
fprintf(stderr, "Could not place all words into the word salad. Placed %u out of %u words.\n", placedWords, wordCount);
exitCode = EXIT_FAILURE;
}
}
else
@@ -51,4 +78,5 @@ int main(int argc, char *argv[])
}
return exitCode;
}
+1 -5
View File
@@ -1,5 +1 @@
Yeti,Nessie Werwolf; Vampir
Monster
Hydra;Frankenstein
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
Gespenst, Oger
test