Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
bab68bb04b | |||
ddfcc33563 | |||
37791de41c | |||
ef8ee7f71b | |||
113700116c | |||
3d8c7cf091 | |||
2c020bcfbe | |||
eb4d95e236 | |||
c745cdd148 | |||
b6bc24b6d2 | |||
f3cdd0b307 | |||
2579dede52 | |||
e2747c285f | |||
48c0aadace | |||
8d94db8e56 | |||
2895866ef0 | |||
0c0a0f39c2 | |||
bc913662a2 | |||
e6d4b2d00d |
21
.gitignore
vendored
Normal file
21
.gitignore
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
I2_Wortsalat/Start_Mac/wordsalad_initial
|
||||||
|
I2_Wortsalat/Start_Mac/.DS_Store
|
||||||
|
I2_Wortsalat/.DS_Store
|
||||||
|
I2_Wortsalat/Start_Mac/input.o
|
||||||
|
.gitignore
|
||||||
|
I2_Wortsalat/Start_Mac/game.o
|
||||||
|
I2_Wortsalat/Start_Mac/runTests
|
||||||
|
.o
|
||||||
|
.a
|
||||||
|
.idea/editor.xml
|
||||||
|
.idea/vcs.xml
|
||||||
|
.idea/workspace.xml
|
||||||
|
I2_Wortsalat/.idea/editor.xml
|
||||||
|
I2_Wortsalat/.idea/I2_Wortsalat.iml
|
||||||
|
I2_Wortsalat/.idea/modules.xml
|
||||||
|
I2_Wortsalat/Start_Mac/main.o
|
||||||
|
I2_Wortsalat/Start_Mac/.DS_Store
|
||||||
|
I2_Wortsalat/Start_Mac/.DS_Store
|
||||||
|
I2_Wortsalat/Start_Mac/wordsalad
|
||||||
|
I2_Wortsalat/Start_Mac/graphicalGame.o
|
||||||
|
.DS_Store
|
8
I2_Wortsalat/.idea/.gitignore
generated
vendored
Normal file
8
I2_Wortsalat/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
6
I2_Wortsalat/.idea/vcs.xml
generated
Normal file
6
I2_Wortsalat/.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -10,14 +10,116 @@
|
|||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||||
|
|
||||||
|
void fillRestWithRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
if (salad[i][j] == EMPTY_CHAR) {
|
||||||
|
salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// Creates the word salad by placing words randomly and filling empty spaces
|
||||||
|
// - 2D array salad -> fertiger Wortsalat
|
||||||
|
// - searchFieldLen -> Dimension der Salatschüssel
|
||||||
|
// - 2D array words -> gegebene Wörter
|
||||||
|
// - wordCount -> anzahl an Wörtern
|
||||||
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;
|
||||||
|
|
||||||
|
//Feld komplettmit EMPTY_CHAR füllen
|
||||||
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wörter plazieren
|
||||||
|
for (int wordNumber = 0; wordNumber < wordCount; wordNumber++)
|
||||||
|
{
|
||||||
|
int wordLength = strlen(words[wordNumber]);
|
||||||
|
int placed = 0;
|
||||||
|
|
||||||
|
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
|
||||||
|
{
|
||||||
|
int horizontal = rand() % 2; // 0 = vertikal, 1 = horizontal
|
||||||
|
|
||||||
|
int x = rand() % searchFieldLen;
|
||||||
|
int y = rand() % searchFieldLen;
|
||||||
|
|
||||||
|
if (horizontal)
|
||||||
|
{
|
||||||
|
if (x + wordLength > searchFieldLen) continue; // passt nicht
|
||||||
|
|
||||||
|
//Prüfen ob kein anderes Wort im Weg
|
||||||
|
int fits = 1;
|
||||||
|
for (int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
if (salad[y][x + i] != EMPTY_CHAR)
|
||||||
|
{
|
||||||
|
fits = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wort plazieren
|
||||||
|
if (fits)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
salad[y][x + i] = words[wordNumber][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else //Wort vertikal plazieren
|
||||||
|
{
|
||||||
|
if (y + wordLength > searchFieldLen) continue;
|
||||||
|
|
||||||
|
//Prüfen ob kein anderes Wort im Weg
|
||||||
|
int fits = 1;
|
||||||
|
for (int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
if (salad[y + i][x] != EMPTY_CHAR)
|
||||||
|
{
|
||||||
|
fits = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wort plazieren
|
||||||
|
if (fits)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
salad[y + i][x] = words[wordNumber][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fillRestWithRandom(salad, searchFieldLen);
|
||||||
|
return placedWords;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
for( int i = 0; i < searchFieldLen; i++ )
|
||||||
|
{
|
||||||
|
for( int j = 0; j < searchFieldLen; j++ )
|
||||||
|
{
|
||||||
|
printf("%c", salad[i][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,42 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <ctype.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei
|
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||||
// (words.txt) einliest und als C-String zurückgibt.
|
|
||||||
|
|
||||||
// Bedingungen, die in main.c geprüft werden:
|
|
||||||
// - existiert die angegebene Datei
|
|
||||||
|
|
||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
int wordCount = 0;
|
char lines [MAX_LINE_LEN];
|
||||||
|
int word_counter = 0;
|
||||||
|
|
||||||
return wordCount;
|
while (fgets(lines, sizeof(lines) , file) != NULL)
|
||||||
|
{
|
||||||
|
|
||||||
|
for (int i = 0; lines[i] != '\0'; i++) //Entfernen von \n aus dem String
|
||||||
|
{
|
||||||
|
lines[i] = toupper(lines[i]);
|
||||||
|
if (lines[i] == '\n')
|
||||||
|
{
|
||||||
|
lines[i] = '\0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char *single_word = strtok(lines, " ;,");
|
||||||
|
|
||||||
|
while (single_word != NULL && word_counter < maxWordCount)
|
||||||
|
{
|
||||||
|
strncpy(words[word_counter], single_word, MAX_WORD_LEN - 1);
|
||||||
|
words [word_counter][MAX_WORD_LEN -1] = '\0'; //Zur Sicherheit, damit \0 auf alle Fälle vorhanden ist
|
||||||
|
word_counter++;
|
||||||
|
single_word = strtok(NULL, " ;,");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return word_counter;
|
||||||
}
|
}
|
@ -36,15 +36,21 @@ 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
|
if(placedWords == wordCount)
|
||||||
// error message if some words couldn't be placed
|
{
|
||||||
|
printf("Eingabe war erfolgreich!\nDas Spiel beginnt:");
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, SALAD_SIZE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Fehler! Es konnten nicht alle Woerter hinzugefuegt werden!\n Bitte Spiel neustarten!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Print error message if file couldn't be opened
|
// Print error message if file couldn't be opened
|
||||||
|
|
||||||
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
||||||
exitCode = EXIT_FAILURE;
|
exitCode = EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
BIN
I2_Wortsalat/Start_Mac/.DS_Store
vendored
Normal file
BIN
I2_Wortsalat/Start_Mac/.DS_Store
vendored
Normal file
Binary file not shown.
@ -10,14 +10,116 @@
|
|||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||||
|
|
||||||
|
void fillRestWithRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
if (salad[i][j] == EMPTY_CHAR) {
|
||||||
|
salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// Creates the word salad by placing words randomly and filling empty spaces
|
||||||
|
// - 2D array salad -> fertiger Wortsalat
|
||||||
|
// - searchFieldLen -> Dimension der Salatschüssel
|
||||||
|
// - 2D array words -> gegebene Wörter
|
||||||
|
// - wordCount -> anzahl an Wörtern
|
||||||
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;
|
||||||
|
|
||||||
|
//Feld komplettmit EMPTY_CHAR füllen
|
||||||
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wörter plazieren
|
||||||
|
for (int wordNumber = 0; wordNumber < wordCount; wordNumber++)
|
||||||
|
{
|
||||||
|
int wordLength = strlen(words[wordNumber]);
|
||||||
|
int placed = 0;
|
||||||
|
|
||||||
|
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
|
||||||
|
{
|
||||||
|
int horizontal = rand() % 2; // 0 = vertikal, 1 = horizontal
|
||||||
|
|
||||||
|
int x = rand() % searchFieldLen;
|
||||||
|
int y = rand() % searchFieldLen;
|
||||||
|
|
||||||
|
if (horizontal)
|
||||||
|
{
|
||||||
|
if (x + wordLength > searchFieldLen) continue; // passt nicht
|
||||||
|
|
||||||
|
//Prüfen ob kein anderes Wort im Weg
|
||||||
|
int fits = 1;
|
||||||
|
for (int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
if (salad[y][x + i] != EMPTY_CHAR)
|
||||||
|
{
|
||||||
|
fits = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wort plazieren
|
||||||
|
if (fits)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
salad[y][x + i] = words[wordNumber][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else //Wort vertikal plazieren
|
||||||
|
{
|
||||||
|
if (y + wordLength > searchFieldLen) continue;
|
||||||
|
|
||||||
|
//Prüfen ob kein anderes Wort im Weg
|
||||||
|
int fits = 1;
|
||||||
|
for (int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
if (salad[y + i][x] != EMPTY_CHAR)
|
||||||
|
{
|
||||||
|
fits = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wort plazieren
|
||||||
|
if (fits)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
salad[y + i][x] = words[wordNumber][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fillRestWithRandom(salad, searchFieldLen);
|
||||||
|
return placedWords;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
for( int i = 0; i < searchFieldLen; i++ )
|
||||||
|
{
|
||||||
|
for( int j = 0; j < searchFieldLen; j++ )
|
||||||
|
{
|
||||||
|
printf("%c", salad[i][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,5 +7,6 @@
|
|||||||
|
|
||||||
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);
|
||||||
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);
|
||||||
|
void fillRestWithRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -8,5 +8,35 @@
|
|||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
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 lines [MAX_LINE_LEN];
|
||||||
|
int word_counter = 0;
|
||||||
|
|
||||||
|
while (fgets(lines, sizeof(lines) , file) != NULL)
|
||||||
|
{
|
||||||
|
|
||||||
|
for (int i = 0; lines[i] != '\0'; i++) //Entfernen von \n aus dem String
|
||||||
|
{
|
||||||
|
lines[i] = toupper(lines[i]);
|
||||||
|
if (lines[i] == '\n')
|
||||||
|
{
|
||||||
|
lines[i] = '\0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char *single_word = strtok(lines, " ;,");
|
||||||
|
|
||||||
|
while (single_word != NULL && word_counter < maxWordCount)
|
||||||
|
{
|
||||||
|
strncpy(words[word_counter], single_word, MAX_WORD_LEN - 1);
|
||||||
|
words [word_counter][MAX_WORD_LEN -1] = '\0'; //Zur Sicherheit, damit \0 auf alle Fälle vorhanden ist
|
||||||
|
word_counter++;
|
||||||
|
single_word = strtok(NULL, " ;,");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return word_counter;
|
||||||
}
|
}
|
@ -36,15 +36,21 @@ 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
|
if(placedWords == wordCount)
|
||||||
// error message if some words couldn't be placed
|
{
|
||||||
|
printf("Eingabe war erfolgreich!\nDas Spiel beginnt:");
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Fehler! Es konnten nicht alle Woerter hinzugefuegt werden!\n Bitte Spiel neustarten!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Print error message if file couldn't be opened
|
// Print error message if file couldn't be opened
|
||||||
|
|
||||||
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
||||||
exitCode = EXIT_FAILURE;
|
exitCode = EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ game.o: game.c
|
|||||||
$(CC) -c $(CFLAGS) game.c
|
$(CC) -c $(CFLAGS) game.c
|
||||||
|
|
||||||
graphicalGame.o: graphicalGame.c
|
graphicalGame.o: graphicalGame.c
|
||||||
$(CC) -I$(raylibfolder) -c $(CFLAGS) graphicalGame.c
|
$(CC) -I$(raylib_folder) -c $(CFLAGS) graphicalGame.c
|
||||||
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
@ -10,14 +10,116 @@
|
|||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||||
|
|
||||||
|
void fillRestWithRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
if (salad[i][j] == EMPTY_CHAR) {
|
||||||
|
salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// Creates the word salad by placing words randomly and filling empty spaces
|
||||||
|
// - 2D array salad -> fertiger Wortsalat
|
||||||
|
// - searchFieldLen -> Dimension der Salatschüssel
|
||||||
|
// - 2D array words -> gegebene Wörter
|
||||||
|
// - wordCount -> anzahl an Wörtern
|
||||||
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;
|
||||||
|
|
||||||
|
//Feld komplettmit EMPTY_CHAR füllen
|
||||||
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wörter plazieren
|
||||||
|
for (int wordNumber = 0; wordNumber < wordCount; wordNumber++)
|
||||||
|
{
|
||||||
|
int wordLength = strlen(words[wordNumber]);
|
||||||
|
int placed = 0;
|
||||||
|
|
||||||
|
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
|
||||||
|
{
|
||||||
|
int horizontal = rand() % 2; // 0 = vertikal, 1 = horizontal
|
||||||
|
|
||||||
|
int x = rand() % searchFieldLen;
|
||||||
|
int y = rand() % searchFieldLen;
|
||||||
|
|
||||||
|
if (horizontal)
|
||||||
|
{
|
||||||
|
if (x + wordLength > searchFieldLen) continue; // passt nicht
|
||||||
|
|
||||||
|
//Prüfen ob kein anderes Wort im Weg
|
||||||
|
int fits = 1;
|
||||||
|
for (int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
if (salad[y][x + i] != EMPTY_CHAR)
|
||||||
|
{
|
||||||
|
fits = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wort plazieren
|
||||||
|
if (fits)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
salad[y][x + i] = words[wordNumber][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else //Wort vertikal plazieren
|
||||||
|
{
|
||||||
|
if (y + wordLength > searchFieldLen) continue;
|
||||||
|
|
||||||
|
//Prüfen ob kein anderes Wort im Weg
|
||||||
|
int fits = 1;
|
||||||
|
for (int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
if (salad[y + i][x] != EMPTY_CHAR)
|
||||||
|
{
|
||||||
|
fits = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wort plazieren
|
||||||
|
if (fits)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
salad[y + i][x] = words[wordNumber][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fillRestWithRandom(salad, searchFieldLen);
|
||||||
|
return placedWords;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
for( int i = 0; i < searchFieldLen; i++ )
|
||||||
|
{
|
||||||
|
for( int j = 0; j < searchFieldLen; j++ )
|
||||||
|
{
|
||||||
|
printf("%c", salad[i][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,5 +8,35 @@
|
|||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
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 lines [MAX_LINE_LEN];
|
||||||
|
int word_counter = 0;
|
||||||
|
|
||||||
|
while (fgets(lines, sizeof(lines) , file) != NULL)
|
||||||
|
{
|
||||||
|
|
||||||
|
for (int i = 0; lines[i] != '\0'; i++) //Entfernen von \n aus dem String
|
||||||
|
{
|
||||||
|
lines[i] = toupper(lines[i]);
|
||||||
|
if (lines[i] == '\n')
|
||||||
|
{
|
||||||
|
lines[i] = '\0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char *single_word = strtok(lines, " ;,");
|
||||||
|
|
||||||
|
while (single_word != NULL && word_counter < maxWordCount)
|
||||||
|
{
|
||||||
|
strncpy(words[word_counter], single_word, MAX_WORD_LEN - 1);
|
||||||
|
words [word_counter][MAX_WORD_LEN -1] = '\0'; //Zur Sicherheit, damit \0 auf alle Fälle vorhanden ist
|
||||||
|
word_counter++;
|
||||||
|
single_word = strtok(NULL, " ;,");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return word_counter;
|
||||||
}
|
}
|
@ -36,15 +36,21 @@ 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
|
if(placedWords == wordCount)
|
||||||
// error message if some words couldn't be placed
|
{
|
||||||
|
printf("Eingabe war erfolgreich!\nDas Spiel beginnt:");
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, SALAD_SIZE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Fehler! Es konnten nicht alle Woerter hinzugefuegt werden!\n Bitte Spiel neustarten!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Print error message if file couldn't be opened
|
// Print error message if file couldn't be opened
|
||||||
|
|
||||||
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
||||||
exitCode = EXIT_FAILURE;
|
exitCode = EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user