generated from freudenreichan/info2Praktikum-Wortsalat
added new, updated files
This commit is contained in:
parent
dc722627e3
commit
a373e95c9f
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\\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"
|
||||||
|
}
|
||||||
@ -2,22 +2,129 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 100
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
//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 */
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
int wordlength;
|
||||||
|
int direction;
|
||||||
|
int horizontal;
|
||||||
|
int vertical;
|
||||||
|
int possible_coordinate = 0;
|
||||||
|
int place_word = 0;
|
||||||
|
// Initialisierung Zufallszahl
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
// Generierung des Wortfeldes: Alle Felder auf "Leer" setzen
|
||||||
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
salad[i][j] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int w = 0; w < wordCount; w++)
|
||||||
|
{
|
||||||
|
wordlength = strlen(words[w]);
|
||||||
|
possible_coordinate = 0;
|
||||||
|
|
||||||
|
|
||||||
|
// Versuch bis zu MAX_RAND_TRIES_PER_WORD eine Position zu finden
|
||||||
|
for (int c = 0; c < MAX_RAND_TRIES_PER_WORD && possible_coordinate == 0; c++)
|
||||||
|
{
|
||||||
|
possible_coordinate = 1;
|
||||||
|
|
||||||
|
direction = rand() % 2;
|
||||||
|
|
||||||
|
if (direction == 0) // Vertikale Wortpositionierung
|
||||||
|
{
|
||||||
|
vertical = rand() % (searchFieldLen - wordlength + 1);
|
||||||
|
horizontal = rand() % searchFieldLen;
|
||||||
|
|
||||||
|
for (int i = 0; i < wordlength; i++)
|
||||||
|
{
|
||||||
|
char current = salad[vertical + i][horizontal];
|
||||||
|
|
||||||
|
// Feld muss leer oder mit gleichem Buchstaben belegt sein
|
||||||
|
if (current != '\0' && current != words[w][i])
|
||||||
|
{
|
||||||
|
possible_coordinate = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else // Horizontale Wortpositionierung
|
||||||
|
{
|
||||||
|
vertical = rand() % searchFieldLen;
|
||||||
|
horizontal = rand() % (searchFieldLen - wordlength + 1);
|
||||||
|
|
||||||
|
for (int i = 0; i < wordlength; i++)
|
||||||
|
{
|
||||||
|
char current = salad[vertical][horizontal + i];
|
||||||
|
|
||||||
|
if (current != '\0' && current != words[w][i])
|
||||||
|
{
|
||||||
|
possible_coordinate = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (possible_coordinate == 0)
|
||||||
|
return place_word; // Rückgabe: Anzahl der positionierten Wörter
|
||||||
|
|
||||||
|
// Platzierung des Wortes
|
||||||
|
if (direction == 0) // Vertikal Wortpositionierung
|
||||||
|
{
|
||||||
|
for (int i = 0; i < wordlength; i++)
|
||||||
|
{
|
||||||
|
salad[vertical + i][horizontal] = words[w][i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else // Horizontal Wortpositionierung
|
||||||
|
{
|
||||||
|
for (int i = 0; i < wordlength; i++)
|
||||||
|
{
|
||||||
|
salad[vertical][horizontal + i] = words[w][i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
place_word = w + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auffüllen der leeren Felder
|
||||||
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
if (salad[i][j] == '\0')
|
||||||
|
{
|
||||||
|
salad[i][j] = 'A' + rand() % 26;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return place_word;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
printf("---Wortsalat---\n");
|
||||||
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
printf("%c", salad[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,54 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
// 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
|
// Read words from file and store in 'words' array
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
|
{
|
||||||
|
if (file == NULL)
|
||||||
|
{
|
||||||
|
printf("Error! File cannot be opened!\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int count = 0;
|
||||||
|
char line[MAX_LINE_LEN];
|
||||||
|
const char *delimiter = " ,;.\n";
|
||||||
|
|
||||||
|
while (count < maxWordCount && fgets(line, MAX_LINE_LEN, file) != NULL)
|
||||||
|
{
|
||||||
|
char *token = strtok(line, delimiter); //Teilt die Zeile nach den Trennzeichen auf
|
||||||
|
|
||||||
|
while (token != NULL && count < maxWordCount)
|
||||||
|
{
|
||||||
|
strncpy(words[count], token, MAX_WORD_LEN - 1); //Kopieren des Wortes in dasd Array words
|
||||||
|
words[count][MAX_WORD_LEN - 1] = '\0'; //Händisches Einfügen des Null-Termminators, da dieser nicht automatisch eingefügt wird
|
||||||
|
|
||||||
|
toUpperCase(words[count]); //Umwandeln der Kleinbuchstaben in Großbuchstaben
|
||||||
|
|
||||||
|
count++;
|
||||||
|
|
||||||
|
token = strtok(NULL, delimiter);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
printf("Total words read: %d\n", count);
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
void toUpperCase(char *str)
|
||||||
|
{
|
||||||
|
for (int i = 0; str[i] != '\0'; i++)
|
||||||
|
{
|
||||||
|
str[i] = toupper(str[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@ -7,5 +7,6 @@
|
|||||||
#define MAX_LINE_LEN 1024
|
#define MAX_LINE_LEN 1024
|
||||||
|
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount);
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount);
|
||||||
|
void toUpperCase(char *str);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -36,12 +36,22 @@ 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);
|
||||||
|
|
||||||
// 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
|
||||||
// error message if some words couldn't be placed
|
// error message if some words couldn't be placed
|
||||||
|
|
||||||
|
if (placedWords == wordCount)
|
||||||
|
{
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, placedWords, 800);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("The game can not start! %d words can not be placed!", wordCount - placedWords);
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Print error message if file couldn't be opened
|
// Print error message if file couldn't be opened
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user