Compare commits

..

18 Commits

Author SHA1 Message Date
maxgrf
8097bf4bb3 update game.c 2025-11-10 13:44:35 +01:00
maxgrf
a83f9f0bef update game.c 2025-11-10 13:43:26 +01:00
maxgrf
b09e9a5d90 update makefile 2025-11-10 13:31:14 +01:00
maxgrf
5929da2766 update main 2025-11-10 13:30:02 +01:00
maxgrf
31fbbb3153 game update 2025-11-10 13:25:34 +01:00
maxgrf
a108133a59 game update 2025-11-10 12:01:10 +01:00
maxgrf
670c7d1cfc return game.c 2025-11-10 11:59:36 +01:00
maxgrf
f83992ef61 update makefile 2025-11-10 11:58:44 +01:00
maxgrf
673eae7d17 test13 2025-11-07 17:45:25 +01:00
maxgrf
c4b0ef5427 game, ctype hinzugefügt, punkt von string als char gewechselt 2025-11-07 17:39:18 +01:00
maxgrf
ec4b32c2a5 Logikfehler bei Probe auf Ueberlappung verbessert 2025-11-07 17:35:10 +01:00
maxgrf
7b215682a1 game.c update 2025-11-06 14:36:00 +01:00
maxgrf
841e147b9b game.c buchstaben eingabe 2025-11-06 12:07:03 +01:00
maxgrf
6a3b7700e8 game.c bugfix 2025-11-06 11:55:49 +01:00
maxgrf
c596dc20c3 discovery of logic mistake in overlap 2025-11-06 10:57:18 +01:00
maxgrf
a5d5cf82b6 game.c Bugfix Startpunkt 2025-11-06 10:38:19 +01:00
maxgrf
b2118b4c24 game.c, createWordSalad finished, not tested 2025-11-05 22:38:42 +01:00
maxgrf
45fb1f651b 1.Test game 2025-11-05 20:15:20 +01:00
13 changed files with 197 additions and 150 deletions

View File

