Very buggy simulation

This is supposed to be a simulation of Bacteria. However, it obviously has a lot of bugs. I was wondering if anyone could give suggestions on how to improve it. Particularly, the Move_Bac function and the general AI for the bacteria is completely ineffective. I'm fairly new to most of the concepts involved in this simulation, though I do have some general C++ and windows programming knowledge. Any help or commentary is welcome! Thank You!

here's the code for the Bacteria Class:
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
class Bacteria
{
public:
	int xpos,ypos;//position of bacteria
	int xvel,yvel;//velocity of bacteria
	int speed;//maximum distance covered in one frame
	int sight;//radius of bacteria sensory input
	int nutrients;//hunger of bacteria
	int deathnum;//hunger at which bacteria dies
	int reprate;//amount of food bacteria needs to live
	int mutrate;//percent chance of mutation per bit
	bool mem, advbrain;//memory and thinking algorithm model
	int mempos[1];//vector representation of last place it found food[x,y]
	bool DNA[21];//DNA
	bool mDNA[21];//DNA after mutation
	bool plasmids[10][10];//two-dimensional array where each row is a seperate plasmid and each column a field of plasmid DNA coding
	Bacteria* pNext;// pointer to next element of the linked list

	void Mutate(bool DNA[21])
	{
		int checker[21];
		for(int i=0;i<21;i++)
		{
		checker[i]=rand()%100+1;
		if(checker[i]<=mutrate)
		{
			if(DNA[i]==true)
			{
				DNA[i]=false;
			}
			else
			{
                DNA[i]=true;
			}
		}
		Bacteria::mDNA[i]=DNA[i];
		}
		
	}

	void Reproduce()
	{
		Mutate(Bacteria::DNA);
		Generate_Bac(Bacteria::xpos,Bacteria::ypos,Bacteria::xvel,Bacteria::yvel,Bacteria::mDNA,Bacteria::plasmids,Bacteria::pNext);//fix this later
		Bacteria::nutrients-=(Bacteria::reprate/2);
	}

	void Generate_Bac(int xpos, int ypos,int xvel,int yvel, bool DNA[21],bool plasmids[10][10],Bacteria* pBAC)
	{
		//Bacteria* pBAC=new Bacteria;
		pBAC->pNext=0;
		pBAC->ypos=ypos;
		pBAC->xpos=xpos;
		pBAC->xvel=xvel;
		pBAC->yvel=yvel;
		for(int i=0;i<22;i++)
		{
		pBAC->DNA[i]=DNA[i];
		}
		int c=0;
		for(int i=0;i<10;i++)
		{
		pBAC->plasmids[i][c]=plasmids[i][c];
		c++;
		}
		//parse the DNA sequence
		if(DNA[0]==true)
		{
			pBAC->mem=true;
			pBAC->mempos[0]=0;
			pBAC->mempos[1]=0;
		}
		else
		{
			pBAC->mem=false;
		}
		if(DNA[1]==true)
		{
			pBAC->speed=BASE_SPEED+2;
		}
		if(DNA[2]==true)
		{
            pBAC->speed=BASE_SPEED+2;
		}
		if(DNA[3]==true)
		{
			pBAC->sight=BASE_SIGHT+20;
		}
		if(DNA[4]==true)
		{
			pBAC->sight=BASE_SIGHT+20;
		}
		if(DNA[5]==true)
		{
			pBAC->deathnum=BASE_DNUM-50;
		}
		if(DNA[6]==true)
		{
			pBAC->deathnum=BASE_DNUM-50;
		}
		if(DNA[7]==true)
		{
			pBAC->reprate=BASE_REPRATE-100;
		}
		if(DNA[8]==true)
		{
			pBAC->reprate=BASE_REPRATE-100;
		}
		if(DNA[9]==true)
		{
			pBAC->mutrate=BASE_MUTRATE+5;
		}
		if(DNA[10]==true)
		{
			pBAC->mutrate=BASE_MUTRATE+5;
		}
		if(DNA[11]==true)
		{
			pBAC->speed=BASE_SPEED-2;
		}
		if(DNA[12]==true)
		{
			pBAC->speed=BASE_SPEED-2;
		}
		if(DNA[13]==true)
		{
			pBAC->sight=BASE_SIGHT-10;
		}
		if(DNA[14]==true)
		{
			pBAC->sight=BASE_SIGHT-10;
		}
		if(DNA[15]==true)
		{
			pBAC->deathnum=BASE_DNUM+50;
		}
		if(DNA[16]==true)
		{
			pBAC->deathnum=BASE_DNUM+50;
		}
		if(DNA[17]==true)
		{
			pBAC->reprate=BASE_REPRATE+100;
		}
		if(DNA[18]==true)
		{
			pBAC->reprate=BASE_REPRATE+100;
		}
		if(DNA[19]==true)
		{
			pBAC->mutrate=BASE_MUTRATE-5;
		}
		if(DNA[20]==true)
		{
			pBAC->mutrate=BASE_MUTRATE-5;
		}
		if(DNA[21]==true)
		{
			pBAC->advbrain=true;
		}
		pBAC->nutrients=500;
		return;
		//don't forget to call add_bacteria after every call to Generate_Bac
	};


