Compare commits

...
13 Commits
7 changed files with 138 additions and 13 deletions

No files matched your search

+85 -1
View File
@@ -2,16 +2,100 @@
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
#define MAX_ATTEMPTS 42 //Die Antwort auf das Leben, das Universum, alles
#define FEHLER_LAENGE_WORT -1
#define FEHLER_SETZEN -2
#define ERFOLG 1
//TODO: Spiellogik implementieren: //TODO: Spiellogik implementieren:
/* * 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 */
// 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)
{ {
//befüllen des Arrays mit '\0'
for(unsigned int i = 0; i < searchFieldLen; i++)
{
for(unsigned int j = 0; j < searchFieldLen; j++)
{
salad[i][j] = '\0';
}
}
//erstellen von Richtungsvektoren
int dx[] = {1, 0};
int dy[] = {0, 1};
//befüllen mit den Wörtern
for(unsigned int w = 0; w < wordCount; w++)
{
int laenge = strlen(words[w]);
//test ob Wort theoretisch reinpasst; was machen wir aber wenn ein Wort fast genauso lang ist wie das Feld? Wenn am Ende noch ein riesiges Wort eingesetzt werden soll, wird es nie reinpassen
if(laenge > searchFieldLen)
{
return FEHLER_LAENGE_WORT;
}
int placed = 0;
int attempts = 0;
while(!placed && attempts < MAX_ATTEMPTS)
{
attempts =+ 1;
int dir = rand() % 2; //Richtung: 0 = vertikal; 1 = horizontal
int zeile = rand() % searchFieldLen;
int spalte = rand() % searchFieldLen;
if ((dir == 0 && (zeile + laenge > searchFieldLen)) ||
(dir == 1 && (spalte + laenge > searchFieldLen)))
{
continue;
}
int kollision = 0;
for(int i = 0; i < laenge; i++) //Kollision mit anderen Wörtern vermeiden
{
int z = zeile + dy[dir] * i;
int s = spalte + dx[dir] * i;
if(salad[z][s] != '\0' && salad[z][s] != words[w][i])
{
kollision = 1;
break;
}
}
if(!kollision) // keine kollision, also setzten der buchstaben
{
for(int i = 0; i < laenge; i++)
{
int z = zeile + dy[dir] * i;
int s = spalte + dx[dir] * i;
salad[z][s] = words[w][i];
}
placed = 1;
}
}
if(!placed) //überprüft ob das Wort gesetzt wurde, wenn nicht Fehlerausgabe
{
return FEHLER_SETZEN;
}
}
//befüllen mit zufälligen Buchstaben
for(unsigned int i = 0; i < searchFieldLen; i++)
{
for(unsigned int j = 0; j < searchFieldLen; j++)
{
if(salad[i][j] == '\0')
{
salad[i][j] = 'A' + rand() % 26;
}
}
}
return (int)wordCount;
} }
+1
View File
@@ -7,6 +7,7 @@
#define MAX_MESSAGE_LEN 256 #define MAX_MESSAGE_LEN 256
#define MAX_SOLUTION_WORD_LEN 16 #define MAX_SOLUTION_WORD_LEN 16
typedef struct typedef struct
{ {
Vector2 startPosition; Vector2 startPosition;
+37 -3
View File
@@ -5,8 +5,42 @@
// TODO: // TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
// Read words from file and store in 'words' array // Read words from file and store in 'words'
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
int readWords(FILE *file, char words[][MAX_WORD_LEN],
unsigned int maxWordCount)
{
if (file == NULL) {
return 0;
}
char line[MAX_LINE_LEN];
unsigned int wordCount = 0;
while (wordCount < maxWordCount &&
fgets(line, sizeof(line), file) != NULL) {
char *word = strtok(line, " ,;\n\t\r");
while (word != NULL && wordCount < maxWordCount)
{
strncpy(words[wordCount], word, MAX_WORD_LEN - 1);
words[wordCount][MAX_WORD_LEN - 1] = '\0';
unsigned int i = 0;
while (words[wordCount][i] != '\0') {
words[wordCount][i] =
(char)toupper((unsigned char)words[wordCount][i]);// char davor da toupper einen int wert liefert und einen unsigned char bekommt
i++;
}
wordCount++;
word = strtok(NULL, " ,;\n\t\r");
}
}
return (int)wordCount;
} }
+11 -9
View File
@@ -1,24 +1,26 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <time.h>
#include "input.h" #include "input.h"
#include "game.h" #include "game.h"
#include <time.h>
#include "graphicalGame.h" #include "graphicalGame.h"
#define MAX_NUMBER_OF_WORDS 100 #define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20 #define SALAD_SIZE 20
int main(int argc, char *argv[]) int main(int argc, char *argv[]) {
{
int exitCode = EXIT_SUCCESS; int exitCode = EXIT_SUCCESS;
srand(time(NULL)); //Seed für Zufallszahlen
// Check if the correct number of arguments is provided // Check if the correct number of arguments is provided
if(argc != 2) if(argc != 2)
{ {
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]); fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
exitCode = EXIT_FAILURE; exitCode = EXIT_FAILURE;
} }
else else {
{
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
unsigned int wordCount = 0; unsigned int wordCount = 0;
@@ -36,11 +38,10 @@ 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: if (placedWords == (int)wordCount)
// Check if all words were successfully placed {
// Start the game if successful // Alle Wörter wurden erfolgreich platziert
// error message if some words couldn't be placed startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
} }
else else
{ {
@@ -52,3 +53,4 @@ int main(int argc, char *argv[])
return exitCode; return exitCode;
} }
}
+1
View File
@@ -0,0 +1 @@
Hund,Katze; Maus
View File
Whitespace-only changes.
+3
View File
@@ -0,0 +1,3 @@
Apfel
Banane
Kiwi