gute version
This commit is contained in:
parent
629f5ed15e
commit
992119f42c
@ -1,223 +1,79 @@
|
||||
#include "game.h"
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR '.'
|
||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||
unsigned int size, const char words[][MAX_WORD_LEN],
|
||||
unsigned int wordCount) {
|
||||
#define EMPTY_CHAR '.'
|
||||
typedef enum { HORIZONTAL, VERTICAL } Direction;
|
||||
|
||||
void initializeArray() {
|
||||
for (unsigned int y = 0; y < size; y++)
|
||||
for (unsigned int x = 0; x < size; x++)
|
||||
salad[y][x] = EMPTY_CHAR;
|
||||
}
|
||||
|
||||
// TODO: Spiellogik implementieren:
|
||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||
|
||||
|
||||
|
||||
static void initializeArray(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) //Array initialisieren & mit Filler füllen
|
||||
{
|
||||
|
||||
for (int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
|
||||
for (int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
salad[i][j] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void col_row (unsigned int searchFieldLen, int *x, int *y) //Zufällige x/y Koordinate für Anfang von Wort
|
||||
{
|
||||
*x = rand() % searchFieldLen;
|
||||
|
||||
*y = rand() % searchFieldLen;
|
||||
}
|
||||
|
||||
|
||||
static int place(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], int searchFieldLen, int len [],const char words [] [MAX_WORD_LEN], unsigned int wordCount) //Prüfen ob plazierbar und plazieren
|
||||
{
|
||||
int x, y;
|
||||
int placedWords = 0; //Counter plazierte Wörter
|
||||
|
||||
|
||||
for(int i = 0; i < wordCount; i++)
|
||||
{
|
||||
int placed = 0;
|
||||
|
||||
|
||||
for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++)
|
||||
{
|
||||
len[i] = (int)strlen(words[i]);
|
||||
|
||||
col_row(searchFieldLen, &x, &y);
|
||||
|
||||
int orient = rand() % 2;
|
||||
|
||||
|
||||
if(orient == 0) //wir prüfen und füllen horizontal (in x-Richtung)
|
||||
{
|
||||
int works = 1;
|
||||
|
||||
if(((x + len[i]) > searchFieldLen))
|
||||
{
|
||||
|
||||
works = 0;
|
||||
|
||||
}
|
||||
|
||||
int canPlace(unsigned int x, unsigned int y, const char *word, Direction dir) {
|
||||
unsigned int len = strlen(word);
|
||||
if ((dir == HORIZONTAL && x + len > size) || (dir == VERTICAL && y + len > size))
|
||||
return 0;
|
||||
for (unsigned int i = 0; i < len; i++) {
|
||||
if ((dir == HORIZONTAL && salad[y][x + i] != EMPTY_CHAR) ||
|
||||
(dir == VERTICAL && salad[y + i][x] != EMPTY_CHAR))
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void placeWord(unsigned int x, unsigned int y, const char *word, Direction dir) {
|
||||
for (unsigned int i = 0; i < strlen(word); i++) {
|
||||
if (dir == HORIZONTAL)
|
||||
salad[y][x + i] = word[i];
|
||||
else
|
||||
{
|
||||
salad[y + i][x] = word[i];
|
||||
}
|
||||
}
|
||||
|
||||
for(int j = x; j < (x + len[i]); j++)
|
||||
{
|
||||
|
||||
if(salad[y][j] != EMPTY_CHAR)
|
||||
{
|
||||
|
||||
works = 0;
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
void fillRandomLetters() {
|
||||
for (unsigned int y = 0; y < size; y++) {
|
||||
for (unsigned int x = 0; x < size; x++) {
|
||||
if (salad[y][x] == EMPTY_CHAR)
|
||||
salad[y][x] = 'A' + (rand() % 26);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ab hier platzieren wir
|
||||
|
||||
if(works)
|
||||
{
|
||||
|
||||
for (int k = 0; k < len[i]; k++)
|
||||
{
|
||||
|
||||
salad[y][x + k] = words[i][k];
|
||||
|
||||
}
|
||||
|
||||
placed = 1;
|
||||
placedWords++;
|
||||
srand(time(NULL));
|
||||
initializeArray();
|
||||
|
||||
int placed = 0;
|
||||
for (unsigned int i = 0; i < wordCount; i++) {
|
||||
int success = 0;
|
||||
for (int tries = 0; tries < 1000 && !success; tries++) {
|
||||
unsigned int x = rand() % size;
|
||||
unsigned int y = rand() % size;
|
||||
Direction dir = rand() % 2 ? HORIZONTAL : VERTICAL;
|
||||
if (canPlace(x, y, words[i], dir)) {
|
||||
placeWord(x, y, words[i], dir);
|
||||
success = 1;
|
||||
placed++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fillRandomLetters();
|
||||
|
||||
else //wir prüfen und füllen vertikal (in y-Richtung)
|
||||
{
|
||||
int works = 1;
|
||||
|
||||
if(((y + len[i]) > searchFieldLen))
|
||||
{
|
||||
|
||||
works = 0;
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
|
||||
for(int j = y; j < (y + len[i]); j++)
|
||||
{
|
||||
|
||||
if(salad[j][x] != EMPTY_CHAR)
|
||||
{
|
||||
|
||||
works = 0;
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ab hier platzieren wir
|
||||
|
||||
if(works)
|
||||
{
|
||||
|
||||
for (int k = 0; k < len[i]; k++)
|
||||
{
|
||||
|
||||
salad[y + k][x] = words[i][k];
|
||||
|
||||
}
|
||||
|
||||
placed = 1;
|
||||
placedWords++;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placedWords;
|
||||
|
||||
return placed;
|
||||
}
|
||||
|
||||
|
||||
static void fillLetters(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) //Übrige freie Felder mit Buchstaben füllen
|
||||
{
|
||||
|
||||
for (int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
|
||||
for (int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
|
||||
if (salad[i][j] == EMPTY_CHAR)
|
||||
{
|
||||
|
||||
char random_letter = 'A' + (rand() % 26);
|
||||
salad[i][j] = random_letter;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Erstellt Wortsalat, plaziert Wörter zufällig & füllt die Lücken
|
||||
|
||||
int createWordSalad(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words [] [MAX_WORD_LEN], unsigned int wordCount)
|
||||
{
|
||||
srand(time(NULL));
|
||||
|
||||
initializeArray(salad, searchFieldLen);
|
||||
|
||||
int len [wordCount];
|
||||
int count = 0;
|
||||
|
||||
for(int i = 0; i < wordCount; i++)
|
||||
{
|
||||
|
||||
if(words [i] [0] != '\0')
|
||||
count++;
|
||||
|
||||
}
|
||||
|
||||
int placeWords = place(salad, searchFieldLen, len, words, count);
|
||||
|
||||
|
||||
fillLetters(salad, searchFieldLen);
|
||||
|
||||
return placeWords;
|
||||
|
||||
}
|
||||
|
||||
// Ausgabe von Wortsalat auf Konsole
|
||||
|
||||
void showWordSalad(const char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
{
|
||||
|
||||
for (int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
|
||||
for (int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
|
||||
printf("%c ", salad [i][j]);
|
||||
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
}
|
||||
}
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int size) {
|
||||
for (unsigned int y = 0; y < size; y++) {
|
||||
for (unsigned int x = 0; x < size; x++) {
|
||||
printf("%c ", salad[y][x]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@ -11,44 +11,50 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
int exitCode = EXIT_SUCCESS;
|
||||
|
||||
// Check if the correct number of arguments is provided
|
||||
if(argc != 2)
|
||||
if (argc != 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
|
||||
exitCode = EXIT_FAILURE;
|
||||
fprintf(stderr, "Benutzung: %s <Pfad zur Wörterdatei>\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
else
|
||||
|
||||
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN];
|
||||
unsigned int wordCount = 0;
|
||||
|
||||
FILE *file = fopen(argv[1], "r");
|
||||
if (!file)
|
||||
{
|
||||
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;
|
||||
}
|
||||
fprintf(stderr, "Konnte Datei %s nicht öffnen.\n", argv[1]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
unsigned int placedWords = 0;
|
||||
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
|
||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
||||
fclose(file);
|
||||
|
||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
printf("\nWortsalat-Initialisierung\n");
|
||||
printf("-------------------------\n");
|
||||
printf("Gelesene Wörter: %u\n", wordCount);
|
||||
printf("Platzierte Wörter: %u\n", placedWords);
|
||||
|
||||
if (placedWords < wordCount)
|
||||
printf("Hinweis: %u Wörter konnten nicht platziert werden.\n", wordCount - placedWords);
|
||||
|
||||
printf("\nSpielfeld (%dx%d):\n\n", SALAD_SIZE, SALAD_SIZE);
|
||||
for (int i = 0; i < SALAD_SIZE; i++)
|
||||
{
|
||||
for (int j = 0; j < SALAD_SIZE; j++)
|
||||
printf("%c ", wordSalad[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\nInitialisierung abgeschlossen.\n");
|
||||
|
||||
// Später evtl.:
|
||||
// startGraphicalGame(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
return exitCode;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user