generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
12
Commits
e5bd6cee97
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c9460eb99 | ||
|
|
81d0e35517 | ||
|
|
25f7fb4d39 | ||
|
|
3e0fe5f977 | ||
|
|
acb4154bec | ||
|
|
85c2c57e7f | ||
|
|
5fe13e782a | ||
|
|
9a7d3d3766 | ||
|
|
b07cf08f26 | ||
|
|
aa9b4dc61a | ||
|
|
62bd5aedde | ||
|
|
0fbfc98c85 |
No files matched your search
+116
-3
@@ -6,18 +6,131 @@
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
|
||||
//TODO: Spiellogik implementieren:
|
||||
// TODO: Spiellogik implementieren:
|
||||
/* * 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)
|
||||
{
|
||||
srand(time(NULL));
|
||||
unsigned int placedWordsCount = 0;
|
||||
// salad = spielfeld (100*100)
|
||||
// searchFieldLen = salad Size = 20
|
||||
// words = array mit den zu plazierenden wörtern
|
||||
// anzahl an zu plazierenden wörtern
|
||||
const char buchstaben[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const int anzahlBuchstaben = 26;
|
||||
|
||||
int positionX = 0;
|
||||
int positionY = 0;
|
||||
int voll = 0;
|
||||
int tries = 0;
|
||||
int leange = 0;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
VERTIKAL,
|
||||
HORIZONTAL
|
||||
} Richtung;
|
||||
|
||||
for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||
{
|
||||
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
salad[i][j] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < wordCount; i++)
|
||||
{
|
||||
|
||||
Richtung vertikal_horizontal = rand() % 2; // 0 = vertikal //1 = horizontal
|
||||
leange = strlen(words[i]);
|
||||
|
||||
tries = 0;
|
||||
|
||||
if (vertikal_horizontal == VERTIKAL)
|
||||
{
|
||||
do
|
||||
{
|
||||
voll = 0;
|
||||
positionX = rand() % (searchFieldLen);
|
||||
positionY = rand() % (searchFieldLen - leange);
|
||||
for (int j = positionY; j < positionY + leange; j++)
|
||||
{ // überprüfung ob alle positionen 0 sind
|
||||
if (salad[j][positionX] != EMPTY_CHAR)
|
||||
{
|
||||
voll = 1;
|
||||
}
|
||||
}
|
||||
tries++;
|
||||
} while (voll == 1 && tries <= MAX_RAND_TRIES_PER_WORD);
|
||||
if (voll == 0)
|
||||
{
|
||||
for (int k = positionY, l = 0; k < positionY + leange; k++, l++)
|
||||
{ // buchstaben holen und setzen
|
||||
salad[k][positionX] = words[i][l];
|
||||
}
|
||||
placedWordsCount++;
|
||||
}
|
||||
else if (voll == 1)
|
||||
printf("Das Wort '%s' konnte nicht plaziert werden\n", words[i]);
|
||||
}
|
||||
else if (vertikal_horizontal == HORIZONTAL) //bessser als funktion, weil fast doppelt
|
||||
{
|
||||
do
|
||||
{
|
||||
voll = 0;
|
||||
positionY = rand() % (searchFieldLen);
|
||||
positionX = rand() % (searchFieldLen - leange);
|
||||
for (int j = positionX; j < positionX + leange; j++)
|
||||
{ // überprüfung ob alle positionen 0 sind
|
||||
if (salad[positionY][j] != EMPTY_CHAR)
|
||||
{
|
||||
voll = 1;
|
||||
}
|
||||
}
|
||||
tries++;
|
||||
} while (voll == 1 && tries <= MAX_RAND_TRIES_PER_WORD);
|
||||
if (voll == 0)
|
||||
{
|
||||
for (int k = positionX, l = 0; k < positionX + leange; k++, l++)
|
||||
{ // buchstaben holen und setzen
|
||||
salad[positionY][k] = words[i][l];
|
||||
}
|
||||
placedWordsCount++;
|
||||
}
|
||||
else if (voll == 1)
|
||||
printf("Das Wort '%s' konnte nicht plaziert werden\n", words[i]); //besser als perror
|
||||
}
|
||||
else
|
||||
printf("Fehler bei Vertikal Horizontal Wert: %d", vertikal_horizontal);
|
||||
}
|
||||
|
||||
for (int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
if (salad[i][j] == EMPTY_CHAR)
|
||||
{
|
||||
salad[i][j] = buchstaben[rand() % anzahlBuchstaben];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placedWordsCount;
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
{
|
||||
|
||||
for (int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
printf(" %c", salad[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
Binary file not shown.
+50
-3
@@ -8,7 +8,54 @@
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
FILE *fopen (const char *file, const char *r); // != NULL???
|
||||
unsigned int count = 0;
|
||||
|
||||
fclose(*file );
|
||||
}
|
||||
char line[MAX_LINE_LEN];
|
||||
const char teiler[] = " ,;\n\t"; //literale
|
||||
char *token = NULL;
|
||||
|
||||
while(fgets(line, sizeof(line), file)){
|
||||
|
||||
//Yeti,Nessie Werwolf; Vampir
|
||||
// token = Yeti
|
||||
token = strtok(line, teiler); //besser wäre: strpbrk
|
||||
|
||||
while (token != NULL)
|
||||
{
|
||||
if (strlen(token) >= MAX_WORD_LEN) {
|
||||
printf("Fehler: eigelesenes Wort ist zu lang\n");
|
||||
return count;
|
||||
}
|
||||
if (count >= maxWordCount){
|
||||
printf("Fehler: zu viele Wörter\n");
|
||||
return count;
|
||||
}
|
||||
|
||||
//hier müsste man den token string in großbuchstaben formatieren
|
||||
for (int i = 0; i < strlen(token); i++)
|
||||
{
|
||||
token[i] = toupper(token[i]);
|
||||
}
|
||||
strncpy(words[count], token, MAX_WORD_LEN-1);
|
||||
words[count][MAX_WORD_LEN-1] = '\0';
|
||||
|
||||
//Nessie Werwolf; Vampir
|
||||
//token= Nessie
|
||||
token = strtok(NULL, teiler);
|
||||
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
// 00M 01a 02x
|
||||
// 10W 11a 12l 13t 14e 15r
|
||||
// 20T 21o 22b 23i 24a 25s
|
||||
|
||||
// array[3][2]
|
||||
// 00 01
|
||||
// 10 11
|
||||
// 20 21
|
||||
Binary file not shown.
+1
-1
@@ -28,7 +28,7 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
unsigned int placedWords = 0;
|
||||
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
||||
|
||||
|
||||
// Read words from file and store in 'words' array
|
||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
||||
fclose(file);
|
||||
|
||||
Binary file not shown.
Binary file not shown.
+115
-3
@@ -6,18 +6,130 @@
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
|
||||
//TODO: Spiellogik implementieren:
|
||||
// TODO: Spiellogik implementieren:
|
||||
/* * 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)
|
||||
{
|
||||
srand(time(NULL));
|
||||
unsigned int placedWordsCount = 0;
|
||||
// salad = spielfeld (100*100)
|
||||
// searchFieldLen = salad Size = 20
|
||||
// words = array mit den zu plazierenden wörtern
|
||||
// anzahl an zu plazierenden wörtern
|
||||
const char buchstaben[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const int anzahlBuchstaben = 26;
|
||||
|
||||
for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||
{
|
||||
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
salad[i][j] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
typedef enum
|
||||
{
|
||||
VERTIKAL,
|
||||
HORIZONTAL
|
||||
} Richtung;
|
||||
|
||||
for (int i = 0; i < wordCount; i++)
|
||||
{
|
||||
Richtung vertikal_horizontal = rand() % 2; // 1 = vertikal //0 = horizontal
|
||||
int leange = strlen(words[i]);
|
||||
int positionX = 0;
|
||||
int positionY = 0;
|
||||
int voll = 0;
|
||||
int tries = 0;
|
||||
|
||||
if (vertikal_horizontal == VERTIKAL)
|
||||
{
|
||||
|
||||
tries = 0;
|
||||
do
|
||||
{
|
||||
voll = 0;
|
||||
positionX = rand() % (searchFieldLen);
|
||||
positionY = rand() % (searchFieldLen - leange);
|
||||
for (int j = positionY; j < positionY + leange; j++)
|
||||
{ // überprüfung ob alle positionen 0 sind
|
||||
if (salad[j][positionX] != EMPTY_CHAR)
|
||||
{
|
||||
voll = 1;
|
||||
}
|
||||
}
|
||||
tries++;
|
||||
} while (voll == 1 && tries <= MAX_RAND_TRIES_PER_WORD);
|
||||
if (voll == 0)
|
||||
{
|
||||
for (int k = positionY, l = 0; k < positionY + leange; k++, l++)
|
||||
{ // buchstaben holen und setzen
|
||||
salad[k][positionX] = words[i][l];
|
||||
}
|
||||
placedWordsCount++;
|
||||
}
|
||||
else if (voll == 1)
|
||||
printf("Das Wort '%s' konnte nicht plaziert werden", words[i]);
|
||||
}
|
||||
else if (vertikal_horizontal == HORIZONTAL)
|
||||
{
|
||||
|
||||
tries = 0;
|
||||
do
|
||||
{
|
||||
voll = 0;
|
||||
positionY = rand() % (searchFieldLen);
|
||||
positionX = rand() % (searchFieldLen - leange);
|
||||
for (int j = positionX; j < positionX + leange; j++)
|
||||
{ // überprüfung ob alle positionen 0 sind
|
||||
if (salad[positionY][j] != EMPTY_CHAR)
|
||||
{
|
||||
voll = 1;
|
||||
}
|
||||
}
|
||||
tries++;
|
||||
} while (voll == 1 && tries <= MAX_RAND_TRIES_PER_WORD);
|
||||
if (voll == 0)
|
||||
{
|
||||
for (int k = positionX, l = 0; k < positionX + leange; k++, l++)
|
||||
{ // buchstaben holen und setzen
|
||||
salad[positionY][k] = words[i][l];
|
||||
}
|
||||
placedWordsCount++;
|
||||
}
|
||||
else if (voll == 1)
|
||||
printf("Das Wort '%s' konnte nicht plaziert werden", words[i]);
|
||||
}
|
||||
else
|
||||
printf("Fehler bei Vertikal Horizontal Wert: %d", vertikal_horizontal);
|
||||
}
|
||||
|
||||
for (int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
if (salad[i][j] == EMPTY_CHAR)
|
||||
{
|
||||
salad[i][j] = buchstaben[rand() % anzahlBuchstaben];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placedWordsCount;
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
{
|
||||
|
||||
for (int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
printf(" %c", salad[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
Binary file not shown.
+50
-1
@@ -8,5 +8,54 @@
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
unsigned int count = 0;
|
||||
|
||||
}
|
||||
char line[MAX_LINE_LEN];
|
||||
char *teiler = " ,;\n\t";
|
||||
char *token;
|
||||
|
||||
while(fgets(line, sizeof(line), file)){
|
||||
|
||||
//Yeti,Nessie Werwolf; Vampir
|
||||
// token = Yeti
|
||||
token = strtok(line, teiler);
|
||||
|
||||
while (token != NULL)
|
||||
{
|
||||
if (strlen(token) >= MAX_WORD_LEN) {
|
||||
printf("Fehler: eigelesenes Wort ist zu lang\n");
|
||||
return count;
|
||||
}
|
||||
if (count >= maxWordCount){
|
||||
printf("Fehler: zu viele Wörter\n");
|
||||
return count;
|
||||
}
|
||||
|
||||
//hier müsste man den token string in großbuchstaben formatieren
|
||||
for (int i = 0; i < strlen(token); i++)
|
||||
{
|
||||
token[i] = toupper(token[i]);
|
||||
}
|
||||
strncpy(words[count], token, MAX_WORD_LEN-1);
|
||||
words[count][MAX_WORD_LEN-1] = '\0';
|
||||
|
||||
//Nessie Werwolf; Vampir
|
||||
//token= Nessie
|
||||
token = strtok(NULL, teiler);
|
||||
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
// 00M 01a 02x
|
||||
// 10W 11a 12l 13t 14e 15r
|
||||
// 20T 21o 22b 23i 24a 25s
|
||||
|
||||
// array[3][2]
|
||||
// 00 01
|
||||
// 10 11
|
||||
// 20 21
|
||||
Binary file not shown.
+13
-5
@@ -12,7 +12,7 @@ int main(int argc, char *argv[])
|
||||
int exitCode = EXIT_SUCCESS;
|
||||
|
||||
// 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]);
|
||||
exitCode = EXIT_FAILURE;
|
||||
@@ -24,7 +24,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
FILE *file = fopen(argv[1], "r");
|
||||
|
||||
if(file != NULL)
|
||||
if (file != NULL)
|
||||
{
|
||||
unsigned int placedWords = 0;
|
||||
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
||||
@@ -38,9 +38,17 @@ int main(int argc, char *argv[])
|
||||
|
||||
// TODO:
|
||||
// Check if all words were successfully placed
|
||||
// Start the game if successful
|
||||
// error message if some words couldn't be placed
|
||||
|
||||
if (placedWords == wordCount)
|
||||
{
|
||||
// Start the game if successful
|
||||
showWordSalad(wordSalad, SALAD_SIZE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// error message if some words couldn't be placed
|
||||
printf("Some words could not be placed");
|
||||
exitCode = EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
+11
-2
@@ -1,6 +1,6 @@
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall
|
||||
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
|
||||
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
|
||||
BINARIES = ./windows
|
||||
|
||||
raylibfolder = ./raylib
|
||||
@@ -12,10 +12,19 @@ unityfolder = ./unity
|
||||
wordsalad_initial:
|
||||
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Eigenes Spiel bauen
|
||||
# --------------------------
|
||||
|
||||
wordsalad_my: main.o graphicalGame.o $(BINARIES)/libraylib.a
|
||||
$(CC) $(CFLAGS) -o wordsalad_my main.o graphicalGame.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Normales Spiel bauen
|
||||
# --------------------------
|
||||
all: 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)
|
||||
|
||||
main.o: main.c
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
Hund,Katze; Maus
|
||||
Whitespace-only changes.
@@ -0,0 +1,3 @@
|
||||
Apfel
|
||||
Banane
|
||||
Kiwi
|
||||
Reference in new issue
Block a user