简体   繁体   中英

I'm trying to assign random integers to struct members, but it doesn't work properly

I'm doing an assignment where I have to randomly place players on a field with random coordinates, but those coordinates must be unique or else they must be regenerated. I'm trying to randomize integers and assign them to struct members. However, what I'm doing right now doesn't seem to work properly according to my professor. This is the struct:

struct player {

   int x;
   int y;
   int direction;
   int id;
   int presence;
};

Those are my two arrays, one for the field and one for the size of the team on the field:

 int field [25][25];
 struct player team [25];

This is the part of the function where I might have made a mistake:

int main (){

   int i;
   int j;
   int randx, randy;
   int SIZE_TEAM = 25;


   srand (time(NULL));

    for (i = 0; i < SIZE_TEAM; i++){

       randx = (rand () % 25); <-- Randomizing row position
       randy = (rand () % 25); <-- Randomizing column position

       while (field[randx][randy] != 0){ <-- While loop occurs if position in field is taken.
            randx = (rand () % 25);      <-- In this case, the coordinates are regenerated.
            randy = (rand () % 25);
       }
     team [i].x = randx; <--Where the mistake might lie
     team [i].y = randy; <--Where the mistake might lie
     team [i].id = i + 1;
     team [i].presence = 1;
     field [team [i].x][team [i].y] = team [i].id; <--Where the mistake might lie
  }

I'm not sure how I should "lock" the randomly generated values once I've assigned them to the relevant player. Do you think that my algorithm of assigning positions to players is incorrect?

Also, this is the abridged version of another question I posted, but that question was too long and no one bothered to help me.

Well --- I'm not sure if this is the real cause of the problem --- but I notice one problem:

No place do you initialize the values of the places on the field. All variables need to be initialized before the first time that their value is used. You have a two-dimentional array called "field" where each element you just assume is zero to begin with --- but you don't know that, because you never set these elements to zero. You need a code where you set all of the elements of this two-dimensional array to their initial value before you start putting the players on the field.

I might add (as an aside) regarding this statement:

int SIZE_TEAM = 25;

instead of making it an integer within the main function it should be declared as a macro globally -- like this ...

int field [25][25];
#define SIZE_TEAM 25
struct player team [SIZE_TEAM];

That way, if you ever have to change the size of the team, you will only have to change it in one place rather than two places.

Solving it the way @WhozCraig suggested is actually not complex:

static void random_permutation(int *x, int n)
{
    int i, r;
    /* mark all elements as "unset" */
    for (i = 0; i < n; i++) {
        x[i] = -1;
    }

    for (i = 0; i < n; i++)
    {
        /* start from a random value and find the first unset element */
        for (r = rand() % n; x[r] >= 0; r = (r + 1) % n)
            ;
        x[r] = i;
    }
}

#define SIZE_TEAM 25

int main (void)
{
    int randx[SIZE_TEAM], randy[SIZE_TEAM];
    srand (time(NULL));

    random_permutation(randx, SIZE_TEAM);
    random_permutation(randy, SIZE_TEAM);

    for (i = 0; i < SIZE_TEAM; i++) {
        int x = randx[i], y = randy[i];
        team[i].x = x;
        team[i].y = y;
        team[i].presence = 1;
        team[i].id = i + 1;
        field[x][y] = team[i].id;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM