Template
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
40932dfabc | ||
|
|
b0951bcb45 | ||
|
|
6c72054f42 | ||
|
|
e121605cbf | ||
|
|
39bec1ede5 | ||
|
|
6eb6f6acc3 | ||
|
|
4f09d24c90 | ||
|
|
ea276d358e | ||
|
|
afdd93f035 | ||
|
|
42a1ab8168 | ||
|
|
71071cbbc8 | ||
|
|
3c240b1bb2 | ||
|
|
4a1488da75 |
No files matched your search
+113
-1
@@ -3,7 +3,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.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:
|
//TODO: Spiellogik implementieren:
|
||||||
@@ -11,7 +11,119 @@
|
|||||||
* 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)
|
||||||
{
|
{
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
int startpoint_x, startpoint_y, direction, possible, tries, wordsplaced = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < searchFieldLen; i++){
|
||||||
|
for(int j = 0; j < searchFieldLen; j++){
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < wordCount; i++){
|
||||||
|
if(strlen(words[i]) <= searchFieldLen){
|
||||||
|
possible = 0;
|
||||||
|
tries = 0;
|
||||||
|
while(possible == 0 && tries < MAX_RAND_TRIES_PER_WORD){
|
||||||
|
direction = rand() % 4;
|
||||||
|
possible = 1;
|
||||||
|
switch(direction){
|
||||||
|
case 0: //X-Positiv
|
||||||
|
startpoint_x = rand() % (searchFieldLen - strlen(words[i]) + 1);
|
||||||
|
startpoint_y = rand() % searchFieldLen;
|
||||||
|
for(int j = 0; j < strlen(words[i]); j++){
|
||||||
|
if(salad[startpoint_x + j][startpoint_y] != EMPTY_CHAR){
|
||||||
|
possible = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(possible == 0){
|
||||||
|
tries += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int j = 0; j < strlen(words[i]); j++){
|
||||||
|
salad[startpoint_x + j][startpoint_y] = words[i][j];
|
||||||
|
}
|
||||||
|
wordsplaced += 1;
|
||||||
|
break;
|
||||||
|
case 1: //X-Negativ
|
||||||
|
startpoint_x = strlen(words[i]) - 1 + rand() % (searchFieldLen - strlen(words[i]) + 1);
|
||||||
|
startpoint_y = rand() % searchFieldLen;
|
||||||
|
for(int j = 0; j < strlen(words[i]); j++){
|
||||||
|
if(salad[startpoint_x - j][startpoint_y] != EMPTY_CHAR){
|
||||||
|
possible = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(possible == 0){
|
||||||
|
tries += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int j = 0; j < strlen(words[i]); j++){
|
||||||
|
salad[startpoint_x - j][startpoint_y] = words[i][j];
|
||||||
|
}
|
||||||
|
wordsplaced += 1;
|
||||||
|
break;
|
||||||
|
case 2: //Y-Positiv
|
||||||
|
startpoint_x = rand() % searchFieldLen;
|
||||||
|
startpoint_y = rand() % (searchFieldLen - strlen(words[i]) + 1);
|
||||||
|
for(int j = 0; j < strlen(words[i]); j++){
|
||||||
|
if(salad[startpoint_x][startpoint_y + j] != EMPTY_CHAR){
|
||||||
|
possible = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(possible == 0){
|
||||||
|
tries += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int j = 0; j < strlen(words[i]); j++){
|
||||||
|
salad[startpoint_x ][startpoint_y + j] = words[i][j];
|
||||||
|
}
|
||||||
|
wordsplaced += 1;
|
||||||
|
break;
|
||||||
|
case 3: //Y-Negativ
|
||||||
|
startpoint_x = rand() % searchFieldLen;
|
||||||
|
startpoint_y = strlen(words[i]) - 1 + rand() % (searchFieldLen - strlen(words[i]) + 1);
|
||||||
|
for(int j = 0; j < strlen(words[i]); j++){
|
||||||
|
if(salad[startpoint_x][startpoint_y - j] != EMPTY_CHAR){
|
||||||
|
possible = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(possible == 0){
|
||||||
|
tries += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int j = 0; j < strlen(words[i]); j++){
|
||||||
|
salad[startpoint_x][startpoint_y - j] = words[i][j];
|
||||||
|
}
|
||||||
|
wordsplaced += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < searchFieldLen; i++){
|
||||||
|
for(int j = 0; j < searchFieldLen; j++){
|
||||||
|
if(salad[i][j] == EMPTY_CHAR){
|
||||||
|
salad[i][j] = rand() % 26 + 'A';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return wordsplaced;
|
||||||
}
|
}
|
||||||
+33
-3
@@ -2,11 +2,41 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
int wordCount = 0;
|
||||||
|
int charCount = 0;
|
||||||
|
int c;
|
||||||
|
|
||||||
|
while ((c = fgetc(file)) != EOF) {
|
||||||
|
|
||||||
|
if (c == ',' || c == ';' || c == '\n' || c == '\r') {
|
||||||
|
|
||||||
|
if (charCount > 0) {
|
||||||
|
words[wordCount][charCount] = '\0';
|
||||||
|
wordCount++;
|
||||||
|
charCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wordCount >= maxWordCount) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c == ' ' || c == '\t') {
|
||||||
|
continue; // Leerzeichen und Tabs überspringen
|
||||||
|
}
|
||||||
|
else if (charCount < MAX_WORD_LEN - 1) {
|
||||||
|
words[wordCount][charCount++] = (char)toupper((unsigned char)c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (charCount > 0 && wordCount < maxWordCount) {
|
||||||
|
words[wordCount][charCount] = '\0';
|
||||||
|
wordCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
return wordCount;
|
||||||
}
|
}
|
||||||
@@ -41,6 +41,14 @@ int main(int argc, char *argv[])
|
|||||||
// 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){
|
||||||
|
//start game
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, placedWords, 1000);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
printf("Error! Words coundn't be placed...\n");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ wordsalad_initial:
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Normales Spiel bauen
|
# Normales Spiel bauen
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
wordsalad_myversion: main.o input.o game.o graphicalGame.o
|
||||||
|
$(CC) $(CFLAGS) -o wordsalad_myversion.exe main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
wordsalad: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
|
wordsalad: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
|
||||||
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user