@ -4,6 +4,7 @@
#include <string.h>
#include <ctype.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
@ -15,108 +16,111 @@
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));
int placedWords = 0;
// Array mit . füllen
for (int r = 0; r < searchFieldLen; r++){
for(int s = 0; s < searchFieldLen; s++){
salad[r][s] = '.';
unsigned int attemps = 0;
unsigned int check_overlap = 0;
int zeile = 0, spalte = 0, len = 0;
//Alle Felder mit ? befüllen
for(int m = 0; m < searchFieldLen; m++) //m Zeilen
{
for(int n = 0; n < searchFieldLen; n++) //n Spalte
{
salad[m][n] = '?';
}
}
// -> erst wenn die ganze Länge des Wortes frei ist, array hinzufügen
// MAX Versuche das Wort zu platzieren == 10
//Prüfen ob Wort in Array geschrieben darf
for(int num_words = 0; num_words < wordCount; num_words++) //eingelesen Wörter zählen
{
len = strlen(words[num_words]); //Größe des Wortes ermitteln
// Wörter in salad einsortieren
for (int num_words = 0; num_words < wordCount; num_words++){
int laenge = strlen(words[num_words]);
// muss noch prüfen, ob in der Zeile schon was ist
// 1 ist horizontal, 0 ist vertikal
// isalpha == 0 -> kein Buchstabe
while(attemps < MAX_RAND_TRIES_PER_WORD)
{
// zufällige Richtung auswählen
int direction = rand() % 2; // 0 = VERTIKAL, 1 = HORIZONTAL
// zufälliger Startpunkt, Startkoordinaten
if (direction == 0) // 0 = VERTIKAL
{
zeile = rand() % searchFieldLen;
spalte = rand() % searchFieldLen;
}
for(int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++){
int richtung = rand()% 2;
int belegt = 0;
if (direction == 1) // 1 = HORIZONTAL
{
zeile = rand() % searchFieldLen;
spalte = rand() % searchFieldLen;
}
// horizontale Eingabe
if (richtung == 1){
int zeile = rand()% searchFieldLen;
int spalte = rand()% (searchFieldLen- laenge +1);
//prüft, ob die herausgesuchte Zeile noch frei ist
// belegt == 1 -> Zeile nicht frei
for (int o = spalte; o < spalte + laenge; o++){
if(salad[zeile][o] != '.'){
belegt = 1;
for(int i = 0; i < len; i++)
{
if (direction == 0) // 0 = VERTIKAL
{
//Prüfe ob Feld bereits belegt
if (salad[zeile+i][spalte] != '?')
{
check_overlap = 1; // 1 = Überlappung
break;
}
}
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
if (belegt == 0){
for(int k = 0; k < laenge; k++){
salad[zeile][spalte + k] = words[num_words][k];
//Prüfe auf Groeße
if (spalte + len >= searchFieldLen-1 || check_overlap == 1)
{
attemps++;
}
placedWords++;
break;
}
}
// vertikale Eingabe
else if (richtung == 0){
int zeile = rand()% (searchFieldLen - laenge +1);
int spalte = rand()% searchFieldLen;
else
{
for(int k; k < len; k++)
{
salad[zeile][spalte+k] = words[i][k];
placedWords++;
}
}
}
//prüft, ob die herausgesuchte Zeile noch frei ist
for (int n = zeile; n < zeile + laenge; n++){
if(salad[n][spalte] != '.'){
belegt = 1;
if (direction == 1) // 1 = HORZIZONTAL
{
//Prüfe ob Feld bereits belegt
if (salad[zeile][spalte+i] != '?')
{
check_overlap = 1; // 1 = Überlappung
break;
}
}
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
if (belegt == 0){
for(int j = 0; j < laenge; j++){
salad[zeile + j][spalte] = words[num_words][j];
//Prüfe auf Groeße
if (zeile + len >= searchFieldLen-1 || check_overlap == 1)
{
attemps++;
}
placedWords++;
break;
}
}
else{
break;
else
{
for(int k; k < len; k++)
{
salad[zeile+k][spalte] = words[i][k];
placedWords++;
}
}
}
}
}
}
// fügt zufällige Buchstaben ein
for(int l = 0; l < searchFieldLen; l++){
for(int m = 0; m < searchFieldLen; m++ ){
if(isalpha(salad[l][m]) == 0 ){
// zufällige Buchstaben erzeugen
char alphabet [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int laenge = strlen(alphabet);
int zufallszahl = rand()% laenge;
char zufallsbuchstabe = alphabet[zufallszahl];
// zufällige Buchstaben einfügen
salad[l][m] = zufallsbuchstabe;
}
}
}
return placedWords;
return placedWords; //platzierte Wörter zurückgeben
}
// 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 < searchFieldLen; p++){
for(int q = 0; q < searchFieldLen; q++){
for(int p = 0; p < searchFieldLen; p++)
{
for(int q = 0; q < searchFieldLen; q++)
{
printf("%c", salad[p][q]);
}
printf("\n");

Binary file not shown.

View File

@ -8,28 +8,37 @@
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
char puffer[MAX_LINE_LEN]; //Array für eingelesene Zeile
int counter = 0; //Zähler für Anzahl eingelesener Wörter
int i;
// MAX_WORD_LEN 100
// MAX_LINE_LEN 1024
// *file ist Datei
// words ist Array
// MAX_WORD_LEN ist maximale Wortlänge
// maxWordCount ist maximale Anzahl an Wörtern
char puffer[MAX_LINE_LEN];
int counter = 0;
while(fgets(puffer, MAX_LINE_LEN-1, file) != NULL && counter < maxWordCount)
{
char *word_parts = strtok(puffer, ",;\n\t/. "); //Wörter aufteilen
char *parts = strtok(puffer, ",;\n\t/. ");
while(word_parts != NULL && counter < maxWordCount)
while(parts != NULL && counter < maxWordCount)
{
// Kopiere das Wort in words-Array
strncpy(words[counter], word_parts, MAX_WORD_LEN - 1);
words[counter][MAX_WORD_LEN - 1] = '\0';
// Alles in Großbuchstaben umwandeln
for(i = 0; words[counter][i] != '\0'; i++)
{
words[counter][i] = toupper(words[counter][i]);
}
counter++; // Wort eingelesen, Wortzähler erhöhen
word_parts = strtok(NULL, ",;\n\t/. "); //NULL für Zeiger angeben
//strncpy(words[counter][MAX_WORD_LEN], puffer, MAX_LINE_LEN-1);
//words[counter][MAX_WORD_LEN-1] = "\0";
//Großbuchstaben
for(int i = 0; i < MAX_WORD_LEN -1 && parts[i] != '\0'; i++)
{
words[counter][i] = toupper(parts[i]);
words[counter][i] = '\0';
}
}
counter++; // Wort eingelesen -> Wortzähler erhöhen
parts = strtok(NULL, ",;\n\t/. ");
}
return counter;
}
}
//TODO: Wörter auf Zahlen, Umlaute überprüfen -> ändern

Binary file not shown.

View File

@ -1,62 +1,99 @@
#include <stdlib.h>
#include <stdio.h>
#include "input.h"
#include "game.h"
#include "graphicalGame.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
int main(int argc, char *argv[])
{
int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided
if(argc != 2)
{
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
exitCode = EXIT_FAILURE;
}
else
{
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
unsigned int wordCount = 0;
FILE *file = fopen(argv[1], "r");
//TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
if(file != NULL)
{
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
// 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 addedWords = 0;
int attempts = 0; //Anzahl an Versuchen ein Wort hinzuzufügen
int added = 0; //0 Wort nicht hinzugefügt , 1 Wort hinzugefügt
int space = 1; // 0 Wort zu lang, 1 Wort passt an diese Stelle
unsigned long long wordLength = 0;
// Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file);
for (int i = 0; i < searchFieldLen; i++) {
for (int j = 0; j < searchFieldLen; j++) {
salad[i][j] = EMPTY_CHAR; //Füllen des gesamten Arrays mit Nullen
}
}
for (int i=0; i < wordCount; i++) {
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
wordLength = strlen(words[i]);
// TODO:
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
if (placedWords == wordCount){
startGame(wordSalad,SALAD_SIZE,words, wordCount,750);
}
else
while (attempts <= MAX_RAND_TRIES_PER_WORD && added != 1) {
int direction = rand() % 2;// 0 Horizontal, 1 Vertikal
if (direction == 0) {
int collumn = rand() % searchFieldLen;
int row = rand() % searchFieldLen;
for (int j = 0; j < wordLength; j++)
{
printf("Error. The words couldn't be placed.\n");
return -1;
if (salad[row][collumn+j] != EMPTY_CHAR) {space = 0; break;}
//Prüfen ob das Wort ein bereits vorhandenes Wort überspeichern würde
}
}
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;
}
}
if ((collumn + wordLength >= searchFieldLen-1) || (space == 0)) {attempts++;}
//Prüfen ob Wort an der Stelle (Zeile, Spalte) in das Array passt
else {for (int j = 0; j < wordLength; j++)
{
salad[row][collumn+j] = words[i][j]; //Einfügen des Wortes Buchstabe für Buchstabe
}
added=1;
addedWords++;
return exitCode;
}
}
else if (direction == 1) {// Vertikal funktioniert analog zu horizontal
int collumn = rand() % searchFieldLen;
int row = rand() % searchFieldLen;
for (int j = 0; j < wordLength; j++)
{
if (salad[row+j][collumn] != EMPTY_CHAR) {space = 0; break;}
}
if ((row + wordLength >= searchFieldLen-1) || (space == 0)) {attempts++;}
else {
for (int j = 0; j < wordLength; j++)
{
salad[row+j][collumn] = words[i][j];
}
added=1;
addedWords++;
}
}
space = 1;
}
attempts = 0;
added = 0;
}
for (int i = 0; i < searchFieldLen; i++) {
for (int j = 0; j < searchFieldLen; j++) {
if (salad[i][j] == EMPTY_CHAR)
{
salad[i][j] = rand()%('Z'-'A'+1)+'A';//Alle Felder, die noch nicht befüllt sind, werden zufällig befüllt
}
}
}
return addedWords;
}
// Prints the word salad to console
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]);//Ausgabe des Arrays auf die Konsole
}
}
}

View File

@ -46,4 +46,5 @@ clean:
#hier eigene wordsalad kopieren, library rauslassen
wordsalad_myversion: main.o input.o game.o graphicalGame.o
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)

Binary file not shown.

View File

@ -1 +0,0 @@
Hund,Katze; Maus

View File

@ -1,3 +0,0 @@
Apfel
Banane
Kiwi

BIN
Start_Windows/unity/unity.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.