generated from freudenreichan/info2Praktikum-Wortsalat
fertige debuggte Version
This commit is contained in:
parent
765879d085
commit
b4cc13d96a
@ -2,6 +2,7 @@
|
|||||||
#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
|
||||||
@ -13,96 +14,107 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
//prüfen, ob Wörter vorhanden sind mit isalpha
|
srand(time(NULL));
|
||||||
|
int placed_words = 0;
|
||||||
|
|
||||||
|
// Array mit 0 füllen
|
||||||
|
for (int r = 0; r < searchFieldLen; r++){
|
||||||
|
for(int s = 0; s < searchFieldLen; s++){
|
||||||
|
salad[r][s] = '.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// -> erst wenn die ganze Länge des Wortes frei ist, array hinzufügen
|
// -> erst wenn die ganze Länge des Wortes frei ist, array hinzufügen
|
||||||
// MAX Versuche das Wort zu platzieren == 10
|
// MAX Versuche das Wort zu platzieren == 10
|
||||||
// isalpha == 0 -> kein Buchstabe
|
|
||||||
|
|
||||||
|
|
||||||
// Wörter in salad einsortieren
|
// Wörter in salad einsortieren
|
||||||
for (int i = 0; i < wordCount; i++){
|
for (int i = 0; i < wordCount; i++){
|
||||||
char einzelwort[] = words[i]
|
|
||||||
int einzelwort_laenge = strlen(einzelwort);
|
|
||||||
|
|
||||||
|
int laenge = strlen(words[i]);
|
||||||
|
|
||||||
// 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
|
// frei == 1 -> Zeile nicht frei
|
||||||
|
// 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 frei = 0;
|
||||||
|
|
||||||
// horizontale Eingabe
|
// horizontale Eingabe
|
||||||
if (richtung == 1){
|
if (richtung == 1){
|
||||||
int zeile = rand()% MAX_SEARCH_FIELD_LEN;
|
int zeile = rand()% searchFieldLen;
|
||||||
int spalte = rand()% MAX_SEARCH_FIELD_LEN- einzelwort_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
|
||||||
for (int o = spalte; o < spalte + einzelwort_laenge; o++){
|
for (int o = spalte; o < spalte + laenge; o++){
|
||||||
if(isalpha(salad[zeile][o]) == 0){
|
if(salad[zeile][o] != '.'){
|
||||||
frei = 1;
|
frei = 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 (frei == 0){
|
||||||
for(int k = 0; k < einzelwort_laenge; k++){
|
for(int k = 0; k < laenge; k++){
|
||||||
salad[zeile][spalte + k] = einzelwort[k];
|
salad[zeile][spalte + k] = words[i][k];
|
||||||
versuch = MAX_RAND_TRIES_PER_WORD;
|
|
||||||
}
|
}
|
||||||
|
placed_words++;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// vertikale Eingabe
|
// vertikale Eingabe
|
||||||
else if (richtung == 0){
|
else if (richtung == 0){
|
||||||
|
|
||||||
int zeile = rand()% MAX_SEARCH_FIELD_LEN - einzelwort_laenge + 1;
|
int zeile = rand()% (searchFieldLen - laenge + 1);
|
||||||
int spalte = rand()% MAX_SEARCH_FIELD_LEN;
|
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 + einzelwort_laenge; n++){
|
for (int n = zeile; n < zeile + laenge; n++){
|
||||||
if(isalpha(salad[n][spalte]) == 0){
|
if(salad[n][spalte] != '.'){
|
||||||
frei = 1;
|
frei = 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 (frei == 0){
|
||||||
for(int j = 0; j < einzelwort_laenge; j++){
|
for(int j = 0; j < laenge; j++){
|
||||||
salad[zeile + j][spalte] = einzelwort[j];
|
salad[zeile + j][spalte] = words[i][j];
|
||||||
versuch = MAX_RAND_TRIES_PER_WORD;
|
|
||||||
}
|
}
|
||||||
|
placed_words++;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// gibt über searchFieldLen zurück, dass ein Wort nicht einsortiert wurde
|
|
||||||
if(versuch = MAX_RAND_TRIES_PER_WORD-1){
|
|
||||||
searchFieldLen = 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fügt zufällige Buchstaben ein
|
// fügt zufällige Buchstaben ein
|
||||||
for(int l = 0; l < MAX_SEARCH_FIELD_LEN; l++){
|
for(int l = 0; l < searchFieldLen; l++){
|
||||||
for(int m = 0; m < MAX_SEARCH_FIELD_LEN; 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
|
||||||
srand(time(NULL));
|
|
||||||
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];
|
||||||
// zufällige Buchstaben einfügen
|
// zufällige Buchstaben einfügen
|
||||||
salad[m][n] == zufallsbuchstabe;
|
salad[l][m] = zufallsbuchstabe;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return searchFieldLen
|
|
||||||
|
return placed_words;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 < MAX_SEARCH_FIELD_LEN; p++){
|
for(int p = 0; p < searchFieldLen; p++){
|
||||||
for(int q = 0; q < MAX_SEARCH_FIELD_LEN; q++){
|
for(int q = 0; q < searchFieldLen; q++){
|
||||||
printf("%s", salad[p][q]);
|
printf("%c", salad[p][q]);
|
||||||
}
|
}
|
||||||
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
Start_Windows/game.o
Normal file
BIN
Start_Windows/game.o
Normal file
Binary file not shown.
BIN
Start_Windows/graphicalGame.o
Normal file
BIN
Start_Windows/graphicalGame.o
Normal file
Binary file not shown.
@ -40,12 +40,12 @@ int main(int argc, char *argv[])
|
|||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
// 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
|
||||||
int placed = showWordSalad(salad, searchFieldLen)
|
|
||||||
if ( placed != 1){
|
if (placedWords == wordCount){
|
||||||
showWordSalad(salad, searchFieldLen);
|
showWordSalad(wordSalad, placedWords);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
printf("Error. The words couldn't be placed.");
|
printf("Error. The words couldn't be placed.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@ -44,3 +44,5 @@ test: input.o game.o unit_tests.c
|
|||||||
clean:
|
clean:
|
||||||
del /f *.o *.exe
|
del /f *.o *.exe
|
||||||
|
|
||||||
|
wordsalad_myversion: main.o input.o game.o graphicalGame.o $(BINARIES)/libwordsalad.a
|
||||||
|
$(CC) $(CFLAGS) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libwordsalad.a $(LDFLAGS)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user