Compare commits

...
6 Commits
Author SHA1 Message Date
moedlsi99502 ba8df4c6c8 noch bessere version 2025-11-06 00:03:29 +01:00
moedlsi99502 78f631fefb bessere version 2025-11-05 23:04:19 +01:00
moedlsi99502 992119f42c gute version 2025-11-05 18:19:15 +01:00
lewedeian98923 629f5ed15e fertog 2025-11-04 16:43:46 +01:00
lewedeian98923 1f22de8f99 Merge remote-tracking branch 'origin/main' 2025-11-04 16:28:39 +01:00
herzogfl100239 e355223d7c Meine Änderungen an game.c 2025-11-04 15:17:56 +01:00
12 changed files with 435 additions and 90 deletions

No files matched your search

+207 -7
View File
@@ -4,20 +4,220 @@
#include <string.h> #include <string.h>
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR '.'
//TODO: Spiellogik implementieren:
// TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */ * 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)
static void initializeArray(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) //Array initialisieren & mit Filler füllen
{ {
for (int i = 0; i < searchFieldLen; i++)
{
for (int j = 0; j < searchFieldLen; j++)
{
salad[i][j] = EMPTY_CHAR;
}
}
}
static void col_row (unsigned int searchFieldLen, int *x, int *y) //Zufällige x/y Koordinate für Anfang von Wort
{
*x = rand() % searchFieldLen;
*y = rand() % searchFieldLen;
}
static int place(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], int searchFieldLen, int len [],const char words [] [MAX_WORD_LEN], unsigned int wordCount) //Prüfen ob plazierbar und plazieren
{
int x, y;
int placedWords = 0; //Counter plazierte Wörter
for(int i = 0; i < wordCount; i++)
{
int placed = 0;
for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++)
{
len[i] = (int)strlen(words[i]);
col_row(searchFieldLen, &x, &y);
int orient = rand() % 2;
if(orient == 0) //wir prüfen und füllen horizontal (in x-Richtung)
{
int works = 1;
if(((x + len[i]) > searchFieldLen))
{
works = 0;
}
else
{
for(int j = x; j < (x + len[i]); j++)
{
if(salad[y][j] != EMPTY_CHAR)
{
works = 0;
break;
}
}
}
// ab hier platzieren wir
if(works)
{
for (int k = 0; k < len[i]; k++)
{
salad[y][x + k] = words[i][k];
}
placed = 1;
placedWords++;
}
}
else //wir prüfen und füllen vertikal (in y-Richtung)
{
int works = 1;
if(((y + len[i]) > searchFieldLen))
{
works = 0;
}
else
{
for(int j = y; j < (y + len[i]); j++)
{
if(salad[j][x] != EMPTY_CHAR)
{
works = 0;
break;
}
}
}
// ab hier platzieren wir
if(works)
{
for (int k = 0; k < len[i]; k++)
{
salad[y + k][x] = words[i][k];
}
placed = 1;
placedWords++;
}
}
}
}
return placedWords;
} }
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) static void fillLetters(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) //Übrige freie Felder mit Buchstaben füllen
{ {
for (int i = 0; i < searchFieldLen; i++)
{
for (int j = 0; j < searchFieldLen; j++)
{
if (salad[i][j] == EMPTY_CHAR)
{
char random_letter = 'A' + (rand() % 26);
salad[i][j] = random_letter;
}
}
}
}
//Erstellt Wortsalat, plaziert Wörter zufällig & füllt die Lücken
int createWordSalad(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words [] [MAX_WORD_LEN], unsigned int wordCount)
{
srand(time(NULL));
initializeArray(salad, searchFieldLen);
int len [wordCount];
int count = 0;
for(int i = 0; i < wordCount; i++)
{
if(words [i] [0] != '\0')
count++;
}
int placeWords = place(salad, searchFieldLen, len, words, count);
fillLetters(salad, searchFieldLen);
return placeWords;
}
// Ausgabe von Wortsalat auf Konsole
void showWordSalad(const char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
for (int i = 0; i < searchFieldLen; i++)
{
for (int j = 0; j < searchFieldLen; j++)
{
printf("%c ", salad [i][j]);
}
printf("\n");
}
} }
BIN
View File
Binary file not shown.
+28 -1
View File
@@ -1,6 +1,7 @@
#include "input.h" #include "input.h"
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <stdio.h>
// TODO: // TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
@@ -8,5 +9,31 @@
// Read words from file and store in 'words' array // Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{ {
unsigned int count = 0;
char line[MAX_LINE_LEN];
while(fgets(line, sizeof(line), file)!= NULL && count < maxWordCount)
{
const char *delims = "\t\n\r ,;";
char *token = strtok(line, delims);
while (token != NULL && count < maxWordCount)
{
for(unsigned int i = 0; token[i] != '\0'; i++)
{
token[i] = toupper((unsigned char)token[i]);
}
strncpy(words[count], token, MAX_WORD_LEN -1);
words[count][MAX_WORD_LEN -1] = '\0';
count++;
token = strtok(NULL, delims); //hier
}
}
return count;
}
}
BIN
View File
Binary file not shown.
+42 -3
View File
@@ -7,18 +7,27 @@
#define MAX_NUMBER_OF_WORDS 100 #define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20 #define SALAD_SIZE 20
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int exitCode = EXIT_SUCCESS; int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided // Check if the correct number of arguments is provided
if(argc != 2) if(argc != 2)
{ {
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]; // Array to hold the words to be used in the game char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
unsigned int wordCount = 0; unsigned int wordCount = 0;
@@ -26,29 +35,59 @@ int main(int argc, char *argv[])
if(file != NULL) if(file != NULL)
{ {
unsigned int placedWords = 0; unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad char wordSalad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
// Read words from file and store in 'words' array // Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file); fclose(file);
// Create the word salad by placing words into grid // Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, MAX_NUMBER_OF_WORDS);
// TODO: // TODO:
// Check if all words were successfully placed // Check if all words were successfully placed
// Start the game if successful
if(placedWords == wordCount)
{
printf("Alle %u Wörter wurden erfolgreich platziert.", placedWords);
// Start the game if successful
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
}
// error message if some words couldn't be placed // error message if some words couldn't be placed
else
{
printf("Es konnten nicht alle Wörter ausgegeben werden. Nur %u von %u Wörtern wurde platziert.\n", placedWords, wordCount);
}
} }
else else
{ {
// Print error message if file couldn't be opened // Print error message if file couldn't be opened
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]); fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
exitCode = EXIT_FAILURE; exitCode = EXIT_FAILURE;
} }
} }
return exitCode; return exitCode;
} }
BIN
View File
Binary file not shown.
+16 -5
View File
@@ -1,5 +1,16 @@
Yeti,Nessie Werwolf; Vampir YETI
Monster NESSIE
Hydra;Frankenstein WERWOLF
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist VAMPIR
Gespenst, Oger MONSTER
HYDRA
FRANKENSTEIN
DRACULA
KINGKONG
GREMLIN
KOBOLD
HEXE
POLTERGEIST
GESPENST
OGER
APFEL
+74 -37
View File
@@ -1,45 +1,82 @@
#include "game.h" #include "game.h"
#include <time.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "game.h"
#define MAX_RAND_TRIES_PER_WORD 10 //große Funktion (Gitter bauen, Wörter legen, Zufallsbuchstaben)
#define EMPTY_CHAR '.' int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], //Gitter
unsigned int size, const char words[][MAX_WORD_LEN], //größe Quadrat,liste von Wörtern
unsigned int wordCount) { //anzahl an Wörtern in der Liste
#define EMPTY_CHAR '.' // leeres kästchen = "."
typedef enum { HORIZONTAL, VERTICAL } Direction; //neuer typ: waagrecht oder senkrecht
//TODO: Spiellogik implementieren: //1.kleine Funktion(setzt jedes Kästchen auf ".")
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren void initializeArray() {
* restliche Felder mit zufälligen Buchstaben füllen */ for (unsigned int y = 0; y < size; y++) //Zeilen durchlaufen
for (unsigned int x = 0; x < size; x++) //Spalten durchlaufen
static void initializeArray(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], salad[y][x] = EMPTY_CHAR; //punkt schreiben
unsigned int SearchFieldLen) //Array initialisieren & mit Filler füllen }
{ //2.kleine Funktion (prüfen ob Wort passt)
for (int i = 0; i < SearchFieldLen; i++) int canPlace(unsigned int x, unsigned int y, const char *word, Direction dir) { //Startposition, das Wort, Richtung
{ unsigned int len = strlen(word); //länge des Wortes
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) if ((dir == HORIZONTAL && x + len > size) || (dir == VERTICAL && y + len > size)) //prüfen ob Wort über rechten bzw. unteren Rand geht
{ return 0; //passt das wort nicht -> 0
salad[i][j] = EMPTY_CHAR; for (unsigned int i = 0; i < len; i++) { //jedes zeichen nacheinander prüfen
} if ((dir == HORIZONTAL && salad[y][x + i] != EMPTY_CHAR) || //prüfen ob nach rechts schon ein wort kommt
} (dir == VERTICAL && salad[y + i][x] != EMPTY_CHAR)) //prüfen ob nach unten ein Wort kommt
} return 0; //ist dort bereits ein wort -> 0
}
static void col_roq (unsigned int searchFieldLen, int *x, int *y) //Zufällige x/y Koordinate für Wortanfang return 1; //passt das Wort -> 1
{ }
*x = rand() % searchFieldLen; //3.kleine Funktion (Wort ins Gitter schreiben)
void placeWord(unsigned int x, unsigned int y, const char *word, Direction dir) { //Startposition, das Wort, Richtung
*y = rand() % searchFieldLen; for (unsigned int i = 0; i < strlen(word); i++) { //Buchstabe für Buchstabe
} if (dir == HORIZONTAL) //i-ten Buchstaben an y,x+i schreiben bei waagrecht
salad[y][x + i] = word[i];
else //i-ten Buchstaben an y+i,x schreiben bei senkrecht
// Creates the word salad by placing words randomly and filling empty spaces salad[y + i][x] = word[i];
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) }
{ }
//4.kleine Funktion (alle leeren Kästchen bekommen Zufallsbuchstaben)
} void fillRandomLetters() { //geht wieder über alle Zeilen und Spalten oh man
for (unsigned int y = 0; y < size; y++) {
// Prints the word salad to console for (unsigned int x = 0; x < size; x++) {
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) if (salad[y][x] == EMPTY_CHAR) //wenns leer ist Zufallsbuchstabe
{ salad[y][x] = 'A' + (rand() % 26);
}
}
}
//Hauptschleife(Wörter zufällig platzieren)
srand(time(NULL)); //durch aktuelle Sekundenzeit wirkklich zufällig
initializeArray(); //1.kleine Funktion
int placed = 0; //wie viele Wörter wurden gelegt?
for (unsigned int i = 0; i < wordCount; i++) { //jedes Wort durchgehen
int success = 0; //für dieses Wort schon Platz gefunden?
for (int tries = 0; tries < 1000 && !success; tries++) { //1000 verschiedene Stellen und Richtungen probieren
unsigned int x = rand() % size; //zufällige Position Spalte
unsigned int y = rand() % size; //zufällige Postion Zeile
Direction dir = rand() % 2 ? HORIZONTAL : VERTICAL; //zufällige richtung 1-> Horizontal 0-> Vertical
if (canPlace(x, y, words[i], dir)) { //2. kleine Funktion
placeWord(x, y, words[i], dir); //3. kleine Funktion
success = 1; //Wort erfolgreich platziert
placed++; //Zähler der gelegten Wörter erhöhen
}
}
}
fillRandomLetters(); //4. kleine Funktion
return placed; //Rückgabe wie viele Wörter plaziert wurden
} }
//Anzeige-Funktion
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int size) { //funktion printet das wortsalatfeld
for (unsigned int y = 0; y < size; y++) {
for (unsigned int x = 0; x < size; x++) {
printf("%c ", salad[y][x]);
}
printf("\n");
}
}
+1
View File
@@ -9,3 +9,4 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
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);
#endif #endif
+27 -1
View File
@@ -1,12 +1,38 @@
#include "input.h" #include "input.h"
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <stdio.h>
// TODO: // TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. // 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) int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{ {
unsigned int count = 0; // wie viele Wörter bisher gespeichert wurden
char line[MAX_LINE_LEN]; // Puffer für EINE gelesene Textzeile
while(fgets(line, sizeof(line), file)!= NULL && count < maxWordCount) //fgets liest eine komplette Zeile wenn es noch eine Zeile gibt und noch Platz für mehr Wörter ist
{
const char *delims = "\t\n\r ,;"; //Liste der Trenner (z.B. Komma)
char *token = strtok(line, delims); //strok sucht in line das erste Wort, indem es an Trennzeichen teilt. Wenn kein Wort gefunden, wird NULL zurück gegeben
while (token != NULL && count < maxWordCount) //Solange es noch ein Wort (token) gibt und noch Platz in words ist
{
for(unsigned int i = 0; token[i] != '\0'; i++)
{
token[i] = toupper((unsigned char)token[i]); //toupper macht jeden Buchstaben groß.
}
strncpy(words[count], token, MAX_WORD_LEN -1); //Kopiert das Wort in den nächsten freien Slot words[count].
words[count][MAX_WORD_LEN -1] = '\0';
count++; //Wir habenein Wort abgespeichert count + 1
token = strtok(NULL, delims); //hier
}
}
return count;
} }
+37 -35
View File
@@ -11,44 +11,46 @@ int main(int argc, char *argv[])
{ {
int exitCode = EXIT_SUCCESS; int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided if (argc != 2) //wenn keine zwei argumente gefunde -> alarm
if(argc != 2)
{ {
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]); fprintf(stderr, "Benutzung: %s <Pfad zur Wörterdatei>\n", argv[0]);
exitCode = EXIT_FAILURE; return EXIT_FAILURE;
} }
else
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; //array mit eingelesenen wörtern
unsigned int wordCount = 0;
FILE *file = fopen(argv[1], "r"); //schaut in die words.txt datei
if (!file) //wenn keine datei -> alarm
{ {
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game fprintf(stderr, "Konnte Datei %s nicht öffnen.\n", argv[1]);
unsigned int wordCount = 0; return EXIT_FAILURE;
FILE *file = fopen(argv[1], "r");
if(file != NULL)
{
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
// Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file);
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// TODO:
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
}
else
{
// Print error message if file couldn't be opened
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
exitCode = EXIT_FAILURE;
}
} }
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); //holt sich aus readwords funktion die wortanzahl
fclose(file);
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); //holt sich anzahl der platzierten wörter von createwordsalad funktion
printf("\nWortsalat-Initialisierung\n"); //ganz viel printen
printf("-------------------------\n");
printf("Gelesene Woerter: %u\n", wordCount);
printf("Platzierte Woerter: %u\n", placedWords);
if (placedWords < wordCount) //macht checker, ob alle wörter salatiert wurden
printf("Hinweis: %u Woerter konnten nicht platziert werden.\n", wordCount - placedWords);
printf("\nSpielfeld (%dx%d):\n\n", SALAD_SIZE, SALAD_SIZE);
showWordSalad(wordSalad, SALAD_SIZE);
printf("\nInitialisierung abgeschlossen.\n");
// Später evtl.:
// startGraphicalGame(wordSalad, SALAD_SIZE, words, wordCount);
return exitCode; return exitCode;
} }
+3 -1
View File
@@ -2,4 +2,6 @@ Yeti,Nessie Werwolf; Vampir
Monster Monster
Hydra;Frankenstein Hydra;Frankenstein
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
Gespenst, Oger Gespenst, Oger
tomasschwester, fortnite, lababage, eckesbokesseresekes
Doener, Kebab, nacho, alejandrogarnacho, megafon, Telefon, gianluigibuffon, mehrweorter, flosschwester, gingerlatina