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

View File

@ -35,11 +35,13 @@ 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: // Start game if all valid words were placed
// Check if all words were successfully placed if (placedWords == wordCount) {
// Start the game if successful startGame(wordSalad, SALAD_SIZE, words, wordCount);
// error message if some words couldn't be placed } else {
printf("Could not place all valid words. Placed %d out of %d words.\n", placedWords, wordCount);
}
} }
else else