forked from freudenreichan/info2Praktikum-Wortsalat
Compare commits
4
Commits
1d87ea69bc
...
Spiellogik
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60bb394fb7 | ||
|
|
d7c7fe76f3 | ||
|
|
1725da2148 | ||
|
|
bd830b2044 |
No files matched your search
+17
-12
@@ -6,7 +6,7 @@
|
|||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
//TODO: Spiellogik implementieren:
|
//DONE: 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 */
|
||||||
|
|
||||||
@@ -16,16 +16,21 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
|
|
||||||
srand((unsigned int)time(NULL));
|
srand((unsigned int)time(NULL));
|
||||||
|
|
||||||
|
typedef enum{
|
||||||
|
Horizontal,
|
||||||
|
Vertikal
|
||||||
|
} Direction;
|
||||||
|
|
||||||
for (unsigned int row = 0; row < searchFieldLen; row++)
|
for (unsigned int row = 0; row < searchFieldLen; row++)
|
||||||
{
|
{
|
||||||
|
|
||||||
for (unsigned int col = 0; col < searchFieldLen; col++)
|
for (unsigned int col = 0; col < searchFieldLen; col++)
|
||||||
{
|
{
|
||||||
salad[row][col] = EMPTY_CHAR; // Initialize all cells to empty
|
salad[row][col] = EMPTY_CHAR; // Alle Felder leer setzen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int placedWords = 0;
|
unsigned int placedWords = 0;
|
||||||
|
|
||||||
// 2. Alle Wörter nacheinander platzieren
|
// 2. Alle Wörter nacheinander platzieren
|
||||||
for (unsigned int i = 0; i < wordCount; i++)
|
for (unsigned int i = 0; i < wordCount; i++)
|
||||||
@@ -43,9 +48,9 @@ for (unsigned int row = 0; row < searchFieldLen; row++)
|
|||||||
|
|
||||||
unsigned int startRow = 0;
|
unsigned int startRow = 0;
|
||||||
unsigned int startCol = 0;
|
unsigned int startCol = 0;
|
||||||
unsigned int direction = rand() % 2;
|
Direction direction = (Direction)(rand()%2);
|
||||||
|
|
||||||
if (direction == 0) // Horizontal
|
if (direction == Horizontal)
|
||||||
{
|
{
|
||||||
startRow = rand() % searchFieldLen;
|
startRow = rand() % searchFieldLen;
|
||||||
startCol = rand() % (searchFieldLen - wordLen + 1);
|
startCol = rand() % (searchFieldLen - wordLen + 1);
|
||||||
@@ -56,12 +61,12 @@ for (unsigned int row = 0; row < searchFieldLen; row++)
|
|||||||
startCol = rand() % searchFieldLen;
|
startCol = rand() % searchFieldLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Überprüfen, ob das Wort an der gewählten Position platziert werden kann
|
// Überprüfen ob das Wort an der gewählten Position platziert werden kann
|
||||||
int canPlace = 1;
|
int canPlace = 1;
|
||||||
|
|
||||||
for (unsigned int j = 0; j < wordLen; j++)
|
for (unsigned int j = 0; j < wordLen; j++)
|
||||||
{
|
{
|
||||||
if (direction == 0) // Horizontal
|
if (direction == Horizontal)
|
||||||
{
|
{
|
||||||
if (salad[startRow][startCol + j] != EMPTY_CHAR && salad[startRow][startCol + j] != words[i][j])
|
if (salad[startRow][startCol + j] != EMPTY_CHAR && salad[startRow][startCol + j] != words[i][j])
|
||||||
{
|
{
|
||||||
@@ -84,7 +89,7 @@ for (unsigned int row = 0; row < searchFieldLen; row++)
|
|||||||
// Wort platzieren
|
// Wort platzieren
|
||||||
for (unsigned int j = 0; j < wordLen; j++)
|
for (unsigned int j = 0; j < wordLen; j++)
|
||||||
{
|
{
|
||||||
if (direction == 0) // Horizontal
|
if (direction == Horizontal)
|
||||||
{
|
{
|
||||||
salad[startRow][startCol + j] = words[i][j];
|
salad[startRow][startCol + j] = words[i][j];
|
||||||
}
|
}
|
||||||
@@ -94,9 +99,9 @@ for (unsigned int row = 0; row < searchFieldLen; row++)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
placedWords++;
|
placedWords++;
|
||||||
|
placed = 1; //Markiert als gesetzt
|
||||||
}
|
printf("Wort '%s' nach %u Versuchen platziert.\n", words[i], attempt + 1); //Ausgabe als Test
|
||||||
placed = 1; // Mark as placed to exit the attempt loop
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -115,5 +120,5 @@ for (unsigned int row = 0; row < searchFieldLen; row++)
|
|||||||
|
|
||||||
|
|
||||||
printf("Placed %u words out of %u\n", placedWords, wordCount);
|
printf("Placed %u words out of %u\n", placedWords, wordCount);
|
||||||
return placedWords; // Return the number of successfully placed words
|
return placedWords; // Gibt Anzahl gesetzter Wörter zurück
|
||||||
}
|
}
|
||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
#define MAX_SEARCH_FIELD_LEN 100
|
#define MAX_SEARCH_FIELD_LEN 20
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
|||||||
+14
-3
@@ -1,21 +1,32 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "graphicalGame.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||||
|
|
||||||
const char words[][MAX_WORD_LEN] = {
|
char words[][MAX_WORD_LEN] = {
|
||||||
"HALLO",
|
"HALLO",
|
||||||
"WELT",
|
"WELT",
|
||||||
"AUTO"
|
"AUTO",
|
||||||
|
"BIRNE",
|
||||||
|
"APFEL",
|
||||||
|
"BANANE",
|
||||||
|
"LANGESWORT",
|
||||||
|
"REITSPORT",
|
||||||
|
"APFELBAUM",
|
||||||
|
"DRACULA",
|
||||||
|
"LAPTOP",
|
||||||
|
"BILDSCHRIM",
|
||||||
|
"HAUS"
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned int wordCount = sizeof(words) / sizeof(words[0]);
|
unsigned int wordCount = sizeof(words) / sizeof(words[0]);
|
||||||
|
|
||||||
int placedWords = createWordSalad(salad, MAX_SEARCH_FIELD_LEN, words, wordCount);
|
int placedWords = createWordSalad(salad, MAX_SEARCH_FIELD_LEN, words, wordCount);
|
||||||
|
startGame(salad, MAX_SEARCH_FIELD_LEN, words, wordCount, 1000);
|
||||||
printf("Placed words: %d\n", placedWords);
|
printf("Placed words: %d\n", placedWords);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in new issue
Block a user