Programmierung von game.c & main.c
This commit is contained in:
parent
f398d5b87a
commit
4331fc66e4
28
.vscode/tasks.json
vendored
Normal file
28
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"type": "cppbuild",
|
||||||
|
"label": "C/C++: gcc.exe build active file",
|
||||||
|
"command": "C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/gcc.exe",
|
||||||
|
"args": [
|
||||||
|
"-fdiagnostics-color=always",
|
||||||
|
"-g",
|
||||||
|
"${file}",
|
||||||
|
"-o",
|
||||||
|
"${fileDirname}\\${fileBasenameNoExtension}.exe"
|
||||||
|
],
|
||||||
|
"options": {
|
||||||
|
"cwd": "C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin"
|
||||||
|
},
|
||||||
|
"problemMatcher": [
|
||||||
|
"$gcc"
|
||||||
|
],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
},
|
||||||
|
"detail": "Task generated by Debugger."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": "2.0.0"
|
||||||
|
}
|
||||||
7
Start_Windows/.vscode/launch.json
vendored
Normal file
7
Start_Windows/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": []
|
||||||
|
}
|
||||||
3
Start_Windows/.vscode/settings.json
vendored
Normal file
3
Start_Windows/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"C_Cpp.errorSquiggles": "disabled"
|
||||||
|
}
|
||||||
28
Start_Windows/.vscode/tasks.json
vendored
Normal file
28
Start_Windows/.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"type": "cppbuild",
|
||||||
|
"label": "C/C++: gcc.exe build active file",
|
||||||
|
"command": "C:\\ProgramData\\mingw64\\mingw64\\bin\\gcc.exe",
|
||||||
|
"args": [
|
||||||
|
"-fdiagnostics-color=always",
|
||||||
|
"-g",
|
||||||
|
"${file}",
|
||||||
|
"-o",
|
||||||
|
"${fileDirname}\\${fileBasenameNoExtension}.exe"
|
||||||
|
],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${fileDirname}"
|
||||||
|
},
|
||||||
|
"problemMatcher": [
|
||||||
|
"$gcc"
|
||||||
|
],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
},
|
||||||
|
"detail": "Task generated by Debugger."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": "2.0.0"
|
||||||
|
}
|
||||||
@ -10,14 +10,101 @@
|
|||||||
/* * 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 */
|
||||||
|
|
||||||
// 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)
|
//Funktion: Initialisiert das Spielfeld mit leeren Zeichen
|
||||||
|
static void initSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int feldGroesse)
|
||||||
{
|
{
|
||||||
|
for (unsigned int i = 0; i < feldGroesse; i++)
|
||||||
|
for (unsigned int j = 0; j < feldGroesse; j++)
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Funktion: Versucht, ein Wort an einer bestimmten Position zu platzieren
|
||||||
|
// Richtung: 0 = horizontal, 1 = vertikal
|
||||||
|
static int platziereWort(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int feldGroesse, const char *wort,
|
||||||
|
int zeile, int spalte, int richtung)
|
||||||
|
{
|
||||||
|
unsigned int wortLaenge = strlen(wort);
|
||||||
|
|
||||||
|
// Überprüfen, ob das Wort ins Feld passt
|
||||||
|
if (richtung == 0 && spalte + wortLaenge > feldGroesse) return 0; // horizontal
|
||||||
|
if (richtung == 1 && zeile + wortLaenge > feldGroesse) return 0; // vertikal
|
||||||
|
|
||||||
|
// Prüfen, ob die Positionen frei oder kompatibel sind
|
||||||
|
for (unsigned int i = 0; i < wortLaenge; i++) {
|
||||||
|
char c = (richtung == 0) ? salad[zeile][spalte + i] : salad[zeile + i][spalte];
|
||||||
|
if (c != EMPTY_CHAR && c != wort[i])
|
||||||
|
return 0; // Kollision
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wort eintragen
|
||||||
|
for (unsigned int i = 0; i < wortLaenge; i++) {
|
||||||
|
if (richtung == 0)
|
||||||
|
salad[zeile][spalte + i] = wort[i];
|
||||||
|
else
|
||||||
|
salad[zeile + i][spalte] = wort[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
srand((unsigned int)time(NULL));
|
||||||
|
initSalad(salad, searchFieldLen);
|
||||||
|
|
||||||
|
unsigned int erfolgreichGesetzt = 0;
|
||||||
|
|
||||||
|
//Wörter zufällig platzieren
|
||||||
|
for(unisgned int w = 0; w < wordCount; w++) {
|
||||||
|
int platziert = 0;
|
||||||
|
|
||||||
|
for (unsigned int w = 0; w < wordCount; w++)
|
||||||
|
{
|
||||||
|
for (int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD && !platziert; versuch++) {
|
||||||
|
int richtung = rand() % 2; // 0 = horizontal, 1 = vertikal
|
||||||
|
int zeile = rand() % searchFieldLen;
|
||||||
|
int spalte = rand() % searchFieldLen;
|
||||||
|
platziert = platziereWort(salad, searchFieldLen, words[w], zeile, spalte, richtung);
|
||||||
|
}
|
||||||
|
if (platziert)
|
||||||
|
erfolgreichGesetzt++;
|
||||||
|
else
|
||||||
|
fprintf(stderr, "Warnung: Konnte Wort nicht platzieren: %s\n", words[w]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 erfolgreichGesetzt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 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 (unsigned int i = 0; i < searchFieldLen; i++) {
|
||||||
|
for (unsigned int j = 0; j < searchFieldLen; j++) {
|
||||||
|
printf("%c ", salad[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,6 +41,21 @@ int main(int argc, char *argv[])
|
|||||||
// Start the game if successful
|
// Start the game if successful
|
||||||
// error message if some words couldn't be placed
|
// error message if some words couldn't be placed
|
||||||
|
|
||||||
|
if (placedWords == wordCount)
|
||||||
|
{
|
||||||
|
printf("Alle %u Wörter wurden erfolgreich platziert!\n\n", wordCount);
|
||||||
|
|
||||||
|
// Spiel starten
|
||||||
|
showWordSalad(wordSalad, SALAD_SIZE);
|
||||||
|
// oder: startGraphicalGame(wordSalad, SALAD_SIZE, words, wordCount);
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr,"Fehler: Nur %u von %u Wörtern konnten platziert werden.\n", placedWords, wordCount);
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
BIN
Start_Windows/wordsalad_initial.exe
Normal file
BIN
Start_Windows/wordsalad_initial.exe
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user