help please!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    for (int i=0; i<size; i++)
    {
        b[i]=bug(initMaxX,initMaxY);
        //cout<<b[i].getX()<<","<<b[i].getY()<<">>";
        
        temp.getX()==b[i].getX();
        temp.getY()==b[i].getY();
        
        b[i].move();
        while (b[i].getX()==temp.getX() && b[i].getY()==temp.getY())
            b[i].move();
        
        //cout<<b[i].getX()<<","<<b[i].getY()<<endl;
    }


this code loops through an array of size=size, initializing each instance of the array with random x and y values. these instances move randomly in one of 8 possible directions. Following is the class function "bug::move()"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void bug::move(){

            int xMove, yMove, bMove;
            
            xMove=rand()%3;
            yMove=rand()%3;
            
            if (alive=true){
           
                {
                    if (xMove==0 && x-1>0)
                        x--;
                    if (xMove==2 && x+1<maxX)
                        x++;
                    else
                        x=x;
                }
           
           
                {
                    if (yMove==0 && y-1>0)
                        y--;
                    if (yMove==2 && y+1<maxY)
                        y++;
                    else
                        y=y;
                }
           
                
            }


this code works pretty well (the bugs stay in valid range), but i keep getting instances in which a bug will not move (by random chance, the values just dont change). I'm not detecting, logically, what is causing this. that is why i put in a while loop after b[i].move() to move again if the values are both the same. Which also poses a problem of my bug moving more than once (i have no code to reset the values to their previous state, i believe that re-initializing them with random values isnt an option).
Last edited on
a) they won't move if they try to move into a wall
b) they won't move if both xMove and yMove will be 1, which has 1/9 chance of happening.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
        void bug::move(){

            int xMove, yMove;
            
            xMove=rand()%3;
            yMove=rand()%3;
            
            if (alive=true){
           
                {
                    if (xMove==0 && x>0)
                        x--;
                    else if (xMove==2 && x<maxX)
                        x++;
                    else
                        x=x;
                }
           
           
                {
                    if (yMove==0 && y>0)
                        y--;
                    else if (yMove==2 && y<maxY)
                        y++;
                    else
                        y=y;
                }
           
                
            }



should i revise the method "bug::move" like this?
correct me if i'm wrong but shouldn't it be if(alive==true)?
Topic archived. No new replies allowed.