generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e551a025be | ||
|
|
b427e8e547 | ||
|
|
5f567fa968 | ||
|
|
36bfbcb156 | ||
|
|
a78c1e2ea2 | ||
|
|
c18b6a4529 | ||
|
|
013eba7b3d | ||
|
|
cd3dd0a4d6 | ||
|
|
b9138e2097 | ||
|
|
28a2aa1ea1 | ||
|
|
cddbfb3029 | ||
|
|
5c1aea7a88 | ||
|
|
f47f3aef7c | ||
|
|
b34074233d | ||
|
|
bbad6afd45 | ||
|
|
9e172b664f | ||
|
|
c26a55f965 | ||
|
|
16b0b9a450 | ||
|
|
85a79e9235 | ||
|
|
506c091bf6 | ||
|
|
4fea2c9f7b | ||
|
|
817f35a154 |
@ -2,6 +2,7 @@
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
@ -14,88 +15,83 @@
|
||||
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));
|
||||
|
||||
int placedWords = 0;
|
||||
unsigned int tries = 0;
|
||||
unsigned int check_direction = 0, check_overlap = 0;
|
||||
int zeile = 0, spalte = 0, len = 0;
|
||||
|
||||
//1. Schritt: Alle Felder mit 0 befüllen
|
||||
for(int m = 0; m < searchFieldLen; m++) //m Zeilen
|
||||
{
|
||||
for(int n = 0; n < searchFieldLen; n++) //n Spalte
|
||||
{
|
||||
salad[m][n] = 0;
|
||||
|
||||
// Array mit . füllen
|
||||
for (int r = 0; r < searchFieldLen; r++){
|
||||
for(int s = 0; s < searchFieldLen; s++){
|
||||
salad[r][s] = '.';
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Schritt: Prüfen ob Wort in Array geschrieben darf
|
||||
for(int num_words = 0; num_words < wordCount && num_words < searchFieldLen; num_words++) //eingelesen Wörter zählen
|
||||
{
|
||||
len = strlen(words[num_words]); //Größe des Wortes ermitteln
|
||||
// -> erst wenn die ganze Länge des Wortes frei ist, array hinzufügen
|
||||
// MAX Versuche das Wort zu platzieren == 10
|
||||
|
||||
|
||||
if (len < searchFieldLen)
|
||||
return -1; //ERROR, falls Wort größer als Größe des Feldes (unnötig, da bei worteingabe bereits überprüft)
|
||||
// Wörter in salad einsortieren
|
||||
for (int num_words = 0; num_words < wordCount; num_words++){
|
||||
|
||||
int laenge = strlen(words[num_words]);
|
||||
|
||||
// muss noch prüfen, ob in der Zeile schon was ist
|
||||
// 1 ist horizontal, 0 ist vertikal
|
||||
// isalpha == 0 -> kein Buchstabe
|
||||
|
||||
while(tries < MAX_RAND_TRIES_PER_WORD)
|
||||
{
|
||||
// zufällige Richtung auswählen
|
||||
int direction = rand() % 2; // 0 = VERTIKAL, 1 = HORIZONTAL
|
||||
|
||||
// zufälliger Startpunkt, Startkoordinaten
|
||||
if (direction == 0) // 0 = VERTIKAL -> Spalte egal
|
||||
{
|
||||
zeile = rand() % (searchFieldLen - len);
|
||||
spalte = rand() % (searchFieldLen);
|
||||
check_direction = 1;
|
||||
}
|
||||
for(int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++){
|
||||
int richtung = rand()% 2;
|
||||
int belegt = 0;
|
||||
|
||||
if (direction == 1) // 1 = HORIZONTAL -> Zeile egal
|
||||
{
|
||||
zeile = rand() % (searchFieldLen);
|
||||
spalte = rand() % (searchFieldLen - len);
|
||||
check_direction = 1;
|
||||
}
|
||||
|
||||
//Prüfen auf Überlappung ----------> Logikfehler
|
||||
for(int i_overlap = 0; i_overlap < len; i_overlap++)
|
||||
{
|
||||
if (direction == 0) // 0 = VERTIKAL
|
||||
{
|
||||
if (words[num_words][i_overlap] == salad[zeile][spalte + i_overlap]);
|
||||
check_overlap = 1;
|
||||
}
|
||||
else if (direction == 1) // 1 = HORIZONTAL
|
||||
{
|
||||
if (words[num_words][i_overlap] == salad[zeile + i_overlap][spalte]);
|
||||
check_overlap = 1;
|
||||
}
|
||||
if(check_overlap == 0)
|
||||
{
|
||||
tries++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//3. Schritt: Wort in Wortsalat schreiben
|
||||
if(check_direction == 1 && check_overlap == 1 && tries < MAX_RAND_TRIES_PER_WORD)
|
||||
{
|
||||
for(int i_set = 0; i_set < len; i_set++)
|
||||
{
|
||||
if (direction == 0) // 0 = VERTIKAL
|
||||
{
|
||||
salad[zeile + i_set][spalte] = words[num_words][i_set];
|
||||
placedWords++;
|
||||
}
|
||||
|
||||
if (direction == 1) // 1 = HORIZONTAL
|
||||
{
|
||||
salad[zeile][spalte + i_set] = words[num_words][i_set];
|
||||
placedWords++;
|
||||
}
|
||||
// horizontale Eingabe
|
||||
if (richtung == 1){
|
||||
int zeile = rand()% searchFieldLen;
|
||||
int spalte = rand()% (searchFieldLen- laenge +1);
|
||||
|
||||
//prüft, ob die herausgesuchte Zeile noch frei ist
|
||||
// belegt == 1 -> Zeile nicht frei
|
||||
for (int o = spalte; o < spalte + laenge; o++){
|
||||
if(salad[zeile][o] != '.'){
|
||||
belegt = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
|
||||
if (belegt == 0){
|
||||
for(int k = 0; k < laenge; k++){
|
||||
salad[zeile][spalte + k] = words[num_words][k];
|
||||
|
||||
}
|
||||
placedWords++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// vertikale Eingabe
|
||||
else if (richtung == 0){
|
||||
|
||||
int zeile = rand()% (searchFieldLen - laenge +1);
|
||||
int spalte = rand()% searchFieldLen;
|
||||
|
||||
//prüft, ob die herausgesuchte Zeile noch frei ist
|
||||
for (int n = zeile; n < zeile + laenge; n++){
|
||||
if(salad[n][spalte] != '.'){
|
||||
belegt = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
|
||||
if (belegt == 0){
|
||||
for(int j = 0; j < laenge; j++){
|
||||
salad[zeile + j][spalte] = words[num_words][j];
|
||||
|
||||
}
|
||||
placedWords++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// fügt zufällige Buchstaben ein
|
||||
@ -103,7 +99,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
for(int m = 0; m < searchFieldLen; m++ ){
|
||||
if(isalpha(salad[l][m]) == 0 ){
|
||||
// zufällige Buchstaben erzeugen
|
||||
char alphabet [] = "abcdefghijklmnopqrstuvwxyz";
|
||||
char alphabet [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
int laenge = strlen(alphabet);
|
||||
int zufallszahl = rand()% laenge;
|
||||
char zufallsbuchstabe = alphabet[zufallszahl];
|
||||
@ -112,20 +108,17 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placedWords; //platzierte Wörter zurückgeben
|
||||
|
||||
return placedWords;
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
{
|
||||
for(int p = 0; p < searchFieldLen; p++)
|
||||
{
|
||||
for(int q = 0; q < searchFieldLen; q++)
|
||||
{
|
||||
for(int p = 0; p < searchFieldLen; p++){
|
||||
for(int q = 0; q < searchFieldLen; q++){
|
||||
printf("%c", salad[p][q]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@ -8,37 +8,28 @@
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
// MAX_WORD_LEN 100
|
||||
// MAX_LINE_LEN 1024
|
||||
// *file ist Datei
|
||||
// words ist Array
|
||||
// MAX_WORD_LEN ist maximale Wortlänge
|
||||
// maxWordCount ist maximale Anzahl an Wörtern
|
||||
char puffer[MAX_LINE_LEN];
|
||||
int counter = 0;
|
||||
char puffer[MAX_LINE_LEN]; //Array für eingelesene Zeile
|
||||
int counter = 0; //Zähler für Anzahl eingelesener Wörter
|
||||
int i;
|
||||
|
||||
while(fgets(puffer, MAX_LINE_LEN-1, file) != NULL && counter < maxWordCount)
|
||||
{
|
||||
char *parts = strtok(puffer, ",;\n\t/. ");
|
||||
char *word_parts = strtok(puffer, ",;\n\t/. "); //Wörter aufteilen
|
||||
|
||||
while(parts != NULL && counter < maxWordCount)
|
||||
while(word_parts != NULL && counter < maxWordCount)
|
||||
{
|
||||
//strncpy(words[counter][MAX_WORD_LEN], puffer, MAX_LINE_LEN-1);
|
||||
//words[counter][MAX_WORD_LEN-1] = "\0";
|
||||
//Großbuchstaben
|
||||
for(int i = 0; i < MAX_WORD_LEN -1 && parts[i] != '\0'; i++)
|
||||
{
|
||||
words[counter][i] = toupper(parts[i]);
|
||||
words[counter][i] = '\0';
|
||||
}
|
||||
// Kopiere das Wort in words-Array
|
||||
strncpy(words[counter], word_parts, MAX_WORD_LEN - 1);
|
||||
words[counter][MAX_WORD_LEN - 1] = '\0';
|
||||
|
||||
// Alles in Großbuchstaben umwandeln
|
||||
for(i = 0; words[counter][i] != '\0'; i++)
|
||||
{
|
||||
words[counter][i] = toupper(words[counter][i]);
|
||||
}
|
||||
counter++; // Wort eingelesen, Wortzähler erhöhen
|
||||
word_parts = strtok(NULL, ",;\n\t/. "); //NULL für Zeiger angeben
|
||||
}
|
||||
counter++; // Wort eingelesen -> Wortzähler erhöhen
|
||||
parts = strtok(NULL, ",;\n\t/. ");
|
||||
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//TODO: Wörter auf Zahlen, Umlaute überprüfen -> ändern
|
||||
}
|
||||
Binary file not shown.
@ -40,9 +40,15 @@ int main(int argc, char *argv[])
|
||||
// Check if all words were successfully placed
|
||||
// Start the game if successful
|
||||
// error message if some words couldn't be placed
|
||||
|
||||
// wordcount mit placedwords vergleichen
|
||||
|
||||
if (placedWords == wordCount){
|
||||
startGame(wordSalad,SALAD_SIZE,words, wordCount,750);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Error. The words couldn't be placed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@ -33,19 +33,17 @@ graphicalGame.o: graphicalGame.c
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
# --------------------------
|
||||
TEST_BIN = runTests
|
||||
TEST_BIN = runTests2
|
||||
|
||||
test: input.o game.o unit_tests.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
# --------------------------
|
||||
clean:
|
||||
del /f *.o *.exe
|
||||
|
||||
#hier eigene wordsalad kopieren, library rauslassen
|
||||
|
||||
wordsalad_myversion: main.o input.o game.o graphicalGame.o $(BINARIES)/libwordsalad.a
|
||||
$(CC) $(CFLAGS) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libwordsalad.a $(LDFLAGS)
|
||||
wordsalad_myversion: main.o input.o game.o graphicalGame.o
|
||||
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
BIN
Start_Windows/runTests2.exe
Normal file
BIN
Start_Windows/runTests2.exe
Normal file
Binary file not shown.
1
Start_Windows/testwords_delims.txt
Normal file
1
Start_Windows/testwords_delims.txt
Normal file
@ -0,0 +1 @@
|
||||
Hund,Katze; Maus
|
||||
0
Start_Windows/testwords_empty.txt
Normal file
0
Start_Windows/testwords_empty.txt
Normal file
3
Start_Windows/testwords_simple.txt
Normal file
3
Start_Windows/testwords_simple.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Apfel
|
||||
Banane
|
||||
Kiwi
|
||||
Binary file not shown.
Binary file not shown.
BIN
Start_Windows/windows/unity.o
Normal file
BIN
Start_Windows/windows/unity.o
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user