game.c_Logik_Selina

This commit is contained in:
Selina Buhlheller 2025-11-05 20:30:54 +01:00
parent c7ee263a19
commit 3d0b1d6729
7 changed files with 207 additions and 3 deletions

View File

@ -14,6 +14,52 @@
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
{
// Wörter in salad einsortieren
// strtok durch words ersetzen, pro Zeile ein Wort
char * einzelwort = strtok(words, "\0");
for (int i = 0; i < wordCount; i++){
int einzelwort_laenge = strlen(einzelwort);
int richtung = rand()% 2;
// muss noch prüfen, ob in der Zeile schon was ist
// 1 ist horizontal, 0 ist vertikal
if (richtung == 1){
for(int k = 0; k < einzelwort_laenge; k++){
int zeile = rand()% MAX_SEARCH_FIELD_LEN;
int spalte = rand()% MAX_SEARCH_FIELD_LEN- einzelwort_laenge + 1;
salad[zeile][spalte + k] = einzelwort[k];
}
}
else if (richtung == 0){
for(int j = 0; j < einzelwort_laenge; j++){
int zeile = rand()% MAX_SEARCH_FIELD_LEN - einzelwort_laenge + 1;
int spalte = rand()% MAX_SEARCH_FIELD_LEN;
salad[zeile + k][spalte] = einzelwort[k];
}
}
strtok(NULL, "\0");
}
// zufällige Buchstaben erzeugen
srand(time(NULL));
char alphabet [] = "abcdefghijklmnopqrstuvwxyz";
int laenge = strlen(alphabet);
int zufallszahl = rand()% laenge;
char zufallsbuchstabe = alphabet[zufallszahl];
//prüfen, ob Wörter vorhanden sind mit isalpha
// -> erst wenn die ganze Länge des Wortes frei ist, array hinzufügen
// MAX Versuche das Wort zu platzieren == 10 -> hinzufügen
// isalpha == 0 -> kein Buchstabe
for(int l = 0; l < MAX_SEARCH_FIELD_LEN; l++){
for(int m = 0; m < MAX_SEARCH_FIELD_LEN; m++ ){
if(isalpha(salad[l][m]) == 0 )
salad[m][n] == zufallsbuchstabe;
}
}
}
// Prints the word salad to console

View File

@ -2,11 +2,89 @@
#include <string.h>
#include <ctype.h>
#define MAX_NUMBER_OF_WORDS 100
#define MAX_WORD_LEN 100
// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
char line [MAX_WORD_LEN];
printf("H");
int j = 0;
int count = 0;
char single_word[MAX_WORD_LEN];
while(fgets(line, MAX_WORD_LEN, file) != NULL ){
printf("A");
for(int i = 0; line[i] != '\0'; i++){
printf("L");
//prüft, ob das gesammelte Zeichen ein Zeichen ist, schiebt es dann Großgeschrieben in das Array word
if (isalpha(line[i]) != 0){
single_word[j++] = toupper(line[i]);
}
// wenn das Wort fertig ist, dann wird dem Wort eine 0 angefügt um das Ende des Strings zu signalisieren, dann wird es in das Array words kopiert
else{
single_word[j] = '\0';
strncpy(words[count], single_word, MAX_WORD_LEN);
count++;
j = 0;
}
}
}
for(int n = 0; n < count ; n++)
{
printf("%s \n", words[n]);
printf("O");
}
return count;
}
int main(){
int wordCount = 0;
FILE *file = fopen("word.txt", "r");
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN];
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file);
return wordCount;
}
/* 1. Versuch
while(fgets(line, maxWordCount, file) != NULL ){
int i = 0;
int count = 0;
char line [MAX_WORD_LEN];
while(line[i] != '\0' && line[i+1] != '\0'){
char single_word[MAX_WORD_LEN];
//prüft, ob das gesammelte Zeichen ein Zeichen ist, schiebt es dann Großgeschrieben in das Array word
if (isalpha(line[i]) != 0){
single_word[i++] = toupper(line[i]);
}
// wenn das Wort fertig ist, dann wird dem Wort eine 0 angefügt um das Ende des Strings zu signalisieren, dann wird es in das Array words kopiert
else{
single_word[i++] = '\0';
strncpy(single_word, words, i)
words[i][0] = '\0';
}
i++;
}
*/

BIN
Start_Windows/input.o Normal file

Binary file not shown.

View File

@ -36,10 +36,17 @@ int main(int argc, char *argv[])
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// TODO:
// TODO: (if Schleife implementieren, die das prüft)
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
if (wordsalad[] == words[]){
showWordSalad(salad, searchFieldLen);
}
else{
printf("Error. The words couldn't be placed.");
return -1;
}
}
else
@ -52,3 +59,5 @@ int main(int argc, char *argv[])
return exitCode;
}
//

BIN
Start_Windows/main.o Normal file

Binary file not shown.

View File

@ -43,3 +43,4 @@ test: input.o game.o unit_tests.c
# --------------------------
clean:
del /f *.o *.exe

70
Start_Windows/test.c Normal file
View File

@ -0,0 +1,70 @@
#include "input.h"
#include <string.h>
#include <ctype.h>
// launch
#define MAX_NUMBER_OF_WORDS 100
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
char line [MAX_WORD_LEN];
int i = 0;
int j = 0;
int count = 0;
file = fopen("words.txt", "r");
char single_word[MAX_WORD_LEN];
while(fgets(line, maxWordCount, file) != NULL ){
while(line[i] != '\0' && line[i+1] != '\0'){
//prüft, ob das gesammelte Zeichen ein Zeichen ist, schiebt es dann Großgeschrieben in das Array word
if (isalpha(line[i]) != 0){
single_word[j++] = toupper(line[i]);
}
// wenn das Wort fertig ist, dann wird dem Wort eine 0 angefügt um das Ende des Strings zu signalisieren, dann wird es in das Array words kopiert
else{
single_word[j++] = '\0';
strncpy(words[count], single_word, i);
i = 0;
count++;
int j = 0;
}
i++;
}
}
for(int n = 0; n < count ; n++)
{
printf("%s \n", words[n]);
}
}
int main()
{
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN];
FILE*file;
unsigned int wordCount = 0;
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
}