generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
1
Commits
main
..
0feb915e43
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0feb915e43 |
No files matched your search
@@ -1,9 +0,0 @@
|
|||||||
# Wortsalat - Praktikum Informatik 2
|
|
||||||
|
|
||||||
Ein klassisches Wortsuchspiel, bei dem Wörter aus einer Textdatei zufällig horizontal oder vertikal in einem Gitter platziert werden. Der Spieler muss diese Wörter im Spielfeld finden.
|
|
||||||
|
|
||||||
## Bedienung & Kompilierung
|
|
||||||
|
|
||||||
1. **Eigenes Target bauen:**
|
|
||||||
|
|
||||||
make wordsalad_myversion
|
|
||||||
@@ -13,89 +13,5 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
if (salad == NULL || words == NULL || searchFieldLen == 0){
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
//1. Spielfeld mit Nullen (Leerzeichen/leeren Zellen) Vorinitialisieren
|
|
||||||
for(unsigned int i = 0; i< searchFieldLen; i++){
|
|
||||||
for(unsigned int j = 0; j < searchFieldLen; j++)
|
|
||||||
{
|
|
||||||
salad[i][j] = '\0';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Zufallszahlengenerator initialisation
|
|
||||||
srand((unsigned int)time(NULL));
|
|
||||||
|
|
||||||
unsigned int sucessfullyPlaced = 0;
|
|
||||||
|
|
||||||
//2. Jedes Wort einzeln im Gitter Plazieren
|
|
||||||
for(unsigned int w = 0; w < wordCount; w++)
|
|
||||||
{
|
|
||||||
int len = (int)strlen(words[w]);
|
|
||||||
if((unsigned int)len > searchFieldLen){
|
|
||||||
//wort ist zu lang für das Suchfeld
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
int placed = 0;
|
|
||||||
int attempts = 0;
|
|
||||||
|
|
||||||
//MAximum Versuche, um eine passende Position zu finden, um ENdlosshleifen zu vermeiden
|
|
||||||
while(!placed && attempts < 5000){
|
|
||||||
attempts++;
|
|
||||||
int direction = rand() %2; // 0 = horizontal, 1 = vertical
|
|
||||||
int row = 0;
|
|
||||||
int col = 0;
|
|
||||||
|
|
||||||
if(direction == 0) // horizontal
|
|
||||||
{
|
|
||||||
row = rand() % searchFieldLen;
|
|
||||||
col = rand() % (searchFieldLen - len + 1);
|
|
||||||
}
|
|
||||||
else // vertical
|
|
||||||
{
|
|
||||||
row = rand() % (searchFieldLen - len + 1);
|
|
||||||
col = rand() % searchFieldLen;
|
|
||||||
}
|
|
||||||
|
|
||||||
// KOllisionsprüfung
|
|
||||||
|
|
||||||
int collision = 0;
|
|
||||||
for(int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
int r = row + (direction == 1? i : 0);
|
|
||||||
int c = col + (direction == 0 ? i : 0);
|
|
||||||
|
|
||||||
|
|
||||||
if(salad[r][c] != '\0' && salad[r][c] != words[w][i]){
|
|
||||||
collision = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//wenn keine kollision vorliegt, Wort ins Gitter schreiben
|
|
||||||
|
|
||||||
if(!collision){
|
|
||||||
for(int i = 0; i < len; i++){
|
|
||||||
int r = row + (direction == 1 ? i : 0);
|
|
||||||
int c = col + (direction == 0 ? i : 0);
|
|
||||||
salad[r][c] = words[w][i];
|
|
||||||
}
|
|
||||||
sucessfullyPlaced++;
|
|
||||||
placed = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Alle restlichen (leeren) Felder mit zufäalligen Großbuchstaben ('A' - 'Z') auffullen
|
|
||||||
for(unsigned int i = 0; i < searchFieldLen; i++){
|
|
||||||
for(unsigned int j = 0; j < searchFieldLen; j++)
|
|
||||||
{
|
|
||||||
if(salad[i][j] == '\0'){
|
|
||||||
salad[i][j] = (char)('A' + (rand() % 26));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (int)sucessfullyPlaced;
|
|
||||||
}
|
}
|
||||||
@@ -4,52 +4,9 @@
|
|||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||||
// HIlfsfunktion: LIest das nächste Wort(Tocken) aus der Datei und gibt es als C-String züruck
|
|
||||||
char* readNextWord(FILE *file, char *lineBuffer, unsigned int maxLineLen, int *isNewLineNeeded){
|
|
||||||
const char *delimiters = " ,;\t\r\n";
|
|
||||||
char *token = NULL;
|
|
||||||
|
|
||||||
//Falls wir schon in einer Zeile sind, Versuche das nächste Word daraus zu holen
|
|
||||||
if(!(*isNewLineNeeded)){
|
|
||||||
token = strtok(NULL, delimiters);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (token == NULL)
|
|
||||||
{
|
|
||||||
if(fgets(lineBuffer, maxLineLen, file) == NULL)
|
|
||||||
{
|
|
||||||
// Dateiend (EOF) erreicht
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
token = strtok(lineBuffer, delimiters);
|
|
||||||
*isNewLineNeeded = 0;
|
|
||||||
}
|
|
||||||
return token;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
if(file == NULL || words == NULL || maxWordCount == 0){
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
char lineBuffer[MAX_LINE_LEN];
|
|
||||||
int isNewLineNeeded = 1;
|
|
||||||
unsigned int count = 0;
|
|
||||||
char *word = NULL;
|
|
||||||
|
|
||||||
while(count < maxWordCount && (word = readNextWord(file, lineBuffer, MAX_LINE_LEN, &isNewLineNeeded)) != NULL)
|
|
||||||
{
|
|
||||||
//Sicher kopieren mit strncpy und Nulltermination garantieren
|
|
||||||
strncpy(words[count], word, MAX_WORD_LEN - 1);
|
|
||||||
words[count][MAX_WORD_LEN - 1] = '\0';
|
|
||||||
|
|
||||||
//changing all leter to caplogs
|
|
||||||
|
|
||||||
for(unsigned int i = 0; words[count][i] != '\0'; i++){
|
|
||||||
words[count][i] = (char)toupper((unsigned char)words[count][i]);
|
|
||||||
}
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
return (int)count;
|
|
||||||
}
|
}
|
||||||
@@ -40,14 +40,6 @@ 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
|
||||||
if(placedWords == wordCount){
|
|
||||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
fprintf(stderr, "Error: Could not place all worlds into the world salad (%u of %u placed).\n", placedWords, wordCount);
|
|
||||||
|
|
||||||
exitCode = EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -23,17 +23,6 @@ wordsalad_initial:
|
|||||||
$(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
$(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
#---------------------------
|
|
||||||
# My version
|
|
||||||
#-----------------------
|
|
||||||
#wordsalad_myversion: main.o
|
|
||||||
# $(CC) $(CFLAGS) -o wordsalad_myversion main.o -L. $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
|
||||||
|
|
||||||
#-----------------------------
|
|
||||||
#Building myversion
|
|
||||||
#------------------------
|
|
||||||
wordsalad_myversion: main.o graphicalGame.o
|
|
||||||
$(CC) $(CFLAGS) -o wordsalad_myversion main.o graphicalGame.o -L. $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
|
||||||
# Normales Spiel bauen
|
# Normales Spiel bauen
|
||||||
# --------------------------
|
# --------------------------
|
||||||
wordsalad: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
|
wordsalad: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user