generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
17 Commits
45fb1f651b
...
8097bf4bb3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8097bf4bb3 | ||
|
|
a83f9f0bef | ||
|
|
b09e9a5d90 | ||
|
|
5929da2766 | ||
|
|
31fbbb3153 | ||
|
|
a108133a59 | ||
|
|
670c7d1cfc | ||
|
|
f83992ef61 | ||
|
|
673eae7d17 | ||
|
|
c4b0ef5427 | ||
|
|
ec4b32c2a5 | ||
|
|
7b215682a1 | ||
|
|
841e147b9b | ||
|
|
6a3b7700e8 | ||
|
|
c596dc20c3 | ||
|
|
a5d5cf82b6 | ||
|
|
b2118b4c24 |
@ -2,6 +2,8 @@
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
@ -13,79 +15,114 @@
|
||||
// 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)
|
||||
{
|
||||
/*
|
||||
wordSalad: char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN] -> bereits definiert
|
||||
searchFieldLen: SALAD_SIZE = 20
|
||||
words: words_array
|
||||
wordcount: Anzahl der eingelesen Wörter
|
||||
srand(time(NULL));
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
// 1. Schritt: Alle Felder mit zufälligen Zahlen befüllen
|
||||
srand(time(NULL)); //Seed zufällig ändern
|
||||
int size_word;
|
||||
int placed_words;
|
||||
int placedWords = 0;
|
||||
unsigned int attemps = 0;
|
||||
unsigned int check_overlap = 0;
|
||||
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] = 'A' + rand() % 26; //ASCII-Form
|
||||
salad[m][n] = '?';
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
// 2. Schritt: Wörter in das Array überschreiben
|
||||
|
||||
for(int i = 0; i < wordCount && i < searchFieldLen; i++) //eingelesen Wörter zählen + Länge des Feldes nicht überschreiten
|
||||
//Prüfen ob Wort in Array geschrieben darf
|
||||
for(int num_words = 0; num_words < wordCount; num_words++) //eingelesen Wörter zählen
|
||||
{
|
||||
size_t len = strlen(words[i]); //Größe des Wortes ermitteln
|
||||
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
|
||||
|
||||
unsigned int succesfully_placed = 0;
|
||||
unsigned int tries = 0;
|
||||
|
||||
while(succesfully_placed == 0 && tries < MAX_RAND_TRIES_PER_WORD)
|
||||
while(attemps < MAX_RAND_TRIES_PER_WORD)
|
||||
{
|
||||
// zufälliger Startpunkt
|
||||
int row = rand() % searchFieldLen; // Reihe
|
||||
int col = rand() % searchFieldLen; // Spalte
|
||||
|
||||
// zufällige Richtung auswählen
|
||||
int direction = rand() % 2; // 0 = VERTIKAL, 1 = HORIZONTAL
|
||||
|
||||
// Prüfen der Länge in Richtung, sodass Feld nicht überschritten
|
||||
int fitsin = 1;
|
||||
|
||||
for(int t = 0; t < len; t++)
|
||||
// zufälliger Startpunkt, Startkoordinaten
|
||||
if (direction == 0) // 0 = VERTIKAL
|
||||
{
|
||||
if(direction = 0)
|
||||
|
||||
|
||||
zeile = rand() % searchFieldLen;
|
||||
spalte = rand() % searchFieldLen;
|
||||
}
|
||||
|
||||
// Prüfen, ob bereits buchstaben in dem Feld sich befinden, bzw. der Buchstabe mit dem anderen übereinstimmt
|
||||
|
||||
if (direction == 1) // 1 = HORIZONTAL
|
||||
{
|
||||
zeile = rand() % searchFieldLen;
|
||||
spalte = rand() % searchFieldLen;
|
||||
}
|
||||
|
||||
|
||||
if(xxx)
|
||||
for(int i = 0; i < len; i++)
|
||||
{
|
||||
if (direction == 0) // 0 = VERTIKAL
|
||||
{
|
||||
succesfully_placed = 1; // erfolgreich eingefügt
|
||||
//Prüfe ob Feld bereits belegt
|
||||
if (salad[zeile+i][spalte] != '?')
|
||||
{
|
||||
check_overlap = 1; // 1 = Überlappung
|
||||
break;
|
||||
}
|
||||
|
||||
//Prüfe auf Groeße
|
||||
if (spalte + len >= searchFieldLen-1 || check_overlap == 1)
|
||||
{
|
||||
attemps++;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
for(int k; k < len; k++)
|
||||
{
|
||||
salad[zeile][spalte+k] = words[i][k];
|
||||
placedWords++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (direction == 1) // 1 = HORZIZONTAL
|
||||
{
|
||||
//Prüfe ob Feld bereits belegt
|
||||
if (salad[zeile][spalte+i] != '?')
|
||||
{
|
||||
check_overlap = 1; // 1 = Überlappung
|
||||
break;
|
||||
}
|
||||
|
||||
//Prüfe auf Groeße
|
||||
if (zeile + len >= searchFieldLen-1 || check_overlap == 1)
|
||||
{
|
||||
attemps++;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
for(int k; k < len; k++)
|
||||
{
|
||||
salad[zeile+k][spalte] = words[i][k];
|
||||
placedWords++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placedWords;
|
||||
}
|
||||
|
||||
return placedWords; //platzierte Wörter zurückgeben
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Prints the word salad to console
|
||||
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 q = 0; q < searchFieldLen; q++)
|
||||
{
|
||||
printf("%c", salad[p][q]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,54 +1,99 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "input.h"
|
||||
#include "game.h"
|
||||
#include "graphicalGame.h"
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_NUMBER_OF_WORDS 100
|
||||
#define SALAD_SIZE 20
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
||||
|
||||
//TODO: Spiellogik implementieren:
|
||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||
|
||||
// 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 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++)
|
||||
{
|
||||
if (salad[row][collumn+j] != EMPTY_CHAR) {space = 0; break;}
|
||||
//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++;
|
||||
|
||||
}
|
||||
}
|
||||
else if (direction == 1) {// Vertikal funktioniert analog zu horizontal
|
||||
int collumn = rand() % searchFieldLen;
|
||||
int row = rand() % searchFieldLen;
|
||||
for (int j = 0; j < wordLength; j++)
|
||||
{
|
||||
if (salad[row+j][collumn] != EMPTY_CHAR) {space = 0; break;}
|
||||
}
|
||||
if ((row + wordLength >= searchFieldLen-1) || (space == 0)) {attempts++;}
|
||||
else {
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return addedWords;
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
{
|
||||
int exitCode = EXIT_SUCCESS;
|
||||
|
||||
// Check if the correct number of arguments is provided
|
||||
if(argc != 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
|
||||
exitCode = EXIT_FAILURE;
|
||||
}
|
||||
else
|
||||
{
|
||||
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
|
||||
unsigned int wordCount = 0;
|
||||
|
||||
FILE *file = fopen(argv[1], "r");
|
||||
|
||||
if(file != NULL)
|
||||
{
|
||||
unsigned int placedWords = 0;
|
||||
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
||||
|
||||
// Read words from file and store in 'words' array
|
||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
||||
fclose(file);
|
||||
|
||||
// Create the word salad by placing words into grid
|
||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
// TODO:
|
||||
// Check if all words were successfully placed
|
||||
// Start the game if successful
|
||||
// error message if some words couldn't be placed
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// Print error message if file couldn't be opened
|
||||
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
||||
exitCode = EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
return exitCode;
|
||||
for(int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for(int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
printf("%c", salad[i][j]);//Ausgabe des Arrays auf die Konsole
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@ -33,7 +33,7 @@ graphicalGame.o: graphicalGame.c
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
# --------------------------
|
||||
TEST_BIN = runTests
|
||||
TEST_BIN = runTests2
|
||||
|
||||
test: input.o game.o unit_tests.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a
|
||||
@ -43,8 +43,8 @@ test: input.o game.o unit_tests.c
|
||||
# --------------------------
|
||||
clean:
|
||||
del /f *.o *.exe
|
||||
|
||||
#hier eigene wordsalad kopieren, library rauslassen
|
||||
|
||||
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)
|
||||
wordsalad_myversion: main.o input.o game.o graphicalGame.o
|
||||
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
|
||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user