generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
9 Commits
Verschoene
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd75457f10 | ||
|
|
918d049e14 | ||
|
|
ceb9262589 | ||
|
|
6b30eb3145 | ||
|
|
9624270bc3 | ||
|
|
e2de303489 | ||
|
|
27dc4dd02b | ||
|
|
1ed6c27d72 | ||
|
|
80fd4e832a |
@ -8,24 +8,13 @@
|
||||
#define EMPTY_CHAR 0
|
||||
#define MAX_NUMBER_OF_WORDS 100
|
||||
|
||||
//TODO: Spiellogik implementieren:
|
||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||
|
||||
//words und salad sind char arrays --> laengstes wort sind 100 zeichen
|
||||
|
||||
// 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 placedWords = 0;
|
||||
srand(time(NULL));
|
||||
|
||||
int usedWords[MAX_NUMBER_OF_WORDS];
|
||||
for(int i=0; i< MAX_NUMBER_OF_WORDS; i++){
|
||||
usedWords[i] = -1;
|
||||
}
|
||||
|
||||
placedWords = fillSalad(salad, searchFieldLen, words, wordCount, usedWords);
|
||||
placedWords = fillSalad(salad, searchFieldLen, words, wordCount);
|
||||
showWordSalad(salad);
|
||||
printf("\nPlacedWords: %d\n", placedWords);
|
||||
return placedWords;
|
||||
@ -65,10 +54,10 @@ int emptyPlaces(unsigned int wordCount)
|
||||
}
|
||||
|
||||
|
||||
// fills salad array with max. words from word array either horizontally or vertically
|
||||
int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int usedWords[MAX_NUMBER_OF_WORDS])
|
||||
int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||
{
|
||||
int addedWords = 0;
|
||||
|
||||
// empties salad
|
||||
for(unsigned int i = 0; i < MAX_SEARCH_FIELD_LEN; i++) {
|
||||
for(unsigned int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) {
|
||||
@ -77,35 +66,30 @@ int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned i
|
||||
}
|
||||
|
||||
// inserts words
|
||||
for(unsigned int w = 0; w < wordCount; w++) {
|
||||
for(unsigned int w = 0; w < wordCount; w++) { //geht words array linear durch, damit jedes Wort ueberprueft wird
|
||||
int tries = 0;
|
||||
int placed = 0;
|
||||
|
||||
while(tries < MAX_RAND_TRIES_PER_WORD && !placed) {
|
||||
int numWord = whichWord(wordCount, usedWords);
|
||||
if(numWord == -5) {
|
||||
// found no unused word
|
||||
break;
|
||||
}
|
||||
|
||||
int horizontal = printHorizontal();
|
||||
while(tries < MAX_RAND_TRIES_PER_WORD && !placed) { //solange die max. Anzahl an Versuchen pro Wort nicht ueberschritten wird und das Wort nicht plaziert wurde:
|
||||
int horizontal = printHorizontal(); //horozontal oder vertikal?
|
||||
|
||||
if(horizontal) {
|
||||
int row = rand() % searchFieldLen;
|
||||
// checks if words fits
|
||||
unsigned int wordLen = strlen(words[numWord]);
|
||||
if(wordLen <= searchFieldLen) {
|
||||
int startCol = rand() % (searchFieldLen - wordLen + 1);
|
||||
int row = rand() % searchFieldLen; //sucht random row aus
|
||||
unsigned int wordLen = strlen(words[w]);
|
||||
if(wordLen <= searchFieldLen) { //wenn wort von der Laenge her in Zeile passt
|
||||
int startCol = rand() % (searchFieldLen - wordLen + 1); //bestimmt einen zufaelligen Startpunkt in der row zhwischen 0 und max. Index, damit das WOrt noch passt
|
||||
int canPlace = 1;
|
||||
for(unsigned int i = 0; i < wordLen; i++) { //checks if word fits
|
||||
for(unsigned int i = 0; i < wordLen; i++) { //schaut, ob der Platz in jeder Zelle in salad frei ist oder ob Ueberlappung zweier Woerter moeglich ist
|
||||
if(salad[row][startCol + i] != '\0') {
|
||||
if(salad[row][startCol + i] != words[w][i]) {
|
||||
canPlace = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(canPlace) { //word fits and is inserted
|
||||
}
|
||||
if(canPlace) { //Wort kann plaziert werden -> fuellt Wort in random row ab random Zelle mit Wort und addiert erfolgreich plazierte Woerter
|
||||
for(unsigned int i = 0; i < wordLen; i++) {
|
||||
salad[row][startCol + i] = words[numWord][i];
|
||||
salad[row][startCol + i] = words[w][i];
|
||||
}
|
||||
placed = 1;
|
||||
addedWords++;
|
||||
@ -114,19 +98,21 @@ int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned i
|
||||
} else {
|
||||
// vertically
|
||||
int col = rand() % searchFieldLen;
|
||||
unsigned int wordLen = strlen(words[numWord]);
|
||||
unsigned int wordLen = strlen(words[w]);
|
||||
if(wordLen <= searchFieldLen) {
|
||||
int startRow = rand() % (searchFieldLen - wordLen + 1);
|
||||
int canPlace = 1;
|
||||
for(unsigned int i = 0; i < wordLen; i++) {
|
||||
if(salad[startRow + i][col] != '\0') {
|
||||
if(salad[startRow + i][col] != words[w][i]) {
|
||||
canPlace = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(canPlace) {
|
||||
for(unsigned int i = 0; i < wordLen; i++) {
|
||||
salad[startRow + i][col] = words[numWord][i];
|
||||
salad[startRow + i][col] = words[w][i];
|
||||
}
|
||||
placed = 1;
|
||||
addedWords++;
|
||||
@ -137,98 +123,12 @@ int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned i
|
||||
tries++;
|
||||
}
|
||||
}
|
||||
|
||||
fillRandom(salad);
|
||||
return addedWords;
|
||||
}
|
||||
|
||||
|
||||
int whichWord(unsigned int wordCount, int usedWords[MAX_NUMBER_OF_WORDS])
|
||||
{
|
||||
int tries = 0;
|
||||
|
||||
while(tries < MAX_RAND_TRIES_PER_WORD) {
|
||||
int numWord = rand() % wordCount; // 0..wordCount-1
|
||||
|
||||
int alreadyUsed = 0;
|
||||
for(int f = 0; f < MAX_NUMBER_OF_WORDS; f++){
|
||||
if(usedWords[f] == numWord){
|
||||
alreadyUsed = 1;
|
||||
break; // word already used
|
||||
}
|
||||
}
|
||||
|
||||
if(!alreadyUsed){
|
||||
// Eintragen in usedWords
|
||||
for(int f = 0; f < MAX_NUMBER_OF_WORDS; f++){
|
||||
if(usedWords[f] == -1){ // unused
|
||||
usedWords[f] = numWord;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return numWord; // unused row found
|
||||
}
|
||||
|
||||
tries++;
|
||||
}
|
||||
|
||||
return -5;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//Fills word in salad and deletes row of words of the used word
|
||||
void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],unsigned int searchFieldLen,const char words[][MAX_WORD_LEN], int numWord,int row)
|
||||
{
|
||||
unsigned int wordLen = strlen(words[numWord]);
|
||||
|
||||
// Returns if word longer than game field
|
||||
if (wordLen > searchFieldLen || wordLen == 0)
|
||||
return;
|
||||
|
||||
// Determines random starting point in the row to place word
|
||||
int startCol = rand() % (searchFieldLen - wordLen + 1);
|
||||
|
||||
// Determines wether word fits
|
||||
for (unsigned int i = 0; i < wordLen; i++)
|
||||
{
|
||||
if (salad[row][startCol + i] != '\0')
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (unsigned int i = 0; i < wordLen; i++)
|
||||
{
|
||||
salad[row][startCol + i] = words[numWord][i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Fills word in salad and deletes row of words of the used word, vertical from top to bottom
|
||||
void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], int numWord, int column)
|
||||
{
|
||||
unsigned int wordLen = strlen(words[numWord]);
|
||||
|
||||
if (wordLen > searchFieldLen || wordLen == 0)
|
||||
return;
|
||||
|
||||
int startRow = rand() % (searchFieldLen - wordLen + 1);
|
||||
|
||||
for (unsigned int i = 0; i < wordLen; i++)
|
||||
{
|
||||
if (salad[startRow + i][column] != '\0')
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < wordLen; i++)
|
||||
{
|
||||
salad[startRow + i][column] = words[numWord][i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// if emptyPlaces, fill these with random letters
|
||||
void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]) {
|
||||
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) {
|
||||
@ -240,3 +140,4 @@ void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -10,10 +10,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]);
|
||||
int printHorizontal();
|
||||
int emptyPlaces(unsigned int wordCount);
|
||||
int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int usedWords[MAX_NUMBER_OF_WORDS]);
|
||||
int whichWord(unsigned int wordCount, int usedWords[MAX_NUMBER_OF_WORDS]);
|
||||
void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], int numWord, int row);
|
||||
void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], int numWord, int column);
|
||||
int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
|
||||
void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]);
|
||||
|
||||
|
||||
|
||||
@ -2,11 +2,122 @@
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// TODO:
|
||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||
/*
|
||||
* ------------------------------------------------------------
|
||||
* HILFSFUNKTION:
|
||||
* Liest genau EIN Wort aus einer Datei und speichert es in 'buffer'.
|
||||
*
|
||||
* - Trennt Wörter anhand von Komma, Semikolon oder Leerzeichen.
|
||||
* - Wandelt alle Buchstaben in GROSSBUCHSTABEN um (damit Tests passen).
|
||||
* - Gibt 1 zurück, wenn ein Wort gefunden wurde.
|
||||
* - Gibt 0 zurück, wenn nichts mehr zu lesen ist (EOF oder Fehler).
|
||||
* ------------------------------------------------------------
|
||||
*/
|
||||
static int readSingleWord(FILE *file, char *buffer, size_t bufferSize)
|
||||
{
|
||||
int c; // aktuelles Zeichen aus der Datei
|
||||
size_t len = 0; // Anzahl der bisher eingelesenen Zeichen im aktuellen Wort
|
||||
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
// --- Sicherheitsprüfung: Sind die Übergabeparameter gültig? ---
|
||||
if (file == NULL || buffer == NULL || bufferSize == 0)
|
||||
{
|
||||
return 0; // nichts tun, wenn Datei oder Speicher ungültig ist
|
||||
}
|
||||
|
||||
// --- 1. Führende Trennzeichen überspringen ---
|
||||
// Solange Kommas, Semikolons oder Leerzeichen kommen, einfach weiter lesen.
|
||||
// Erst wenn ein anderes Zeichen kommt, fängt das Wort an.
|
||||
while ((c = fgetc(file)) != EOF)
|
||||
{
|
||||
if (c == ',' || c == ';' || isspace((unsigned char)c))
|
||||
{
|
||||
continue; // noch kein Buchstabe -> weiter zum nächsten Zeichen
|
||||
}
|
||||
else
|
||||
{
|
||||
break; // erstes Nicht-Trennzeichen gefunden -> Wort beginnt hier
|
||||
}
|
||||
}
|
||||
|
||||
// --- 2. Falls direkt EOF (End of File) erreicht wurde, kein Wort mehr vorhanden ---
|
||||
if (c == EOF)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --- 3. Erstes Zeichen des Wortes in den Buffer schreiben ---
|
||||
// Dabei gleich in Großbuchstaben umwandeln
|
||||
char upperChar = (char) toupper((unsigned char) c);
|
||||
buffer[len++] = upperChar;
|
||||
|
||||
// --- 4. Rest des Wortes lesen, bis ein Trennzeichen oder das Dateiende erreicht ist ---
|
||||
while ((c = fgetc(file)) != EOF)
|
||||
{
|
||||
// Wenn Komma, Semikolon oder Leerzeichen -> Wort ist zu Ende
|
||||
if (c == ',' || c == ';' || isspace((unsigned char)c))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Nur schreiben, solange noch Platz im Buffer ist (letztes Zeichen für '\0' reservieren!)
|
||||
if (len < bufferSize - 1)
|
||||
{
|
||||
upperChar = (char) toupper((unsigned char) c); // Zeichen in Großbuchstaben
|
||||
buffer[len++] = upperChar; // Zeichen im Buffer speichern
|
||||
}
|
||||
// Wenn kein Platz mehr, restliche Zeichen ignorieren (Vermeidung von Speicherfehlern)
|
||||
}
|
||||
|
||||
// --- 5. String beenden ---
|
||||
buffer[len] = '\0'; // Nullterminator am Ende -> macht aus dem Array einen C-String
|
||||
|
||||
// --- 6. Erfolg melden ---
|
||||
return 1; // 1 = Wort erfolgreich eingelesen
|
||||
}
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------------
|
||||
* HAUPTFUNKTION:
|
||||
* Liest ALLE Wörter aus einer Datei und speichert sie in 'words'.
|
||||
*
|
||||
* - Ruft die Hilfsfunktion 'readSingleWord()' mehrfach auf,
|
||||
* um ein Wort nach dem anderen zu lesen.
|
||||
* - Kopiert jedes gefundene Wort in das große 'words'-Array.
|
||||
* - Zählt, wie viele Wörter tatsächlich gefunden wurden.
|
||||
* ------------------------------------------------------------
|
||||
*/
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
unsigned int count = 0; // zählt, wie viele Wörter gefunden wurden
|
||||
char buffer[MAX_WORD_LEN]; // Zwischenspeicher für EIN Wort (kommt aus der Hilfsfunktion)
|
||||
|
||||
// --- Sicherheitsprüfung ---
|
||||
// Datei darf nicht NULL sein, das Array muss existieren, und es muss Platz vorhanden sein.
|
||||
if (file == NULL || words == NULL || maxWordCount == 0)
|
||||
{
|
||||
return 0; // ungültige Eingaben -> keine Wörter gelesen
|
||||
}
|
||||
|
||||
// --- Hauptschleife ---
|
||||
// Solange:
|
||||
// - noch Platz im 'words'-Array ist (count < maxWordCount)
|
||||
// - UND readSingleWord() ein neues Wort liefern kann
|
||||
while (count < maxWordCount && readSingleWord(file, buffer, sizeof(buffer)))
|
||||
{
|
||||
// --- 1. Wort aus dem Buffer ins große Array kopieren ---
|
||||
// strcpy wäre unsicher -> strncpy schützt vor Überlauf
|
||||
strncpy(words[count], buffer, MAX_WORD_LEN);
|
||||
|
||||
// --- 2. Sicherstellen, dass das Wort immer korrekt endet ---
|
||||
// Wenn das Wort genau MAX_WORD_LEN Zeichen lang war,
|
||||
// könnte der '\0' durch strncpy fehlen -> hier wird er garantiert gesetzt.
|
||||
words[count][MAX_WORD_LEN - 1] = '\0';
|
||||
|
||||
// --- 3. Zähler erhöhen -> nächstes Wort kommt in words[count+1] ---
|
||||
count++;
|
||||
}
|
||||
|
||||
// --- Schleife beendet: entweder kein Wort mehr oder Array ist voll ---
|
||||
// Rückgabe: Anzahl der tatsächlich eingelesenen Wörter
|
||||
return count;
|
||||
}
|
||||
BIN
Start_Windows/libraylib.a
Normal file
BIN
Start_Windows/libraylib.a
Normal file
Binary file not shown.
@ -36,17 +36,16 @@ int main(int argc, char *argv[])
|
||||
// Create the word salad by placing words into grid
|
||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
// TODO:
|
||||
// Check if all words were successfully placed
|
||||
if (placedWords != 0)
|
||||
{
|
||||
// Start the game if successful
|
||||
// error message if some words couldn't be placed
|
||||
|
||||
startGame(wordSalad, SALAD_SIZE, words, wordCount,800);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Print error message if file couldn't be opened
|
||||
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
||||
exitCode = EXIT_FAILURE;
|
||||
fprintf(stderr, "Warning: Only %u out of %u words could be placed.\n", placedWords, wordCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,18 +1,20 @@
|
||||
# --------------------------
|
||||
# Compiler und Flags
|
||||
# --------------------------
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall $(raylibfolder)
|
||||
CFLAGS = -g -Wall -I$(raylibfolder)
|
||||
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
|
||||
BINARIES = ./windows
|
||||
|
||||
raylib_folder = ./raylib
|
||||
raylibfolder = ./raylib
|
||||
unityfolder = ./unity
|
||||
|
||||
# --------------------------
|
||||
# initiales Spiel bauen
|
||||
# Initiales Spiel bauen
|
||||
# --------------------------
|
||||
wordsalad_initial:
|
||||
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Normales Spiel bauen
|
||||
# --------------------------
|
||||
@ -29,7 +31,13 @@ game.o: game.c
|
||||
$(CC) -c $(CFLAGS) game.c
|
||||
|
||||
graphicalGame.o: graphicalGame.c
|
||||
$(CC) -I$(raylib_folder) -c $(CFLAGS) graphicalGame.c
|
||||
$(CC) $(CFLAGS) -c graphicalGame.c
|
||||
|
||||
# --------------------------
|
||||
# Eigene Version bauen
|
||||
# --------------------------
|
||||
wordsalad_myversion: main.c
|
||||
$(CC) $(CFLAGS) -o wordsalad_myversion main.c $(BINARIES)/libwordsalad.a $(LDFLAGS)
|
||||
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
|
||||
1708
Start_Windows/raylib.h
Normal file
1708
Start_Windows/raylib.h
Normal file
File diff suppressed because it is too large
Load Diff
2941
Start_Windows/raymath.h
Normal file
2941
Start_Windows/raymath.h
Normal file
File diff suppressed because it is too large
Load Diff
5262
Start_Windows/rlgl.h
Normal file
5262
Start_Windows/rlgl.h
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user