15 Commits
9 changed files with 159 additions and 22 deletions
+82 -18
View File
@@ -31,16 +31,61 @@ typedef struct
} wordPosition;
void initializeWordsalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
int i = 0;
int j = 0;
for (i = 0; i < searchFieldLen; i++)
{
for (j = 0; j < searchFieldLen; j++)
{
salad[i][j] = '=';
}
}
}
void sortWordList(char words[][MAX_WORD_LEN], unsigned int wordCount, char nullWord[MAX_WORD_LEN])
{
int i = 0;
int j = 0;
char wordBuffer[MAX_WORD_LEN];
strcpy(wordBuffer, nullWord);
for (j = wordCount - 1; j > 0; j--)
{
for (i = 0; i < j; i++)
{
if (strlen(words[i]) < strlen(words[j]))
{
strcpy(wordBuffer, words[j]);
strcpy(words[j], nullWord);
strcpy(words[j], words[i]);
strcpy(words[i], nullWord);
strcpy(words[i], wordBuffer);
strcpy(wordBuffer, nullWord);
}
}
}
}
// Choses a random Position for the current Word
wordPosition choserandomPosition(unsigned int searchFieldLen, unsigned int wordLength)
{
wordPosition position = {0, 0, 0};
srand(time(NULL));
position.alignment = rand()%(VERTIKAL - HORIZONTAL + 1) + HORIZONTAL;
position.rowOrColumn = rand()%(searchFieldLen);
position.startingCell = rand()%(searchFieldLen - wordLength);
return position;
}
@@ -60,7 +105,7 @@ int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]
// by scanning each column in the given row
for (i = position.startingCell; (i < serchFieldLen) && !(letterFound); i++)
{
if ((salad[position.rowOrColumn][i] >= 'A') && (salad[position.rowOrColumn][i] <= 'z'))
if (((salad[position.rowOrColumn][i] >= 'A') && (salad[position.rowOrColumn][i] <= 'Z')) || ((salad[position.rowOrColumn][i] >= 'a') && (salad[position.rowOrColumn][i] <= 'z')))
{
letterFound = 1;
}
@@ -72,7 +117,7 @@ int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]
// by scanning each row in the given column
for (i = position.startingCell; (i < serchFieldLen) && !(letterFound); i++)
{
if ((salad[i][position.rowOrColumn] >= 'A') && (salad[i][position.rowOrColumn] <= 'z'))
if (((salad[i][position.rowOrColumn] >= 'A') && (salad[i][position.rowOrColumn] <= 'z')) || ((salad[i][position.rowOrColumn] >= 'a') && (salad[i][position.rowOrColumn] <= 'z')))
{
letterFound = 1;
}
@@ -91,27 +136,28 @@ int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]
int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPosition position, const char currentWord[], unsigned int currentWordLen, unsigned int serchFieldLen)
{
int i = 0;
int j = 0;
if (position.alignment == HORIZONTAL)
{
for (i = position.startingCell; (i < serchFieldLen) && (i < currentWordLen); i++)
for (i = position.startingCell; (i < serchFieldLen) && (currentWord[j] != '\0'); i++)
{
salad[position.rowOrColumn][i] = currentWord[i];
salad[position.rowOrColumn][i] = currentWord[j];
j++;
}
salad[position.rowOrColumn][i] = '\0';
}
else if (position.alignment == VERTIKAL)
{
for (i = position.startingCell; (i < serchFieldLen) && (i < currentWordLen); i++)
for (i = position.startingCell; (i < serchFieldLen) && (currentWord[j] != '\0'); i++)
{
salad[i][position.rowOrColumn] = currentWord[i];
salad[i][position.rowOrColumn] = currentWord[j];
j++;
}
salad[i][position.rowOrColumn] = '\0';
}
return 1;
}
@@ -123,13 +169,11 @@ void placeRandomLetters(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
int j = 0;
srand(time(NULL));
for (i = 0; i < searchFieldLen; i++)
{
for (j = 0; i < searchFieldLen; j++)
for (j = 0; j < searchFieldLen; j++)
{
if ((salad[i][j] < 'A') && (salad[i][j] > 'z'))
if ((salad[i][j] < 'A') || (salad[i][j] > 'z') || ((salad[i][j] < 'a') && (salad[i][j] > 'Z')))
{
salad[i][j] = rand()%('Z' - 'A' + 1) + 'A';
}
@@ -140,28 +184,48 @@ void placeRandomLetters(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
// Creates the word salad by placing words randomly and filling empty spaces
// returnes the number of sucessfully placed words
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, unsigned int maxWordCount)
{
int i = 0;
int j = 0;
int k = 0;
int positionFound = 0;
int placedWords = 0;
char nullWord[MAX_WORD_LEN];
char currentWord[MAX_WORD_LEN];
char sortedWords[maxWordCount][MAX_WORD_LEN];
wordPosition currentWordPosition = {0,0,0};
for (k = 0; k < MAX_WORD_LEN; k++)
{
nullWord[k] = '\0';
}
for (k = 0; k < wordCount; k++)
{
strcpy(sortedWords[k], words[k]);
}
srand(time(NULL));
initializeWordsalad(salad, searchFieldLen);
sortWordList(sortedWords, wordCount, nullWord);
for (i = 1; i <= wordCount; i++)
{
j = 0;
strcpy(currentWord, nullWord);
strcpy(currentWord, sortedWords[i-1]);
do
{
currentWordPosition = choserandomPosition(searchFieldLen, strlen(words[i]));
positionFound = checkIfPositionIsFree(salad, currentWordPosition, strlen(words[i]), searchFieldLen);
currentWordPosition = choserandomPosition(searchFieldLen, strlen(currentWord));
positionFound = checkIfPositionIsFree(salad, currentWordPosition, strlen(currentWord), searchFieldLen);
j++;
} while ((j < MAX_RAND_TRIES_PER_WORD) && !(positionFound));
if (positionFound)
{
strcpy(currentWord, words[i-1]);
placedWords += placeWord(salad, currentWordPosition, currentWord, strlen(currentWord), searchFieldLen);
}
}
@@ -182,7 +246,7 @@ void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
for (i = 0; i < searchFieldLen; i++)
{
for (j = 0; i < searchFieldLen; j++)
for (j = 0; j < searchFieldLen; j++)
{
printf("%c", salad[i][j]);
}
+1 -1
View File
@@ -5,7 +5,7 @@
#define MAX_SEARCH_FIELD_LEN 100
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, unsigned int maxWordCount);
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
#endif
Binary file not shown.
+71 -1
View File
@@ -2,7 +2,75 @@
#include <string.h>
#include <ctype.h>
static char *buffer;
// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
/*
char *readSingleWord(FILE *file)
{
if (file == NULL)
{
return 0;
}
fgets(singleWordBuffer, MAX_LINE_LEN, file);
}
*/
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
int currentlyInWord = 0;
int word = 0;
int wordChar = 0;
char currentChar = 0;
do
{
currentChar = fgetc(file);
if ((currentChar >= 'A') && (currentChar <= 'Z'))
{
if (!currentlyInWord)
{
currentlyInWord = 1;
}
words[word][wordChar] = currentChar;
wordChar++;
}
else if ((currentChar >= 'a') && (currentChar <= 'z'))
{
if (!currentlyInWord)
{
currentlyInWord = 1;
}
words[word][wordChar] = toupper(currentChar);
wordChar++;
}
else if (currentlyInWord)
{
words[word][wordChar] = '\0';
word++;
wordChar = 0;
currentlyInWord = 0;
}
} while (((currentChar != EOF) && (word <= maxWordCount)));
return word;
}
/*
#define MAX_BUFFER_LEN 256
// TODO?
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
@@ -59,3 +127,5 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
return word + 1;
}
*/
Binary file not shown.
+4 -1
View File
@@ -41,8 +41,10 @@ int main(int argc, char *argv[])
fclose(file);
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount, MAX_NUMBER_OF_WORDS);
printf("placed Words: %d\n", placedWords);
printf("wordCount = %d\n", wordCount);
// DONE:
// Check if all words were successfully placed
// Start the game if successful
@@ -59,6 +61,7 @@ int main(int argc, char *argv[])
// annahme: windowWidth wird in Pixeln gesucht,
// da auch andere funktionen, wie createCharSquarePanel
// in Pixeln arbeiten
showWordSalad(wordSalad, SALAD_SIZE);
startGame(wordSalad, SALAD_SIZE, words, placedWords, WINDOW_WIDTH);
}
Binary file not shown.
Binary file not shown.
Binary file not shown.