generated from freudenreichan/info2Praktikum-Wortsalat
test 7
This commit is contained in:
parent
aa61e44a0e
commit
b8f395aedb
@ -4,7 +4,6 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.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
|
||||||
|
|
||||||
@ -13,92 +12,86 @@
|
|||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||||
|
|
||||||
// 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));
|
srand(time(NULL));
|
||||||
|
|
||||||
int placedWords = 0;
|
int placedWords = 0;
|
||||||
unsigned int tries = 0;
|
|
||||||
unsigned int check_direction = 0, check_overlap = 0;
|
|
||||||
int zeile = 0, spalte = 0, len = 0;
|
|
||||||
|
|
||||||
//1. Schritt: Alle Felder mit 0 befüllen
|
// Array mit . füllen
|
||||||
for(int m = 0; m < searchFieldLen; m++) //m Zeilen
|
for (int r = 0; r < searchFieldLen; r++){
|
||||||
{
|
for(int s = 0; s < searchFieldLen; s++){
|
||||||
for(int n = 0; n < searchFieldLen; n++) //n Spalte
|
salad[r][s] = '.';
|
||||||
{
|
|
||||||
salad[m][n] = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Schritt: Prüfen ob Wort in Array geschrieben darf
|
// -> erst wenn die ganze Länge des Wortes frei ist, array hinzufügen
|
||||||
for(int num_words = 0; num_words < wordCount && num_words < searchFieldLen; num_words++) //eingelesen Wörter zählen
|
// MAX Versuche das Wort zu platzieren == 10
|
||||||
{
|
|
||||||
len = strlen(words[num_words]); //Größe des Wortes ermitteln
|
|
||||||
|
|
||||||
if (len < searchFieldLen)
|
|
||||||
return -1; //ERROR, falls Wort größer als Größe des Feldes (unnötig, da bei worteingabe bereits überprüft)
|
|
||||||
|
|
||||||
while(tries < MAX_RAND_TRIES_PER_WORD)
|
// Wörter in salad einsortieren
|
||||||
{
|
for (int num_words = 0; num_words < wordCount; num_words++){
|
||||||
// zufällige Richtung auswählen
|
|
||||||
int direction = rand() % 2; // 0 = VERTIKAL, 1 = HORIZONTAL
|
|
||||||
|
|
||||||
// zufälliger Startpunkt, Startkoordinaten
|
int laenge = strlen(words[num_words]);
|
||||||
if (direction == 0) // 0 = VERTIKAL -> Spalte egal
|
|
||||||
{
|
// muss noch prüfen, ob in der Zeile schon was ist
|
||||||
zeile = rand() % (searchFieldLen - len);
|
// 1 ist horizontal, 0 ist vertikal
|
||||||
spalte = rand() % (searchFieldLen);
|
// isalpha == 0 -> kein Buchstabe
|
||||||
check_direction = 1;
|
|
||||||
|
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];
|
||||||
|
|
||||||
if (direction == 1) // 1 = HORIZONTAL -> Zeile egal
|
|
||||||
{
|
|
||||||
zeile = rand() % (searchFieldLen);
|
|
||||||
spalte = rand() % (searchFieldLen - len);
|
|
||||||
check_direction = 1;
|
|
||||||
}
|
}
|
||||||
|
placedWords++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// vertikale Eingabe
|
||||||
|
else if (richtung == 0){
|
||||||
|
|
||||||
//Prüfen auf Überlappung ----------> Logikfehler
|
int zeile = rand()% (searchFieldLen - laenge +1);
|
||||||
for(int i_overlap = 0; i_overlap < len; i_overlap++)
|
int spalte = rand()% searchFieldLen;
|
||||||
{
|
|
||||||
if (direction == 0) // 0 = VERTIKAL
|
//prüft, ob die herausgesuchte Zeile noch frei ist
|
||||||
{
|
for (int n = zeile; n < zeile + laenge; n++){
|
||||||
if (words[num_words][i_overlap] == salad[zeile][spalte + i_overlap]);
|
if(salad[n][spalte] != '.'){
|
||||||
check_overlap = 1;
|
belegt = 1;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else if (direction == 1) // 1 = HORIZONTAL
|
|
||||||
{
|
|
||||||
if (words[num_words][i_overlap] == salad[zeile + i_overlap][spalte]);
|
|
||||||
check_overlap = 1;
|
|
||||||
}
|
}
|
||||||
if(check_overlap == 0)
|
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
|
||||||
{
|
if (belegt == 0){
|
||||||
tries++;
|
for(int j = 0; j < laenge; j++){
|
||||||
|
salad[zeile + j][spalte] = words[num_words][j];
|
||||||
|
|
||||||
|
}
|
||||||
|
placedWords++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//3. Schritt: Wort in Wortsalat schreiben
|
|
||||||
if(check_direction == 1 && check_overlap == 1 && tries < MAX_RAND_TRIES_PER_WORD)
|
|
||||||
{
|
|
||||||
for(int i_set = 0; i_set < len; i_set++)
|
|
||||||
{
|
|
||||||
if (direction == 0) // 0 = VERTIKAL
|
|
||||||
{
|
|
||||||
salad[zeile + i_set][spalte] = words[num_words][i_set];
|
|
||||||
placedWords++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (direction == 1) // 1 = HORIZONTAL
|
|
||||||
{
|
|
||||||
salad[zeile][spalte + i_set] = words[num_words][i_set];
|
|
||||||
placedWords++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fügt zufällige Buchstaben ein
|
// fügt zufällige Buchstaben ein
|
||||||
@ -106,7 +99,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
for(int m = 0; m < searchFieldLen; m++ ){
|
for(int m = 0; m < searchFieldLen; m++ ){
|
||||||
if(isalpha(salad[l][m]) == 0 ){
|
if(isalpha(salad[l][m]) == 0 ){
|
||||||
// zufällige Buchstaben erzeugen
|
// zufällige Buchstaben erzeugen
|
||||||
char alphabet [] = "abcdefghijklmnopqrstuvwxyz";
|
char alphabet [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
int laenge = strlen(alphabet);
|
int laenge = strlen(alphabet);
|
||||||
int zufallszahl = rand()% laenge;
|
int zufallszahl = rand()% laenge;
|
||||||
char zufallsbuchstabe = alphabet[zufallszahl];
|
char zufallsbuchstabe = alphabet[zufallszahl];
|
||||||
@ -116,16 +109,14 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return placedWords; //platzierte Wörter zurückgeben
|
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 p = 0; p < searchFieldLen; p++){
|
||||||
{
|
for(int q = 0; q < searchFieldLen; q++){
|
||||||
for(int q = 0; q < searchFieldLen; q++)
|
|
||||||
{
|
|
||||||
printf("%c", salad[p][q]);
|
printf("%c", salad[p][q]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|||||||
@ -23,6 +23,7 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|||||||
for(i = 0; i < MAX_WORD_LEN -1 && word_parts[i] != '\0'; i++)
|
for(i = 0; i < MAX_WORD_LEN -1 && word_parts[i] != '\0'; i++)
|
||||||
{
|
{
|
||||||
printf("????");
|
printf("????");
|
||||||
|
|
||||||
words[counter][i] = toupper(word_parts[i]); //Großbuchstaben erzeugen
|
words[counter][i] = toupper(word_parts[i]); //Großbuchstaben erzeugen
|
||||||
}
|
}
|
||||||
words[counter][i] = '\0'; //Stringdefinition vervollständigen
|
words[counter][i] = '\0'; //Stringdefinition vervollständigen
|
||||||
|
|||||||
Binary file not shown.
BIN
Start_Windows/runTests2.exe
Normal file
BIN
Start_Windows/runTests2.exe
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user