	void Move_Bac()
	{
		if(Bacteria::xvel>1)
		{
			Bacteria::xvel=1;
		}
		if(Bacteria::yvel>1)
		{
			Bacteria::yvel=1;
		}
		if(Bacteria::xvel<1)
		{
			Bacteria::xvel=-1;
		}
		if(Bacteria::yvel<1)
		{
			Bacteria::yvel=-1;
		}
		int finalvelx;
		int finalvely;
		finalvelx = Bacteria::xvel*Bacteria::speed;
		finalvely = Bacteria::yvel*Bacteria::speed;
		Bacteria::xpos+=finalvelx;
		Bacteria::ypos+=finalvely;
		nutrients-=1;
	};

	void Think_Bac()//remember to check for bacteria death by hunger here
	{

		if(Bacteria::advbrain==true)
		{
			//Bacteria::Evade();
			if(nutrients>=reprate)
			{
				Bacteria::Reproduce();
				Bacteria::Find_Food();
			}
			else
			{
				Bacteria::Find_Food();
			}
			Bacteria::Move_Bac();
			return;
		}
		else
		{
			//Bacteria::Evade();
			if(nutrients>=reprate)
			{
				Bacteria::Reproduce();
				Bacteria::Wander();
			}
			else
			{
				Bacteria::Wander();
			}
			Bacteria::Move_Bac();
			return;
		}

	};

	void Terminate_Bac()
	{
		//deletes object from linked list






	};

	void Find_Food()
	{
		int xdist[FOOD_NUM];
		int ydist[FOOD_NUM];
		int fdist[FOOD_NUM];
		int lodist;
		int loID;
		for(int i=0;i<FOOD_NUM;i++)//compute approximate distance b/w Bacteria and every single food object
		{
			xdist[i]=abs(Bacteria::xpos-food[i].xpos);
			ydist[i]=abs(Bacteria::ypos-food[i].ypos);
			fdist[i]=xdist[i]+ydist[i];
		}//END loop
		lodist=fdist[0];
		for(int i=0;i<FOOD_NUM;i++)//find lowest distance
		{
			if(fdist[i]<lodist&&food[i].exists==true)
			{
				lodist=fdist[i];
				loID=i;
			}//END if
		}//END loop
		if(lodist<Bacteria::sight)
		{
			if(food[loID].xpos>Bacteria::xpos)
			{
				Bacteria::xvel++;
			}
			if(food[loID].xpos<Bacteria::xpos)
			{
				Bacteria::xvel--;
			}
			if(food[loID].ypos>Bacteria::ypos)
			{
				Bacteria::yvel++;
			}
			if(food[loID].ypos<Bacteria::ypos)
			{
				Bacteria::yvel--;
			}
			if(food[loID].xpos==Bacteria::xpos)
			{
				food[loID].exists=false;
				Bacteria::nutrients+=100;
			}
			Bacteria::Move_Bac
				();
			return;
		}
		else
		{
			if(Bacteria::mem==true)
			{
				if(Bacteria::mempos[0]!=0&&Bacteria::mempos[1]!=0)
				{
				if(Bacteria::mempos[0]>Bacteria::xpos)
				{
					Bacteria::xvel++;
				}
				if(Bacteria::mempos[0]<Bacteria::xpos)
				{
					Bacteria::xvel--;
				}
				if(Bacteria::mempos[1]>Bacteria::ypos)
				{
					Bacteria::yvel++;
				}
				if(Bacteria::mempos[1]<Bacteria::ypos)
				{
					Bacteria::yvel--;
				}
				Bacteria::Move_Bac();
				return;
				}
				Bacteria::Wander();
			}
			else
			{
				Bacteria::Wander();
				return;
			}
		}
	};

	void Evade()
	{
		int dist;
		dist=sqrtf(((Bacteria::xpos-hazard.xpos)^2)+(Bacteria::ypos-hazard.ypos)^2);
		if(dist<=sight)
		{
			if(hazard.xpos>Bacteria::xpos)
			{
				Bacteria::xvel--;
			}
			if(hazard.xpos<Bacteria::xpos)
			{
				Bacteria::xvel++;
			}
			if(hazard.ypos>Bacteria::ypos)
			{
				Bacteria::yvel--;
			}
			if(hazard.ypos<Bacteria::ypos)
			{
				Bacteria::yvel++;
			}
			if(hazard.xpos==Bacteria::xpos)
			{
				Bacteria::Terminate_Bac();
				return;
			}
			Bacteria::Move_Bac();
			return;
		}

	};

	void Wander()
	{
		Bacteria::xvel+=rand()%3+1;
		Bacteria::yvel+=rand()%3+1;
	};

	//possibly add plasmid interaction functions here
	
};


I've implemented a linked list of Bacteria, as you can probably tell
Topic archived. No new replies allowed.