Makefile überarbeitet; readWords() implementiert; createWordSalad implementiert; main.c fertiggestellt und unsere Namen zu words.txt hinzugefügt
This commit is contained in:
1 parent
f2e54529a4
commit
fb760d76fe
5 files changed
+111
-9
No files matched your search
@@ -1,5 +1,4 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include <time.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -13,5 +12,77 @@
|
|||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// 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)
|
||||||
{
|
{
|
||||||
|
// 0. Grundlegende Validierung der Parameter (nur bei echten Fehlern -1)
|
||||||
|
if (salad == NULL || words == NULL || searchFieldLen == 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Spielfeld mit EMPTY_CHAR (0) initialisieren
|
||||||
|
for (unsigned int i = 0; i < searchFieldLen; i++) {
|
||||||
|
for (unsigned int j = 0; j < searchFieldLen; j++) {
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int placedWordsCount = 0;
|
||||||
|
|
||||||
|
// 2. Jedes Wort der Liste einzeln im Spielfeld platzieren
|
||||||
|
for (unsigned int w = 0; w < wordCount; w++) {
|
||||||
|
const char *word = words[w];
|
||||||
|
unsigned int len = strlen(word);
|
||||||
|
|
||||||
|
if (len == 0 || len > searchFieldLen) {
|
||||||
|
continue; // Zu lange Wörter überspringen
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++) {
|
||||||
|
int direction = rand() % 2; // 0 = Horizontal, 1 = Vertikal
|
||||||
|
|
||||||
|
unsigned int startRow = 0;
|
||||||
|
unsigned int startCol = 0;
|
||||||
|
|
||||||
|
if (direction == 0) { // Horizontal
|
||||||
|
startRow = rand() % searchFieldLen;
|
||||||
|
startCol = rand() % (searchFieldLen - len + 1);
|
||||||
|
} else { // Vertikal
|
||||||
|
startRow = rand() % (searchFieldLen - len + 1);
|
||||||
|
startCol = rand() % searchFieldLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kollisionsprüfung
|
||||||
|
int collision = 0;
|
||||||
|
for (unsigned int i = 0; i < len; i++) {
|
||||||
|
unsigned int currRow = (direction == 0) ? startRow : (startRow + i);
|
||||||
|
unsigned int currCol = (direction == 0) ? (startCol + i) : startCol;
|
||||||
|
|
||||||
|
char currentCell = salad[currRow][currCol];
|
||||||
|
if (currentCell != EMPTY_CHAR && currentCell != word[i]) {
|
||||||
|
collision = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wenn keine Kollision vorliegt, eintragen
|
||||||
|
if (!collision) {
|
||||||
|
for (unsigned int i = 0; i < len; i++) {
|
||||||
|
unsigned int currRow = (direction == 0) ? startRow : (startRow + i);
|
||||||
|
unsigned int currCol = (direction == 0) ? (startCol + i) : startCol;
|
||||||
|
salad[currRow][currCol] = word[i];
|
||||||
|
}
|
||||||
|
placedWordsCount++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Übrige leere Felder mit zufälligen Buchstaben füllen
|
||||||
|
for (unsigned int i = 0; i < searchFieldLen; i++) {
|
||||||
|
for (unsigned int j = 0; j < searchFieldLen; j++) {
|
||||||
|
if (salad[i][j] == EMPTY_CHAR) {
|
||||||
|
salad[i][j] = 'A' + (rand() % 26);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return placedWordsCount;
|
||||||
}
|
}
|
||||||
@@ -8,5 +8,28 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
|
const char *delim = ", ; \n\t";
|
||||||
|
char buffer[MAX_WORD_LEN * MAX_WORD_LEN];
|
||||||
|
int i = 0; // Wort-Zähler
|
||||||
|
|
||||||
|
// Prüfen, ob das Lesen erfolgreich war (EOF oder Fehler abfangen)
|
||||||
|
while (fgets(buffer, sizeof(buffer), file) != NULL && i<maxWordCount) {
|
||||||
|
|
||||||
|
|
||||||
|
char *wordbuffer = strtok(buffer, delim);
|
||||||
|
|
||||||
|
while (wordbuffer != NULL && i<maxWordCount) {
|
||||||
|
// snprintf ist quasi strncpy nur mit \0 und sicher
|
||||||
|
snprintf(words[i], MAX_WORD_LEN, "%s", wordbuffer);
|
||||||
|
|
||||||
|
for (int j=0; words[i][j] != '\0'; j++) {
|
||||||
|
words[i][j] = toupper((unsigned char)words[i][j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
wordbuffer = strtok(NULL, delim);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "graphicalGame.h"
|
#include "graphicalGame.h"
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#define MAX_NUMBER_OF_WORDS 100
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
#define SALAD_SIZE 20
|
#define SALAD_SIZE 20
|
||||||
@@ -10,6 +11,7 @@
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int exitCode = EXIT_SUCCESS;
|
int exitCode = EXIT_SUCCESS;
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
// Check if the correct number of arguments is provided
|
// Check if the correct number of arguments is provided
|
||||||
if(argc != 2)
|
if(argc != 2)
|
||||||
@@ -36,6 +38,15 @@ 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);
|
||||||
|
|
||||||
|
if (placedWords == wordCount)
|
||||||
|
{
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, 100);
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Es konnten nicht alle Wörter platziert werden :(\n");
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
// Start the game if successful
|
// Start the game if successful
|
||||||
|
|||||||
@@ -44,20 +44,17 @@ codefolder = .
|
|||||||
# Direkt einen bauen
|
# Direkt einen bauen
|
||||||
# -------------------------------
|
# -------------------------------
|
||||||
|
|
||||||
wordsalad: main.o $(BINARIES)/libwordsalad.a graphicalGame.o $(BINARIES)/libraylib.a
|
wordsalad: main.o game.o input.o graphicalGame.o $(BINARIES)/libraylib.a
|
||||||
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
libwordsalad: game.o input.o
|
|
||||||
$(CC) $(CFLAGS) -o libwordsalad game.o input.o
|
|
||||||
|
|
||||||
main.o: $(codefolder)/main.c
|
main.o: $(codefolder)/main.c
|
||||||
$(CC) -c $(CFLAGS) $(codefolder)/main.c
|
$(CC) -c $(CFLAGS) $(codefolder)/main.c -o main.o
|
||||||
|
|
||||||
input.o: $(codefolder)/input.c
|
input.o: $(codefolder)/input.c
|
||||||
$(CC) -c $(CFLAGS) $(codefolder)/input.c
|
$(CC) -c $(CFLAGS) $(codefolder)/input.c -o input.o
|
||||||
|
|
||||||
game.o: $(codefolder)/game.c
|
game.o: $(codefolder)/game.c
|
||||||
$(CC) -c $(CFLAGS) $(codefolder)/game.c
|
$(CC) -c $(CFLAGS) $(codefolder)/game.c -o game.o
|
||||||
|
|
||||||
graphicalGame.o: $(codefolder)/graphicalGame.c
|
graphicalGame.o: $(codefolder)/graphicalGame.c
|
||||||
$(CC) -I$(raylibfolder) -c $(CFLAGS) $(codefolder)/graphicalGame.c
|
$(CC) -I$(raylibfolder) -c $(CFLAGS) $(codefolder)/graphicalGame.c
|
||||||
|
|||||||
Reference in new issue
Block a user