generated from freudenreichan/info2Praktikum-Wortsalat
Erste Verbesserungen und nochmal eine Umstrukturierung einiger Funktionen
This commit is contained in:
parent
197c9cb5e7
commit
b88fa9dcef
@ -16,8 +16,13 @@
|
|||||||
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 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));
|
srand(time(NULL));
|
||||||
|
|
||||||
|
int usedWords[MAX_NUMBERS_OF_WORDS];
|
||||||
|
for(int i=0; i< MAX_NUMBERS_OF_WORDS; i++){
|
||||||
|
usedWords[i] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
fillSalad(salad, searchFieldLen, words, wordCount);
|
fillSalad(salad, searchFieldLen, words, wordCount, usedWords);
|
||||||
showWordSalad(salad, searchFieldLen);
|
showWordSalad(salad, searchFieldLen);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -40,19 +45,14 @@ void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
|||||||
// returns 1, when horizontal
|
// returns 1, when horizontal
|
||||||
int printHorizontal(unsigned int wordCount)
|
int printHorizontal(unsigned int wordCount)
|
||||||
{
|
{
|
||||||
int rand = rand() % (wordCount - (-wordCount) + 1) - wordCount;
|
return rand() % 2;
|
||||||
if(rand > 0)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// checks wether after all words are filled, empty spaces are left
|
// checks wether after all words are filled, empty spaces are left
|
||||||
// returns 1, when empty spaces are left
|
// returns 1, when empty spaces are left
|
||||||
int emptyPlaces(unsigned int wordCount)
|
int emptyPlaces(unsigned int wordCount)
|
||||||
{
|
{
|
||||||
if(wordCount < MAX_SEARCH_FIELD_LEN)
|
if(wordCount < MAX_NUMBERS_OF_WORDS)
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -61,85 +61,164 @@ int emptyPlaces(unsigned int wordCount)
|
|||||||
|
|
||||||
|
|
||||||
// fills salad array with max. words from word array either horizontally or vertically
|
// fills salad array with max. words from word array either horizontally or vertically
|
||||||
void fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
void fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int usedWords[MAX_NUMBERS_OF_WORDS])
|
||||||
{
|
{
|
||||||
for(int i= 0; i< searchFieldLen; i++) //for every word in salad, it is determined wether hor or ver
|
// empties salad
|
||||||
{
|
for(int i = 0; i < searchFieldLen; i++) {
|
||||||
if(printHorizontal(wordCount)) //horizontal
|
for(int j = 0; j < searchFieldLen; j++) {
|
||||||
{
|
salad[i][j] = '\0';
|
||||||
for(int j= 0; j < searchFieldLen; j++) //sucht freie Zeile in salad
|
}
|
||||||
{
|
}
|
||||||
if(salad[j][0] == NULL)
|
|
||||||
{
|
// inserts words
|
||||||
int numWord = whichWord(words, wordCount); //determines, which word will be written in salad
|
for(int w = 0; w < wordCount; w++) {
|
||||||
if(numWord != -5){
|
int tries = 0;
|
||||||
fillWordinRow(salad, searchFieldLen, words, wordCount, numWord, j); // fills word horizontally in salad and deletes the word afterwards
|
int placed = 0;
|
||||||
}
|
|
||||||
else{
|
while(tries < MAX_RAND_TRIES_PER_WORD && !placed) {
|
||||||
fillRandom(salad, searchFieldLen, words, wordCount, emptyPlaces(wordCount));
|
int numWord = whichWord(words, wordCount, usedWords);
|
||||||
}
|
if(numWord == -5) {
|
||||||
|
// found no unused word
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
int horizontal = printHorizontal(wordCount);
|
||||||
else{ //vertical, immer von oben nach unten
|
|
||||||
for(int j = 0; j< searchFieldLen; j++)
|
if(horizontal) {
|
||||||
{
|
int row = rand() % searchFieldLen;
|
||||||
if(salad[0][j] == NULL)
|
// checks if words fits
|
||||||
{
|
int wordLen = strlen(words[numWord]);
|
||||||
int numWord = whichWord(words, wordCount);
|
if(wordLen <= searchFieldLen) {
|
||||||
if(numWord != -5){
|
int startCol = rand() % (searchFieldLen - wordLen + 1);
|
||||||
fillWordinColumn(salad, searchFieldLen, words, wordCount, numWord, j);
|
int canPlace = 1;
|
||||||
}
|
for(int i = 0; i < wordLen; i++) {
|
||||||
else{
|
if(salad[row][startCol + i] != '\0') {
|
||||||
fillRandom(salad, searchFieldLen, words, wordCount, emptyPlaces(wordCount));
|
canPlace = 0;
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(canPlace) {
|
||||||
|
for(int i = 0; i < wordLen; i++) {
|
||||||
|
salad[row][startCol + i] = words[numWord][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// vertically
|
||||||
|
int col = rand() % searchFieldLen;
|
||||||
|
int wordLen = strlen(words[numWord]);
|
||||||
|
if(wordLen <= searchFieldLen) {
|
||||||
|
int startRow = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
int canPlace = 1;
|
||||||
|
for(int i = 0; i < wordLen; i++) {
|
||||||
|
if(salad[startRow + i][col] != '\0') {
|
||||||
|
canPlace = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(canPlace) {
|
||||||
|
for(int i = 0; i < wordLen; i++) {
|
||||||
|
salad[startRow + i][col] = words[numWord][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
tries++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
fillRandom(salad, searchFieldLen, words, wordCount, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determines, which word from words is filled into salad
|
|
||||||
int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
|
||||||
{
|
|
||||||
for(int i = 0; i< MAX_NUMBERS_OF_WORDS; i++){
|
|
||||||
int numWord = rand()% (wordCount + 1);
|
|
||||||
if(words[numWord][0] != NULL)
|
|
||||||
{
|
|
||||||
return numWord; //return random row, which has not been used yet
|
|
||||||
}
|
|
||||||
return -5;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount, int usedWords[MAX_NUMBERS_OF_WORDS])
|
||||||
|
{
|
||||||
|
int tries = 0;
|
||||||
|
|
||||||
|
while(tries < MAX_RAND_TRIES_PER_WORD) {
|
||||||
|
int numWord = rand() % wordCount; // 0..wordCount-1
|
||||||
|
|
||||||
|
int alreadyUsed = 0;
|
||||||
|
for(int f = 0; f < MAX_NUMBERS_OF_WORDS; f++){
|
||||||
|
if(usedWords[f] == numWord){
|
||||||
|
alreadyUsed = 1;
|
||||||
|
break; // word already used
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!alreadyUsed){
|
||||||
|
// Eintragen in usedWords
|
||||||
|
for(int f = 0; f < MAX_NUMBERS_OF_WORDS; f++){
|
||||||
|
if(usedWords[f] == -1){ // unused
|
||||||
|
usedWords[f] = numWord;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return numWord; // unused row found
|
||||||
|
}
|
||||||
|
|
||||||
|
tries++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Fills word in salad and deletes row of words of the used word
|
//Fills word in salad and deletes row of words of the used word
|
||||||
void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int row)
|
void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],unsigned int searchFieldLen,const char words[][MAX_WORD_LEN],unsigned int wordCount,int numWord,int row)
|
||||||
{
|
{
|
||||||
|
int wordLen = strlen(words[numWord]);
|
||||||
for(int h = 0; h < MAX_SEARCH_FIELD_LEN; h++)
|
|
||||||
{
|
// Returns if word longer than game field
|
||||||
for(int g = 0; g < MAX_WORD_LEN; g++)
|
if (wordLen > searchFieldLen || wordLen == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Determines random starting point in the row to place word
|
||||||
|
int startCol = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
|
||||||
|
// Determines wether word fits
|
||||||
|
for (int i = 0; i < wordLen; i++)
|
||||||
|
{
|
||||||
|
if (salad[row][startCol + i] != '\0')
|
||||||
{
|
{
|
||||||
salad[row][h] = words[numWord][g];
|
return;
|
||||||
words[numWord][g] = NULL;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (int i = 0; i < wordLen; i++)
|
||||||
|
{
|
||||||
|
salad[row][startCol + i] = words[numWord][i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Fills word in salad and deletes row of words of the used word, vertical from top to bottom
|
//Fills word in salad and deletes row of words of the used word, vertical from top to bottom
|
||||||
void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int column)
|
void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int column)
|
||||||
{
|
{int wordLen = strlen(words[numWord]);
|
||||||
for(int h = 0; h< MAX_SEARCH_FIELD_LEN; h++)
|
|
||||||
{
|
if (wordLen > searchFieldLen || wordLen == 0)
|
||||||
for(int g = 0; g < MAX_WORD_LEN; g++)
|
return;
|
||||||
|
|
||||||
|
int startRow = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
|
||||||
|
for (int i = 0; i < wordLen; i++)
|
||||||
|
{
|
||||||
|
if (salad[startRow + i][column] != '\0')
|
||||||
{
|
{
|
||||||
salad[h][column] = word[numWord][g];
|
return;
|
||||||
words[numWord][g] = NULL;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < wordLen; i++)
|
||||||
|
{
|
||||||
|
salad[startRow + i][column] = words[numWord][i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// if emptyPlaces, fill these with random letters
|
// if emptyPlaces, fill these with random letters
|
||||||
void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int empty)
|
void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int empty)
|
||||||
{
|
{
|
||||||
@ -149,9 +228,9 @@ void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned
|
|||||||
{
|
{
|
||||||
for(int h= 0; h < MAX_SEARCH_FIELD_LEN; h++)
|
for(int h= 0; h < MAX_SEARCH_FIELD_LEN; h++)
|
||||||
{
|
{
|
||||||
if(salad[j][h] == NULL)
|
if(salad[j][h] == '\0')
|
||||||
{
|
{
|
||||||
int num = rand()%(26 - 1 + 1) - 1;
|
int num = rand()%(26 - 1 + 1) + 1;
|
||||||
char letter = 64 + num;
|
char letter = 64 + num;
|
||||||
salad[j][h] = letter;
|
salad[j][h] = letter;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,8 +9,8 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
|
||||||
int printHorizontal(unsigned int wordCount);
|
int printHorizontal(unsigned int wordCount);
|
||||||
int emptyPlaces(unsigned int wordCount);
|
int emptyPlaces(unsigned int wordCount);
|
||||||
void fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
|
void fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int usedWords[MAX_NUMBER_OF_WORDS]);
|
||||||
int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount);
|
int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount, int usedWords[MAX_NUMBER_OF_WORDS]);
|
||||||
void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int row);
|
void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int row);
|
||||||
void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int column);
|
void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int column);
|
||||||
void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int empty);
|
void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int empty);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user