generated from freudenreichan/info2Praktikum-Wortsalat
Programm aufgerauemt
This commit is contained in:
parent
918d049e14
commit
fd75457f10
@ -8,24 +8,13 @@
|
|||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
#define MAX_NUMBER_OF_WORDS 100
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
|
|
||||||
//TODO: Spiellogik implementieren:
|
|
||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
|
||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
|
||||||
|
|
||||||
//words und salad sind char arrays --> laengstes wort sind 100 zeichen
|
|
||||||
|
|
||||||
// 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)
|
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 placedWords = 0;
|
int placedWords = 0;
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
int usedWords[MAX_NUMBER_OF_WORDS];
|
|
||||||
for(int i=0; i< MAX_NUMBER_OF_WORDS; i++){
|
|
||||||
usedWords[i] = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
placedWords = fillSalad(salad, searchFieldLen, words, wordCount, usedWords);
|
placedWords = fillSalad(salad, searchFieldLen, words, wordCount);
|
||||||
showWordSalad(salad);
|
showWordSalad(salad);
|
||||||
printf("\nPlacedWords: %d\n", placedWords);
|
printf("\nPlacedWords: %d\n", placedWords);
|
||||||
return placedWords;
|
return placedWords;
|
||||||
@ -65,7 +54,7 @@ int emptyPlaces(unsigned int wordCount)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int 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 fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||||
{
|
{
|
||||||
int addedWords = 0;
|
int addedWords = 0;
|
||||||
|
|
||||||
@ -77,20 +66,20 @@ int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned i
|
|||||||
}
|
}
|
||||||
|
|
||||||
// inserts words
|
// inserts words
|
||||||
for(unsigned int w = 0; w < wordCount; w++) {
|
for(unsigned int w = 0; w < wordCount; w++) { //geht words array linear durch, damit jedes Wort ueberprueft wird
|
||||||
int tries = 0;
|
int tries = 0;
|
||||||
int placed = 0;
|
int placed = 0;
|
||||||
|
|
||||||
while(tries < MAX_RAND_TRIES_PER_WORD && !placed) {
|
while(tries < MAX_RAND_TRIES_PER_WORD && !placed) { //solange die max. Anzahl an Versuchen pro Wort nicht ueberschritten wird und das Wort nicht plaziert wurde:
|
||||||
int horizontal = printHorizontal();
|
int horizontal = printHorizontal(); //horozontal oder vertikal?
|
||||||
|
|
||||||
if(horizontal) {
|
if(horizontal) {
|
||||||
int row = rand() % searchFieldLen;
|
int row = rand() % searchFieldLen; //sucht random row aus
|
||||||
unsigned int wordLen = strlen(words[w]);
|
unsigned int wordLen = strlen(words[w]);
|
||||||
if(wordLen <= searchFieldLen) {
|
if(wordLen <= searchFieldLen) { //wenn wort von der Laenge her in Zeile passt
|
||||||
int startCol = rand() % (searchFieldLen - wordLen + 1);
|
int startCol = rand() % (searchFieldLen - wordLen + 1); //bestimmt einen zufaelligen Startpunkt in der row zhwischen 0 und max. Index, damit das WOrt noch passt
|
||||||
int canPlace = 1;
|
int canPlace = 1;
|
||||||
for(unsigned int i = 0; i < wordLen; i++) {
|
for(unsigned int i = 0; i < wordLen; i++) { //schaut, ob der Platz in jeder Zelle in salad frei ist oder ob Ueberlappung zweier Woerter moeglich ist
|
||||||
if(salad[row][startCol + i] != '\0') {
|
if(salad[row][startCol + i] != '\0') {
|
||||||
if(salad[row][startCol + i] != words[w][i]) {
|
if(salad[row][startCol + i] != words[w][i]) {
|
||||||
canPlace = 0;
|
canPlace = 0;
|
||||||
@ -98,7 +87,7 @@ int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned i
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(canPlace) {
|
if(canPlace) { //Wort kann plaziert werden -> fuellt Wort in random row ab random Zelle mit Wort und addiert erfolgreich plazierte Woerter
|
||||||
for(unsigned int i = 0; i < wordLen; i++) {
|
for(unsigned int i = 0; i < wordLen; i++) {
|
||||||
salad[row][startCol + i] = words[w][i];
|
salad[row][startCol + i] = words[w][i];
|
||||||
}
|
}
|
||||||
@ -133,12 +122,6 @@ int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned i
|
|||||||
|
|
||||||
tries++;
|
tries++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Weiter zum nächsten Wort, auch wenn dieses nicht platziert werden konnte
|
|
||||||
if(!placed) {
|
|
||||||
// Optional: Hinweis ausgeben
|
|
||||||
// printf("Could not place word: %s\n", words[w]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fillRandom(salad);
|
fillRandom(salad);
|
||||||
@ -146,94 +129,6 @@ int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned i
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
int whichWord(unsigned int wordCount, int usedWords[MAX_NUMBER_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_NUMBER_OF_WORDS; f++){
|
|
||||||
if(usedWords[f] == numWord){
|
|
||||||
alreadyUsed = 1;
|
|
||||||
break; // word already used
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!alreadyUsed){
|
|
||||||
// Eintragen in usedWords
|
|
||||||
for(int f = 0; f < MAX_NUMBER_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
|
|
||||||
void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],unsigned int searchFieldLen,const char words[][MAX_WORD_LEN], int numWord,int row)
|
|
||||||
{
|
|
||||||
unsigned int wordLen = strlen(words[numWord]);
|
|
||||||
|
|
||||||
// Returns if word longer than game field
|
|
||||||
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 (unsigned int i = 0; i < wordLen; i++)
|
|
||||||
{
|
|
||||||
if (salad[row][startCol + i] != '\0')
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (unsigned 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
|
|
||||||
void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], int numWord, int column)
|
|
||||||
{
|
|
||||||
unsigned int wordLen = strlen(words[numWord]);
|
|
||||||
|
|
||||||
if (wordLen > searchFieldLen || wordLen == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
int startRow = rand() % (searchFieldLen - wordLen + 1);
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < wordLen; i++)
|
|
||||||
{
|
|
||||||
if (salad[startRow + i][column] != '\0')
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned 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]) {
|
void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]) {
|
||||||
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) {
|
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) {
|
||||||
|
|||||||
@ -10,10 +10,7 @@ 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]);
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]);
|
||||||
int printHorizontal();
|
int printHorizontal();
|
||||||
int emptyPlaces(unsigned int wordCount);
|
int emptyPlaces(unsigned int wordCount);
|
||||||
int 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 fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
|
||||||
//int whichWord(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], 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], int numWord, int column);
|
|
||||||
void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]);
|
void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user