I used to be dragged into Battlecode 2024 by one in all our teammates. I had by no means heard of Battlecode earlier than, however I used to be excited to attempt it out. I had numerous enjoyable and I’m glad I did it, and we ended up qualifying for the finals and putting thirteenth total. So I figured I’d write a weblog publish in regards to the expertise from the angle of a primary time participant.
This was cross-posted from my weblog, so when you’re verify extra on the market.
Battlecode is a contest the place you write code to manage a workforce of robots which have to perform some activity. You may learn extra about it right here.
This 12 months the sport was a duck based mostly seize the flag sport the place our aim was to seize all 3 of the flags from the opponent. The sport is performed on a grid and every workforce has a base the place they spawn geese. Geese can transfer across the map, construct traps, and assault different geese. The sport is performed in rounds, and every around the geese are given a set quantity of bytecode to execute their actions.
Right here’s a fast snippet of what the sport seems like
There’s lots occurring in that gif so let’s break it down slightly bit.
Every duck has just a few actions to assist assault or defend flags that it might probably carry out every flip/spherical (proven on the backside of the gif above). Every motion has a selected cooldown so you may’t simply spam the identical motion again and again.
- Transfer to a brand new tile
- Construct traps: water, bombs, stuns, dig a water pit, and fill a water pit
- Choose up flag
- Assault an enemy duck
- Heal
- Respawn
– When a duck dies, it might probably respawn at one of many workforce’s spawn factors after ~20 rounds
There’s additionally a forex referred to as crumbs you could get from two methods: just a few spawn on the map every spherical which you’ll decide up, and also you additionally get some for killing enemy geese of their territory.
Actions that construct traps require crumbs, which requires you to be strategic about how you employ your crumbs.
For the reason that identical code will get deployed to all of the geese, it’s a must to write code that may deal with numerous totally different conditions. Perhaps you need some geese to give attention to defending and constructing round base, perhaps you need some designated to exit and seize flags, and perhaps you need some to be aggressive and assault the enemy geese.
A couple of issues that make it troublesome:
- There’s a restricted dimension shared array that every duck can learn and write
– That is the one method to talk data between your geese in regards to the state of the sport
– This implies it’s a must to be strategic about what data you retailer within the array and the way you employ it, and in case you have all of your geese performing the identical motion based mostly on the identical data it’s a must to watch out as a result of in any other case they’ll cluster up doing the identical factor so it’s a must to watch out about the way you distribute the work - Every duck is given 25K bytecode to execute per flip
– Because of this every flip every duck will get to execute a sure variety of actions, calculations, and reminiscence reads and writes.
– If you would like tremendous good pathfinding (which we didn’t have) it’s a must to watch out about how you employ your bytecode in intensive calculations like this in any other case you’ll run out of bytecode and your geese will simply sit there and do nothing
– Fortunately we didn’t have to fret about this an excessive amount of since 25K is sort of giant and apparently in earlier years its been a lot smaller
I additionally really feel prefer it’s actually laborious to inform in case your new bot is healthier than the earlier model, like often it’s higher in some conditions and worse in others. Not like most software program I’m used to engaged on it’s simple to inform if what you’ve made is healthier than earlier than since when you added a brand new characteristic it’s higher than earlier than.
For this I’d say like at the least half of the options I attempted to develop didn’t work tremendous properly and simply ended up scrapping the adjustments. There’s so many branches for lifeless options that simply didn’t work out.
Okay so now that we’ve a fundamental understanding of the sport, let’s speak in regards to the precise competitors.
Leaderboard
The leaderboard is among the most necessary components of the competition. It’s a semi-live leaderboard that exhibits the rankings of all of the groups within the contest. Each 4 hours there can be ranked video games performed towards your relative neighbors on the leaderboard and when you win you acquire extra factors and when you lose you lose factors.
Moreover, you may manually queue your individual ranked video games towards different groups so long as they permit it. That is necessary as a result of you may’t simply queue video games towards the identical workforce again and again to farm factors.
Nevertheless, you can too queue unranked video games towards any workforce which is necessary for testing your bot towards different groups to see if the adjustments you made are literally higher than earlier than towards actual opponents.
Sprints
Sprints are a separate a part of the competition the place the organizers run a bracket of video games and livestream the outcomes. Sprints are performed throughout all groups whether or not they’re in your division or not. The sprints are a enjoyable method to see how your bot is doing towards different groups and it’s additionally a great way to see how the opposite groups are doing.
The seeding of your workforce within the sprints relies in your leaderboard rating, so when you’re doing properly within the leaderboard you’ll be seeded increased within the sprints.
Tournaments
Tournaments are additionally livestreamed and are performed throughout the groups in your divison in the identical approach because the sprints. The seeding of your workforce within the tournaments relies in your leaderboard rating, so when you’re doing properly within the leaderboard you’ll be seeded increased within the tournaments.
In our 12 months the highest 12 US faculty groups and the highest 4 worldwide groups certified for the finals. As well as there’s different divisions like highschool and new to battlecode groups which have their very own event.
Okay now that we all know extra about what battlecode is and the way the competition is structured, let’s speak about our robotic and a few of the methods we used. This part goes to be slightly bit blurry as a result of I’m penning this ~2 months after the competition and I don’t bear in mind all the pieces we did.
Our Methods
We had just a few totally different methods and variations of our bot throughout the event that we tried out particularly as the sport received tweaked for stability and we discovered extra in regards to the sport.
- Setup Part
– For the primary 200 out of the 250 rounds of setup we simply had all of our geese transfer round randomly to attempt to discover as a lot of it as we may
– After that we had our geese transfer to the middle of the map and construct traps subsequent to the wall dividing the 2 sides of the map - Base Protection
– We had designated geese that will keep always guarding our flags and constructing traps round our base and particularly proper on the flag itself which we positioned down bombs and stun traps proper on the flag in order that if the enemy tried to select it up they’d get surprised and we may assault them.
– We additionally determined to make a water moat checkboard grid sample round our flags in order that the enemy geese would both must spend crumbs making an attempt to fill the water or undergo on the diagonals which made certain that the enemy geese couldnt utterly swarm our base directly and it made them funnel by way of just a few paths which made it simpler for our geese to defend.
– If a duck was guarding the flag and it noticed an enemy duck close by it will set a world variable within the shared array that will name different geese to come back assist defend the flag. We additionally made certain that if these protection factors have been referred to as that we would like geese to spawn close by to assist defend the flag.
– However we needed to be cautious about this as a result of if we referred to as too many geese to defend the flag then we wouldn’t have sufficient geese to exit and seize the enemy flags. - Flag Passing
– We seen that always our bots would get caught surrounding the duck with the flag and stopping it from actually shifting anyplace so we carried out a flag passing technique the place it will move the flag to a different duck nearer to the place the flag wanted to be captured
– We added this beautiful late within the event and it labored fairly properly. - Flag Return
– When a duck was returning the flag we had logic to assign close by geese to escort the duck returning the flag to guarantee that it may get again to our base safely
– It additionally focused to return to the bottom that was closest to the place the flag provider was
- Pathfinding
– We took code from a earlier 12 months’s bot for doing BugNav pathfinding however of their 12 months the bytecode restrict was a lot smaller so we had much more bytecode that we may’ve been utilizing to do smarter pathfinding however didn’t transfer to one thing higher.
– Our pathfinding was so dangerous that within the sprints we might usually get caught making an attempt to return the flag and infrequently misplaced video games due to it and the casters would make enjoyable of us for it 😭 however it was humorous so I didn’t thoughts - Higher Distribution of Specializations
– One factor I didn’t point out was that when a duck does an motion sufficient instances it’ll specialise in that motion and get extra environment friendly at it. This specialization additionally limits it’s peak effectivity in different actions so we must always’ve in all probability been extra cautious about this since for lots of the event most of our geese have been specialised in therapeutic and we didn’t really want that many geese specialised in therapeutic because it made us worse at preventing.
– I did make this slightly bit extra properly balanced between healers and fighters on the finish however I feel that is one thing we must always’ve accomplished in a extra calculated method to attempt to maximize probably the most out of our geese - Caught Flag Duck
– We simply didn’t write any code to deal with the case the place a duck picks up the enemy flag and will get caught someplace like on an island surrounded by water and have been simply too lazy to repair it and it didn’t occur usually sufficient for us to care about it an excessive amount of. - Duck Standoff
– Typically our geese would simply sit there and do nothing and never transfer as a result of each teams of geese calculated that they have been at a drawback and that they shouldn’t be aggressive which was annoying and we didn’t actually have a great way to deal with this. - Shifting Flags
– We tried to maneuver the flags across the map in setup which helps you to change the placement the place your flags are however we didn’t make investments sufficient time into this for it to be tremendous efficient.
– Different groups have been capable of transfer their flags across the map in more practical methods. Particularly after they have been scoring spots of the map based mostly on issues just like the # of partitions across the flag and the gap away from pleasant spawns and enemy spawns.
– That is undoubtedly a characteristic that we must always’ve spent extra time on because it was fairly efficient for different groups
You will discover our code right here
I simply thought this is able to be enjoyable to incorporate however to qualify for the finals we needed to win this match and we have been fairly excited after we did. Right here’s a video of us profitable the match
This part is extra devoted to our expertise in Boston for the finals, which was fairly wonderful however if you wish to skip this section and go to reflection part beneath that’s cool too.
The Expertise
The finals have been held at MIT in Boston, and all of us received flown on the market for the occasion and it was my first time in reminiscence being in Boston and the town was actually fairly. We additionally ended up staying within the MIT dorms which was thrilling due to the opposite individuals we met there by way of being within the dorms. Though, if I have been to do it once more I might’ve in all probability stayed in a resort as a result of the dorms are properly ya know faculty dorms and will’ve been cleaner and I may’ve had a cushty mattress.
We additionally all have been invited to a dinner earlier than the finals the place we received to satisfy the opposite groups and the organizers. It was fascinating to see the place everybody was from as well as we received some very nice meals from them for the dinner.
The Outcomes
The finals was hosted of their auditorium and was fairly enjoyable to look at even when we went 0–2, however we did place thirteenth total and it was already an enormous accomplishment to make it this far.
Additionally they ran a estimathon the place we needed to guess a spread on some query and I ended up profitable a duck from accurately estimating the variety of MIT dorm residents that supplied to host groups for the finals. Right here’s a video of me catching the duck:
Enjoyable reality is that I put a pocket book underneath my jacket once I earlier than I went strolling up there so the video seems kinda foolish due to how I’m strolling 😭
I’m undoubtedly not an skilled on Battlecode, however I do have just a few key takeaways from the expertise and issues I’d enhance on if I have been to do it once more.
Make investments Time in Writing Organized and Clear Code
Though that it initially may seem to be a reasonably easy sport even in a 12 months the place our sport was comparatively easy in comparison with different years it’s criticial to take a position numerous time in writing organized and clear code.
We didn’t do code opinions, and didn’t refactor our code as a lot as we must always’ve and it ended up biting us all through the remainder of the event as we needed to repair bugs and simply attempt to perceive what our code was doing. That is one thing that I might counsel everybody invests extra time in particularly firstly to arrange basis and construction to your challenge so that you could construct on it and make adjustments extra simply.
A part of the rationale we didn’t do it is because we felt we have been rushed to get options out for the sprints to see how we have been doing towards everybody else and wished our video games to be proven on the stream.
Sprints Aren’t All the pieces
This brings us into the subsequent level that sprints aren’t all the pieces and don’t matter an excessive amount of. You undoubtedly ought to give attention to the principle occasion itself and never getting too caught up in making your bot excellent for the sprints.
As well as, when you don’t find yourself making it that far within the sprints it’s not the tip of the world. We by no means received our video games proven on the dash stream and certaintly didn’t win them. At the very least our 12 months it felt just like the bracket we have been in (US faculty) was comparatively small and when you don’t find yourself making it tremendous far within the dash it’s not over to your workforce and you’ll nonetheless qualify for the finals.
Scrimmage a Lot
As I discussed earlier, it’s tremendous troublesome to inform in case your new bot is healthier than the earlier model since you may solely domestically take a look at your bot towards different variants of code that you’ve got and in numerous instances it’s not tremendous useful to check towards your earlier variations of your bot as a result of in the event that they’re too similiar they’ll usually draw lots otherwise you’ll see that the change you made is healthier in some conditions and worse in others.
This is the reason it’s necessary to scrimmage numerous different groups to see in case your adjustments are literally higher than earlier than towards actual opponents that you just’ll doubtless face in ranked video games and the tournaments.
It didn’t cross my thoughts till the tip of the event when one other workforce shared that they did this, however I feel it’s a extremely good thought to make a script to mechanically queue video games towards different groups and with some kind of analysis to see in case your adjustments are literally higher than earlier than. One script is linked right here
Even when you don’t have a script to mechanically do that, it’s nonetheless necessary to manually queue video games towards different groups to see in case your adjustments are literally higher than earlier than with the adjustments you made. We didn’t and we nonetheless queued 818 complete unranked scrimmaged which is the eighth most of any workforce. And seventh most complete scrimmages with 1,294 in complete.
Watch Your Video games
One of many downsides to an automatic script to queue video games is that you just in all probability wouldn’t be watching your video games as usually. It’s tremendous invaluable to look at the video games that you just’re taking part in to see what conditions your bot is doing properly in and what conditions it’s doing poorly in.
This provides you numerous details about what you may doubtlessly do to enhance your bot and your methods.
Watch the Different Groups
It’s additionally equally necessary to look at the opposite groups video games (particularly if they’ve auto scrimmages off) to see what methods they’re utilizing and the way their bot is doing.
In the event you see a workforce that’s doing significantly better than you otherwise you see some fascinating methods that they’re utilizing, don’t be afraid to steal and increase their methods to suit your bot doubtlessly even higher than how they’re utilizing it.
Use Neighborhood Sources
The Battlecode group is tremendous useful and there’s numerous sources on the market that will help you get began and enhance your bot. There’s numerous groups which have shared their code and techniques from earlier years and it’s a good way to get began and see what different groups are doing.
As well as there’s numerous code from earlier years that additionally works yearly and is a good way to get began and see what different groups are doing. For instance: shared array communication, pathfinding, and many others. All of those are issues that you could find in earlier years code and use in your bot.
Recommendation to New Individuals
It may be overwhelming to get began with Battlecode as a result of there’s numerous issues to find out about: the sport specs, the competition construction, methods, limitations on the sport, the way to talk between your geese, operating video games domestically, and many others.
To not point out that it may be troublesome to begin writing code as a result of it’s a must to get used to what strategies exist and what actions you may carry out and what data you may and can’t entry and may be troublesome to work with. I undoubtedly extremely counsel that you just use an IDE with plenty of autocomplete so you may preview all out there strategies. It feels lots like working with a library like Pandas the place there’s numerous strategies and it’s a must to get used to what’s out there and kind of looks like one other language solely.
It undoubtedly pissed off me at the beginning as a result of I simply wished so as to add some fundamental options however as a mixture of: our challenge not being tremendous organized, me not being aware of the sport, or the way to write code for it, it was fairly annoying to get began. Nevertheless it’s value it to push by way of and get began as a result of it’s numerous enjoyable and also you’ll study lots.