I am stuck understanding my own logic here, why so many babies?

Why so many Bunnys right at the beginning but no more later on? theres supposed to be 6 to start with, instead theres a bunny invasion, and then no more babies after that! I dont see whats wrong with my logic, I told my rubber ducky everything and it all adds up, I was also having a crash pushing a bunny back on the vector but that went away mysteriously!

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

class bunny
{
public:
   ~~~~
    void update_rect(int x,int y) //this updates the objects rect for collisions
    {
        obscolbox.x=x;
        obscolbox.y=y;
        obscolbox.w=50;
        obscolbox.h=50;
    }

    bunny()//the constructor initializes loads of random starting points
    {
        int sex = (1+rand()%2);
        if (sex == 2)
        {
            female = true;
        }
        start_pos_x = (40+rand()%540);
        start_pos_y = (40+rand()%300);
        int dir = (1+rand()%4);
        if(dir==4)
        {
            up=true;
        }
        if(dir==1)
        {
            down = true;
        }
        if(dir==2)
        {
            left = true;
        }
        if(dir==3)
        {
            right = true;
        }


    }

    ~~~

    bool bunny_collision(SDL_Rect &A,SDL_Rect &B)//good old fasioned collision detecting// function, takes two rects 
    {
        int leftA, leftB;
        int rightA, rightB;
        int topA, topB;
        int bottomA, bottomB;

        leftA =A.x;
        rightA = A.x + A.w;
        topA = A.y;
        bottomA = A.y + A.h;

        leftB = B.x;
        rightB = B.x + B.w;
        topB = B.y;
        bottomB = B.y + B.h;

        if( bottomA <= topB )
        {
            return false;
        }

        if( topA >= bottomB )
        {
            return false;
        }

        if( rightA <= leftB )
        {
            return false;
        }

        if( leftA >= rightB )
        {
            return false;
        }

        collided=true;
        return true;
    }

    void new_direction()//when this is called new direction logic is initialized
    {
        int dir = 0;
        dir = (1+rand()%5);
        if(dir==1)
        {
            up=true;
        }
        if(dir==2)
        {
            down = true;
        }
        if(dir==3)
        {

            left = true;
        }
        if(dir==4)
        {

            right = true;
        }
        if(dir==5)
        {
            still = true;
        }
    }


    bool check_pos()//so the bunny doesnt roam off the edge
    {
        if ((start_pos_x < 5)||(start_pos_x > 640-51)||(start_pos_y<5)||(start_pos_y>475-47)||(collided==true))
            return true;

        else
            return false;
    }

    bool pregnancy_test (bool humping)//if succesful mating occured :D
    {
     pregnant = true;
     Mix_PlayChannel(-1,note,0);
     return true;
    }

    void roam_about()//each loop position, animation frame and direction logic are updated
    {
        if (still == true)
        {
            apply_surface(start_pos_x,start_pos_y,bunnysheet,screen,&clips[stillclips]);
            stillclips++;
            roamtime++;
            if (stillclips == 23)
            {
                stillclips =12;
            }
            if(roamtime == 12)
            {
                still=false;
                roamtime = 0;
                new_direction();
            }
        }

        else if (up == true)
        {

            apply_surface(start_pos_x,start_pos_y = start_pos_y+5,bunnysheet,screen,&clips[upclips]);
            upclips++;
            roamtime++;
            if(upclips == 3)
            {
                upclips = 0;
            }

            if(check_pos()==true)
            {
                up = false;
                roamtime = 0;
                down = true;
            }
            else if(roamtime == 9)
            {
                up = false;
                roamtime = 0;
                new_direction();
            }
        }

        else if (down == true)
        {

            apply_surface(start_pos_x,start_pos_y = start_pos_y-5,bunnysheet,screen,&clips[downclips]);
            downclips++;
            if(downclips == 6)
            {
                downclips = 3;
            }
            if(check_pos() ==true)
            {
                down = false;
                roamtime = 0;
                up = true;
            }
            else if(roamtime == 9)
            {
                down = false;
                roamtime = 0;
                new_direction();
            }
        }

        else if (left == true)
        {

            apply_surface(start_pos_x = start_pos_x-5,start_pos_y = start_pos_y,bunnysheet,screen,&clips[leftclips]);
            leftclips++;
            roamtime++;
            if(leftclips == 12)
            {
                leftclips = 9;
            }
            if(check_pos() == true)
            {
                left = false;
                roamtime = 0;
                right = true;
            }
            else if(roamtime == 9)
            {
                left = false;
                roamtime = 0;
                new_direction();
            }
        }

        else if (right == true)
        {

            apply_surface(start_pos_x = start_pos_x +5,start_pos_y = start_pos_y,bunnysheet,screen,&clips[rightclips]);
            rightclips++;
            roamtime++;
            if(rightclips == 9)
            {
                rightclips = 6;
            }
            if(check_pos()==true)
            {
                right = false;
                roamtime = 0;
                left = true;
            }
            else if(roamtime == 9)
            {
                right = false;
                roamtime = 0;
                new_direction();
            }
        }


        collided = false;//gotta set that to false again otherwise bunny isnt allowed to go anywhere
    }
};


