127 lines
2.7 KiB
C
127 lines
2.7 KiB
C
#include "game.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.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)
|
|
{
|
|
unsigned int placedWords = 0;
|
|
|
|
//printf("start game.c\n");
|
|
|
|
emptyField(searchFieldLen, salad);
|
|
|
|
for(int i = 0; i < wordCount; i++)
|
|
{
|
|
placedWords += placement(salad, searchFieldLen, words[i]);
|
|
|
|
//printf("[%d] of words: %s\n", i, words[i]);
|
|
}
|
|
|
|
fillRandAlpha(searchFieldLen, salad);
|
|
|
|
printf("placedWords:%d\n", placedWords);
|
|
|
|
for(int i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for(int j = 0; j < searchFieldLen; j++)
|
|
{
|
|
printf("%c", salad[j][i]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
return placedWords;
|
|
}
|
|
|
|
|
|
|
|
|
|
char alphaRand()
|
|
{
|
|
int letter = (rand() % 26) + 'A';
|
|
|
|
return (char) letter;
|
|
}
|
|
|
|
int placement(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word)
|
|
{
|
|
int saladX = rand() % ( searchFieldLen - strlen(word) );
|
|
int saladY = rand() % ( searchFieldLen - strlen(word) );
|
|
|
|
printf("%s\n", word);
|
|
|
|
if(rand() % 2)
|
|
{
|
|
for(int i = 0; i < strlen(word); i++)
|
|
{
|
|
if(isalpha(salad[saladX][saladY + i]))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
}
|
|
for(int i = 0; i < strlen(word); i++)
|
|
{
|
|
salad[saladX][saladY + i] = word[i];
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
for(int i = 0; i < strlen(word); i++)
|
|
{
|
|
if(isalpha(salad[saladX + i][saladY]))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
}
|
|
for(int i = 0; i < strlen(word); i++)
|
|
{
|
|
salad[saladX + i][saladY] = word[i];
|
|
}
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
void emptyField(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN])
|
|
{
|
|
for(size_t i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for(size_t j = 0; j < searchFieldLen; j++)
|
|
{
|
|
//salad[i][j] = '\0';
|
|
salad[i][j] = ' ';
|
|
printf("%c", salad[i][j]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
char fillRandAlpha(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN])
|
|
{
|
|
for(size_t i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for(size_t j = 0; j < searchFieldLen; j++)
|
|
{
|
|
if(!isalpha(salad[i][j]))
|
|
{
|
|
salad[i][j] = alphaRand();
|
|
//printf("%c", salad[i][j]);
|
|
}
|
|
}
|
|
printf("\n");
|
|
}
|
|
return 0;
|
|
}
|