forked from freudenreichan/info2Praktikum-Wortsalat
Compare commits
18
Commits
a39f7748de
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
216f99ff6a | ||
|
|
dda045fee3 | ||
|
|
cf2e2ae7aa | ||
|
|
9f17181f9e | ||
|
|
add999961f | ||
|
|
07e7ea5d59 | ||
|
|
c2d35dfe89 | ||
|
|
6340b63273 | ||
|
|
f81b98bc1c | ||
|
|
2368d5aa38 | ||
|
|
cbfd7d5071 | ||
|
|
b55d0a22e6 | ||
|
|
9e22ee3299 | ||
|
|
54d2b81fae | ||
|
|
ae1adadfee | ||
|
|
7bd095a2bb | ||
|
|
96082d1781 | ||
|
|
8bf87d00ed |
No files matched your search
+112
-11
@@ -1,17 +1,118 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include <time.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#define MAX_ATTEMPTS 10
|
||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
//TODO: Spiellogik implementieren:
|
typedef enum
|
||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
|
||||||
* 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)
|
|
||||||
{
|
{
|
||||||
//das hier ist ein Test von dem Repository du Lausebube
|
HORIZONTAL,
|
||||||
}
|
VERTICAL
|
||||||
|
} Direction;
|
||||||
|
|
||||||
|
|
||||||
|
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 placedWords = 0;
|
||||||
|
/* Alle Felder initialisieren */
|
||||||
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Richtungsvektoren
|
||||||
|
int dx[] = {1, 0};
|
||||||
|
int dy[] = {0, 1};
|
||||||
|
|
||||||
|
// Alle Woerter durchlaufen
|
||||||
|
for (unsigned int w = 0; w < wordCount; w++)
|
||||||
|
{
|
||||||
|
size_t wordLen = strlen(words[w]);
|
||||||
|
|
||||||
|
//Leere oder zu lange Woerter ueberspringen
|
||||||
|
if (wordLen == 0 || wordLen > searchFieldLen)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int placed = 0;
|
||||||
|
int attempts = 0;
|
||||||
|
|
||||||
|
while (!placed && attempts < MAX_ATTEMPTS)
|
||||||
|
{
|
||||||
|
attempts++;
|
||||||
|
|
||||||
|
/* Zufallsrichtung bestimmen */
|
||||||
|
Direction dir = rand() % 2;
|
||||||
|
|
||||||
|
unsigned int Zeile;
|
||||||
|
unsigned int Spalte;
|
||||||
|
|
||||||
|
/* Zufallsposition so waehlen, dass das Wort hineinpasst */
|
||||||
|
if (dir == HORIZONTAL)
|
||||||
|
{
|
||||||
|
Zeile = rand() % searchFieldLen;
|
||||||
|
Spalte = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Zeile = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
Spalte = rand() % searchFieldLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
int collision = 0;
|
||||||
|
|
||||||
|
// Pruefen, ob die Position geeignet ist (cillision mit anderen Wörtern)
|
||||||
|
for (size_t i = 0; i < wordLen; i++)
|
||||||
|
{
|
||||||
|
unsigned int z = Zeile + dy[dir] * i;
|
||||||
|
unsigned int s = Spalte + dx[dir] * i;
|
||||||
|
|
||||||
|
char letter = words[w][i];
|
||||||
|
|
||||||
|
if (salad[z][s] != EMPTY_CHAR &&
|
||||||
|
salad[z][s] != letter)
|
||||||
|
{
|
||||||
|
collision = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wenn keine collision besteht wort eintragen
|
||||||
|
if (!collision)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < wordLen; i++)
|
||||||
|
{
|
||||||
|
unsigned int z = Zeile + dy[dir] * i;
|
||||||
|
unsigned int s = Spalte + dx[dir] * i;
|
||||||
|
|
||||||
|
salad[z][s] = words[w][i];
|
||||||
|
}
|
||||||
|
|
||||||
|
placed = 1;
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Leere Felder mit zufaelligen Buchstaben fuellen
|
||||||
|
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);// 26 Buchstaben insgesammt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return placedWords;
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
#define MAX_MESSAGE_LEN 256
|
#define MAX_MESSAGE_LEN 256
|
||||||
#define MAX_SOLUTION_WORD_LEN 16
|
#define MAX_SOLUTION_WORD_LEN 16
|
||||||
|
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
Vector2 startPosition;
|
Vector2 startPosition;
|
||||||
|
|||||||
+38
-4
@@ -5,8 +5,42 @@
|
|||||||
// 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'
|
||||||
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) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char line[MAX_LINE_LEN];
|
||||||
|
unsigned int wordCount = 0;
|
||||||
|
|
||||||
|
while (wordCount < maxWordCount &&
|
||||||
|
fgets(line, sizeof(line), file) != NULL) {
|
||||||
|
|
||||||
|
char *word = strtok(line, " ,;\n\t\r");
|
||||||
|
|
||||||
|
while (word != NULL && wordCount < maxWordCount)
|
||||||
|
{
|
||||||
|
strncpy(words[wordCount], word, MAX_WORD_LEN - 1);
|
||||||
|
words[wordCount][MAX_WORD_LEN - 1] = '\0';
|
||||||
|
|
||||||
|
unsigned int i = 0;
|
||||||
|
|
||||||
|
while (words[wordCount][i] != '\0') {
|
||||||
|
words[wordCount][i] =
|
||||||
|
(char)toupper((unsigned char)words[wordCount][i]);// char davor da toupper einen int wert liefert und einen unsigned char bekommt
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
wordCount++;
|
||||||
|
|
||||||
|
word = strtok(NULL, " ,;\n\t\r");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int)wordCount;
|
||||||
|
}
|
||||||
+19
-17
@@ -1,24 +1,26 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
#include <time.h>
|
||||||
#include "graphicalGame.h"
|
#include "graphicalGame.h"
|
||||||
|
|
||||||
#define MAX_NUMBER_OF_WORDS 100
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
#define SALAD_SIZE 20
|
#define SALAD_SIZE 20
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[]) {
|
||||||
{
|
|
||||||
int exitCode = EXIT_SUCCESS;
|
int exitCode = EXIT_SUCCESS;
|
||||||
|
|
||||||
|
srand(time(NULL)); //Seed für Zufallszahlen
|
||||||
|
|
||||||
// Check if the correct number of arguments is provided
|
// Check if the correct number of arguments is provided
|
||||||
if(argc != 2)
|
if(argc != 2)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
|
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
|
||||||
exitCode = EXIT_FAILURE;
|
exitCode = EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
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;
|
||||||
|
|
||||||
@@ -36,19 +38,19 @@ 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:
|
if (placedWords == (int)wordCount)
|
||||||
// Check if all words were successfully placed
|
{
|
||||||
// Start the game if successful
|
// Alle Wörter wurden erfolgreich platziert
|
||||||
// error message if some words couldn't be placed
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Print error message if file couldn't be opened
|
||||||
|
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
return exitCode;
|
||||||
else
|
|
||||||
{
|
|
||||||
// Print error message if file couldn't be opened
|
|
||||||
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
|
||||||
exitCode = EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return exitCode;
|
|
||||||
}
|
}
|
||||||
+1
-1
@@ -2,4 +2,4 @@ Yeti,Nessie Werwolf; Vampir
|
|||||||
Monster
|
Monster
|
||||||
Hydra;Frankenstein
|
Hydra;Frankenstein
|
||||||
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
||||||
Gespenst, Oger
|
Gespenst
|
||||||
Reference in new issue
Block a user