Files
Info2-Praktikum/Startcode/game.c
T
2026-10-17 17:58:42 +02:00

103 lines
2.9 KiB
C

#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 x = 0;
int y = 0;
int placedWords = 0;
typedef enum {vertikal, horizontal} Platzierungsrichtung;
for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++) //Array Salad mit 0 füllen um es nachher als vergleichsoperator zu nutzen
{
for( int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
{
salad[i][j] = '0';
}
}
for(int i = 0; i < wordCount; i++)
{
int platziert = 0;
int platzierversuche = 0;
while(platziert == 0 && platzierversuche < MAX_RAND_TRIES_PER_WORD )
{
platzierversuche++;
x = rand() % searchFieldLen;
y = rand() % searchFieldLen;
Platzierungsrichtung zufaellige_richtung = rand() % 2;
if(zufaellige_richtung == vertikal && strlen(words[i]) <= (searchFieldLen-y))
{
int check = 0;
for(int k = 0; k < strlen(words[i]); k++)
{
if(salad[y+k][x] == '0' || salad[y+k][x] == words[i][k])
{
check ++;
}
}
if(check == strlen(words[i]))
{
for(int j = 0; j < strlen(words[i]); j++)
{
salad[y+j][x] = words[i][j];
}
placedWords++;
platziert = 1;
}
}
else if( zufaellige_richtung == horizontal && strlen(words[i]) <= (searchFieldLen-x))
{
int check = 0;
for(int k = 0; k < strlen(words[i]); k++)
{
if(salad[y][x+k] == '0' || salad[y][x+k] == words[i][k])
{
check ++;
}
}
if(check == strlen(words[i]))
{
for(int j = 0; j < strlen(words[i]); j++)
{
salad[y][x+j] = words[i][j];
}
placedWords++;
platziert = 1;
}
}
}
}
for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++)
{
for( int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
{
if(salad[i][j] == '0')
{
salad[i][j] = rand() % (90-65+1)+ 65;
}
}
}
return placedWords;
}