Template
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e2e20ccf0a | ||
|
|
18eef548c8 | ||
|
|
4459715ea1 | ||
|
|
5bd71ca48d | ||
|
|
ee9b83a82f | ||
|
|
a125bd02cb | ||
|
|
b3d425aeb8 | ||
|
|
a080b5c0c2 | ||
|
|
43f85de5a1 | ||
|
|
14c2c5d4f3 |
No files matched your search
@@ -13,5 +13,102 @@
|
||||
// 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)
|
||||
{
|
||||
srand(time(NULL));
|
||||
/***********************************************************************************
|
||||
Salat initialisieren
|
||||
***********************************************************************************/
|
||||
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||
{
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
salad[i][j] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************************************************
|
||||
Wörter im Feld platzieren
|
||||
***********************************************************************************/
|
||||
int placed_counter = 0;
|
||||
int x_pos, y_pos;
|
||||
int richtung; //horizontal: 0, vertikal: 1
|
||||
|
||||
for(int i = 0;i < wordCount; i++) //Schleife um nacheinander durch alle Wörter zu gehen
|
||||
{
|
||||
int is_valid = 0;
|
||||
int counter = 0;
|
||||
int laenge = strlen(words[i]);
|
||||
|
||||
richtung = rand() % 2; //Richtung zufällig bestimmen
|
||||
|
||||
if(richtung == 0) //Richtung: horizontal
|
||||
{
|
||||
while(is_valid == 0 && counter < MAX_RAND_TRIES_PER_WORD)
|
||||
{
|
||||
counter++;
|
||||
is_valid = 1;
|
||||
y_pos = rand() % (MAX_SEARCH_FIELD_LEN ); //y-Koordinate bei horizontal egal
|
||||
x_pos = rand() % (MAX_SEARCH_FIELD_LEN - laenge + 1); //x-Koordinate bei horizontal nur in dem Bereich, in den das Wort auch reinpasst
|
||||
|
||||
for(int j = 0; j < laenge; j++) //Schleife um Buchstaben ab Startposition zu prüfen
|
||||
{
|
||||
if(salad[x_pos + j][y_pos] != '\0') //überprüfen ob Feld schon voll ist
|
||||
{
|
||||
is_valid = 0;
|
||||
}
|
||||
}
|
||||
if(is_valid == 1)
|
||||
{
|
||||
for(int j = 0; j < laenge; j++) //Schleife um Buchstaben ab Startposition zu setzen
|
||||
{
|
||||
salad[x_pos + j][y_pos] = words[i][j]; //horizontal: y-Koordinate fest, x-Koordinate erhöht sich pro Durchlauf um 1
|
||||
}
|
||||
placed_counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(richtung == 1) //Richtung: vertikal
|
||||
{
|
||||
while(is_valid == 0 && counter < MAX_RAND_TRIES_PER_WORD)
|
||||
{
|
||||
counter++;
|
||||
is_valid = 1;
|
||||
x_pos = rand() % (MAX_SEARCH_FIELD_LEN); //x-Koordinate bei vertikal egal
|
||||
y_pos = rand() % (MAX_SEARCH_FIELD_LEN - laenge + 1); //y-Koordinate bei vertikal nur in dem Bereich, in den das Wort auch reinpasst
|
||||
|
||||
for(int j = 0; j < laenge; j++) //Schleife um Buchstaben ab Startposition zu prüfen
|
||||
{
|
||||
if(salad[x_pos][y_pos + j] != '\0') //überprüfen ob Feld schon voll ist
|
||||
{
|
||||
is_valid = 0;
|
||||
}
|
||||
}
|
||||
if(is_valid == 1)
|
||||
{
|
||||
for(int j = 0; j < laenge; j++) //Schleife um Buchstaben ab Startposition zu setzen
|
||||
{
|
||||
salad[x_pos][y_pos + j] = words[i][j]; //vertikal: x-Koordinate fest, y-Koordinate erhöht sich pro Durchlauf um 1
|
||||
}
|
||||
placed_counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************************************************
|
||||
übrige leere Felder mit zufälligen Buchstaben füllen
|
||||
***********************************************************************************/
|
||||
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||
{
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
if(salad[i][j] == '\0')
|
||||
{
|
||||
salad[i][j] = 'A' + rand() % 26;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placed_counter;
|
||||
}
|
||||
@@ -8,5 +8,33 @@
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
char line[1024];
|
||||
unsigned int count = 0;
|
||||
|
||||
if (file == NULL)
|
||||
return 0;
|
||||
|
||||
while (count < maxWordCount && fgets(line, sizeof(line), file) != NULL)
|
||||
{
|
||||
char *word = strtok(line, ",; \t\r\n");
|
||||
|
||||
while (word != NULL && count < maxWordCount)
|
||||
{
|
||||
strncpy(words[count], word, MAX_WORD_LEN - 1);
|
||||
words[count][MAX_WORD_LEN - 1] = '\0';
|
||||
|
||||
count++;
|
||||
word = strtok(NULL, ",; \t\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < maxWordCount; i++)
|
||||
{
|
||||
for(int j = 0; j < MAX_WORD_LEN; j++)
|
||||
{
|
||||
words[i][j] = toupper(words[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
+8
-1
@@ -38,7 +38,14 @@ int main(int argc, char *argv[])
|
||||
|
||||
// TODO:
|
||||
// Check if all words were successfully placed
|
||||
// Start the game if successful
|
||||
if (wordCount == placedWords) {
|
||||
startGame(wordSalad, SALAD_SIZE, words, wordCount,800);
|
||||
}
|
||||
//Start the game if successful
|
||||
else {
|
||||
fprintf(stderr,"Error: Only %u of %u words could be placed.\n", placedWords, wordCount);
|
||||
exitCode = EXIT_FAILURE;
|
||||
}
|
||||
// error message if some words couldn't be placed
|
||||
|
||||
}
|
||||
|
||||
+5
-1
@@ -40,7 +40,11 @@ game.o: $(codefolder)/game.c
|
||||
graphicalGame.o: $(codefolder)/graphicalGame.c
|
||||
$(CC) -I$(raylibfolder) -c $(CFLAGS) $(codefolder)/graphicalGame.c
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Unser Spiel bauen
|
||||
# --------------------------
|
||||
wordsalad_ourversion: main.o graphicalGame.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a
|
||||
$(CC) $(CFLAGS) -o wordsalad_ourversion main.o graphicalGame.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
# --------------------------
|
||||
|
||||
Executable
BIN
Binary file not shown.
+5
-5
@@ -1,5 +1,5 @@
|
||||
Yeti,Nessie Werwolf; Vampir
|
||||
Monster
|
||||
Hydra;Frankenstein
|
||||
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
||||
Gespenst, Oger
|
||||
Fee,Apfel Lilli; Lennart
|
||||
Maus
|
||||
Info;Samuel
|
||||
Praktikum;Prinz;branch;Linux,Commit;Gitk
|
||||
Makefile, Prinzessin
|
||||
Reference in new issue
Block a user