I am trying to have a simple loop for random words that may be placed horizontally and/or vertically and/or diagonally depending on the allowed space and wordLength. Below, is what I came up with but there is significant overlapping and broken words. Is there a way to have the loop in a single 'put' routine for all direction?
All my attempts have resulted in buggier outcomes. Thanks
All my attempts have resulted in buggier outcomes. Thanks
C:
//Check horizontal orientation and place the word if available spaces allow it
void putHorizzontalWord(char word[10])
{
int rRow, rCol , ok , i;
do
{
rRow = rand() % 10;
rCol = rand() % 10;
ok = 1;
if(rCol + strlen(word) < 10)
{
for(i = 0;i < strlen(word);i++)
{
if(puzzle[rRow][rCol + i] == '*' ||
puzzle[rRow][rCol + i] == word[i])
{
puzzle[rRow][rCol + i] = word[i];
}
else
{
ok = 0;
}
}
}
else
{
ok = 0;
}
}
while(ok == 0);
}
//Check vertical orientation and place the word if available spaces allow it
void putVerticalWord(char word[10]) //this, doesn't seem to work'
{
int rRow, rCol , ok , i;
do
{
rRow = rand() % 10;
rCol = rand() % 10;
ok = 0;
if(rRow + strlen(word) < 10)
{
for(i = 0;i < strlen(word);i++)
{
if(puzzle[rRow + i][rCol] == '*' ||
puzzle[rRow + i][rCol] == word[i])
{
puzzle[rRow + i][rCol] = word[i];
}
else
{
ok = 1;
}
}
}
else
{
ok = 1;
}
}
while(ok == 1);
}
//Check diagonal orientation and place the word if available spaces allow it
void putDiagonalWord(char word[10]) //this, doesn't seem to work'
{
int rRow, rCol , ok , i;
do
{
rRow = rand() % 10;
rCol = rand() % 10;
ok = 2;
if(rRow + strlen(word) < 10)
{
for(i = 0;i < strlen(word);i++)
{
if(puzzle[rRow + i][rCol + i] == '*' ||
puzzle[rRow + i][rCol + i] == word[i])
{
puzzle[rRow + i][rCol + i] = word[i];
}
else
{
ok = 2;
}
}
}
else
{
ok = 1;
}
}
while(ok == 0);
}
void fillPuzzleWithWords()
{
int i , orientation;
getFourRandomWords();
for(i=0; i<4; i++)
{
orientation = rand() % 3;
if(orientation == 0)
{
putHorizzontalWord(fourWords[i]);
}
else if(orientation == 1)
{
putVerticalWord(fourWords[i]);
}
else
{
putDiagonalWord(fourWords[i]);
}
}
}