generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
No commits in common. "main" and "input" have entirely different histories.
@ -2,7 +2,6 @@
|
|||||||
#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
|
||||||
@ -14,111 +13,11 @@
|
|||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// 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)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
|
||||||
int placedWords = 0;
|
|
||||||
|
|
||||||
// Array mit . füllen
|
|
||||||
for (int r = 0; r < searchFieldLen; r++){
|
|
||||||
for(int s = 0; s < searchFieldLen; s++){
|
|
||||||
salad[r][s] = '.';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// -> erst wenn die ganze Länge des Wortes frei ist, array hinzufügen
|
|
||||||
// MAX Versuche das Wort zu platzieren == 10
|
|
||||||
|
|
||||||
|
|
||||||
// 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
|
|
||||||
|
|
||||||
for(int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++){
|
|
||||||
int richtung = rand()% 2;
|
|
||||||
int belegt = 0;
|
|
||||||
|
|
||||||
// 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
|
|
||||||
for(int l = 0; l < searchFieldLen; l++){
|
|
||||||
for(int m = 0; m < searchFieldLen; m++ ){
|
|
||||||
if(isalpha(salad[l][m]) == 0 ){
|
|
||||||
// zufällige Buchstaben erzeugen
|
|
||||||
char alphabet [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
||||||
int laenge = strlen(alphabet);
|
|
||||||
int zufallszahl = rand()% laenge;
|
|
||||||
char zufallsbuchstabe = alphabet[zufallszahl];
|
|
||||||
// zufällige Buchstaben einfügen
|
|
||||||
salad[l][m] = zufallsbuchstabe;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return placedWords;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
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++){
|
}
|
||||||
printf("%c", salad[p][q]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Binary file not shown.
BIN
Start_Windows/graphicalGame.o
Normal file
BIN
Start_Windows/graphicalGame.o
Normal file
Binary file not shown.
@ -8,6 +8,7 @@
|
|||||||
// 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 puffer[MAX_LINE_LEN]; //Array für eingelesene Zeile
|
char puffer[MAX_LINE_LEN]; //Array für eingelesene Zeile
|
||||||
int counter = 0; //Zähler für Anzahl eingelesener Wörter
|
int counter = 0; //Zähler für Anzahl eingelesener Wörter
|
||||||
int i;
|
int i;
|
||||||
@ -18,18 +19,14 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|||||||
|
|
||||||
while(word_parts != NULL && counter < maxWordCount)
|
while(word_parts != NULL && counter < maxWordCount)
|
||||||
{
|
{
|
||||||
// Kopiere das Wort in words-Array
|
for(i = 0; i < MAX_WORD_LEN -1 && word_parts[i] != '\0'; i++)
|
||||||
strncpy(words[counter], word_parts, MAX_WORD_LEN - 1);
|
{
|
||||||
words[counter][MAX_WORD_LEN - 1] = '\0';
|
words[counter][i] = toupper(word_parts[i]); //Großbuchstaben erzeugen
|
||||||
|
}
|
||||||
// Alles in Großbuchstaben umwandeln
|
words[counter][i] = '\0'; //Stringdefinition vervollständigen
|
||||||
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
|
||||||
|
word_parts = strtok(NULL, ",;\n\t/. "); //NULL für Zeiger angeben
|
||||||
}
|
}
|
||||||
return counter;
|
return counter;
|
||||||
}
|
}
|
||||||
Binary file not shown.
@ -40,15 +40,7 @@ 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,750);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("Error. The words couldn't be placed.\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
BIN
Start_Windows/main.o
Normal file
BIN
Start_Windows/main.o
Normal file
Binary file not shown.
@ -33,7 +33,7 @@ graphicalGame.o: graphicalGame.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
TEST_BIN = runTests2
|
TEST_BIN = runTests
|
||||||
|
|
||||||
test: input.o game.o unit_tests.c
|
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
|
$(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a
|
||||||
@ -43,7 +43,8 @@ test: input.o game.o unit_tests.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
clean:
|
clean:
|
||||||
del /f *.o *.exe
|
del /f *.o *.exe
|
||||||
|
|
||||||
#hier eigene wordsalad kopieren, library rauslassen
|
#hier eigene wordsalad kopieren, library rauslassen
|
||||||
|
|
||||||
wordsalad_myversion: main.o input.o game.o graphicalGame.o
|
wordsalad_myversion: main.o input.o game.o graphicalGame.o $(BINARIES)/libwordsalad.a
|
||||||
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
$(CC) $(CFLAGS) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libwordsalad.a $(LDFLAGS)
|
||||||
Binary file not shown.
@ -1 +0,0 @@
|
|||||||
Hund,Katze; Maus
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
Apfel
|
|
||||||
Banane
|
|
||||||
Kiwi
|
|
||||||
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user