generated from freudenreichan/info2Praktikum-Wortsalat
verbesserte kommentare und kleine änderung in input.c
This commit is contained in:
parent
76ecf700bb
commit
2b28d19e79
@ -10,22 +10,21 @@
|
|||||||
/* * 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 */
|
||||||
|
|
||||||
//edited
|
//enum für richtungen
|
||||||
typedef enum { HORIZONTAL, VERTICAL } Direction;
|
typedef enum { HORIZONTAL, VERTICAL } Direction;
|
||||||
|
|
||||||
//edited: bestimmt zufällige richtung
|
//bestimmt zufällige richtung
|
||||||
static Direction randomDirection(void)
|
static Direction randomDirection(void)
|
||||||
{
|
{
|
||||||
return (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
|
return (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
//edited
|
|
||||||
// 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)); // seed bei jedem funktionsaufruf neu setzen
|
srand(time(NULL)); //damit bei jedme aufruf "neuer zufall" (seed wird immer auf aktuelle zeit gesetzt)
|
||||||
|
|
||||||
// spielfeld initialisieren (leer)
|
// spielfeld initialisieren mit nullen
|
||||||
for (int y = 0; y < searchFieldLen; y++) {
|
for (int y = 0; y < searchFieldLen; y++) {
|
||||||
for (int x = 0; x < searchFieldLen; x++) {
|
for (int x = 0; x < searchFieldLen; x++) {
|
||||||
salad[y][x] = EMPTY_CHAR;
|
salad[y][x] = EMPTY_CHAR;
|
||||||
@ -34,25 +33,25 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
|
|
||||||
int placedWrdsAmnt = 0;
|
int placedWrdsAmnt = 0;
|
||||||
|
|
||||||
//wörter platzieren, 1 schleife = 1 wort
|
//wörter platzieren
|
||||||
for(int w = 0; w < wordCount; w++){
|
for(int w = 0; w < wordCount; w++){
|
||||||
const char *word = words[w]; // *word ist immer ein wort von "words" (zeigt auf eine zeile)
|
const char *word = words[w];
|
||||||
int len = strlen(word);
|
int len = strlen(word);
|
||||||
|
|
||||||
if(len > searchFieldLen ) // wenn wort zu lang, nimm nächstes wort
|
if(len > searchFieldLen )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int placed = 0;
|
int placed = 0;
|
||||||
|
|
||||||
// versuche mehrere random positionen, 1 schleife = 1 position
|
//versucht max 10 versch. positionen
|
||||||
for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++){
|
for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++){
|
||||||
Direction dir = randomDirection();
|
Direction dir = randomDirection();
|
||||||
int startX = 0;
|
int startX = 0;
|
||||||
int startY = 0;
|
int startY = 0;
|
||||||
|
|
||||||
if(dir == HORIZONTAL){
|
if(dir == HORIZONTAL){
|
||||||
startX = rand() % (searchFieldLen - len + 1); //zeile ist 10 (0-9) breit, wort 5 lang -> höchstens in 5-9 -> 10 - 5 + 1 = 6 (startet random iwo von 0 bis 5)✅
|
startX = rand() % (searchFieldLen - len + 1); //zb zeile ist 10 breit (0-9), wort 5 lang -> start nur in 0-5 möglich -> 10 - 5 + 1 = 6 (rand() % 6 = 0 bis 5)
|
||||||
startY = rand() % searchFieldLen; //egal
|
startY = rand() % searchFieldLen; //egal wo
|
||||||
}else{
|
}else{
|
||||||
startX = rand() % searchFieldLen;
|
startX = rand() % searchFieldLen;
|
||||||
startY = rand() % (searchFieldLen - len + 1);
|
startY = rand() % (searchFieldLen - len + 1);
|
||||||
@ -61,18 +60,19 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
//passt wort rein? (komplett leer/hat einen überschneidenen buchstaben) --> geht ganze wort platz durch (vert oder horiz), 1 schleife = 1 buchstabe im wort
|
//passt wort rein? (komplett leer/hat einen überschneidenen buchstaben) --> geht ganze wort platz durch (vert oder horiz), 1 schleife = 1 buchstabe im wort
|
||||||
int fits = 1;
|
int fits = 1;
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
int x = startX + (dir == HORIZONTAL ? i : 0);
|
int x = startX + (dir == HORIZONTAL ? i : 0); //wenn zb horiz. dann erhöhe x um i, sonst bleibt x konst.
|
||||||
int y = startY + (dir == VERTICAL ? i : 0);
|
int y = startY + (dir == VERTICAL ? i : 0);
|
||||||
if (salad[y][x] != EMPTY_CHAR && salad[y][x] != word[i]) { // wenn die reihe mit einem buchstaben befüllt der sich nd mit wort überschneidet -> fitted nd
|
//wenn i.d. reihe/spalte ein buchstabe der sich nicht mit dem aktuellen wort überschneidet -> fitted nicht, stopt DIESE for loop
|
||||||
|
if (salad[y][x] != EMPTY_CHAR && salad[y][x] != word[i]) {
|
||||||
fits = 0;
|
fits = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!fits){
|
if(!fits){
|
||||||
continue; // versucht nächste position
|
continue; //wenn wort nicht fitted nächster positionsversuch
|
||||||
}
|
}
|
||||||
|
|
||||||
//place word wenn posi passt
|
//wenn wort fitted -> setze ins spielfeld
|
||||||
for(int i = 0; i < len; i++){
|
for(int i = 0; i < len; i++){
|
||||||
int x = startX + (dir == HORIZONTAL ? i : 0);
|
int x = startX + (dir == HORIZONTAL ? i : 0);
|
||||||
int y = startY + (dir == VERTICAL ? i : 0);
|
int y = startY + (dir == VERTICAL ? i : 0);
|
||||||
|
|||||||
@ -9,29 +9,30 @@
|
|||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
char zeile[500];
|
char zeile[500];
|
||||||
|
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) //words[zeilen also wörter][spalten also anzahl]
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
int anzahlWoerter = 0;
|
int anzahlWoerter = 0;
|
||||||
|
|
||||||
while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){ // eine zeile
|
//1 durchlauf = 1 zeile, solange wortlimit nicht erreicht und fgets eine zeile liefern kann
|
||||||
|
while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){
|
||||||
char *wort = strtok(zeile, ",; \n");
|
char *wort = strtok(zeile, ",; \n");
|
||||||
|
|
||||||
|
//1 durchlauf = 1 wort, solange weiteres wort vorhanden und limit nicht erreicht
|
||||||
while(wort != NULL && anzahlWoerter <= maxWordCount){
|
while(wort != NULL && anzahlWoerter <= maxWordCount){
|
||||||
if(strlen(wort) == 0){
|
if(strlen(wort) == 0){
|
||||||
wort = strtok(NULL, ",; \n");
|
wort = strtok(NULL, ",; \n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
strncpy(words[anzahlWoerter], wort, MAX_WORD_LEN -1);
|
strcpy(words[anzahlWoerter], wort);
|
||||||
words[anzahlWoerter][MAX_WORD_LEN - 1]= '\0';
|
|
||||||
|
|
||||||
|
//aktuelles wort gross schreiben
|
||||||
for(int i = 0; words[anzahlWoerter][i]; i++){
|
for(int i = 0; words[anzahlWoerter][i]; i++){
|
||||||
words[anzahlWoerter][i] = toupper((unsigned char)words[anzahlWoerter][i]); //jedes wort das eingelesen wurde wird komplett gross geschrieben, unsigned damit toupper kein neg. arg bekommt (zb umlaute)
|
words[anzahlWoerter][i] = toupper((unsigned char)words[anzahlWoerter][i]); //unsigned damit toupper keie neg. zahlen übergeben bekommt bei zb sonderzeichen und umlauten
|
||||||
}
|
}
|
||||||
|
|
||||||
anzahlWoerter++;
|
anzahlWoerter++;
|
||||||
wort = strtok(NULL, ",; \n");
|
wort = strtok(NULL, ",; \n");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return anzahlWoerter;
|
return anzahlWoerter;
|
||||||
|
|||||||
@ -30,18 +30,18 @@ int main(int argc, char *argv[])
|
|||||||
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
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
|
// Read words from file and store in 'words' array
|
||||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); //funktion aus input.c, returned anzahl wörter
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
// 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); //funktion aus game.c, returned in spielfeld platzierte wörter
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// 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
|
||||||
int windowWidth = 880;
|
int windowWidth = 600;
|
||||||
|
|
||||||
|
//checkt ob alle wörter aus .txt übernommen wurden
|
||||||
if(placedWords == wordCount){
|
if(placedWords == wordCount){
|
||||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, windowWidth);
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, windowWidth);
|
||||||
}else{
|
}else{
|
||||||
|
|||||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user