1

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.stack.imgur.com/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);
}
Takari
  • 29
  • 2
  • 3
  • 8
  • I'm sorry , but I was still editing the topic. I've also added an image of how the program is looking. – Takari Jan 05 '16 at 20:43

1 Answers1

0

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.

mikedu95
  • 1,725
  • 2
  • 12
  • 24
  • Thanks for the explaining Mike. Your comments really made it easier for me to understand the code. I thought I had the answer to print them vertical by swapping [rRow][rCol +i] in the for loop to [rCol][rRow +i] . It made a lot of sense in my mind however it still didn't print them vertically. (they still got printed horizontally) Am I missing something important ? Thanks in advance. – Takari Jan 05 '16 at 22:19
  • It's `[rRow + i][rCol]` to fill vertically. But you also have to change some checks ... – mikedu95 Jan 05 '16 at 22:23
  • Thanks a lot. It is late at night at the moment so I will give the diagonal code a try tomorrow. – Takari Jan 05 '16 at 22:55