Search code examples
cdiagonalvertical-text

Issue with displaying vertical and diagonal letters - C Programming


I'm totally new to C Programming and I'm trying to create a Word Search .

I've got a list of words , where only 4 are randomly picked. These 4 words than are to be printed in a grid horizontally, vertically or diagonally, however I can only get them to print horizontally. I also must add that I have no idea on how the piece of code works so I really appreciate if someone kind enough can actually help me. So can anybody help me in the right direction to create the random words in a vertical and diagonal alignment ? https://i.sstatic.net/3tk2b.jpg

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);
}

Solution

  • Here are some comments aimed to explain you what does that code does:

    // This function takes a string as input then puts that string
    // at a random "open" location in a 2D grid (puzzle) in an
    // horizontal manner
    //
    // This function expects the size of the string to be at most = 10
    
    void putHorizontalWord(char word[10])
    {
        int rRow, rCol , ok , i;
    
        do
        {
            // Randomly select a location
            rRow = rand() % 10;
            rCol = rand() % 10;
    
            // For now, assume that this location is "ok", i.e. is open
            ok = 1;
    
            // Check that the word fits inside the grid from (rRow, rCol)
            if(rCol + strlen(word) < 10)
            {
                // If it does, then try to put the word at this location
                // Thus, we need to process the word character by character
                for(i = 0;i < strlen(word);i++)
                {
                    // We are inside the for loop
                    // The current character to process is word[i]
                    // And the current cell to fill is (rRow, rCol + i)
                    //
                    // If current cell is empty || is same as the current character
                    // then this cell is "open" i.e. we can use it
                    if(puzzle[rRow][rCol + i] == ' ' || 
                        puzzle[rRow][rCol + i] == word[i])
                    {
                        puzzle[rRow][rCol + i] = word[i];
                    }
                    else
                    {
                        // The cell is not open
                        // => (rRow, rCol) is not "ok"
                        ok = 0;
                    }
                }
            }
            else
            {
                // word not fits inside the grid from the location
                // => (rRow, rCol) is not "ok"
                ok = 0;
            }
        }
        while(ok == 0); // Exit loop while not found a good location
    }
    

    If you have understood, then you can now modify this to write your vertical and diagonal versions. If you still have not understood, let me know what is still not clear.