dimanche 6 décembre 2015

How to randomly place a word in a grid in c

I have an assignment to create a simple word search program using c. I am stuck in implementing an algorithm to place a randomly selected word into a random place in the grid. Here is the code:

int getRandomNumber()
{
    srand((unsigned)time(NULL));
    int number = rand() % 10;
    return number;
}

//placeRandomWords() Function

void placeRandomWords()
{
    int i = 0, k = 0;

    srand((unsigned)time(NULL));
    n = rand() % 10;
    checkRandom[k] = n;
    k ++;
    int rowRandom = getRandomNumber();
    int colRandom = getRandomNumber();
    for(int j = 0; j < 10; j ++)
{
   //FOR NOW I AM NOT CONSIDERING DIAGONALS
    puzzle[rowRandom][colRandom] = words[n][j];
    printf("%c", puzzle[rowRandom][colRandom]);
    rowRandom ++;
    colRandom ++;
}

  do
   {
    n = rand() % 10;

    for(int t = 0; t < 4; t ++)
    {
        if(checkRandom[t] == n)
        {
            found = 1;
            break;
        }
        else
            found = 0;
    }

    if(found == 0)
    {
        checkRandom[k] = n;
        k ++;
        for(int j = 0; j < 10; j ++)
        {
            puzzle[rowRandom][colRandom] = words[n][j];
            printf("%c", puzzle[rowRandom][colRandom]);
            rowRandom ++;
            colRandom ++;
        }
        i++;
     }
  }while(i < 3);
}

Output of current code




Aucun commentaire:

Enregistrer un commentaire