# Thread: Single put routine to avoid overlapping words during iteration

1. ## Single put routine to avoid overlapping words during iteration

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

Code:
``` //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]);
}
}
}```

2. If you really want to check if you have enough space you need to perform an initial iteration to verify that all characters that you want to overwrite are still asterisks.
What is the reason for having only a single put() function? do you want to write into whatever direction the word fits?

3. Originally Posted by aGerman
If you really want to check if you have enough space you need to perform an initial iteration to verify that all characters that you want to overwrite are still asterisks.
What is the reason for having only a single put() function? do you want to write into whatever direction the word fits?
Yes i would like to insert a word in to any direction it fits and I thought that having only one put() function would solve it, but i am not sure anymore as I am new to C

4. Here's something to study.
Code:
```#include<stdio.h>
#include<stdlib.h>

#define GRID_W  10
#define GRID_H  10

int isValidPos(int row, int col) {
return row >= 0 && row < GRID_H &&
col >= 0 && col < GRID_W;
}

int allowed(char grid[GRID_H][GRID_W], int row, int col, int deltar, int deltac, const char *word) {
int result = 1;
while ( result && *word ) {
result = isValidPos(row, col);              // it's in the grid
result = result && (grid[row][col] == ' ' || grid[row][col] == *word);   // the cell is free, or matches
if ( result ) {
row += deltar;  // next pos in the chosen dir
col += deltac;
word++;         // next char
}
}
return result;
}

void addWord(char grid[GRID_H][GRID_W], int row, int col, int deltar, int deltac, const char *word) {
if ( allowed(grid, row, col, deltar, deltac, word) ) {
while ( *word ) {
grid[row][col] = *word;
row += deltar;  // next pos in the chosen dir
col += deltac;
word++;         // next char
}
}
}

void initGrid(char grid[GRID_H][GRID_W]) {
for ( int r = 0 ; r < GRID_H ; r++ ) {
for ( int c = 0 ; c < GRID_W ; c++ ) {
grid[r][c] = ' ';
}
}
}

void printGrid(const char grid[GRID_H][GRID_W]) {
for ( int r = 0 ; r < GRID_H ; r++ ) {
for ( int c = 0 ; c < GRID_W ; c++ ) {
putchar(grid[r][c]);
}
putchar('\n');
}
}

int main ()
{
char grid[GRID_H][GRID_W];
initGrid(grid);
addWord(grid, 0, 0, 1, 1, "hello"); // diagonal hello
addWord(grid, 4, 3, 0, 1, "world"); // horizontal world through end of hello
addWord(grid, 9, 9, -1, -1, "bye");
printGrid(grid);
return 0;
}

\$ gcc foo.c
\$ ./a.out
h
e
l
l
world

e
y
b```

5. <Salem>
thank you for your (supposedly) elegant hint but I am not sure how or where to implement it.
I think for now I will leave my code as it is; it's a little buggier, meaning that every 3-4 runs the words break but, at least it works.
When i tried you add your code to the rest, it completely broke the program.
Obviously it's not personal, it's just me; I simply don't know how.
Thanks again