我试图分配随机整数结构成员,但它不能正常工作 [英] I'm trying to assign random integers to struct members, but it doesn't work properly

查看:115
本文介绍了我试图分配随机整数结构成员,但它不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个任务,我不得不随意放置在随机坐标场球员,但这些坐标必须是唯一的,否则他们必须重新生成。我想随机整数并将其分配给结构成员。然而,似乎我在做什么,现在不按我的教授正常工作。这是结构:

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:

没有地方做初始化场上位置的值。所有变量所需要的第一时间,他们的值被使用之前被初始化。你有一个叫田二维数组,其中每个你刚才的元素假设的从零到开始---但你不知道,因为你永远不设置这些元素为零。你需要一个code其中将所有该二维数组的元素为其初始值的的您开始在场上的球员。

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.

这篇关于我试图分配随机整数结构成员,但它不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