generated from freudenreichan/info2Praktikum-Wortsalat
184 lines
6.0 KiB
C
184 lines
6.0 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 */
|
|
|
|
unsigned int findmaxLength(const char wordlist[][MAX_WORD_LEN], unsigned int Number);
|
|
unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_LEN], char salad[][MAX_SEARCH_FIELD_LEN], unsigned int position);
|
|
void putInTheWord(const char words[][MAX_WORD_LEN], unsigned int wort, char tabelle[][MAX_SEARCH_FIELD_LEN], unsigned int X, unsigned int Y, unsigned int richtung, unsigned int laenge);
|
|
void initializeField(unsigned int groesse, char field[][MAX_SEARCH_FIELD_LEN]);
|
|
void fillTheRest(char field[][MAX_SEARCH_FIELD_LEN], unsigned int sizeOfField);
|
|
unsigned int search(char salad[][MAX_SEARCH_FIELD_LEN], unsigned int length, unsigned int sizeOfField, unsigned int *x, unsigned int *y, unsigned int *direction);
|
|
|
|
// 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 maxLength = 0;
|
|
unsigned int sizeOfField = 0;
|
|
unsigned int returnValue = 0;
|
|
// search for longest word
|
|
maxLength = findmaxLength(words, wordCount);
|
|
// Size of Field!
|
|
if (searchFieldLen < maxLength)
|
|
return returnValue;
|
|
sizeOfField = searchFieldLen;
|
|
initializeField(sizeOfField, salad);
|
|
// for all words put them in
|
|
for (unsigned int i = 0; i < wordCount; i++){
|
|
if (findAPlace(sizeOfField,words,salad,i) == 1) {
|
|
returnValue += 1;
|
|
}
|
|
}
|
|
fillTheRest(salad, sizeOfField);
|
|
return returnValue;
|
|
}
|
|
|
|
unsigned int findmaxLength(const char wordlist[][MAX_WORD_LEN], unsigned int Number)
|
|
{
|
|
unsigned int max = 0;
|
|
for (unsigned int i = 0; i < Number; i++)
|
|
{
|
|
for (unsigned int j = 0; j < MAX_WORD_LEN; j++)
|
|
{
|
|
if(wordlist[i][j] == '\0') {
|
|
if (max < j)
|
|
max = j;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return max;
|
|
}
|
|
|
|
void initializeField(unsigned int groesse, char field[][MAX_SEARCH_FIELD_LEN])
|
|
{
|
|
for (unsigned int j = 0; j < groesse; j++){
|
|
for (unsigned int i = 0; i < groesse; i++)
|
|
{
|
|
field[j][i] = EMPTY_CHAR;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_LEN], char salad[][MAX_SEARCH_FIELD_LEN], unsigned int position)
|
|
{
|
|
unsigned int direction, length = 0;
|
|
unsigned int maxLineColumn = 0;
|
|
unsigned int xNumber, yNumber = 0;
|
|
|
|
direction = rand() % 2; // choose 0 = X oder 1 = Y - direction
|
|
length = strlen(words[position]);
|
|
maxLineColumn = (sizeOfField - length) + 1;
|
|
|
|
for (unsigned int j = 0; j < MAX_RAND_TRIES_PER_WORD; j++)
|
|
{
|
|
if (direction == 0){
|
|
yNumber = rand() % maxLineColumn;
|
|
xNumber = rand() % sizeOfField;
|
|
} // Maximale Werte je nach X - Y - Richtung
|
|
else {
|
|
xNumber = rand() % maxLineColumn;
|
|
yNumber = rand() % sizeOfField;
|
|
}
|
|
if(direction == 1){
|
|
unsigned int a = xNumber;
|
|
while (a < (xNumber + length) && salad[a][yNumber] == EMPTY_CHAR)
|
|
a++;
|
|
if (a == xNumber+length) {
|
|
putInTheWord(words, position, salad, xNumber, yNumber, direction, length);
|
|
return 1;
|
|
}
|
|
}
|
|
else{
|
|
unsigned int a = yNumber;
|
|
while (a < (yNumber + length) && salad[xNumber][a] == EMPTY_CHAR)
|
|
a++;
|
|
if (a == yNumber+length) {
|
|
putInTheWord(words, position, salad, xNumber, yNumber, direction, length);
|
|
return 1;
|
|
}
|
|
}
|
|
if (j == (MAX_RAND_TRIES_PER_WORD -1)){
|
|
if (search(salad,length, sizeOfField, &xNumber, &yNumber, &direction) == 1) {
|
|
putInTheWord(words, position, salad, xNumber, yNumber, direction, length);
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void putInTheWord(const char words[][MAX_WORD_LEN], unsigned int wort, char tabelle[][MAX_SEARCH_FIELD_LEN], unsigned int X, unsigned int Y, unsigned int richtung, unsigned int laenge)
|
|
{
|
|
if (richtung == 0){
|
|
for (unsigned int i=0; i < laenge; i++)
|
|
tabelle[X][(i+Y)] = words[wort][i];
|
|
}
|
|
else {
|
|
for (unsigned int i=0; i < laenge; i++)
|
|
tabelle[X+i][(Y)] = words[wort][i];
|
|
}
|
|
}
|
|
|
|
void fillTheRest(char field[][MAX_SEARCH_FIELD_LEN], unsigned int sizeOfField)
|
|
{
|
|
for (unsigned int i = 0; i < sizeOfField; i++)
|
|
{
|
|
for (unsigned int j = 0; j < sizeOfField; j++)
|
|
{
|
|
if (field[i][j] == EMPTY_CHAR) {
|
|
char random = 'A' + rand() % 26;
|
|
field[i][j] = random;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
unsigned int search(char field[][MAX_SEARCH_FIELD_LEN], unsigned int length, unsigned int sizeOfField, unsigned int *x, unsigned int *y, unsigned int *direction)
|
|
{
|
|
unsigned int freeLength = 0;
|
|
for (unsigned int i = 0; i < sizeOfField; i++) // search for free space in any line
|
|
{
|
|
freeLength = 0;
|
|
for (unsigned int j = 0; j < sizeOfField; j++)
|
|
{
|
|
if (field[i][j] == EMPTY_CHAR)
|
|
freeLength += 1;
|
|
else
|
|
freeLength = 0;
|
|
if (freeLength >= length){
|
|
*x = i;
|
|
*y = j+1-length;
|
|
*direction = 0;
|
|
return 1;
|
|
}
|
|
|
|
}
|
|
}
|
|
for (unsigned int i = 0; i < sizeOfField; i++) // search for free space in any column
|
|
{
|
|
freeLength = 0;
|
|
for (unsigned int j = 0; j < sizeOfField; j++)
|
|
{
|
|
if (field[j][i] == EMPTY_CHAR)
|
|
freeLength += 1;
|
|
else
|
|
freeLength = 0;
|
|
if (freeLength == length){
|
|
*x = j+1-length;
|
|
*y = i;
|
|
*direction = 1;
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
} |