Kommentierung ergänzt
This commit is contained in:
parent
56456b1146
commit
bcafa60824
@ -6,16 +6,14 @@
|
|||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
//TODO: Spiellogik implementieren:
|
// Creates the word salad by placing words randomly and filling empty spaces with random letters
|
||||||
/* * 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)
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||||
{// Länge der searchFieldLen wird in Main unter Salad size festgelegt (20)
|
{
|
||||||
|
|
||||||
srand((unsigned int)time(NULL)); // Seed für Zufallsgenerator
|
// Hinweis: Länge der searchFieldLen wird in Main unter Salad size festgelegt (20)
|
||||||
|
|
||||||
|
// Seed für Zufallsgenerator
|
||||||
|
srand((unsigned int)time(NULL));
|
||||||
|
|
||||||
// Initialisiere das Spielfeld mit EMPTY_CHAR
|
// Initialisiere das Spielfeld mit EMPTY_CHAR
|
||||||
for (unsigned int i = 0; i < searchFieldLen; ++i)
|
for (unsigned int i = 0; i < searchFieldLen; ++i)
|
||||||
@ -26,30 +24,34 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int placedWords = 0;
|
unsigned int placedWords = 0; // Zähler für die Anzahl platzierter Wörter für spätere Überprüfung
|
||||||
|
|
||||||
for (unsigned int word = 0; word < wordCount; ++word)
|
for (unsigned int word = 0; word < wordCount; ++word)
|
||||||
{
|
{
|
||||||
int placed = 0;
|
int placed = 0; //Flag um zu kennzechneinen, ob ein Wort platziert worden ist oder nicht
|
||||||
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; ++tries)
|
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; ++tries)
|
||||||
{
|
{
|
||||||
|
// Festlegen der Parameter zum Platzieren des Worts
|
||||||
int direction = rand() % 2; // 1 = horizontal, 0 = vertical
|
int direction = rand() % 2; // 1 = horizontal, 0 = vertical
|
||||||
int row = rand() % searchFieldLen;
|
int row = rand() % searchFieldLen;
|
||||||
int col = rand() % searchFieldLen;
|
int col = rand() % searchFieldLen;
|
||||||
int len = (int)strlen(words[word]);
|
int len = (int)strlen(words[word]);
|
||||||
|
|
||||||
if (direction == 1 && col + len <= searchFieldLen) // prüft ob das Wort horizontal in die Tabelle passt
|
// prüfen, ob das Wort horizontal in die Tabelle passt
|
||||||
|
if (direction == 1 && col + len <= searchFieldLen)
|
||||||
{
|
{
|
||||||
int canPlace = 1;
|
int canPlace = 1; // Flag um zu kennzeichnen, dass das Wort platziert werden kann
|
||||||
for (int i = 0; i < len; ++i)
|
for (int i = 0; i < len; ++i)
|
||||||
{
|
{
|
||||||
|
// Wenn in den Feldern schon andere Buchstaben stehen, kann das Feld nicht platziert werden
|
||||||
if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != words[word][i])
|
if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != words[word][i])
|
||||||
{
|
{
|
||||||
canPlace = 0;
|
canPlace = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wenn das Wort platziert werden kann, wird es platziert
|
||||||
if (canPlace)
|
if (canPlace)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < len; ++i)
|
for (int i = 0; i < len; ++i)
|
||||||
@ -57,18 +59,22 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
placed = 1;
|
placed = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (direction == 0 && row + len <= searchFieldLen) // vertical
|
|
||||||
|
// prüfen, ob das Wort vertikal in die Tabelle passt
|
||||||
|
else if (direction == 0 && row + len <= searchFieldLen)
|
||||||
{
|
{
|
||||||
int canPlace = 1;
|
int canPlace = 1; // Flag um zu kennzeichnen, dass das Wort platziert werden kann
|
||||||
for (int i = 0; i < len; ++i)
|
for (int i = 0; i < len; ++i)
|
||||||
{
|
{
|
||||||
|
// Wenn in den Feldern schon andere Buchstaben stehen, kann das Feld nicht platziert werden
|
||||||
if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != words[word][i])
|
if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != words[word][i])
|
||||||
{
|
{
|
||||||
canPlace = 0;
|
canPlace = 0;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wenn das Wort platziert werden kann, wird es platziert
|
||||||
if (canPlace)
|
if (canPlace)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < len; ++i)
|
for (int i = 0; i < len; ++i)
|
||||||
@ -78,13 +84,13 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Zähler hochzählen, wenn ein Wort platziert wurde
|
||||||
if (placed)
|
if (placed)
|
||||||
placedWords++;
|
placedWords++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fülle leere Felder mit zufälligen Buchstaben
|
// Fülle leere Felder mit zufälligen Buchstaben
|
||||||
for (unsigned int i = 0; i < searchFieldLen; ++i)
|
for (unsigned int i = 0; i < searchFieldLen; ++i)
|
||||||
|
|
||||||
{
|
{
|
||||||
for (unsigned int j = 0; j < searchFieldLen; ++j)
|
for (unsigned int j = 0; j < searchFieldLen; ++j)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
|
// Nimmt einen String und schreibt dessen Characters in Großbuchstaben
|
||||||
void capitalletters(char *str){
|
void capitalletters(char *str){
|
||||||
while (*str) {
|
while (*str) {
|
||||||
*str = toupper((unsigned char) *str);
|
*str = toupper((unsigned char) *str);
|
||||||
@ -10,16 +10,15 @@ void capitalletters(char *str){
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
// Liest Wörter aus Datei und speichert sie sicher im Array
|
|
||||||
|
// Liest Wörter aus Datei und speichert sie sicher im Array words
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
char buffer[MAX_WORD_LEN];
|
char buffer[MAX_WORD_LEN]; // temporärer Speicher für das aktuell bearbeitete Wort aus der Datei
|
||||||
unsigned int count_word = 0;
|
unsigned int count_word = 0;
|
||||||
|
|
||||||
|
|
||||||
while (count_word < maxWordCount && fgets(buffer, sizeof(buffer), file) != NULL)
|
while (count_word < maxWordCount && fgets(buffer, sizeof(buffer), file) != NULL)
|
||||||
{
|
{
|
||||||
|
|
||||||
// Zeilenumbruch manuell entfernen
|
// Zeilenumbruch manuell entfernen
|
||||||
for (int i = 0; i < MAX_WORD_LEN; i++)
|
for (int i = 0; i < MAX_WORD_LEN; i++)
|
||||||
{
|
{
|
||||||
@ -30,9 +29,10 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Alle Characters des Wort groß schreiben
|
||||||
capitalletters(buffer);
|
capitalletters(buffer);
|
||||||
|
|
||||||
// Sicheres Kopieren in das Zielarray
|
// Sicheres Kopieren des aktuellen Worts in das Zielarray
|
||||||
strncpy(words[count_word], buffer, MAX_WORD_LEN - 1);
|
strncpy(words[count_word], buffer, MAX_WORD_LEN - 1);
|
||||||
words[count_word][MAX_WORD_LEN - 1] = '\0'; // Nullterminierung sicherstellen
|
words[count_word][MAX_WORD_LEN - 1] = '\0'; // Nullterminierung sicherstellen
|
||||||
|
|
||||||
|
|||||||
@ -36,16 +36,10 @@ int main(int argc, char *argv[])
|
|||||||
// Create the word salad by placing words into grid
|
// Create the word salad by placing words into grid
|
||||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
// Start the game if successful
|
|
||||||
// error message if some words couldn't be placed
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (placedWords == wordCount)
|
if (placedWords == wordCount)
|
||||||
{
|
{
|
||||||
// Start the game
|
// Start the game if successful
|
||||||
unsigned int windowSize = 900; //FRAGE: Programm so programmieren, dass man das weglassen kann
|
unsigned int windowSize = 900; //FRAGE: Programm so programmieren, dass man das weglassen kann
|
||||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, windowSize);
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, windowSize);
|
||||||
}
|
}
|
||||||
@ -56,7 +50,6 @@ int main(int argc, char *argv[])
|
|||||||
exitCode = EXIT_FAILURE;
|
exitCode = EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user