int main( int argc, char* args[] )
{

    ~~~~
    vector<bunny>bunny_list;//a vector of objects to be manipulated in the nested loop
     bunny_list.push_back(bunny());
     bunny_list.push_back(bunny());
     bunny_list.push_back(bunny());
     bunny_list.push_back(bunny());//innitial bunnehs
     bunny_list.push_back(bunny());
     bunny_list.push_back(bunny());

    for(int q = 0; q < 1000; q++)//main game loop
    {


        for (int n =0; n<bunny_list.size(); n++ )

//first of nested loop checks bunneh
        {

 //this calls the update collision rect to bunnys location        
bunny_list[n].update_rect(bunny_list[n].start_pos_x,bunny_list[n].start_pos_y);

            for (int z =0; z<bunny_list.size(); z++ )
            {
                if(n==z)//this is supposed to stop n and z being the same so a bunny does not check itself for a collision
                {
                    z++;

                }
                bunny_list[n].bunny_collision(bunny_list[n].obscolbox,bunny_list[z].obscolbox);
//if they are collided then bool colided is set to true
                if((bunny_list[n].collided==true)&&(bunny_list[z].collided==true))
                    {//if the two bunnies that collided are male and female
                        if((bunny_list[n].female==true)&&(bunny_list[z].female==false))
                        {
                            if(bunny_list[n].pregnancy_test(bunny_list[n].collided)==true)
                                {//if nocked up then BABIES! :D
                                bunny_list.push_back(bunny());
                                }
                        }

                    }
            }

        if(bunny_list[n].collided==true)
        {
            bunny_list[n].collided=false;//gotta set it back to false again 
        }

        bunny_list[n].roam_about();
        }

        ~~~
        SDL_Delay(120);

        ~~~~~~
Last edited on
¿wtf am I supposed to look at?
By the way, you've got `~' all over the place.
I actually cut loads of stuff out the code...OMG forgotto comment it sry, I will do that now
okay I just updated it with comments, I will submit the whole code if you think the bits I cut out are important
1
2
3
4
5
if(n==z)//this is supposed to stop n and z being the same so a bunny does not check itself for a collision
{
	z++;

}

This will not work correctly when n = z = bunny_list.size() - 1. z will get set to bunny_list.size() and then the code runs and access bunny_list[z] which is out of bounds.
so I could z-- again perhaps after it has mattered?
so why doesn't the code work if nothing happens if the numbers are the same??

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
31
32
for (int n =0; n<bunny_list.size(); n++ )
        {

         bunny_list[n].update_rect(bunny_list[n].start_pos_x,bunny_list[n].start_pos_y);
            for (int z =0; z<bunny_list.size(); z++ )
            {
                if(n!=z)
                {
                   bunny_list[n].bunny_collision(bunny_list[n].obscolbox,bunny_list[z].obscolbox);
                if((bunny_list[n].collided==true)&&(bunny_list[z].collided==true))
                    {
                        if((bunny_list[n].female==true)&&(bunny_list[z].female==false))
                        {
                            if(bunny_list[n].pregnancy_test(bunny_list[n].collided)==true)
                                {
                                bunny_list.push_back(bunny());
                                }
                        }

                    }

                }

            }

        if(bunny_list[n].collided==true)
        {
            bunny_list[n].collided=false;
        }

        bunny_list[n].roam_about();
        }
^iwouldn't that be the answer? but it still has the same effect?
BABIES:D
FUNK
¿are those bumps?
I'll suggest to refactor your member functions in order for them to be member function (use the state of the caller object)
Also, use the return values.

An assignment description may be helpful too.


> told my rubber ducky everything and it all adds up
Yeah, sure.
if((bunny_list[n].collided==true)&&(bunny_list[z].collided==true)) explain (put details of how `collided' may change)
, what is this refactoring? and i couldnt find state of the caller object on google, I understand using return values, why is it better practice?


Im not doing this for any kind of assignment either its just practice, unless you mean the assignment operator, in which case I dont understand.

thanks for checking this though I can't believe mr quackles would lie to me :/

they are funky bumps
> what is this refactoring?
rewrote it

> i couldnt find state of the caller object on google
state: value of your member variables
caller object: the one the message is send to
1
2
3
4
bunny_list. //caller object
   push_back( //message
      bunny() //parameters (part of the message)
   );

(maybe the word is callee)

You want to check if two bunnies collide (¿?)
if( bunny[K].collide( bunny[L] ) )

> Im not doing this for any kind of assignment either its just practice
What is your program supposed to do
What problem is trying to resolve

> I can't believe mr quackles would lie to me
you lied to mr quackles

Still awaiting your explanation
Last edited on
The bunnies are supposed to roam about freely, they got to breed if they collide, there should be a whole load of functions that determine age, health and other things, like they can pass on diseases, hareditiary deseases,STIs get eaten by foxes, it will all be output onto a txt folder, ie when they were born how they died what happened in their lives.



mr quackles got me drunk and took advantage of me
Last edited on
explain how `collided' may change
If bunny [K].collided(bunnk[k].rect,bunny[L].rect);
bunny[k] has a rect that is within the bounds of bunny[L] that bunnies bool "collided" is set to true,

after all the bunnies have been checked if collided==true their
appropriate direction is called, then set back to false after they have moved one turn.

I know that that is wrong but I don't see how it is yet, if i did lie to mister quackles it was a lie of a mission.
wakkawakkaFUNK
> bunny[k] has a rect that is within the bounds of bunny[L] that bunnies bool "collided" is set to true,
wrong
1
2
3
4
 bunny_collision(SDL_Rect &A,SDL_Rect &B){ //takes rectangles, not another bunny
   //...
   collided=true; //only change this bunny
}
Also, you don't have `collided=false' (if that were the case)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//this is your code
	for (int n =0; n<bunny_list.size(); n++){//take one bunny at the time
		bunny_list[n].update_rect(bunny_list[n].start_pos_x,bunny_list[n].start_pos_y);
		for (int z =0; z<bunny_list.size(); z++) //compare against all others
			if(n!=z)
			{
				//would only modify the nth bunny
				bunny_list[n].bunny_collision(bunny_list[n].obscolbox,bunny_list[z].obscolbox);
				
				if(bunny_list[n].collided 
					and bunny_list[z].collided) //this could never happen
				//...
			}

		if(bunny_list[n].collided)  //reset `collided'
			bunny_list[n].collided=false;

		bunny_list[n].roam_about(); //reset `collided'
	}


Consider instead
1
2
3
4
5
6
7
8
9
	for(size_t K=0; K<bunnies.size(); ++K){
		for(size_t L=0; L<bunnies.size(); ++L){
			if(K==L) continue;
			if( collide(bunnies[K], bunnies[L]) )
				if( bunnies[K].have_baby(bunnies[L]) )
					bunnies.push_back( bunny() );
		}
		bunnies[K].roam_about();
	}


> if i did lie to mister quackles it was a lie of a mission.
Do it seriously next time.
:D, I have to study all of this more thoroughly after some more sleep, mine eyes are drying up.

I have never heard of keyword continue
Last edited on
Topic archived. No new replies allowed.