Compare commits

..

No commits in common. "e3e5d4d43422a10b8f8bf4df31dec407b6eecfa5" and "b4f891d3310f0109e7dc4e462e4363325885ee98" have entirely different histories.

7 changed files with 100 additions and 150 deletions

8
.gitignore vendored
View File

@ -1,8 +1,12 @@
wordsalad_initial wordsalad_initial
game.o
input.o
runTests runTests
main.o
graphicalGame.o
wordsalad wordsalad
*.o wordsalad_initial.exe
*.exe runTests.exe
testwords_delims.txt testwords_delims.txt
testwords_empty.txt testwords_empty.txt
testwords_simple.txt testwords_simple.txt

View File

@ -6,101 +6,81 @@
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
// 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 */
// 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, 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 row, col, placedWords = 0; int row, col, placedWords=0;
// Gesamtes Feld auf 0 setzen //Gesamtes Feld auf 0 setzen
for (row = 0; row < searchFieldLen; row++) for (row=0; row < searchFieldLen; row++) {
{ for (col=0; col < searchFieldLen; col++) {
for (col = 0; col < searchFieldLen; col++) salad [row][col] = EMPTY_CHAR;
{
salad[row][col] = EMPTY_CHAR;
} }
} }
// Wortlänge wird für jedes Wort erfasst //Wortlänge wird für jedes Wort erfasst
for (int w = 0; w < wordCount; w++) for (int w=0; w< wordCount; w++) {
{
const char *word = words[w]; const char *word = words[w];
int wordLen = strlen(word); int wordLen = strlen(word);
if (wordLen == 0 || wordLen > searchFieldLen) if (wordLen == 0 || wordLen > searchFieldLen) {
{
continue; continue;
} }
int placed = 0; int placed = 0;
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) {
{
int dir = (rand() % 2); // 0 oder 1 zur Festlegung von vertikal oder horizontal int dir = (rand() % 2); // 0 oder 1 zur Festlegung von vertikal oder horizontal
row = 0; row = 0;
col = 0; col = 0;
if (dir == 0) if (dir == 0) { //horizontal //Startpunkt so festlegen, dass Wort nicht aus Feld raus ragt
{ // horizontal //Startpunkt so festlegen, dass Wort nicht aus Feld raus ragt row = rand () % searchFieldLen;
row = rand() % searchFieldLen; col = rand () % (searchFieldLen - wordLen + 1);
col = rand() % (searchFieldLen - wordLen + 1); } else { //vertikal
} row = rand () % (searchFieldLen - wordLen + 1);
else col = rand () % searchFieldLen;
{ // vertikal
row = rand() % (searchFieldLen - wordLen + 1);
col = rand() % searchFieldLen;
} }
int placeable = 1; int placeable = 1;
for (int i = 0; i < wordLen; i++) for (int i = 0; i < wordLen; i++) { //Felder ab Startpunkt werden durchgegangen und gecheckt, ob Felder auf Null sind:
{ // Felder ab Startpunkt werden durchgegangen und gecheckt, ob Felder auf Null sind:
// JA =>Wort wird im nächsten Schritt platziert, NEIN => Abbruch, Neuer Startpunkt wird festgelegt // JA =>Wort wird im nächsten Schritt platziert, NEIN => Abbruch, Neuer Startpunkt wird festgelegt
if ((dir == 0 && salad[row][col + i] != EMPTY_CHAR) || (dir == 1 && salad[row + i][col] != EMPTY_CHAR)) if ((dir == 0 && salad[row][col + i] != EMPTY_CHAR) || (dir == 1 && salad[row + i][col] != EMPTY_CHAR)) {
{
placeable = 0; placeable = 0;
break; break;
} }
} }
if (placeable) if (placeable) { //wenn es keine Überschneidungen gibt, wird Wort platziert
{ // wenn es keine Überschneidungen gibt, wird Wort platziert for (int i=0; i<wordLen; i++) {
for (int i = 0; i < wordLen; i++) if (dir == 0) {
{ salad [row][col+i] = word[i];
if (dir == 0) } else {
{ salad [row+i][col] = word[i];
salad[row][col + i] = word[i];
}
else
{
salad[row + i][col] = word[i];
} }
} }
// copy actually placed words to the front of the array
// it's safe to use strcpy here
strcpy(words[placedWords], word);
placed = 1; placed = 1;
placedWords++; placedWords++;
} }
} }
} }
// Felder, in denen keine Buchstaben stehen, werden mit zufälligen Buchstaben befüllt //Felder, in denen keine Buchstaben stehen, werden mit zufälligen Buchstaben befüllt
for (row = 0; row < searchFieldLen; row++) for (row=0; row < searchFieldLen; row++) {
{ for (col=0; col < searchFieldLen; col++) {
for (col = 0; col < searchFieldLen; col++) if (salad[row][col] == EMPTY_CHAR) {
{ salad [row][col] = 'A' + (rand()% 26);
if (salad[row][col] == EMPTY_CHAR)
{
salad[row][col] = 'A' + (rand() % 26);
} }
} }
} }
// return "Anzahl platzierter Wörter", wird in Main dann mit "soll" verglichen //return "Anzahl platzierter Wörter", wird in Main dann mit "soll" verglichen
return placedWords; return placedWords;
} }
@ -109,11 +89,9 @@ void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
{ {
int row, col; int row, col;
// Ausgabe des gesamten Feldes //Ausgabe des gesamten Feldes
for (row = 0; row < searchFieldLen; row++) for (row=0; row < searchFieldLen; row++) {
{ for (col=0; col < searchFieldLen; col++) {
for (col = 0; col < searchFieldLen; col++)
{
printf("%c ", salad[row][col]); printf("%c ", salad[row][col]);
} }
printf("\n"); printf("\n");

View File

@ -5,7 +5,7 @@
#define MAX_SEARCH_FIELD_LEN 100 #define MAX_SEARCH_FIELD_LEN 100
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, 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);
#endif #endif

View File

@ -6,7 +6,6 @@
#define MAX_NUMBER_OF_WORDS 100 #define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20 #define SALAD_SIZE 20
#define WINDOW_WIDTH 800
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -43,17 +42,13 @@ int main(int argc, char *argv[])
{ {
int notPlacedNum = wordCount - placedWords; int notPlacedNum = wordCount - placedWords;
fprintf(stderr, "%d word(s) couldn't be placed\n", notPlacedNum); fprintf(stderr, "%d word(s) couldn't be placed\n", notPlacedNum);
exitCode = EXIT_FAILURE;
} }
// Start game if at least 1 word was successfully placed // Start the game if all words were successfully placed
if (placedWords > 0)
{
startGame(wordSalad, SALAD_SIZE, words, placedWords, WINDOW_WIDTH);
}
else else
{ {
fprintf(stderr, "no word could be placed"); startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
exitCode = EXIT_FAILURE;
} }
} }
else else

View File

@ -6,101 +6,81 @@
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
// 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 */
// 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, 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 row, col, placedWords = 0; int row, col, placedWords=0;
// Gesamtes Feld auf 0 setzen //Gesamtes Feld auf 0 setzen
for (row = 0; row < searchFieldLen; row++) for (row=0; row < searchFieldLen; row++) {
{ for (col=0; col < searchFieldLen; col++) {
for (col = 0; col < searchFieldLen; col++) salad [row][col] = EMPTY_CHAR;
{
salad[row][col] = EMPTY_CHAR;
} }
} }
// Wortlänge wird für jedes Wort erfasst //Wortlänge wird für jedes Wort erfasst
for (int w = 0; w < wordCount; w++) for (int w=0; w< wordCount; w++) {
{
const char *word = words[w]; const char *word = words[w];
int wordLen = strlen(word); int wordLen = strlen(word);
if (wordLen == 0 || wordLen > searchFieldLen) if (wordLen == 0 || wordLen > searchFieldLen) {
{
continue; continue;
} }
int placed = 0; int placed = 0;
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) {
{
int dir = (rand() % 2); // 0 oder 1 zur Festlegung von vertikal oder horizontal int dir = (rand() % 2); // 0 oder 1 zur Festlegung von vertikal oder horizontal
row = 0; row = 0;
col = 0; col = 0;
if (dir == 0) if (dir == 0) { //horizontal //Startpunkt so festlegen, dass Wort nicht aus Feld raus ragt
{ // horizontal //Startpunkt so festlegen, dass Wort nicht aus Feld raus ragt row = rand () % searchFieldLen;
row = rand() % searchFieldLen; col = rand () % (searchFieldLen - wordLen + 1);
col = rand() % (searchFieldLen - wordLen + 1); } else { //vertikal
} row = rand () % (searchFieldLen - wordLen + 1);
else col = rand () % searchFieldLen;
{ // vertikal
row = rand() % (searchFieldLen - wordLen + 1);
col = rand() % searchFieldLen;
} }
int placeable = 1; int placeable = 1;
for (int i = 0; i < wordLen; i++) for (int i = 0; i < wordLen; i++) { //Felder ab Startpunkt werden durchgegangen und gecheckt, ob Felder auf Null sind:
{ // Felder ab Startpunkt werden durchgegangen und gecheckt, ob Felder auf Null sind:
// JA =>Wort wird im nächsten Schritt platziert, NEIN => Abbruch, Neuer Startpunkt wird festgelegt // JA =>Wort wird im nächsten Schritt platziert, NEIN => Abbruch, Neuer Startpunkt wird festgelegt
if ((dir == 0 && salad[row][col + i] != EMPTY_CHAR) || (dir == 1 && salad[row + i][col] != EMPTY_CHAR)) if ((dir == 0 && salad[row][col + i] != EMPTY_CHAR) || (dir == 1 && salad[row + i][col] != EMPTY_CHAR)) {
{
placeable = 0; placeable = 0;
break; break;
} }
} }
if (placeable) if (placeable) { //wenn es keine Überschneidungen gibt, wird Wort platziert
{ // wenn es keine Überschneidungen gibt, wird Wort platziert for (int i=0; i<wordLen; i++) {
for (int i = 0; i < wordLen; i++) if (dir == 0) {
{ salad [row][col+i] = word[i];
if (dir == 0) } else {
{ salad [row+i][col] = word[i];
salad[row][col + i] = word[i];
}
else
{
salad[row + i][col] = word[i];
} }
} }
// copy actually placed words to the front of the array
// it's safe to use strcpy here
strcpy(words[placedWords], word);
placed = 1; placed = 1;
placedWords++; placedWords++;
} }
} }
} }
// Felder, in denen keine Buchstaben stehen, werden mit zufälligen Buchstaben befüllt //Felder, in denen keine Buchstaben stehen, werden mit zufälligen Buchstaben befüllt
for (row = 0; row < searchFieldLen; row++) for (row=0; row < searchFieldLen; row++) {
{ for (col=0; col < searchFieldLen; col++) {
for (col = 0; col < searchFieldLen; col++) if (salad[row][col] == EMPTY_CHAR) {
{ salad [row][col] = 'A' + (rand()% 26);
if (salad[row][col] == EMPTY_CHAR)
{
salad[row][col] = 'A' + (rand() % 26);
} }
} }
} }
// return "Anzahl platzierter Wörter", wird in Main dann mit "soll" verglichen //return "Anzahl platzierter Wörter", wird in Main dann mit "soll" verglichen
return placedWords; return placedWords;
} }
@ -109,11 +89,9 @@ void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
{ {
int row, col; int row, col;
// Ausgabe des gesamten Feldes //Ausgabe des gesamten Feldes
for (row = 0; row < searchFieldLen; row++) for (row=0; row < searchFieldLen; row++) {
{ for (col=0; col < searchFieldLen; col++) {
for (col = 0; col < searchFieldLen; col++)
{
printf("%c ", salad[row][col]); printf("%c ", salad[row][col]);
} }
printf("\n"); printf("\n");

View File

@ -5,7 +5,7 @@
#define MAX_SEARCH_FIELD_LEN 100 #define MAX_SEARCH_FIELD_LEN 100
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, 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);
#endif #endif

View File

@ -6,7 +6,6 @@
#define MAX_NUMBER_OF_WORDS 100 #define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20 #define SALAD_SIZE 20
#define WINDOW_WIDTH 800
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -43,17 +42,13 @@ int main(int argc, char *argv[])
{ {
int notPlacedNum = wordCount - placedWords; int notPlacedNum = wordCount - placedWords;
fprintf(stderr, "%d word(s) couldn't be placed\n", notPlacedNum); fprintf(stderr, "%d word(s) couldn't be placed\n", notPlacedNum);
exitCode = EXIT_FAILURE;
} }
// Start game if at least 1 word was successfully placed // Start the game if all words were successfully placed
if (placedWords > 0)
{
startGame(wordSalad, SALAD_SIZE, words, placedWords, WINDOW_WIDTH);
}
else else
{ {
fprintf(stderr, "no word could be placed"); startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
exitCode = EXIT_FAILURE;
} }
} }
else else