generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4821287319 | ||
|
|
634c15ff32 | ||
|
|
85934a0661 |
No files matched your search
@@ -65,58 +65,3 @@ int createWordSalad(
|
|||||||
|
|
||||||
|
|
||||||
// 5. Vertikale Platzierung prüfen
|
// 5. Vertikale Platzierung prüfen
|
||||||
|
|
||||||
else {
|
|
||||||
|
|
||||||
if (row + wordLen > searchFieldLen) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t j = 0; j < wordLen; j++) {
|
|
||||||
|
|
||||||
if (salad[row + j][col] != EMPTY_CHAR &&
|
|
||||||
salad[row + j][col] != words[i][j]) {
|
|
||||||
|
|
||||||
canPlace = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Platzierung nicht möglich
|
|
||||||
if (!canPlace) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 6. Wort tatsächlich platzieren
|
|
||||||
for (size_t j = 0; j < wordLen; j++) {
|
|
||||||
|
|
||||||
if (direction == 0) {
|
|
||||||
salad[row][col + j] = words[i][j];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
salad[row + j][col] = words[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Erfolgreich platziertes Wort zählen
|
|
||||||
placedWords++;
|
|
||||||
|
|
||||||
// Nächstes Wort bearbeiten
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 7. 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 8. Anzahl erfolgreich platzierter Wörter zurückgeben
|
|
||||||
return (int)placedWords;
|
|
||||||
}
|
|
||||||
+35
-30
@@ -4,44 +4,49 @@
|
|||||||
|
|
||||||
// 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.
|
||||||
// Test 123
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
int c = 0;
|
unsigned int wordCount = 0;
|
||||||
int count = 0;
|
unsigned int charIndex = 0;
|
||||||
int letterCount = 0;
|
int c;
|
||||||
while (count < maxWordCount)
|
|
||||||
{
|
if (file == NULL) {
|
||||||
while ((c = fgetc(file)) != EOF) //Leerzeichen prüfen
|
return 0;
|
||||||
{
|
|
||||||
if (isspace(c) || !isalpha(c)) {
|
|
||||||
if (letterCount > 0) {
|
|
||||||
break; //Wort fertig eingelesen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
continue; // mehrere Leerzeichen oder Sonderzeichen überspringen
|
while ((c = fgetc(file)) != EOF) {
|
||||||
}
|
|
||||||
|
|
||||||
if (letterCount < MAX_WORD_LEN - 1) //wörter Buchstabenweise einlesen
|
if (c == ',' || c == ';' || c == ' ' ||
|
||||||
{
|
c == '\n' || c == '\t' || c == '\r') {
|
||||||
words[count][letterCount] = (char)toupper(c);
|
|
||||||
letterCount++;
|
if (charIndex > 0) {
|
||||||
|
words[wordCount][charIndex] = '\0';
|
||||||
|
|
||||||
|
wordCount++;
|
||||||
|
charIndex = 0;
|
||||||
|
|
||||||
|
if (wordCount >= maxWordCount) {
|
||||||
|
return (int)wordCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (wordCount < maxWordCount &&
|
||||||
|
charIndex < MAX_WORD_LEN - 1) {
|
||||||
|
|
||||||
|
words[wordCount][charIndex] = (char)c;
|
||||||
|
charIndex++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (letterCount == 0) {
|
if (charIndex > 0 && wordCount < maxWordCount) {
|
||||||
break; //kein weiteres Wort gefunden
|
words[wordCount][charIndex] = '\0';
|
||||||
|
wordCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
words[count][letterCount] = '\0';
|
return (int)wordCount;
|
||||||
letterCount = 0;
|
|
||||||
count++;
|
|
||||||
|
|
||||||
if (c == EOF) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
+4
-21
@@ -4,16 +4,12 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "graphicalGame.h"
|
#include "graphicalGame.h"
|
||||||
|
|
||||||
#define MAX_NUMBER_OF_WORDS 100 //Maximale Anzahl an Wörtern, die eingelesen werden können
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
#define SALAD_SIZE 20 //Größe des Wortsalats 20 Zeilen * 20 Spalten
|
#define SALAD_SIZE 20
|
||||||
|
|
||||||
int main(int argc, char *argv[]) // Hauptfunktion des Programms
|
|
||||||
// argc = Anzahl der übergebenen Kommandozeilenargumente
|
|
||||||
// argv = enthält die übergebenen Argumente
|
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int exitCode = EXIT_SUCCESS; // Speichert, ob das Programm erfolgreich beendet wird
|
int exitCode = EXIT_SUCCESS;
|
||||||
// EXIT_SUCCESS = kein Fehler
|
|
||||||
|
|
||||||
// Check if the correct number of arguments is provided
|
// Check if the correct number of arguments is provided
|
||||||
if(argc != 2)
|
if(argc != 2)
|
||||||
@@ -40,19 +36,6 @@ int main(int argc, char *argv[]) // Hauptfunktion des Programms
|
|||||||
// 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)
|
|
||||||
{
|
|
||||||
startGame(wordSalad, SALAD_SIZE, words, wordCount,800);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Not all words could be placed.\n");
|
|
||||||
exitCode = EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 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
|
||||||
|
|||||||
Reference in new issue
Block a user