bessere version
This commit is contained in:
parent
992119f42c
commit
78f631fefb
@ -4,72 +4,75 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||
unsigned int size, const char words[][MAX_WORD_LEN],
|
||||
unsigned int wordCount) {
|
||||
#define EMPTY_CHAR '.'
|
||||
typedef enum { HORIZONTAL, VERTICAL } Direction;
|
||||
//große Funktion (Gitter bauen, Wörter legen, Zufallsbuchstaben)
|
||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], //Gitter
|
||||
unsigned int size, const char words[][MAX_WORD_LEN], //größe Quadrat,liste von Wörtern
|
||||
unsigned int wordCount) { //anzahl an Wörtern in der Liste
|
||||
#define EMPTY_CHAR '.' // leeres kästchen = "."
|
||||
typedef enum { HORIZONTAL, VERTICAL } Direction; //neuer typ: waagrecht oder senkrecht
|
||||
|
||||
//1.kleine Funktion(setzt jedes Kästchen auf ".")
|
||||
void initializeArray() {
|
||||
for (unsigned int y = 0; y < size; y++)
|
||||
for (unsigned int x = 0; x < size; x++)
|
||||
salad[y][x] = EMPTY_CHAR;
|
||||
for (unsigned int y = 0; y < size; y++) //Zeilen durchlaufen
|
||||
for (unsigned int x = 0; x < size; x++) //Spalten durchlaufen
|
||||
salad[y][x] = EMPTY_CHAR; //punkt schreiben
|
||||
}
|
||||
|
||||
int canPlace(unsigned int x, unsigned int y, const char *word, Direction dir) {
|
||||
unsigned int len = strlen(word);
|
||||
if ((dir == HORIZONTAL && x + len > size) || (dir == VERTICAL && y + len > size))
|
||||
return 0;
|
||||
for (unsigned int i = 0; i < len; i++) {
|
||||
if ((dir == HORIZONTAL && salad[y][x + i] != EMPTY_CHAR) ||
|
||||
(dir == VERTICAL && salad[y + i][x] != EMPTY_CHAR))
|
||||
return 0;
|
||||
//2.kleine Funktion (prüfen ob Wort passt)
|
||||
int canPlace(unsigned int x, unsigned int y, const char *word, Direction dir) { //Startposition, das Wort, Richtung
|
||||
unsigned int len = strlen(word); //länge des Wortes
|
||||
if ((dir == HORIZONTAL && x + len > size) || (dir == VERTICAL && y + len > size)) //prüfen ob Wort über rechten bzw. unteren Rand geht
|
||||
return 0; //passt das wort nicht -> 0
|
||||
for (unsigned int i = 0; i < len; i++) { //jedes zeichen nacheinander prüfen
|
||||
if ((dir == HORIZONTAL && salad[y][x + i] != EMPTY_CHAR) || //prüfen ob nach rechts schon ein wort kommt
|
||||
(dir == VERTICAL && salad[y + i][x] != EMPTY_CHAR)) //prüfen ob nach unten ein Wort kommt
|
||||
return 0; //ist dort bereits ein wort -> 0
|
||||
}
|
||||
return 1;
|
||||
return 1; //passt das Wort -> 1
|
||||
}
|
||||
|
||||
void placeWord(unsigned int x, unsigned int y, const char *word, Direction dir) {
|
||||
for (unsigned int i = 0; i < strlen(word); i++) {
|
||||
if (dir == HORIZONTAL)
|
||||
//3.kleine Funktion (Wort ins Gitter schreiben)
|
||||
void placeWord(unsigned int x, unsigned int y, const char *word, Direction dir) { //Startposition, das Wort, Richtung
|
||||
for (unsigned int i = 0; i < strlen(word); i++) { //Buchstabe für Buchstabe
|
||||
if (dir == HORIZONTAL) //i-ten Buchstaben an y,x+i schreiben bei waagrecht
|
||||
salad[y][x + i] = word[i];
|
||||
else
|
||||
else //i-ten Buchstaben an y+i,x schreiben bei senkrecht
|
||||
salad[y + i][x] = word[i];
|
||||
}
|
||||
}
|
||||
|
||||
void fillRandomLetters() {
|
||||
//4.kleine Funktion (alle leeren Kästchen bekommen Zufallsbuchstaben)
|
||||
void fillRandomLetters() { //geht wieder über alle Zeilen und Spalten oh man
|
||||
for (unsigned int y = 0; y < size; y++) {
|
||||
for (unsigned int x = 0; x < size; x++) {
|
||||
if (salad[y][x] == EMPTY_CHAR)
|
||||
if (salad[y][x] == EMPTY_CHAR) //wenns leer ist Zufallsbuchstabe
|
||||
salad[y][x] = 'A' + (rand() % 26);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
srand(time(NULL));
|
||||
initializeArray();
|
||||
//Hauptschleife(Wörter zufällig platzieren)
|
||||
srand(time(NULL)); //durch aktuelle Sekundenzeit wirkklich zufällig
|
||||
initializeArray(); //1.kleine Funktion
|
||||
|
||||
int placed = 0;
|
||||
for (unsigned int i = 0; i < wordCount; i++) {
|
||||
int success = 0;
|
||||
for (int tries = 0; tries < 1000 && !success; tries++) {
|
||||
unsigned int x = rand() % size;
|
||||
unsigned int y = rand() % size;
|
||||
Direction dir = rand() % 2 ? HORIZONTAL : VERTICAL;
|
||||
if (canPlace(x, y, words[i], dir)) {
|
||||
placeWord(x, y, words[i], dir);
|
||||
success = 1;
|
||||
placed++;
|
||||
int placed = 0; //wie viele Wörter wurden gelegt?
|
||||
for (unsigned int i = 0; i < wordCount; i++) { //jedes Wort durchgehen
|
||||
int success = 0; //für dieses Wort schon Platz gefunden?
|
||||
for (int tries = 0; tries < 1000 && !success; tries++) { //1000 verschiedene Stellen und Richtungen probieren
|
||||
unsigned int x = rand() % size; //zufällige Position Spalte
|
||||
unsigned int y = rand() % size; //zufällige Postion Zeile
|
||||
Direction dir = rand() % 2 ? HORIZONTAL : VERTICAL; //zufällige richtung 1-> Horizontal 0-> Vertical
|
||||
if (canPlace(x, y, words[i], dir)) { //2. kleine Funktion
|
||||
placeWord(x, y, words[i], dir); //3. kleine Funktion
|
||||
success = 1; //Wort erfolgreich platziert
|
||||
placed++; //Zähler der gelegten Wörter erhöhen
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fillRandomLetters();
|
||||
fillRandomLetters(); //4. kleine Funktion
|
||||
|
||||
return placed;
|
||||
return placed; //Rückgabe wie viele Wörter plaziert wurden
|
||||
}
|
||||
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int size) {
|
||||
//Anzeige-Funktion
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int size) { //funktion printet das wortsalatfeld
|
||||
for (unsigned int y = 0; y < size; y++) {
|
||||
for (unsigned int x = 0; x < size; x++) {
|
||||
printf("%c ", salad[y][x]);
|
||||
|
||||
@ -37,19 +37,15 @@ int main(int argc, char *argv[])
|
||||
|
||||
printf("\nWortsalat-Initialisierung\n");
|
||||
printf("-------------------------\n");
|
||||
printf("Gelesene Wörter: %u\n", wordCount);
|
||||
printf("Platzierte Wörter: %u\n", placedWords);
|
||||
printf("Gelesene Woerter: %u\n", wordCount);
|
||||
printf("Platzierte Woerter: %u\n", placedWords);
|
||||
|
||||
if (placedWords < wordCount)
|
||||
printf("Hinweis: %u Wörter konnten nicht platziert werden.\n", wordCount - placedWords);
|
||||
printf("Hinweis: %u Woerter konnten nicht platziert werden.\n", wordCount - placedWords);
|
||||
|
||||
printf("\nSpielfeld (%dx%d):\n\n", SALAD_SIZE, SALAD_SIZE);
|
||||
for (int i = 0; i < SALAD_SIZE; i++)
|
||||
{
|
||||
for (int j = 0; j < SALAD_SIZE; j++)
|
||||
printf("%c ", wordSalad[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
showWordSalad(wordSalad, SALAD_SIZE);
|
||||
|
||||
|
||||
printf("\nInitialisierung abgeschlossen.\n");
|
||||
|
||||
|
||||
@ -2,4 +2,6 @@ Yeti,Nessie Werwolf; Vampir
|
||||
Monster
|
||||
Hydra;Frankenstein
|
||||
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
||||
Gespenst, Oger
|
||||
Gespenst, Oger
|
||||
tomasschwester, fortnite, lababage, eckesbokesseresekes
|
||||
Doener, Kebab, nacho, alejandrogarnacho, megafon, Telefon, gianluigibuffon, mehrweorter, flosschwester, gingerlatina
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user