generated from freudenreichan/info2Praktikum-Wortsalat
changes to game.c
This commit is contained in:
parent
6d8371d487
commit
249298afc6
@ -2,127 +2,117 @@
|
|||||||
#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 = 0; // 1 = Wort zu lang für diese Stelle
|
||||||
|
unsigned long long wordLength = 0;
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
int placedWords = 0;
|
for (int i = 0; i < searchFieldLen; i++) {
|
||||||
unsigned int attemps = 0;
|
for (int j = 0; j < searchFieldLen; j++) {
|
||||||
unsigned int check_overlap = 0;
|
salad[i][j] = '.'; //Füllen des gesamten Arrays mit Nullen
|
||||||
int zeile = 0, spalte = 0, len = 0;
|
|
||||||
|
|
||||||
//Alle Felder mit ? befüllen
|
|
||||||
for(int m = 0; m < searchFieldLen; m++) //m Zeilen
|
|
||||||
{
|
|
||||||
for(int n = 0; n < searchFieldLen; n++) //n Spalte
|
|
||||||
{
|
|
||||||
salad[m][n] = '?';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (int i=0; i < wordCount; i++) {
|
||||||
//Prüfen ob Wort in Array geschrieben darf
|
|
||||||
for(int num_words = 0; num_words < wordCount; num_words++) //eingelesen Wörter zählen
|
|
||||||
{
|
|
||||||
len = strlen(words[num_words]); //Größe des Wortes ermitteln
|
|
||||||
|
|
||||||
while(attemps < MAX_RAND_TRIES_PER_WORD)
|
wordLength = strlen(words[i]);
|
||||||
{
|
|
||||||
// zufällige Richtung auswählen
|
|
||||||
int direction = rand() % 2; // 0 = VERTIKAL, 1 = HORIZONTAL
|
|
||||||
|
|
||||||
// zufälliger Startpunkt, Startkoordinaten
|
|
||||||
if (direction == 0) // 0 = VERTIKAL
|
|
||||||
{
|
|
||||||
zeile = rand() % searchFieldLen;
|
|
||||||
spalte = rand() % searchFieldLen;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (direction == 1) // 1 = HORIZONTAL
|
while (attempts <= MAX_RAND_TRIES_PER_WORD && added != 1) {
|
||||||
{
|
int direction = rand() % 2;// 0 Horizontal, 1 Vertikal
|
||||||
zeile = rand() % searchFieldLen;
|
if (direction == 0) {
|
||||||
spalte = rand() % searchFieldLen;
|
int spalte = rand() % searchFieldLen;
|
||||||
}
|
int reihe = rand() % searchFieldLen;
|
||||||
|
for (int j = 0; j < wordLength; j++)
|
||||||
for(int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
if (direction == 0) // 0 = VERTIKAL
|
|
||||||
{
|
{
|
||||||
//Prüfe ob Feld bereits belegt
|
if (salad[reihe][spalte+j] != '.') //Prüfen ob das Wort ein bereits vorhandenes Wort überspeichern würde
|
||||||
if (salad[zeile+i][spalte] != '?')
|
|
||||||
{
|
{
|
||||||
check_overlap = 1; // 1 = Überlappung
|
space = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Prüfe auf Groeße
|
}
|
||||||
if (spalte + len >= searchFieldLen-1 || check_overlap == 1)
|
if ((spalte + wordLength >= searchFieldLen-1) || (space == 1))
|
||||||
{
|
{
|
||||||
attemps++;
|
attempts++;
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for(int k; k < len; k++)
|
|
||||||
{
|
|
||||||
salad[zeile][spalte+k] = words[i][k];
|
|
||||||
placedWords++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (direction == 1) // 1 = HORZIZONTAL
|
//Prüfen ob Wort an der Stelle (Zeile, Spalte) in das Array passt
|
||||||
|
else
|
||||||
{
|
{
|
||||||
//Prüfe ob Feld bereits belegt
|
for (int j = 0; j < wordLength; j++)
|
||||||
if (salad[zeile][spalte+i] != '?')
|
|
||||||
{
|
{
|
||||||
check_overlap = 1; // 1 = Überlappung
|
salad[reihe][spalte+j] = words[i][j]; //Einfügen des Wortes Buchstabe für Buchstabe
|
||||||
|
}
|
||||||
|
added=1;
|
||||||
|
addedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (direction == 1) // Vertikal
|
||||||
|
{
|
||||||
|
int spalte = rand() % searchFieldLen;
|
||||||
|
int reihe = rand() % searchFieldLen;
|
||||||
|
for (int j = 0; j < wordLength; j++)
|
||||||
|
{
|
||||||
|
if (salad[reihe+j][spalte] != '.')
|
||||||
|
{
|
||||||
|
space = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if ((reihe + wordLength >= searchFieldLen-1) || (space == 1))
|
||||||
|
{
|
||||||
|
attempts++;
|
||||||
|
}
|
||||||
|
|
||||||
//Prüfe auf Groeße
|
|
||||||
if (zeile + len >= searchFieldLen-1 || check_overlap == 1)
|
|
||||||
{
|
|
||||||
attemps++;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
for (int j = 0; j < wordLength; j++)
|
||||||
{
|
{
|
||||||
for(int k; k < len; k++)
|
salad[row+j][collumn] = words[i][j];
|
||||||
{
|
|
||||||
salad[zeile+k][spalte] = words[i][k];
|
|
||||||
placedWords++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
added=1;
|
||||||
|
addedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
space = 0;
|
||||||
|
}
|
||||||
|
attempts = 0;
|
||||||
|
added = 0;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < searchFieldLen; j++) {
|
||||||
|
if (salad[i][j] == '.')
|
||||||
|
{
|
||||||
|
salad[i][j] = rand()%('Z'-'A'+1)+'A';//Alle Felder, die noch nicht befüllt sind, werden zufällig befüllt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return placedWords; //platzierte Wörter zurückgeben
|
return addedWords;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 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++)
|
for(int j = 0; j < searchFieldLen; j++)
|
||||||
{
|
{
|
||||||
printf("%c", salad[p][q]);
|
printf("%c", salad[i][j]);//Ausgabe des Arrays auf die Konsole
|
||||||
}
|
}
|
||||||
printf("\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
Start_Windows/game.o
Normal file
BIN
Start_Windows/game.o
Normal file
Binary file not shown.
BIN
Start_Windows/input.o
Normal file
BIN
Start_Windows/input.o
Normal file
Binary file not shown.
@ -46,7 +46,7 @@ int main(int argc, char *argv[])
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
exitCode = EXIT_FAILURE;
|
exitCode = EXIT_FAILURE;
|
||||||
printf("Bei der Platzierung ist etwas falsch gelaufen!");
|
printf("Error. The words couldn't be placed.\n");
|
||||||
}
|
}
|
||||||
// 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
|
||||||
|
|||||||
BIN
Start_Windows/runTests2.exe
Normal file
BIN
Start_Windows/runTests2.exe
Normal file
Binary file not shown.
@ -1 +0,0 @@
|
|||||||
Hund,Katze; Maus
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
Apfel
|
|
||||||
Banane
|
|
||||||
Kiwi
|
|
||||||
Loading…
x
Reference in New Issue
Block a user