Saturday, February 15, 2020

Playing the Antgame in C#

Originally, when I first wished to learn coding, I started of with C++ at the online education platform "Solo Learn". But I didn't went really far and so my first real contact with coding was in my first semester of studying physics. We had a few weeks of python and I liked the programming but the language was a mess to me. I hardly struggled with the obscure syntax, missing a clear and straight logic. When I changed my job-interest from physics to informatics, I had a whole semester learning C from scratch. That moment it was I fell in love with that beautiful language and did nothing than programming in C for a whole semester. Maybe unjustifiable for to days world of object orientated programming, I still take  C above every other language, especially python.... ;P, if I can. 

Leaving university and dropping in my training for becoming a system integrator, I had to take another class of programming. But this time it was not procedural.... it was object orientated... 
Luckily, our teacher chose C#, a language akin to C, so I could recognize a lot and it kept the general main logic I love so much. 

But still, objects were a hard pill to swallow. I wouldn't say I had to fight it like pointer, because the logic is really  easy to get... I mean, it's basically a struct with extra features... But getting the workflow with that "extra features" and not killing myself with them every second line took me some effort XD .

Well, I had my fights with the teacher but in the end I always understood what he wants to see, even if I still do not agree at any point, but I guess that's fine. 

Long story short, after some introductory exercises, he gave us "the Antgame", a quiet popular programming exercise like the euler problems (btw. note to myself: I could start of with them here too), to learn object orientation. The task is to simulate a colony of ants rising and dying, moving around on the console and doing stuff in relation to events. How this activities work and come together is absolutely open to the individual programmer. So in the end, one task produces a tone of different solutions, making it really interesting in my eyes. 

And here it is, my solution for "the Antgame". Definitely expandable but for the effort I took and that it was a "schoolproject", I'm fine with it. :D 


First, have a look at the mainprogram: 


Before entering the main, two functions are defined. The first one initializes a startpopulation of one queen and a custom number of ants and the second the amount of food an obstacles (so far useless stones), also of a custom number. 


The main starts setting some basic parameters like the conolse size and declarates one list for the colony objects and one for the environment objects. After that, the simualtionparamters are taken from the user and then, after setting everything on the console ready for the simulation, the further created lists are initialized. 


After that, the simualtion itslef starts by counting up day by day and simulationg the events inside. Every day, it first checks if an ants position matches a foods position, and if thats the case, restores life of the ant and reduces the lifetime the food has left. 
Then the colony ages and moves and if an ant ends her life, removes her entry from the list. Finally, the visualization of the environment is updated for the next round. 



When updating the environment is done, the colony repoduces after an "algorythm" I figured out with try and error (yess, you could have implemented it with freaky math, you're invited to add it), and the next day begins. 

When the simulation ends, the left food is calculated for....



displaying a short summary of the simualtion. 

So, that's basically what the simulation does. Next, have a look into the objects: 


First Object is the simple ant, having an ID, an x- and y-position, an age and a "deathstatus". Right underneath, u can see the constructor who calls the initialization when the object is called.


The movementfunction of the ant defines the random movement behaviour, prints the ants position and makes clear, that the ant stays in range of the console. 



Also included is a function for the aging of the ant, as well as the initialization function, called to create a new ant. 

The ant object bequeath it's attributes to the queen, which then is a little customized. 



Byside a new constructor, the queen gets a new movementbehaviour and a new init.



Next up is the general blueprint for all environmental objects... 



containing, byside constructor and init, a new display- and a lifetimecounter-function.

The Envobject then bequeath to the following two objects: 



Well, that basically it's. So let's have a look into the running program!