generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
7 Commits
71db293343
...
20465e1dc2
| Author | SHA1 | Date | |
|---|---|---|---|
| 20465e1dc2 | |||
| 80dac61be4 | |||
| 99a0600fd2 | |||
| e8ba3293a6 | |||
| 16134abad0 | |||
| 9ab08b84f6 | |||
| 36b117b57e |
@ -2,123 +2,98 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//TODO: Spiellogik implementieren:
|
//TODO: 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 */
|
||||||
|
|
||||||
// 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) {
|
||||||
|
int addedWords = 0;
|
||||||
|
int attempts = 0; //Anzahl an Versuchen ein Wort hinzuzufügen
|
||||||
|
int added = 0; //0 Wort nicht hinzugefügt , 1 Wort hinzugefügt
|
||||||
|
int space = 1; // 0 Wort zu lang, 1 Wort passt an diese Stelle
|
||||||
|
unsigned long long wordLength = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < searchFieldLen; i++) {
|
||||||
|
for (int j = 0; j < searchFieldLen; j++) {
|
||||||
|
salad[i][j] = EMPTY_CHAR; //Füllen des gesamten Arrays mit Nullen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i=0; i < wordCount; i++) {
|
||||||
|
|
||||||
|
wordLength = strlen(words[i]);
|
||||||
|
|
||||||
|
while (attempts <= MAX_RAND_TRIES_PER_WORD && added != 1) {
|
||||||
|
int direction = rand() % 2;// 0 Horizontal, 1 Vertikal
|
||||||
|
if (direction == 0) {
|
||||||
|
int collumn = rand() % searchFieldLen;
|
||||||
|
int row = rand() % searchFieldLen;
|
||||||
|
for (int j = 0; j < wordLength; j++)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
if (salad[row][collumn+j] != EMPTY_CHAR) {space = 0; break;}
|
||||||
int placedWords = 0;
|
//Prüfen ob das Wort ein bereits vorhandenes Wort überspeichern würde
|
||||||
|
}
|
||||||
|
if ((collumn + wordLength >= searchFieldLen-1) || (space == 0)) {attempts++;}
|
||||||
|
//Prüfen ob Wort an der Stelle (Zeile, Spalte) in das Array passt
|
||||||
|
else {for (int j = 0; j < wordLength; j++)
|
||||||
|
{
|
||||||
|
salad[row][collumn+j] = words[i][j]; //Einfügen des Wortes Buchstabe für Buchstabe
|
||||||
|
}
|
||||||
|
added=1;
|
||||||
|
addedWords++;
|
||||||
|
|
||||||
// Array mit . füllen
|
|
||||||
for (int r = 0; r < searchFieldLen; r++){
|
|
||||||
for(int s = 0; s < searchFieldLen; s++){
|
|
||||||
salad[r][s] = '.';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (direction == 1) {// Vertikal funktioniert analog zu horizontal
|
||||||
// -> erst wenn die ganze Länge des Wortes frei ist, array hinzufügen
|
int collumn = rand() % searchFieldLen;
|
||||||
// MAX Versuche das Wort zu platzieren == 10
|
int row = rand() % searchFieldLen;
|
||||||
|
for (int j = 0; j < wordLength; j++)
|
||||||
|
{
|
||||||
// Wörter in salad einsortieren
|
if (salad[row+j][collumn] != EMPTY_CHAR) {space = 0; break;}
|
||||||
for (int num_words = 0; num_words < wordCount; num_words++){
|
|
||||||
|
|
||||||
int laenge = strlen(words[num_words]);
|
|
||||||
|
|
||||||
// muss noch prüfen, ob in der Zeile schon was ist
|
|
||||||
// 1 ist horizontal, 0 ist vertikal
|
|
||||||
// isalpha == 0 -> kein Buchstabe
|
|
||||||
|
|
||||||
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];
|
|
||||||
|
|
||||||
}
|
|
||||||
placedWords++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// vertikale Eingabe
|
|
||||||
else if (richtung == 0){
|
|
||||||
|
|
||||||
int zeile = rand()% (searchFieldLen - laenge +1);
|
|
||||||
int spalte = rand()% searchFieldLen;
|
|
||||||
|
|
||||||
//prüft, ob die herausgesuchte Zeile noch frei ist
|
|
||||||
for (int n = zeile; n < zeile + laenge; n++){
|
|
||||||
if(salad[n][spalte] != '.'){
|
|
||||||
belegt = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
|
|
||||||
if (belegt == 0){
|
|
||||||
for(int j = 0; j < laenge; j++){
|
|
||||||
salad[zeile + j][spalte] = words[num_words][j];
|
|
||||||
|
|
||||||
}
|
|
||||||
placedWords++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if ((row + wordLength >= searchFieldLen-1) || (space == 0)) {attempts++;}
|
||||||
else {
|
else {
|
||||||
break;
|
for (int j = 0; j < wordLength; j++)
|
||||||
|
{
|
||||||
|
salad[row+j][collumn] = words[i][j];
|
||||||
|
}
|
||||||
|
added=1;
|
||||||
|
addedWords++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
space = 1;
|
||||||
|
}
|
||||||
|
attempts = 0;
|
||||||
|
added = 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
for (int i = 0; i < searchFieldLen; i++) {
|
||||||
|
for (int j = 0; j < searchFieldLen; j++) {
|
||||||
|
if (salad[i][j] == EMPTY_CHAR)
|
||||||
|
{
|
||||||
|
salad[i][j] = rand()%('Z'-'A'+1)+'A';//Alle Felder, die noch nicht befüllt sind, werden zufällig befüllt
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fügt zufällige Buchstaben ein
|
|
||||||
for(int l = 0; l < searchFieldLen; l++){
|
|
||||||
for(int m = 0; m < searchFieldLen; m++ ){
|
|
||||||
if(isalpha(salad[l][m]) == 0 ){
|
|
||||||
// zufällige Buchstaben erzeugen
|
|
||||||
char alphabet [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
||||||
int laenge = strlen(alphabet);
|
|
||||||
int zufallszahl = rand()% laenge;
|
|
||||||
char zufallsbuchstabe = alphabet[zufallszahl];
|
|
||||||
// zufällige Buchstaben einfügen
|
|
||||||
salad[l][m] = zufallsbuchstabe;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return addedWords;
|
||||||
|
|
||||||
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 i = 0; i < searchFieldLen; i++)
|
||||||
for(int q = 0; q < searchFieldLen; q++){
|
{
|
||||||
printf("%c", salad[p][q]);
|
for(int j = 0; j < searchFieldLen; j++)
|
||||||
}
|
{
|
||||||
printf("\n");
|
printf("%c", salad[i][j]);//Ausgabe des Arrays auf die Konsole
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -36,19 +36,21 @@ int main(int argc, char *argv[])
|
|||||||
// Create the word salad by placing words into grid
|
// Create the word salad by placing words into grid
|
||||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||||
|
|
||||||
// TODO: (if Schleife implementieren, die das prüft)
|
// TODO:
|
||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
|
if(wordCount == placedWords)
|
||||||
|
{
|
||||||
|
exitCode = EXIT_SUCCESS;
|
||||||
|
startGame(wordSalad,SALAD_SIZE, words,wordCount,750);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
printf("Bei der Platzierung ist etwas falsch gelaufen!");
|
||||||
|
}
|
||||||
// Start the game if successful
|
// Start the game if successful
|
||||||
// error message if some words couldn't be placed
|
// error message if some words couldn't be placed
|
||||||
|
|
||||||
if (placedWords == wordCount){
|
|
||||||
showWordSalad(wordSalad, placedWords);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
printf("Error. The words couldn't be placed.\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user