erster vollständiger test
This commit is contained in:
parent
a0a0376c7d
commit
06bb1e829b
@ -3,7 +3,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define MAX_RAND_TRIES_PER_WORD 200
|
||||
#define EMPTY_CHAR 0
|
||||
|
||||
//TODO: Spiellogik implementieren:
|
||||
@ -11,18 +11,78 @@
|
||||
* 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 ausrichtung)
|
||||
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 placedWords = 0;
|
||||
// Spielfeld leeren
|
||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||
salad[i][j] = EMPTY_CHAR;
|
||||
|
||||
srand(time(NULL));
|
||||
ausrichtung = rand()%2; // 0=Horizontal 1=Vertikal
|
||||
srand(time(NULL));
|
||||
|
||||
//test
|
||||
for (unsigned int w = 0; w < wordCount; w++) {
|
||||
const char *word = words[w];
|
||||
unsigned int len = strlen(word);
|
||||
int placed = 0;
|
||||
|
||||
// Versuche, das Wort zu platzieren
|
||||
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) {
|
||||
int ausrichtung = rand() % 2; // 0 = horizontal, 1 = vertikal
|
||||
unsigned int x = rand() % searchFieldLen;
|
||||
unsigned int y = rand() % searchFieldLen;
|
||||
|
||||
// Passt das Wort überhaupt ins Feld?
|
||||
if ((ausrichtung == 0 && y + len > searchFieldLen) ||
|
||||
(ausrichtung == 1 && x + len > searchFieldLen))
|
||||
continue;
|
||||
|
||||
// Prüfen auf Überschneidung
|
||||
int fits = 1;
|
||||
for (unsigned int k = 0; k < len; k++) {
|
||||
char c = (ausrichtung == 0) ? salad[x][y + k] : salad[x + k][y];
|
||||
if (c != EMPTY_CHAR && c != word[k]) {
|
||||
fits = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn es passt, Wort eintragen
|
||||
if (fits) {
|
||||
for (unsigned int k = 0; k < len; k++) {
|
||||
if (ausrichtung == 0)
|
||||
salad[x][y + k] = word[k];
|
||||
else
|
||||
salad[x + k][y] = word[k];
|
||||
}
|
||||
placed = 1;
|
||||
placedWords++;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Leere Felder mit Zufallsbuchstaben füllen
|
||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||
if (salad[i][j] == EMPTY_CHAR)
|
||||
salad[i][j] = 'A' + (rand() % 26);
|
||||
|
||||
return placedWords;
|
||||
}
|
||||
|
||||
|
||||
// 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 (unsigned int i = 0; i < searchFieldLen; i++) {
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||
printf("%c ", salad[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -4,54 +4,64 @@
|
||||
#include "game.h"
|
||||
#include "graphicalGame.h"
|
||||
|
||||
//halloooo
|
||||
|
||||
#define MAX_NUMBER_OF_WORDS 100
|
||||
#define SALAD_SIZE 20
|
||||
#define WINDOW_WIDTH 800 // Beispiel: Fensterbreite für das GUI
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int exitCode = EXIT_SUCCESS;
|
||||
char *filename;
|
||||
|
||||
// Check if the correct number of arguments is provided
|
||||
// Prüfen, ob Datei angegeben wurde, sonst Standarddatei verwenden
|
||||
if(argc != 2)
|
||||
{
|
||||
printf("Keine Datei angegeben – versuche Standarddatei 'words.txt'...\n");
|
||||
argv[1] = "words.txt";
|
||||
argc = 2;
|
||||
printf("Keine Datei angegeben – versuche Standarddatei 'words.txt'...\n");
|
||||
filename = "words.txt";
|
||||
}
|
||||
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;
|
||||
}
|
||||
filename = argv[1];
|
||||
}
|
||||
|
||||
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN];
|
||||
unsigned int wordCount = 0;
|
||||
|
||||
FILE *file = fopen(filename, "r");
|
||||
if(file == NULL)
|
||||
{
|
||||
fprintf(stderr, "Konnte Datei %s nicht öffnen.\n", filename);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Wörter einlesen
|
||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
||||
fclose(file);
|
||||
|
||||
if(wordCount == 0)
|
||||
{
|
||||
fprintf(stderr, "Keine Wörter in der Datei gefunden.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
|
||||
// Wortsalat erstellen
|
||||
unsigned int placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
if(placedWords < wordCount)
|
||||
{
|
||||
fprintf(stderr, "Nicht alle Wörter konnten platziert werden: %u von %u.\n", placedWords, wordCount);
|
||||
}
|
||||
if(placedWords == 0) {
|
||||
fprintf(stderr, "Fehler: Keine Wörter konnten platziert werden.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
// Wortsalat in der Konsole anzeigen
|
||||
showWordSalad(wordSalad, SALAD_SIZE);
|
||||
|
||||
// GUI-Spiel starten
|
||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, WINDOW_WIDTH);
|
||||
|
||||
return exitCode;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user