Compare commits
39
Commits
1ae5e39309
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f732b3edf9 | ||
|
|
c859d76447 | ||
|
|
561fd213b6 | ||
|
|
3c43aca7b1 | ||
|
|
49f1a06e02 | ||
|
|
2f0857c711 | ||
|
|
b4ee9ae4e4 | ||
|
|
29551521f0 | ||
|
|
b9c4545cc0 | ||
|
|
edf465fd79 | ||
|
|
ae2024266a | ||
|
|
9c29e702e9 | ||
|
|
45a30468f7 | ||
|
|
cb0bbc68de | ||
|
|
580e84491d | ||
|
|
b732b3d526 | ||
|
|
b6f873833d | ||
|
|
852ae19472 | ||
|
|
9e53289322 | ||
|
|
361e5ddaca | ||
|
|
825aa17877 | ||
|
|
fd8b643197 | ||
|
|
ebd16cedec | ||
|
|
408e58205a | ||
|
|
464c7ff1ad | ||
|
|
b78f7ac560 | ||
|
|
2ef856d08a | ||
|
|
ad2984a808 | ||
|
|
0d0ab8ab7e | ||
|
|
4c76932472 | ||
|
|
ef95895501 | ||
|
|
6c5710f2f1 | ||
|
|
def3034ede | ||
|
|
fb4211872e | ||
|
|
70c6635991 | ||
|
|
213103dad0 | ||
|
|
c3e875f493 | ||
|
|
fbd986c66f | ||
|
|
2100c8da2c |
No files matched your search
+64
-8
@@ -1,17 +1,73 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include <time.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 1000
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
//TODO: Spiellogik implementieren:
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int wordCount, char words[][MAX_WORD_LEN], unsigned int gridSize)
|
||||||
/* * 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)
|
|
||||||
{
|
{
|
||||||
|
// 1. Spielfeld zurücksetzen
|
||||||
|
memset(salad, EMPTY_CHAR, MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN);
|
||||||
|
|
||||||
|
int placedWordsCount = 0;
|
||||||
|
|
||||||
|
// 2. Wörter platzieren
|
||||||
|
for (unsigned int i = 0; i < wordCount; i++) {
|
||||||
|
size_t len = strlen(words[i]);
|
||||||
|
|
||||||
|
// Steuerzeichen am Ende entfernen
|
||||||
|
while (len > 0 && (words[i][len - 1] == '\n' || words[i][len - 1] == '\r' || words[i][len - 1] == ' ' || words[i][len - 1] == '\t')) {
|
||||||
|
words[i][--len] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len == 0 || len > gridSize) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++) {
|
||||||
|
Direction dir = (rand() % 2 == 0) ? DIR_HORIZONTAL : DIR_VERTICAL;
|
||||||
|
|
||||||
|
int dr = (dir == DIR_VERTICAL) ? 1 : 0;
|
||||||
|
int dc = (dir == DIR_HORIZONTAL) ? 1 : 0;
|
||||||
|
|
||||||
|
unsigned int maxRow = (dir == DIR_VERTICAL) ? (gridSize - (unsigned int)len + 1) : gridSize;
|
||||||
|
unsigned int maxCol = (dir == DIR_HORIZONTAL) ? (gridSize - (unsigned int)len + 1) : gridSize;
|
||||||
|
|
||||||
|
unsigned int row = (unsigned int)(rand() % maxRow);
|
||||||
|
unsigned int col = (unsigned int)(rand() % maxCol);
|
||||||
|
|
||||||
|
int canPlace = 1;
|
||||||
|
for (unsigned int k = 0; k < len; k++) {
|
||||||
|
char current = salad[row + k * dr][col + k * dc];
|
||||||
|
char wordChar = (char)toupper((unsigned char)words[i][k]);
|
||||||
|
|
||||||
|
if (current != EMPTY_CHAR && current != wordChar) {
|
||||||
|
canPlace = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (canPlace) {
|
||||||
|
for (unsigned int k = 0; k < len; k++) {
|
||||||
|
salad[row + k * dr][col + k * dc] = (char)toupper((unsigned char)words[i][k]);
|
||||||
|
}
|
||||||
|
placedWordsCount++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Leere Felder mit Zufallsbuchstaben ('A'-'Z') füllen
|
||||||
|
for (unsigned int r = 0; r < gridSize; r++) {
|
||||||
|
for (unsigned int c = 0; c < gridSize; c++) {
|
||||||
|
if (salad[r][c] == EMPTY_CHAR) {
|
||||||
|
salad[r][c] = 'A' + (rand() % 26);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return placedWordsCount;
|
||||||
}
|
}
|
||||||
+11
-1
@@ -3,8 +3,18 @@
|
|||||||
|
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
|
#ifndef MAX_SEARCH_FIELD_LEN
|
||||||
#define MAX_SEARCH_FIELD_LEN 100
|
#define MAX_SEARCH_FIELD_LEN 100
|
||||||
|
#endif
|
||||||
|
|
||||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
|
typedef enum {
|
||||||
|
DIR_HORIZONTAL = 0,
|
||||||
|
DIR_VERTICAL = 1
|
||||||
|
} Direction;
|
||||||
|
|
||||||
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||||
|
unsigned int wordCount,
|
||||||
|
char words[][MAX_WORD_LEN],
|
||||||
|
unsigned int gridSize);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+68
-2
@@ -2,11 +2,77 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
// TODO:
|
// TODO: erledigt
|
||||||
// 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.
|
||||||
|
char* readSingleWord(FILE *file, char *destination, int maxLen)
|
||||||
|
{
|
||||||
|
char buffer[256];
|
||||||
|
int ch;
|
||||||
|
int bufIndex = 0;
|
||||||
|
|
||||||
|
// Alle Trennzeichen überspringen, bis ein echter Buchstabe kommt
|
||||||
|
while ((ch = fgetc(file)) != EOF)
|
||||||
|
{
|
||||||
|
// Prüfen der Buchstaben
|
||||||
|
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
|
||||||
|
{
|
||||||
|
// Wenn Kleinbuchstabe, in Großbuchstabe umwandeln
|
||||||
|
if (ch >= 'a' && ch <= 'z')
|
||||||
|
{
|
||||||
|
ch = ch - 32;
|
||||||
|
}
|
||||||
|
buffer[bufIndex] = ch;
|
||||||
|
bufIndex++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch == EOF)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Den Rest des Wortes einlesen (bricht bei Kommas, Leerzeichen etc. ab)
|
||||||
|
while ((ch = fgetc(file)) != EOF && ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')))
|
||||||
|
{
|
||||||
|
if (bufIndex < 255)
|
||||||
|
{
|
||||||
|
if (ch >= 'a' && ch <= 'z')
|
||||||
|
{
|
||||||
|
ch = ch - 32;
|
||||||
|
}
|
||||||
|
buffer[bufIndex] = ch;
|
||||||
|
bufIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zwischenspeicher abschließen
|
||||||
|
buffer[bufIndex] = '\0';
|
||||||
|
|
||||||
|
// Sicheres Kopieren in das Ziel-Array
|
||||||
|
strncpy(destination, buffer, maxLen - 1);
|
||||||
|
destination[maxLen - 1] = '\0';
|
||||||
|
return destination;
|
||||||
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
unsigned int count = 0;
|
||||||
|
// Schleife läuft, solange noch Platz im Array ist
|
||||||
|
while (count < maxWordCount)
|
||||||
|
{
|
||||||
|
if (readSingleWord(file, words[count], MAX_WORD_LEN) != NULL)
|
||||||
|
{
|
||||||
|
if (strlen(words[count]) > 0) // Prüfen, ob das Wort nicht leer ist
|
||||||
|
{
|
||||||
|
count++; // nächstes Wort
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
break; // Bei Dateiende wird die Schleife abgebrochen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
+10
-1
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#define MAX_NUMBER_OF_WORDS 100
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
#define SALAD_SIZE 20
|
#define SALAD_SIZE 20
|
||||||
|
#define WINDOW_WIDTH 800 //Breite Spielfenster 800 Pixel
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@@ -36,10 +37,18 @@ 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:
|
// TODO: erledigt
|
||||||
// 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) //wenn alle Wörter erfolgreicht plaziert sind
|
||||||
|
{
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, WINDOW_WIDTH); //Startet das Spiel
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Fehler: Es konnten nicht alle Wörter plaziert werden\n"); //Gibt Fehlermeldung aus
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -22,6 +22,13 @@ codefolder = .
|
|||||||
wordsalad_initial:
|
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)
|
||||||
|
|
||||||
|
# --------------------------
|
||||||
|
# meine Version
|
||||||
|
# --------------------------
|
||||||
|
wordsalad_myversion: main.o graphicalGame.o $(BINARIES)/libraylib.a
|
||||||
|
$(CC) $(CFLAGS) -o wordsalad_myversion main.o graphicalGame.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Normales Spiel bauen
|
# Normales Spiel bauen
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
Executable
BIN
Binary file not shown.
+1
-5
@@ -2,8 +2,4 @@ Yeti,Nessie Werwolf; Vampir
|
|||||||
Monster
|
Monster
|
||||||
Hydra;Frankenstein
|
Hydra;Frankenstein
|
||||||
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
||||||
Gespenst, Oger, hund
|
Gespenst, Oger
|
||||||
hase
|
|
||||||
nnna, hello, Info2
|
|
||||||
Gespenst, Oger, Maus
|
|
||||||
Nour
|
|
||||||
Reference in new issue
Block a user