update game.c

This commit is contained in:
maxgrf 2025-11-10 13:43:26 +01:00
parent b09e9a5d90
commit a83f9f0bef
5 changed files with 88 additions and 90 deletions

View File

@ -2,7 +2,7 @@
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
//#include <ctype.h> #include <ctype.h>
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
@ -107,50 +107,13 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
} }
} }
} }
/*
//Wort in Wortsalat schreiben
if(check_direction == 1 && check_overlap == 0 && tries < MAX_RAND_TRIES_PER_WORD)
{
for(int i_set = 0; i_set < len; i_set++)
{
if (direction == 0) // 0 = VERTIKAL
{
salad[zeile + i_set][spalte] = words[num_words][i_set];
placedWords++;
}
if (direction == 1) // 1 = HORIZONTAL
{
salad[zeile][spalte + i_set] = words[num_words][i_set];
placedWords++;
}
}
}
}
}
// fügt zufällige Buchstaben ein
for(int l = 0; l < searchFieldLen; l++){
for(int m = 0; m < searchFieldLen; m++ ){
if(isalpha(salad[l][m]) == 0 ){
// zufällige Buchstaben erzeugen
char alphabet [] = "abcdefghijklmnopqrstuvwxyz";
int laenge = strlen(alphabet);
int zufallszahl = rand()% laenge;
char zufallsbuchstabe = alphabet[zufallszahl];
// zufällige Buchstaben einfügen
salad[l][m] = zufallsbuchstabe;
}
}
}
*/
} }
return placedWords; //platzierte Wörter zurückgeben return placedWords; //platzierte Wörter zurückgeben
} }
// Prints the word salad to console // 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)
{ {

Binary file not shown.

View File

@ -1,64 +1,99 @@
#include <stdlib.h>
#include <stdio.h>
#include "input.h"
#include "game.h" #include "game.h"
#include "graphicalGame.h" #include <time.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUMBER_OF_WORDS 100 #define MAX_RAND_TRIES_PER_WORD 10
#define SALAD_SIZE 20 #define EMPTY_CHAR 0
int main(int argc, char *argv[])
{
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"); //TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
if(file != NULL) // 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) {
unsigned int placedWords = 0; int addedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad 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;
// Read words from file and store in 'words' array for (int i = 0; i < searchFieldLen; i++) {
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); for (int j = 0; j < searchFieldLen; j++) {
fclose(file); salad[i][j] = EMPTY_CHAR; //Füllen des gesamten Arrays mit Nullen
}
}
for (int i=0; i < wordCount; i++) {
// Create the word salad by placing words into grid wordLength = strlen(words[i]);
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// TODO: (if Schleife implementieren, die das prüft) while (attempts <= MAX_RAND_TRIES_PER_WORD && added != 1) {
// Check if all words were successfully placed int direction = rand() % 2;// 0 Horizontal, 1 Vertikal
// Start the game if successful if (direction == 0) {
// error message if some words couldn't be placed int collumn = rand() % searchFieldLen;
int row = rand() % searchFieldLen;
if (placedWords == wordCount){ for (int j = 0; j < wordLength; j++)
startGame(wordSalad, SALAD_SIZE, words, wordCount, 750); {
if (salad[row][collumn+j] != EMPTY_CHAR) {space = 0; break;}
//Prüfen ob das Wort ein bereits vorhandenes Wort überspeichern würde
} }
else{ if ((collumn + wordLength >= searchFieldLen-1) || (space == 0)) {attempts++;}
printf("Error. The words couldn't be placed.\n"); //Prüfen ob Wort an der Stelle (Zeile, Spalte) in das Array passt
return -1; 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 }
{ else if (direction == 1) {// Vertikal funktioniert analog zu horizontal
// Print error message if file couldn't be opened int collumn = rand() % searchFieldLen;
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]); int row = rand() % searchFieldLen;
exitCode = EXIT_FAILURE; 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;
return exitCode; }
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)
{
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
}
}
}

BIN
Start_Windows/main.o Normal file

Binary file not shown.

BIN
Start_Windows/wordsalad.exe Normal file

Binary file not shown.