70 lines
2.2 KiB
C
70 lines
2.2 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));
|
|
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){
|
|
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){
|
|
salad[i][j] = 1;
|
|
}
|
|
}
|
|
int failed = 0;
|
|
for(int i = 0; i < wordCount; i++){
|
|
int wordlen = strlen(words[i]);
|
|
if(wordlen >= searchFieldLen){
|
|
failed++;
|
|
}
|
|
else{
|
|
if(rand() % 2){
|
|
//place horizontal
|
|
while(wordlen){
|
|
int row = rand() % searchFieldLen;
|
|
int col = rand() % (searchFieldLen - wordlen);
|
|
int j = 0;
|
|
for(; (j <= wordlen) && (salad[row][col + j] == 1); j++){}
|
|
if(j == wordlen + 1){
|
|
//word can fit
|
|
wordlen = 0;
|
|
strcpy(&(salad[row][col]), words[i]);
|
|
}
|
|
}
|
|
}
|
|
else{
|
|
//place vertical
|
|
while(wordlen){
|
|
int row = rand() % (searchFieldLen - wordlen);
|
|
int col = rand() % searchFieldLen;
|
|
int j = 0;
|
|
for(; (j <= wordlen) && (salad[row + j][col] == 1); j++){}
|
|
if(j == wordlen + 1){
|
|
//word can fit
|
|
for(int k = 0; k <= wordlen; k++){
|
|
salad[row + k][col] = words[i][k];
|
|
}
|
|
wordlen = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//fill remaining spots with random chars
|
|
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){
|
|
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){
|
|
salad[i][j] = (salad[i][j] == 1) || (salad[i][j] == 0) ? (rand() % 25) + 65 : salad[i][j];
|
|
}
|
|
}
|
|
return wordCount - failed;
|
|
}
|