1

私は C プログラミングがまったく初めてで、Word Searchを作成しようとしています。

私は単語のリストを持っています.4つだけがランダムに選ばれています. これらの 4 つの単語は、水平方向、垂直方向、または斜め方向にグリッドで印刷する必要がありますが、水平方向にしか印刷できません。また、コードの一部がどのように機能するのかまったくわからないことを付け加えなければならないので、誰か親切な人が実際に私を助けてくれれば本当に感謝しています. それで、垂直方向と斜め方向の整列でランダムな単語を作成するために、誰かが正しい方向に私を助けることができますか? http://imgur.com/VSrXf4C

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

1 に答える 1

0

そのコードの機能を説明するためのコメントを次に示します。

// 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
}

理解できたら、これを変更して、垂直バージョンと斜めバージョンを作成できます。まだ理解していない場合は、まだ明確でないことを教えてください。

于 2016-01-05T21:50:30.337 に答える