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