Files
MaWeKa_info2Praktikum-Worts…/Wortsalat_CLion/Startcode/game.c
T
welschph103497 36b381f116 Game.c gebaut, noch nicht getestet
Game.c erweitert, Wörter werden platziert, leere Felder mit random Buchstaben aufgefüllt. Kontrollfunktion in Arbeit
2026-10-26 08:36:27 +01:00

77 lines
2.4 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, int control[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN])
{
int row, col, word, startcol=0, startrow=0;
srand(time(NULL));
for(row=0; row<MAX_SEARCH_FIELD_LEN; row++)
{
for(col=0; col<MAX_SEARCH_FIELD_LEN; col++)
{
salad[row][col] = EMPTY_CHAR;
//control[row][col] = 0;
//printf("%c", salad[row][col]);
}
//printf("\n");
}
for (word=0; word<wordCount; word++)
{
if (rand() % 2 == 0) //horizontal
{
startcol = rand() % (MAX_SEARCH_FIELD_LEN - strlen(words[word])+1);
startrow = rand() % MAX_SEARCH_FIELD_LEN;
for (int i=0; i<strlen(words[word]); i++)
{
salad[startrow][startcol+i] = words[word][i];
//control[startrow][startcol+i] = 1;
}
}
else //vertikal
{
startrow = rand() % (MAX_SEARCH_FIELD_LEN - strlen(words[word])+1);
startcol = rand() % MAX_SEARCH_FIELD_LEN;
for (int j=0; j<strlen(words[word]); j++)
{
salad[startrow+j][startcol] = words[word][j];
//control[startrow+j][startcol] = 1;
}
}
if (rand()%26 !=0)
{
printf("%c",EMPTY_CHAR);
}
}
for (row=0; row<MAX_SEARCH_FIELD_LEN; row++)
{
for (col=0; col<MAX_SEARCH_FIELD_LEN; col++)
{
if (salad[row][col] == EMPTY_CHAR)
{
salad[row][col] = rand()%26+'A';
}
}
}
for (row=0; row<MAX_SEARCH_FIELD_LEN; row++)
{
for (col=0; col<MAX_SEARCH_FIELD_LEN; col++)
{
printf("%c", salad[row][col]);
//printf("%d ", control[row][col]);
}
printf("\n");
}
printf("\n\nPlatzierung abgeschlossen\n\n");
return 0;
}