forked from freudenreichan/info2Praktikum-Wortsalat
70 lines
1.8 KiB
C
70 lines
1.8 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
|
|
|
|
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 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;
|
|
}
|
|
}
|
|
|
|
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];
|
|
}
|
|
}
|
|
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];
|
|
}
|
|
}
|
|
|
|
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("\n");
|
|
}
|
|
printf("\n\nPlatzierung abgeschlossen\n\n");
|
|
return 0;
|
|
}
|