This commit is contained in:
Thomas Rauh Desktop 2025-11-05 22:38:20 +01:00
parent 0da01c3a15
commit 719f4942ed
10 changed files with 289 additions and 12 deletions

View File

@ -13,11 +13,138 @@
// 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)
{
srand(time(NULL));
int wortrest = wordCount;
int akt_word;
int belegtcounter;
int lage;
int spalte_zeile;
int fehlende_woerter;
char saladcpy[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
char demostring[MAX_WORD_LEN];
char comp_string[MAX_WORD_LEN];
int zerocounter;
int maxzero[2];
int ersterbuchstabe;
int erledigt;
//salad befuellen mit "0"
for (int i=0;i<searchFieldLen; i++){
for (int ii=0;ii<searchFieldLen; ii++){
salad[i][ii]= '0';
saladcpy[i][ii]= '0';
}
salad[i][searchFieldLen] = '\0';
saladcpy[i][searchFieldLen] = '\0';
demostring[i]='0';
}
demostring[searchFieldLen-1]='\0';
while(wortrest>0){
akt_word = wordCount - wortrest;
lage = rand()%2;
erledigt = 0;
belegtcounter = MAX_RAND_TRIES_PER_WORD;
while(belegtcounter>0){
strncpy(comp_string,demostring,MAX_WORD_LEN);
strncpy(comp_string,words[akt_word],strlen(words[akt_word]));
zerocounter = 1;
maxzero[0] = 1;
spalte_zeile=rand()%searchFieldLen;
switch (lage){
case 0://Waagerecht
for(int i=1;i<searchFieldLen;i++){
if(salad[spalte_zeile][i-1]=='0' && salad[spalte_zeile][i]=='0'){
zerocounter++;
if(zerocounter>maxzero[0]){
maxzero[0] = zerocounter; //Menge an Nullen
maxzero[1] = i; //Pos der letzten Null
}
}else{
zerocounter = 1;
}
}
if(maxzero[0]>=strlen(words[akt_word])){
ersterbuchstabe = maxzero[1]-(maxzero[0])+1+rand()%(maxzero[0]-strlen(words[akt_word])+1); //Start = letzter freier Platz - anzahl Platz + 1 + rand()%(freierPlatz-benötigterPlatz +1)
for(int i=0;i<strlen(words[akt_word]);i++){ // (teoretischer erster Startpunkt) (für richtigen Startwert) freieLücken + 1 wegen Startpunkt
salad[spalte_zeile][ersterbuchstabe+i]=words[akt_word][i];
}
for(int i=0;i<searchFieldLen;i++){
for(int ii=0;ii<searchFieldLen;ii++){
saladcpy[ii][i]=salad[i][ii];
}
}
erledigt =1;
}
break;
case 1://Senkrecht
for(int i=1;i<searchFieldLen;i++){
if(saladcpy[spalte_zeile][i-1]=='0' && saladcpy[spalte_zeile][i]=='0'){
zerocounter++;
if(zerocounter>maxzero[0]){
maxzero[0] = zerocounter; //Menge an Nullen
maxzero[1] = i; //Pos der letzten Null
}
}else{
zerocounter = 1;
}
}
if(maxzero[0]>=strlen(words[akt_word])){
ersterbuchstabe = maxzero[1]-(maxzero[0])+1+rand()%(maxzero[0]-strlen(words[akt_word])+1); //Start = letzter freier Platz - anzahl Platz + 1 + rand()%(freierPlatz-benötigterPlatz +1)
for(int i=0;i<strlen(words[akt_word]);i++){ // (teoretischer erster Startpunkt) (für richtigen Startwert) freieLücken + 1 wegen Startpunkt
saladcpy[spalte_zeile][ersterbuchstabe+i]=words[akt_word][i];
}
for(int i=0;i<searchFieldLen;i++){
for(int ii=0;ii<searchFieldLen;ii++){
salad[ii][i]=saladcpy[i][ii];
}
}
erledigt = 1;
}
break;
default:
printf("Fehler: game.c: lage-switch");
break;
}
if(erledigt==1){
break;
}
belegtcounter--;
}
if (belegtcounter==0){
fehlende_woerter++;
}
wortrest--;
}
for(int i=0;i<searchFieldLen;i++){ //Rest vom Feld füllen
for(int ii=0;ii<searchFieldLen;ii++){
if(salad[i][ii]=='0'){
salad[i][ii] = 'A' + rand()%26;
}
}
}
return wordCount-fehlende_woerter;
}
// 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++){
printf("%s",salad[i]);
//for(int ii=0;ii<searchFieldLen+1; ii++){
// putchar(salad[i][ii]);
//}
putchar('\n');
}
}

Binary file not shown.

125
Start_Windows/game_old.c Normal file
View File

