generated from freudenreichan/info2Praktikum-Wortsalat
Fertig
This commit is contained in:
parent
b4cc13d96a
commit
56b3b9efa1
@ -15,9 +15,9 @@
|
|||||||
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 placed_words = 0;
|
int placedWords = 0;
|
||||||
|
|
||||||
// Array mit 0 füllen
|
// Array mit . füllen
|
||||||
for (int r = 0; r < searchFieldLen; r++){
|
for (int r = 0; r < searchFieldLen; r++){
|
||||||
for(int s = 0; s < searchFieldLen; s++){
|
for(int s = 0; s < searchFieldLen; s++){
|
||||||
salad[r][s] = '.';
|
salad[r][s] = '.';
|
||||||
@ -29,65 +29,69 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
|
|
||||||
|
|
||||||
// Wörter in salad einsortieren
|
// Wörter in salad einsortieren
|
||||||
for (int i = 0; i < wordCount; i++){
|
for (int num_words = 0; num_words < wordCount; num_words++){
|
||||||
|
|
||||||
int laenge = strlen(words[i]);
|
int laenge = strlen(words[num_words]);
|
||||||
|
|
||||||
// muss noch prüfen, ob in der Zeile schon was ist
|
// muss noch prüfen, ob in der Zeile schon was ist
|
||||||
// 1 ist horizontal, 0 ist vertikal
|
// 1 ist horizontal, 0 ist vertikal
|
||||||
// frei == 1 -> Zeile nicht frei
|
|
||||||
// isalpha == 0 -> kein Buchstabe
|
// isalpha == 0 -> kein Buchstabe
|
||||||
|
|
||||||
for(int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++){
|
for(int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++){
|
||||||
int richtung = rand()% 2;
|
int richtung = rand()% 2;
|
||||||
int frei = 0;
|
int belegt = 0;
|
||||||
|
|
||||||
// horizontale Eingabe
|
// horizontale Eingabe
|
||||||
if (richtung == 1){
|
if (richtung == 1){
|
||||||
int zeile = rand()% searchFieldLen;
|
int zeile = rand()% searchFieldLen;
|
||||||
int spalte = rand()% (searchFieldLen- laenge + 1);
|
int spalte = rand()% (searchFieldLen- laenge +1);
|
||||||
|
|
||||||
//prüft, ob die herausgesuchte Zeile noch frei ist
|
//prüft, ob die herausgesuchte Zeile noch frei ist
|
||||||
|
// belegt == 1 -> Zeile nicht frei
|
||||||
for (int o = spalte; o < spalte + laenge; o++){
|
for (int o = spalte; o < spalte + laenge; o++){
|
||||||
if(salad[zeile][o] != '.'){
|
if(salad[zeile][o] != '.'){
|
||||||
frei = 1;
|
belegt = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
|
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
|
||||||
if (frei == 0){
|
if (belegt == 0){
|
||||||
for(int k = 0; k < laenge; k++){
|
for(int k = 0; k < laenge; k++){
|
||||||
salad[zeile][spalte + k] = words[i][k];
|
salad[zeile][spalte + k] = words[num_words][k];
|
||||||
|
|
||||||
}
|
}
|
||||||
placed_words++;
|
placedWords++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// vertikale Eingabe
|
// vertikale Eingabe
|
||||||
else if (richtung == 0){
|
else if (richtung == 0){
|
||||||
|
|
||||||
int zeile = rand()% (searchFieldLen - laenge + 1);
|
int zeile = rand()% (searchFieldLen - laenge +1);
|
||||||
int spalte = rand()% searchFieldLen;
|
int spalte = rand()% searchFieldLen;
|
||||||
|
|
||||||
//prüft, ob die herausgesuchte Zeile noch frei ist
|
//prüft, ob die herausgesuchte Zeile noch frei ist
|
||||||
for (int n = zeile; n < zeile + laenge; n++){
|
for (int n = zeile; n < zeile + laenge; n++){
|
||||||
if(salad[n][spalte] != '.'){
|
if(salad[n][spalte] != '.'){
|
||||||
frei = 1;
|
belegt = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
|
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
|
||||||
if (frei == 0){
|
if (belegt == 0){
|
||||||
for(int j = 0; j < laenge; j++){
|
for(int j = 0; j < laenge; j++){
|
||||||
salad[zeile + j][spalte] = words[i][j];
|
salad[zeile + j][spalte] = words[num_words][j];
|
||||||
|
|
||||||
}
|
}
|
||||||
placed_words++;
|
placedWords++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fügt zufällige Buchstaben ein
|
// fügt zufällige Buchstaben ein
|
||||||
@ -95,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];
|
||||||
@ -105,7 +109,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return placed_words;
|
return placedWords;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
|
|||||||
Binary file not shown.
@ -2,6 +2,55 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||||
|
|
||||||
|
// Read words from file and store in 'words' array
|
||||||
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
|
{
|
||||||
|
// MAX_WORD_LEN 100
|
||||||
|
// MAX_LINE_LEN 1024
|
||||||
|
// *file ist Datei
|
||||||
|
// words ist Array
|
||||||
|
// MAX_WORD_LEN ist maximale Wortlänge
|
||||||
|
// maxWordCount ist maximale Anzahl an Wörtern
|
||||||
|
char puffer[MAX_LINE_LEN];
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
while(fgets(puffer, MAX_LINE_LEN-1, file) != NULL && counter < maxWordCount)
|
||||||
|
{
|
||||||
|
char *parts = strtok(puffer, ",;\n\t/. ");
|
||||||
|
|
||||||
|
while(parts != NULL && counter < maxWordCount)
|
||||||
|
{
|
||||||
|
//strncpy(words[counter][MAX_WORD_LEN], puffer, MAX_LINE_LEN-1);
|
||||||
|
//words[counter][MAX_WORD_LEN-1] = "\0";
|
||||||
|
//Großbuchstaben
|
||||||
|
for(int i = 0; i < MAX_WORD_LEN -1 && parts[i] != '\0'; i++)
|
||||||
|
{
|
||||||
|
words[counter][i] = toupper(parts[i]);
|
||||||
|
words[counter][i] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
counter++; // Wort eingelesen -> Wortzähler erhöhen
|
||||||
|
parts = strtok(NULL, ",;\n\t/. ");
|
||||||
|
|
||||||
|
}
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* #include "input.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#define MAX_NUMBER_OF_WORDS 100
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
#define MAX_WORD_LEN 100
|
#define MAX_WORD_LEN 100
|
||||||
// TODO:
|
// TODO:
|
||||||
|
|||||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user