Template
Compare commits
25
Commits
main
...
4ba1251b56
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4ba1251b56 | ||
|
|
cbf806ae1b | ||
|
|
90342761fc | ||
|
|
7319049bea | ||
|
|
96917cc4e9 | ||
|
|
69f9945fe0 | ||
|
|
0aa5c5e9e3 | ||
|
|
ed4a9c7520 | ||
|
|
26bc089865 | ||
|
|
90827c9ad6 | ||
|
|
0ee04e2b9e | ||
|
|
c95c264a3a | ||
|
|
147fc502eb | ||
|
|
27eafd14cc | ||
|
|
4041e9ee9b | ||
|
|
11871e8c65 | ||
|
|
6244fad3d4 | ||
|
|
67a8247535 | ||
|
|
5fbc71cf03 | ||
|
|
c97f4bd97d | ||
|
|
1098b0f8c0 | ||
|
|
dc9b59395b | ||
|
|
d7c0c30de9 | ||
|
|
dcb1e44c98 | ||
|
|
970ce3049a |
+13
@@ -0,0 +1,13 @@
|
|||||||
|
wordsalad_initial
|
||||||
|
game.o
|
||||||
|
input.o
|
||||||
|
runTests
|
||||||
|
main.o
|
||||||
|
graphicalGame.o
|
||||||
|
wordsalad
|
||||||
|
wordsalad_initial.exe
|
||||||
|
runTests.exe
|
||||||
|
testwords_delims.txt
|
||||||
|
testwords_empty.txt
|
||||||
|
testwords_simple.txt
|
||||||
|
.vscode
|
||||||
@@ -13,11 +13,87 @@
|
|||||||
// 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 row, col, placedWords=0;
|
||||||
|
|
||||||
|
//Gesamtes Feld auf 0 setzen
|
||||||
|
for (row=0; row < searchFieldLen; row++) {
|
||||||
|
for (col=0; col < searchFieldLen; col++) {
|
||||||
|
salad [row][col] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wortlänge wird für jedes Wort erfasst
|
||||||
|
for (int w=0; w< wordCount; w++) {
|
||||||
|
const char *word = words[w];
|
||||||
|
int wordLen = strlen(word);
|
||||||
|
if (wordLen == 0 || wordLen > searchFieldLen) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int placed = 0;
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
row = 0;
|
||||||
|
col = 0;
|
||||||
|
|
||||||
|
if (dir == 0) { //horizontal //Startpunkt so festlegen, dass Wort nicht aus Feld raus ragt
|
||||||
|
row = rand () % searchFieldLen;
|
||||||
|
col = rand () % (searchFieldLen - wordLen + 1);
|
||||||
|
} else { //vertikal
|
||||||
|
row = rand () % (searchFieldLen - wordLen + 1);
|
||||||
|
col = rand () % searchFieldLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
int placeable = 1;
|
||||||
|
for (int i = 0; i < wordLen; i++) { //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
|
||||||
|
if ((dir == 0 && salad[row][col + i] != EMPTY_CHAR) || (dir == 1 && salad[row + i][col] != EMPTY_CHAR)) {
|
||||||
|
placeable = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (placeable) { //wenn es keine Überschneidungen gibt, wird Wort platziert
|
||||||
|
for (int i=0; i<wordLen; i++) {
|
||||||
|
if (dir == 0) {
|
||||||
|
salad [row][col+i] = word[i];
|
||||||
|
} else {
|
||||||
|
salad [row+i][col] = word[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
placedWords++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Felder, in denen keine Buchstaben stehen, werden mit zufälligen Buchstaben befüllt
|
||||||
|
for (row=0; row < searchFieldLen; row++) {
|
||||||
|
for (col=0; col < searchFieldLen; col++) {
|
||||||
|
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 placedWords;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
int row, col;
|
||||||
|
|
||||||
|
//Ausgabe des gesamten Feldes
|
||||||
|
for (row=0; row < searchFieldLen; row++) {
|
||||||
|
for (col=0; col < searchFieldLen; col++) {
|
||||||
|
printf("%c ", salad[row][col]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
@@ -6,7 +7,39 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
|
char buffer[MAX_WORD_LEN];
|
||||||
|
unsigned int count = 0;
|
||||||
|
|
||||||
|
if (file == NULL)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (fgets(buffer, sizeof(buffer), file) != NULL)
|
||||||
|
{
|
||||||
|
// Zerlege Zeile in einzelne Wörter anhand der Trennzeichen
|
||||||
|
char *token = strtok(buffer, " ,;\n");
|
||||||
|
while (token != NULL && count < maxWordCount)
|
||||||
|
{
|
||||||
|
// In Großbuchstaben umwandeln
|
||||||
|
for (int i = 0; token[i] != '\0'; i++)
|
||||||
|
{
|
||||||
|
token[i] = toupper((unsigned char)token[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Token in das words-Array kopieren
|
||||||
|
strncpy(words[count], token, MAX_WORD_LEN - 1);
|
||||||
|
words[count][MAX_WORD_LEN - 1] = '\0'; // sicher terminieren
|
||||||
|
|
||||||
|
count++;
|
||||||
|
|
||||||
|
// Nächsten Token holen
|
||||||
|
token = strtok(NULL, " ,;\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
+11
-2
@@ -36,11 +36,20 @@ 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
|
|
||||||
// error message if some words couldn't be placed
|
// error message if some words couldn't be placed
|
||||||
|
if (placedWords < wordCount)
|
||||||
|
{
|
||||||
|
int notPlacedNum = wordCount - placedWords;
|
||||||
|
fprintf(stderr, "%d word(s) couldn't be placed\n", notPlacedNum);
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start the game if all words were successfully placed
|
||||||
|
else
|
||||||
|
{
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,11 +13,87 @@
|
|||||||
// 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 row, col, placedWords=0;
|
||||||
|
|
||||||
|
//Gesamtes Feld auf 0 setzen
|
||||||
|
for (row=0; row < searchFieldLen; row++) {
|
||||||
|
for (col=0; col < searchFieldLen; col++) {
|
||||||
|
salad [row][col] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wortlänge wird für jedes Wort erfasst
|
||||||
|
for (int w=0; w< wordCount; w++) {
|
||||||
|
const char *word = words[w];
|
||||||
|
int wordLen = strlen(word);
|
||||||
|
if (wordLen == 0 || wordLen > searchFieldLen) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int placed = 0;
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
row = 0;
|
||||||
|
col = 0;
|
||||||
|
|
||||||
|
if (dir == 0) { //horizontal //Startpunkt so festlegen, dass Wort nicht aus Feld raus ragt
|
||||||
|
row = rand () % searchFieldLen;
|
||||||
|
col = rand () % (searchFieldLen - wordLen + 1);
|
||||||
|
} else { //vertikal
|
||||||
|
row = rand () % (searchFieldLen - wordLen + 1);
|
||||||
|
col = rand () % searchFieldLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
int placeable = 1;
|
||||||
|
for (int i = 0; i < wordLen; i++) { //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
|
||||||
|
if ((dir == 0 && salad[row][col + i] != EMPTY_CHAR) || (dir == 1 && salad[row + i][col] != EMPTY_CHAR)) {
|
||||||
|
placeable = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (placeable) { //wenn es keine Überschneidungen gibt, wird Wort platziert
|
||||||
|
for (int i=0; i<wordLen; i++) {
|
||||||
|
if (dir == 0) {
|
||||||
|
salad [row][col+i] = word[i];
|
||||||
|
} else {
|
||||||
|
salad [row+i][col] = word[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
placedWords++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Felder, in denen keine Buchstaben stehen, werden mit zufälligen Buchstaben befüllt
|
||||||
|
for (row=0; row < searchFieldLen; row++) {
|
||||||
|
for (col=0; col < searchFieldLen; col++) {
|
||||||
|
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 placedWords;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
int row, col;
|
||||||
|
|
||||||
|
//Ausgabe des gesamten Feldes
|
||||||
|
for (row=0; row < searchFieldLen; row++) {
|
||||||
|
for (col=0; col < searchFieldLen; col++) {
|
||||||
|
printf("%c ", salad[row][col]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
@@ -6,7 +7,39 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
|
char buffer[MAX_WORD_LEN];
|
||||||
|
unsigned int count = 0;
|
||||||
|
|
||||||
|
if (file == NULL)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (fgets(buffer, sizeof(buffer), file) != NULL)
|
||||||
|
{
|
||||||
|
// Zerlege Zeile in einzelne Wörter anhand der Trennzeichen
|
||||||
|
char *token = strtok(buffer, " ,;\n");
|
||||||
|
while (token != NULL && count < maxWordCount)
|
||||||
|
{
|
||||||
|
// In Großbuchstaben umwandeln
|
||||||
|
for (int i = 0; token[i] != '\0'; i++)
|
||||||
|
{
|
||||||
|
token[i] = toupper((unsigned char)token[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Token in das words-Array kopieren
|
||||||
|
strncpy(words[count], token, MAX_WORD_LEN - 1);
|
||||||
|
words[count][MAX_WORD_LEN - 1] = '\0'; // sicher terminieren
|
||||||
|
|
||||||
|
count++;
|
||||||
|
|
||||||
|
// Nächsten Token holen
|
||||||
|
token = strtok(NULL, " ,;\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
+13
-2
@@ -36,11 +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
|
|
||||||
// error message if some words couldn't be placed
|
// error message if some words couldn't be placed
|
||||||
|
if (placedWords < wordCount)
|
||||||
|
{
|
||||||
|
int notPlacedNum = wordCount - placedWords;
|
||||||
|
fprintf(stderr, "%d word(s) couldn't be placed\n", notPlacedNum);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// correct parameters for startGame
|
||||||
|
|
||||||
|
// Start the game if at least 1 word has been successfully placed
|
||||||
|
else if (placedWords >= 1)
|
||||||
|
{
|
||||||
|
startGame(wordSalad, 80, words, placedWords, 800);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user