@ -0,0 +1,125 @@
#include "game.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
//TODO: Spiellogik implementieren:
/* * 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)
{
srand(time(NULL));
int wortrest = wordCount;
int akt_word;
int lage;
int pos_x;
int pos_y;
int belegt = 2;
int belegt_counter;
int fehlende_woerter=0;
int startwechsel = 0;
char demostring[searchFieldLen];
char vergleichstring[searchFieldLen];
//salad befuellen mit "0"
for (int i=0;i<searchFieldLen; i++){
for (int ii=0;ii<searchFieldLen; ii++){
salad[i][ii]= '0';
}
salad[i][searchFieldLen] = '\0';
demostring[i]='0';
}
demostring[searchFieldLen-1]='\0';
while (wortrest>0){
belegt_counter = MAX_RAND_TRIES_PER_WORD;
while (belegt_counter > 0){
akt_word = (wordCount-wortrest);
if(belegt_counter<(MAX_RAND_TRIES_PER_WORD/2)){
}
//horizontal/vertikal
lage = rand()%2;
switch (lage){
case 0://Waagerecht
pos_x = rand()%(searchFieldLen-strlen(words[akt_word]));
pos_y = rand()%(searchFieldLen);
for (int leange_counter = 0;leange_counter<strlen(words[akt_word]);leange_counter++){
if (salad[pos_x+leange_counter][pos_y] != '0'){ //checken ob alle Felder frei sind
belegt = 1;
break;
}
}
if (belegt == 2){
for(int i = 0; i < strlen(words[akt_word]); i++){ //Inhalte reinschreiben
salad[pos_x+i][pos_y] = words[akt_word][i];
}
belegt = 0;
}
break;
case 1://Senkrecht
pos_x = rand()%(searchFieldLen);
pos_y = rand()%(searchFieldLen-strlen(words[akt_word]));
for (int leange_counter = 0;leange_counter<strlen(words[akt_word]);leange_counter++){
if (salad[pos_x][pos_y+leange_counter] != '0'){ //checken ob alle Felder frei sind
belegt = 1;
break;
}
}
if (belegt == 2){
for(int i = 0; i < strlen(words[akt_word]); i++){ //Inhalte reinschreiben
salad[pos_x][pos_y+i] = words[akt_word][i];
}
belegt = 0;
}
break;
default:
printf("Fehler: game.c: lage-switch");
break;
}
if(belegt==0){
break;
} //Auslesen Belegtstatus
else if(belegt == 1){
belegt_counter --;
}
}
if(belegt_counter==0){
fehlende_woerter++;
}else if(belegt ==2){
printf("Fehler: game.c: Beschreiben des Salats");
}
belegt = 2;
wortrest--;
startwechsel = 0;
}
return wordCount-fehlende_woerter;
}
// 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++){
printf("%s",salad[i]);
//for(int ii=0;ii<searchFieldLen+1; ii++){
// putchar(salad[i][ii]);
//}
putchar('\n');
}
}

Binary file not shown.

View File

@ -15,6 +15,8 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
char *trenner = " .;,";
char *token;
char *enter = "\n";
char sorter[MAX_WORD_LEN];
//Zeilen auslesen und als ein String speichern
while(fgets(zeile,MAX_LINE_LEN, file) != NULL){
if (strstr(zeile, enter) != NULL){
@ -35,13 +37,31 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
counter ++;
}
// Datei schliessen
fclose(file);
for(int i=0;i<counter;i++){ //Schleife für nur Großbuchstaben
for(int ii=0;ii<strlen(words[i]);ii++){
words[i][ii] = toupper(words[i][ii]);
}
}
for(int i=0;i<counter-1;i++){ //nach Groesse sortieren
for(int ii=0;ii<counter-1-i;ii++){
if(strlen(words[ii]) < strlen(words[ii+1])) {
strncpy(sorter,words[ii],MAX_WORD_LEN);
strncpy(words[ii],words[ii+1], MAX_WORD_LEN);
strncpy(words[ii+1],sorter, MAX_WORD_LEN);
}
strcpy(sorter,"");
}
}
//Test
//for(int i =0;i<counter;i++){
// printf("%s\n", words[i]);
//}
for(int i =0;i<counter;i++){
printf("%s\n", words[i]);
}
return counter;
}
}
//Wörter von lang nach kurz sortieren

Binary file not shown.

View File

@ -10,7 +10,6 @@
int main(int argc, char *argv[])
{
int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided
if(argc != 2)
{
@ -32,7 +31,6 @@ int main(int argc, char *argv[])
// 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);
@ -41,6 +39,13 @@ int main(int argc, char *argv[])
// Start the game if successful
// error message if some words couldn't be placed
showWordSalad(wordSalad,MAX_SEARCH_FIELD_LEN);
if(placedWords == wordCount){
startGame(wordSalad,SALAD_SIZE,words, wordCount, 1000);
}
else{
printf("Es konnten nicht alle Woerter plaziert werden: %d\n", (wordCount-placedWords));
}
}
else
{

Binary file not shown.

View File

@ -15,8 +15,8 @@ wordsalad_initial:
# --------------------------
# Normales Spiel bauen
# --------------------------
wordsalad_myversion: main.o input.o game.o
$(CC) -o wordsalad_myversion $(BINARIES)/libwordsalad.a main.o input.o game.o
wordsalad_myversion: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
$(CC) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
all: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)