first implementation of game.c

This commit is contained in:
hoangto105860 committed 2026-10-24 20:38:13 +02:00
1 parent 187e626191
commit ab558de8b3
4 files changed
+121

No files matched your search

+113
View File
@@ -2,6 +2,7 @@
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
@@ -13,5 +14,117 @@
// 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)
{
srand(time(NULL));
unsigned int placedWords = 0;
//printf("start game.c\n");
emptyField(searchFieldLen, salad);
for(int i = 0; i < wordCount; i++)
{
placedWords += placement(salad, searchFieldLen, words[i]);
//printf("[%d] of words: %s\n", i, words[i]);
}
//fillRandAlpha(searchFieldLen, salad);
printf("placedWords:%d\n", placedWords);
for(int i = 0; i < searchFieldLen; i++)
{
for(int j = 0; j < searchFieldLen; j++)
{
printf("%c", salad[j][i]);
}
printf("\n");
}
return placedWords;
}
char alphaRand()
{
int letter = (rand() % 26) + 'A';
return (char) letter;
}
int placement(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word)
{
int saladX = rand() % ( searchFieldLen - strlen(word) );
int saladY = rand() % ( searchFieldLen - strlen(word) );
printf("%s\n", word);
int verhor = rand() % 2;
if(verhor == 1)
{
for(int i = 0; i < strlen(word); i++)
{
if(isalpha(salad[saladX][saladY + i]))
{
return 0;
}
}
for(int i = 0; i < strlen(word); i++)
{
salad[saladX][saladY + i] = word[i];
}
}
else
{
for(int i = 0; i < strlen(word); i++)
{
if(isalpha(salad[saladX + i][saladY]))
{
return 0;
}
}
for(int i = 0; i < strlen(word); i++)
{
salad[saladX + i][saladY] = word[i];
}
}
return 1;
}
void emptyField(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN])
{
for(size_t i = 0; i < searchFieldLen; i++)
{
for(size_t j = 0; j < searchFieldLen; j++)
{
//salad[i][j] = '\0';
salad[i][j] = ' ';
printf("%c", salad[i][j]);
}
printf("\n");
}
}
char fillRandAlpha(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN])
{
for(size_t i = 0; i < searchFieldLen; i++)
{
for(size_t j = 0; j < searchFieldLen; j++)
{
if(!isalpha(salad[i][j]))
{
salad[i][j] = alphaRand();
//printf("%c", salad[i][j]);
}
}
printf("\n");
}
return 0;
}
+8
View File
@@ -7,4 +7,12 @@
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 emptyField(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]);
char fillRandAlpha(unsigned int searchFieldLen, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]);
int placement(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word);
char alphaRand();
#endif
BIN
View File
Binary file not shown.
Binary file not shown.