Compare commits
No commits in common. "c87072833045a53555341e81ddf7df7cd9b8b260" and "551d0738eeb417eb52bfb7fbd61a567b4ca7b024" have entirely different histories.
c870728330
...
551d0738ee
@ -1,4 +1,3 @@
|
|||||||
//Bearbeitet von Lukas Wörner, 02.11.25
|
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -7,7 +6,6 @@
|
|||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
//Position beinhaltet x, y und Richtung
|
|
||||||
//TODO: Spiellogik implementieren:
|
//TODO: Spiellogik implementieren:
|
||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||||
@ -15,168 +13,11 @@
|
|||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// 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 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));
|
|
||||||
Position position[(2 * MAX_SEARCH_FIELD_LEN * (MAX_SEARCH_FIELD_LEN - MIN_WORD_LEN + 1))]; //Positionsarray Größe: Formel max. nötige Größe (für minimal großes wort)
|
|
||||||
int gesetzteWörter = 0;
|
|
||||||
clearWordSalad(salad, searchFieldLen);
|
|
||||||
gesetzteWörter = fuelleSalatMitWörtern(salad, searchFieldLen, words, position, wordCount);
|
|
||||||
fillWordsaladRand(salad, searchFieldLen);
|
|
||||||
return gesetzteWörter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < searchFieldLen; i++)
|
|
||||||
{
|
|
||||||
for(int k = 0; k < searchFieldLen; k++)
|
|
||||||
{
|
|
||||||
printf("%c", salad[i][k]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
//Logik für createWordSalad: Ein Wort nach dem anderen bis entweder alle Wörter drinnen,
|
|
||||||
//Funktion dafür den gesammten Salat mit . zu befüllen.
|
|
||||||
void clearWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
|
||||||
{
|
|
||||||
for(unsigned int zeile = 0; zeile < searchFieldLen; zeile++)
|
|
||||||
{
|
|
||||||
for(unsigned int spalte = 0; spalte < searchFieldLen; spalte++)
|
|
||||||
{
|
|
||||||
salad[zeile][spalte] = '.'; // salad wird mit Punkten befüllt.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//Funktion für den Schluss: Übrige leere stellen werden random befüllt.
|
|
||||||
void fillWordsaladRand(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
|
||||||
{
|
|
||||||
for(int i = 0; i < searchFieldLen; i++)
|
|
||||||
{
|
|
||||||
for(int k = 0; k < searchFieldLen; k++)
|
|
||||||
{
|
|
||||||
if(salad[i][k] == '.')
|
|
||||||
{
|
|
||||||
salad[i][k] = 'A' + rand() % 26;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//hierbei: alle Positionen werden zunächst geprüft ob leer (.) danach wird nach rechts und nach unten geschaut ob:
|
|
||||||
//a.) genügend Platz wäre, dass nicht aus dem array herausgeschrieben wird und
|
|
||||||
//b.) alle zu besetzenden Felder leer sind (.).
|
|
||||||
//Positionamount gibt aus wie viele mögliche Positionen es für dieses Wort gibt.
|
|
||||||
//Wird mit 0 initialisiert. Wenns null bleibt, bedeutet dies es gibt keine verfügbaren Positionen.
|
|
||||||
int findPossiblePositions(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], int wordidx, Position positions[])
|
|
||||||
{
|
|
||||||
const char *word = words[wordidx];
|
|
||||||
int wordlen = strlen(word);
|
|
||||||
int positionamount = 0;
|
|
||||||
int rechtsfrei = 1;
|
|
||||||
int untenfrei = 1;
|
|
||||||
for(int i = 0; i < searchFieldLen; i++)
|
|
||||||
{
|
|
||||||
for(int k = 0; k < searchFieldLen; k++)
|
|
||||||
{
|
|
||||||
rechtsfrei = 1;
|
|
||||||
untenfrei = 1;
|
|
||||||
if(salad[i][k] == '.')
|
|
||||||
{
|
|
||||||
//nach rechts prüfen
|
|
||||||
if(searchFieldLen - k >= wordlen)
|
|
||||||
{
|
|
||||||
for(int p = k +1; p < k+ wordlen; p++)
|
|
||||||
{
|
|
||||||
if(salad[i][p] != '.')
|
|
||||||
{
|
|
||||||
rechtsfrei = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rechtsfrei = 0;
|
|
||||||
}
|
|
||||||
//nach unten prüfen
|
|
||||||
if(searchFieldLen - i >= wordlen)
|
|
||||||
{
|
|
||||||
for(int q = i+1; q < i+ wordlen; q++)
|
|
||||||
{
|
|
||||||
if(salad[q][k] != '.')
|
|
||||||
{
|
|
||||||
untenfrei = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
untenfrei = 0;
|
|
||||||
}
|
|
||||||
if(rechtsfrei) //Positions array wird mit gültigen Positionen und Richtung befüllt.
|
|
||||||
{
|
|
||||||
positions[positionamount].x = k;
|
|
||||||
positions[positionamount].y = i;
|
|
||||||
positions[positionamount].richtung = RECHTS;
|
|
||||||
positionamount++;
|
|
||||||
}
|
|
||||||
if(untenfrei)
|
|
||||||
{
|
|
||||||
positions[positionamount].x = k;
|
|
||||||
positions[positionamount].y = i;
|
|
||||||
positions[positionamount].richtung = UNTEN;
|
|
||||||
positionamount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return positionamount;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//Nun muss für jedes Wort per zufall ausgesucht werden Wo es beginnt und welche richtung aus
|
|
||||||
//den ganzen möglichen fällen und dann das wort ins grid gezeichnet werden.
|
|
||||||
int fuelleSalatMitWörtern(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], Position position[], unsigned int wordcount)
|
|
||||||
{
|
|
||||||
int gesetzteWörter = 0;
|
|
||||||
int positionsamount;
|
|
||||||
int positiongewählt;
|
|
||||||
const char *word;
|
|
||||||
for(int i = 0; i < wordcount; i++) //jedes Wort.
|
|
||||||
{
|
|
||||||
positionsamount = findPossiblePositions(salad, searchFieldLen, words, i, position); //für dieses Wort werden die Positionen gefunden
|
|
||||||
if(positionsamount > 0)//es gibt Positionen
|
|
||||||
{
|
|
||||||
gesetzteWörter++;
|
|
||||||
positiongewählt = rand() % positionsamount; //die wie vielte Position des Positionsarrays für dieses Wort wir nehmen
|
|
||||||
if(position[positiongewählt].richtung == RECHTS)
|
|
||||||
{
|
|
||||||
word = words[i];
|
|
||||||
int p = position[positiongewählt].x;
|
|
||||||
int t = 0;
|
|
||||||
for(int k = p; k < p + strlen(word); k++)
|
|
||||||
{
|
|
||||||
salad[position[positiongewählt].y][k] = word[t];
|
|
||||||
t++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(position[positiongewählt].richtung == UNTEN)
|
|
||||||
{
|
|
||||||
word = words[i];
|
|
||||||
int up = position[positiongewählt].y;
|
|
||||||
int ut = 0;
|
|
||||||
for(int uk = up; uk < up +strlen(word); uk++)
|
|
||||||
{
|
|
||||||
salad[uk][position[positiongewählt].x] = word[ut];
|
|
||||||
ut++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return gesetzteWörter;
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,26 +1,11 @@
|
|||||||
//Bearbeitet von Lukas Wörner, 02.11.25
|
|
||||||
#ifndef GAME_H
|
#ifndef GAME_H
|
||||||
#define GAME_H
|
#define GAME_H
|
||||||
|
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
#define MAX_SEARCH_FIELD_LEN 100
|
#define MAX_SEARCH_FIELD_LEN 100
|
||||||
#define MIN_WORD_LEN 2
|
|
||||||
|
|
||||||
typedef enum {RECHTS, UNTEN} RICHTUNG;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
int x, y;
|
|
||||||
RICHTUNG richtung;
|
|
||||||
}Position;
|
|
||||||
|
|
||||||
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 createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
|
||||||
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);
|
||||||
void clearWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
|
|
||||||
void fillWordsaladRand(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
|
|
||||||
int findPossiblePositions(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], int wordidx, Position positions[]);
|
|
||||||
int fuelleSalatMitWörtern(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], Position position[], unsigned int wordcount);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
#include "graphicalGame.h"
|
#include "graphicalGame.h"
|
||||||
|
|
||||||
#define MAX_NUMBER_OF_WORDS 100
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
#define SALAD_SIZE 20 //20 Buchstaben breit und lang
|
#define SALAD_SIZE 20
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@ -22,7 +22,7 @@ int main(int argc, char *argv[])
|
|||||||
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
|
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
|
||||||
unsigned int wordCount = 0;
|
unsigned int wordCount = 0;
|
||||||
|
|
||||||
FILE *file = fopen(argv[1], "r"); //FILE ZEIGER, öffne cmd zweites Wort: Zweites Wort ist ja words.txt also Wort file
|
FILE *file = fopen(argv[1], "r");
|
||||||
|
|
||||||
if(file != NULL)
|
if(file != NULL)
|
||||||
{
|
{
|
||||||
@ -36,16 +36,6 @@ 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);
|
||||||
|
|
||||||
if(placedWords == wordCount) //Checks if all words were placed
|
|
||||||
{
|
|
||||||
startGame(wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], SALAD_SIZE, words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN], placedWords, 800);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("Error. Out of %d words just %d words could be placed.\n", wordCount, placedWords);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
// Start the game if successful
|
// Start the game if successful
|
||||||
|
|||||||
@ -1,17 +1,17 @@
|
|||||||
CC = gcc # Compiler-Variable: welcher C-Compiler genutzt wird (hier gcc)
|
CC = gcc
|
||||||
CFLAGS = -g -Wall -I$(raylibfolder) # -Wall -> alle wichtigen Warnungen aktivieren, -I : suche dort (raylibfolder) nach datei.
|
CFLAGS = -g -Wall -I$(raylibfolder)
|
||||||
LDFLAGS = -lGL -lX11 -lm # Linker-Flags: Bibliotheken, die an das Linker-Kommando übergeben werden
|
LDFLAGS = -lGL -lX11 -lm
|
||||||
BINARIES = ./linux
|
BINARIES = ./linux
|
||||||
|
|
||||||
raylib_folder = ./raylib #Pfade zu raylib_folder und unityfolder
|
raylib_folder = ./raylib
|
||||||
unityfolder = ./unity
|
unityfolder = ./unity
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# initiales Spiel bauen
|
# initiales Spiel bauen
|
||||||
# --------------------------
|
# --------------------------
|
||||||
wordsalad_initial: #hier kommen normalerweise Abhängikeiten hin. Z.b die Datei game.o muss vorhanden sein.
|
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)
|
||||||
#wordsalad_initial linkt nur bibliotheken. Deshalb hat es keine Abhängigkeiten.
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Normales Spiel bauen
|
# Normales Spiel bauen
|
||||||
# --------------------------
|
# --------------------------
|
||||||
@ -41,7 +41,5 @@ test: input.o game.o unit_tests.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
wordsalad_myversion: main.o $(BINARIES)/libwordsalad.a #eigenes Programm erstellen.
|
|
||||||
$(CC) -o wordsalad_myversion main.o $(BINARIES)/libwordsalad.a
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o wordsalad $(TEST_BIN)
|
rm -f *.o wordsalad $(TEST_BIN)
|
||||||
|
|||||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user