Template
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efd28dec07 | ||
|
|
61193c20ab | ||
|
|
8c747c61fa | ||
|
|
b7edd65439 | ||
|
|
9d57dbffdd |
No files matched your search
+91
-4
@@ -2,16 +2,103 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#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:
|
typedef enum
|
||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
{
|
||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
HORIZONTAL,
|
||||||
|
VERTICAL
|
||||||
|
} Direction;
|
||||||
|
|
||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// Versucht, ein einzelnes Wort an einer zufaelligen Position zu platzieren; gibt 1 bei Erfolg zurück, sonst 0
|
||||||
|
static int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word)
|
||||||
|
{
|
||||||
|
// Laenge des Wortes bestimmen
|
||||||
|
unsigned int wordLen = strlen(word);
|
||||||
|
|
||||||
|
// Leere Wörter oder Wörter die zu lang sind, werden uebersprungen (es wird 0 ausgegeben)
|
||||||
|
if (wordLen == 0 || wordLen > searchFieldLen)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// Wenn das Spielfeld voll ist, soll es nicht endlos weiterprobiert werden
|
||||||
|
for (int try = 0; try < MAX_RAND_TRIES_PER_WORD; try++)
|
||||||
|
{
|
||||||
|
// Es wird zufällig entschieden, ob das Wort waagerecht oder senkrecht geschrieben wird
|
||||||
|
Direction dir = rand() % 2;
|
||||||
|
unsigned int row, col;
|
||||||
|
|
||||||
|
// Startposition so wählen, dass das Wort ins Spielfeld passt
|
||||||
|
if (dir == HORIZONTAL)
|
||||||
|
{
|
||||||
|
row = rand() % searchFieldLen;
|
||||||
|
col = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
row = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
col = rand() % searchFieldLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pruefen, ob alle benoetigten Felder noch leer sind
|
||||||
|
int isFree = 1;
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < wordLen; i++)
|
||||||
|
{
|
||||||
|
if (dir == HORIZONTAL && salad[row][col + i] != EMPTY_CHAR)
|
||||||
|
isFree = 0;
|
||||||
|
if (dir == VERTICAL && salad[row + i][col] != EMPTY_CHAR)
|
||||||
|
isFree = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isFree)
|
||||||
|
{
|
||||||
|
// Erst nach Pruefung das Wort vollständig eintragen
|
||||||
|
for (unsigned int i = 0; i < wordLen; i++)
|
||||||
|
{
|
||||||
|
if (dir == HORIZONTAL)
|
||||||
|
salad[row][col + i] = toupper(word[i]);
|
||||||
|
else
|
||||||
|
salad[row + i][col] = toupper(word[i]);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wenn jeder Versuch fehlgeschloagen ist, wird 0 zurueckgegeben
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Erstellt den Wortsalat: platziert die Woerter zufaellig und fuellt leere Felder auf
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
static int isSeeded = 0; // Einmaliger Beginn des Zufallsgenerators gefordert
|
||||||
|
int placedWords = 0; // Anzahl erfogreicher Woerter
|
||||||
|
|
||||||
|
// Zufallszahlengenerator nur einmal initialisieren
|
||||||
|
if (!isSeeded)
|
||||||
|
{
|
||||||
|
srand(time(NULL));
|
||||||
|
isSeeded = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Alle Felder als leer markieren
|
||||||
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||||
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
|
||||||
|
// Woerter platzieren, erfolgreiche Woerter zaehlen
|
||||||
|
for (unsigned int i = 0; i < wordCount; i++)
|
||||||
|
placedWords += placeWord(salad, searchFieldLen, words[i]);
|
||||||
|
|
||||||
|
// Restliche (leere) Felder mit zufaelligen Buchstaben fuellen
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Anzahl der erfolgreichen Woerter zurueckgeben
|
||||||
|
return placedWords;
|
||||||
}
|
}
|
||||||
+35
-2
@@ -2,11 +2,44 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
// TODO:
|
// Liest ein einzelnes Wort aus der Datei in 'buffer' (max. bufferSize Zeichen inkl. '\0')
|
||||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
// Gibt 'buffer' zurück oder NULL, wenn kein Wort mehr vorhanden ist
|
||||||
|
static char *readWord(FILE *file, char *buffer, unsigned int bufferSize)
|
||||||
|
{
|
||||||
|
unsigned int len = 0;
|
||||||
|
int c = fgetc(file);
|
||||||
|
|
||||||
|
// Trennzeichen (Leerzeichen, Komma, Semikolon, Zeilenumbruch, ...) überspringen
|
||||||
|
while (c != EOF && !isalpha(c))
|
||||||
|
c = fgetc(file);
|
||||||
|
|
||||||
|
// Buchstaben sammeln, bis ein Trennzeichen oder das Dateiende kommt
|
||||||
|
while (c != EOF && isalpha(c))
|
||||||
|
{
|
||||||
|
if (len < bufferSize - 1)
|
||||||
|
buffer[len++] = toupper(c);
|
||||||
|
c = fgetc(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[len] = '\0';
|
||||||
|
|
||||||
|
if (len == 0)
|
||||||
|
return NULL;
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
// 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 word[MAX_WORD_LEN];
|
||||||
|
unsigned int wordCount = 0;
|
||||||
|
|
||||||
|
while (wordCount < maxWordCount && readWord(file, word, MAX_WORD_LEN) != NULL)
|
||||||
|
{
|
||||||
|
strncpy(words[wordCount], word, MAX_WORD_LEN - 1);
|
||||||
|
words[wordCount][MAX_WORD_LEN - 1] = '\0';
|
||||||
|
wordCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return wordCount;
|
||||||
}
|
}
|
||||||
+9
-1
@@ -40,7 +40,15 @@ int main(int argc, char *argv[])
|
|||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
// Start the game if successful
|
// Start the game if successful
|
||||||
// error message if some words couldn't be placed
|
// error message if some words couldn't be placed
|
||||||
|
if(placedWords == wordCount)
|
||||||
|
{
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Could not place all words (%u of %u placed) ...\n", placedWords, wordCount);
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -28,6 +28,12 @@ wordsalad_initial:
|
|||||||
wordsalad: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
|
wordsalad: main.o input.o game.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)
|
||||||
|
|
||||||
|
# --------------------------
|
||||||
|
# Eigene main.c + Bibliothek (input.o, game.o) bauen
|
||||||
|
# --------------------------
|
||||||
|
wordsalad_myversion: main.o graphicalGame.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a
|
||||||
|
$(CC) $(CFLAGS) -o wordsalad_myversion main.o graphicalGame.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
main.o: $(codefolder)/main.c
|
main.o: $(codefolder)/main.c
|
||||||
$(CC) -c $(CFLAGS) $(codefolder)/main.c
|
$(CC) -c $(CFLAGS) $(codefolder)/main.c
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user