finished main.c -- edited game.c

This commit is contained in:
pvtrx 2025-11-05 13:10:26 +01:00
parent 9d08cf3fee
commit 0c46741f2c
3 changed files with 23 additions and 14 deletions

View File

@ -20,6 +20,8 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
int wordsplaced = 0;
int wordsaborted = 0;
while (wordsplaced < wordCount) {
int direction = rand() % 2; // 0 = horizontal, 1 = vertical
int wordlen = strlen(words[wordsplaced]);
@ -69,9 +71,11 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
}
if (placed) {
wordsplaced++;
} else {
// Could not place the word after max tries
return -1;
}
else if (tries == MAX_RAND_TRIES_PER_WORD) {
printf("Abort trying to place the word -%s-\n", words[wordsplaced]);
wordsplaced++; // skip this word and move to the next
wordsaborted++;
}
}
@ -90,7 +94,7 @@ int main(void) {
int SALAD_SIZE = 10;
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
unsigned int searchFieldLen = SALAD_SIZE;
const char words[][MAX_WORD_LEN] = {"TEST", "WORD", "DEINEMUDDA"}; // Add your words here
const char words[][MAX_WORD_LEN] = {"TEST", "WORD", "DEINEMUDDAAAA"}; // Add your words here
unsigned int wordCount = 3; // Update this based on number of words
int placedWords;
@ -106,7 +110,7 @@ int main(void) {
//code for main.c:
if (placedWords == wordCount) {
printf("All words placed successfully.\n");
printf("All valid words placed successfully.\n");
} else {
printf("Could not place all words. Placed %d out of %d words.\n", placedWords, wordCount);
}

View File

@ -7,7 +7,7 @@
#define EMPTY_CHAR 0
// Creates the word salad by placing words randomly and filling empty spaces
// Fills array with EMPTY_CHAR, tries to place words into the salad and fills empty spaces with random letters. returns number of placed words or -1 if not all words could be placed.
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
{
@ -17,6 +17,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
int column = 0;
int wordsplaced = 0;
int wordsaborted = 0;
while (wordsplaced < wordCount) {
int direction = rand() % 2; // 0 = horizontal, 1 = vertical
@ -67,9 +68,11 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
}
if (placed) {
wordsplaced++;
} else {
// Could not place the word after max tries
return -1;
}
else if (tries == MAX_RAND_TRIES_PER_WORD) {
printf("Abort trying to place the word -%s-\n", words[wordsplaced]);
wordsplaced++; // skip this word and move to the next
wordsaborted++;
}
}

View File

@ -36,10 +36,12 @@ int main(int argc, char *argv[])
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// TODO:
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
// Start game if all valid words were placed
if (placedWords == wordCount) {
startGame(wordSalad, SALAD_SIZE, words, wordCount);
} else {
printf("Could not place all valid words. Placed %d out of %d words.\n", placedWords, wordCount);
}
}
else