This commit is contained in:
Alexei Keller 2025-11-10 13:50:48 +01:00
parent 99a0600fd2
commit 80dac61be4
5 changed files with 126 additions and 93 deletions

View File

@ -2,128 +2,98 @@
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
//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 // 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) {
{ int addedWords = 0;
srand(time(NULL)); 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;
int placedWords = 0; for (int i = 0; i < searchFieldLen; i++) {
unsigned int tries = 0; for (int j = 0; j < searchFieldLen; j++) {
unsigned int check_direction = 0, check_overlap = 0; salad[i][j] = EMPTY_CHAR; //Füllen des gesamten Arrays mit Nullen
int zeile = 0, spalte = 0, len = 0;
//1. Schritt: Alle Felder mit 0 befüllen
for(int m = 0; m < searchFieldLen; m++) //m Zeilen
{
for(int n = 0; n < searchFieldLen; n++) //n Spalte
{
salad[m][n] = 0;
} }
} }
for (int i=0; i < wordCount; i++) {
// 2. Schritt: Prüfen ob Wort in Array geschrieben darf wordLength = strlen(words[i]);
for(int num_words = 0; num_words < wordCount && num_words < searchFieldLen; num_words++) //eingelesen Wörter zählen
{
len = strlen(words[num_words]); //Größe des Wortes ermitteln
if (len < searchFieldLen) while (attempts <= MAX_RAND_TRIES_PER_WORD && added != 1) {
return -1; //ERROR, falls Wort größer als Größe des Feldes (unnötig, da bei worteingabe bereits überprüft) int direction = rand() % 2;// 0 Horizontal, 1 Vertikal
if (direction == 0) {
while(tries < MAX_RAND_TRIES_PER_WORD) int collumn = rand() % searchFieldLen;
{ int row = rand() % searchFieldLen;
// zufällige Richtung auswählen for (int j = 0; j < wordLength; j++)
int direction = rand() % 2; // 0 = VERTIKAL, 1 = HORIZONTAL
// zufälliger Startpunkt, Startkoordinaten
if (direction == 0) // 0 = VERTIKAL -> Spalte egal
{
zeile = rand() % (searchFieldLen - len);
spalte = rand() % (searchFieldLen);
check_direction = 1;
}
if (direction == 1) // 1 = HORIZONTAL -> Zeile egal
{
zeile = rand() % (searchFieldLen);
spalte = rand() % (searchFieldLen - len);
check_direction = 1;
}
//Prüfen auf Überlappung ----------> Logikfehler
for(int i_overlap = 0; i_overlap < len; i_overlap++)
{
if (direction == 0) // 0 = VERTIKAL
{ {
if (words[num_words][i_overlap] == salad[zeile][spalte + i_overlap]); if (salad[row][collumn+j] != EMPTY_CHAR) {space = 0; break;}
check_overlap = 1; //Prüfen ob das Wort ein bereits vorhandenes Wort überspeichern würde
} }
else if (direction == 1) // 1 = HORIZONTAL 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++)
{ {
if (words[num_words][i_overlap] == salad[zeile + i_overlap][spalte]); salad[row][collumn+j] = words[i][j]; //Einfügen des Wortes Buchstabe für Buchstabe
check_overlap = 1;
} }
if(check_overlap == 0) added=1;
{ addedWords++;
tries++;
break;
}
}
//3. Schritt: Wort in Wortsalat schreiben }
if(check_direction == 1 && check_overlap == 1 && tries < MAX_RAND_TRIES_PER_WORD) }
else if (direction == 1) {// Vertikal funktioniert analog zu horizontal
int collumn = rand() % searchFieldLen;
int row = rand() % searchFieldLen;
for (int j = 0; j < wordLength; j++)
{ {
for(int i_set = 0; i_set < len; i_set++) 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++)
{ {
if (direction == 0) // 0 = VERTIKAL salad[row+j][collumn] = words[i][j];
{
salad[zeile + i_set][spalte] = words[num_words][i_set];
placedWords++;
}
if (direction == 1) // 1 = HORIZONTAL
{
salad[zeile][spalte + i_set] = words[num_words][i_set];
placedWords++;
}
} }
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
// 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 addedWords;
return placedWords; //platzierte Wörter zurückgeben
} }
// 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)
{ {
for(int p = 0; p < searchFieldLen; p++){ for(int i = 0; i < searchFieldLen; i++)
for(int q = 0; q < searchFieldLen; q++){ {
printf("%c", salad[p][q]); for(int j = 0; j < searchFieldLen; j++)
{
printf("%c", salad[i][j]);//Ausgabe des Arrays auf die Konsole
} }
printf("\n");
} }
} }

BIN
Start_Windows/game.o Normal file

Binary file not shown.

BIN
Start_Windows/input.o Normal file

Binary file not shown.

View File

@ -49,6 +49,59 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
}#include <stdlib.h>
#include <stdio.h>
#include "input.h"
#include "game.h"
#include "graphicalGame.h"
#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20
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");
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
if(wordCount == placedWords)
{
exitCode = EXIT_SUCCESS;
startGame(wordSalad,SALAD_SIZE, words,wordCount,750);
}
else
{
exitCode = EXIT_FAILURE;
printf("Bei der Platzierung ist etwas falsch gelaufen!");
}
// Start the game if successful
// error message if some words couldn't be placed
} }
else else
{ {
@ -60,3 +113,13 @@ int main(int argc, char *argv[])
return exitCode; return exitCode;
} }
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;
}
}
return exitCode;
}

BIN
Start_Windows/runTests2.exe Normal file

Binary file not shown.