MIT Battlecode Reflections: A First-Time Finalist’s Takeaways | by David Teather | Nov, 2024

Key Takeaways from a First-Time Contestant

Picture created by the creator, utilizing Battlecode’s Engine

I used to be dragged into Battlecode 2024 by certainly one of 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 general. So I figured I’d write a weblog publish concerning the expertise from the angle of a primary time participant.

This was cross-posted from my weblog, so in the event you’re examine extra on the market.

Battlecode is a contest the place you write code to regulate a staff of robots which have to perform some job. You possibly can learn extra about it right here.

This yr the sport was a duck based mostly seize the flag recreation the place our purpose was to seize all 3 of the flags from the opponent. The sport is performed on a grid and every staff 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 appears like

Instance match inside the Battlecode’s Engine, created by the creator

There’s so much occurring in that gif so let’s break it down a bit bit.

Every duck has a couple of actions to assist assault or defend flags that it may carry out every flip/spherical (proven on the backside of the gif above). Every motion has a selected cooldown so you possibly can’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 may respawn at one of many staff’s spawn factors after ~20 rounds

There’s additionally a forex referred to as crumbs which you could get from two methods: a couple of spawn on the map every spherical which you’ll choose 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 completely different conditions. Possibly you need some geese to deal with 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 measurement shared array that every duck can learn and write
    – That is the one method to talk info between your geese concerning the state of the sport
    – This implies it’s a must to be strategic about what info you retailer within the array and the way you employ it, and if in case you have all of your geese performing the identical motion based mostly on the identical info 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
    – Which means that every flip every duck will get to execute a sure variety of actions, calculations, and reminiscence reads and writes.
    – In order for you 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 massive and apparently in earlier years its been a lot smaller

I additionally really feel prefer it’s actually arduous to inform in case your new bot is healthier than the earlier model, like normally it’s higher in some conditions and worse in others. In contrast to most software program I’m used to engaged on it’s straightforward to inform if what you’ve made is healthier than earlier than since in the event 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 effectively and simply ended up scrapping the adjustments. There’s so many branches for useless options that simply didn’t work out.

Okay so now that we’ve got a primary understanding of the sport, let’s speak concerning the precise competitors.

Leaderboard

The leaderboard is without doubt one of the most necessary elements of the competition. It’s a semi-live leaderboard that reveals the rankings of all of the groups within the contest. Each 4 hours there can be ranked video games performed in opposition to your relative neighbors on the leaderboard and in the event you win you achieve extra factors and in the event you lose you lose factors.

Moreover, you possibly can manually queue your individual ranked video games in opposition to different groups so long as they permit it. That is necessary as a result of you possibly can’t simply queue video games in opposition to the identical staff again and again to farm factors.

Nonetheless, you too can queue unranked video games in opposition to any staff which is necessary for testing your bot in opposition to different groups to see if the adjustments you made are literally higher than earlier than in opposition to 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 in opposition to different groups and it’s additionally a great way to see how the opposite groups are doing.

The seeding of your staff within the sprints is predicated in your leaderboard rating, so in the event you’re doing effectively 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 staff within the tournaments is predicated in your leaderboard rating, so in the event you’re doing effectively within the leaderboard you’ll be seeded increased within the tournaments.

In our yr the highest 12 US school 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 match.

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 number of the methods we used. This part goes to be a bit bit blurry as a result of I’m scripting this ~2 months after the competition and I don’t keep in mind every part we did.

Our Methods

We had a couple of completely different methods and variations of our bot throughout the match that we tried out particularly as the sport acquired tweaked for steadiness and we discovered extra concerning the recreation.

  • Setup Section
    – 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 may 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 attempting to fill the water or undergo on the diagonals which made certain that the enemy geese couldnt fully swarm our base directly and it made them funnel via a couple of 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 may name different geese to return 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 observed that usually our bots would get caught surrounding the duck with the flag and stopping it from actually shifting anyplace so we applied a flag passing technique the place it will go the flag to a different duck nearer to the place the flag wanted to be captured
    – We added this beautiful late within the match and it labored fairly effectively.
  • 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 service was
  • Pathfinding
    – We took code from a earlier yr’s bot for doing BugNav pathfinding however of their yr 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 unhealthy that within the sprints we might typically get caught attempting 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 occasions 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 most likely been extra cautious about this since for lots of the match 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 combating.
    – I did make this a bit bit extra effectively balanced between healers and fighters on the finish however I feel that is one thing we must always’ve completed 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 typically 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 situation 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 in a position to transfer their flags across the map in simpler 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

Yow will discover our code right here

I simply thought this may be enjoyable to incorporate however to qualify for the finals we needed to win this match and we have been fairly excited once we did. Right here’s a video of us profitable the match

Us profitable the qualifying recreation on livestream. Video by the creator

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 under that’s cool too.

The Expertise

The finals have been held at MIT in Boston, and all of us acquired 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 folks we met there via being within the dorms. Though, if I have been to do it once more I might’ve most likely stayed in a resort as a result of the dorms are effectively ya know school 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 acquired to satisfy the opposite groups and the organizers. It was fascinating to see the place everybody was from as well as we acquired 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 general and it was already an enormous accomplishment to make it this far.

Additionally they ran a estimathon the place we needed to guess a variety on some query and I ended up profitable a duck from accurately estimating the variety of MIT dorm residents that provided to host groups for the finals. Right here’s a video of me catching the duck:

Video from the Battlecode Finalist Livestream

Enjoyable truth is that I put a pocket book underneath my jacket once I earlier than I went strolling up there so the video appears kinda foolish due to how I’m strolling 😭

I’m undoubtedly not an knowledgeable on Battlecode, however I do have a couple of 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

Although that it initially may look like a reasonably easy recreation even in a yr the place our recreation 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 evaluations, 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 match as we needed to repair bugs and simply attempt to perceive what our code was doing. That is one thing that I might recommend everybody invests extra time in particularly at first to arrange a superb basis and construction on your mission in an effort to construct on it and make adjustments extra simply.

A part of the explanation we didn’t do it’s because we felt we have been rushed to get options out for the sprints to see how we have been doing in opposition to everybody else and wished our video games to be proven on the stream.

Sprints Aren’t Every thing

This brings us into the following level that sprints aren’t every part and don’t matter an excessive amount of. You undoubtedly ought to deal with the primary occasion itself and never getting too caught up in making your bot good for the sprints.

As well as, in the event you don’t find yourself making it that far within the sprints it’s not the tip of the world. We by no means acquired our video games proven on the dash stream and certaintly didn’t win them. No less than our yr it felt just like the bracket we have been in (US school) was comparatively small and in the event you don’t find yourself making it tremendous far within the dash it’s not over on your staff and you may 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 possibly can solely domestically check your bot in opposition to different variants of code that you’ve got and in numerous circumstances it’s not tremendous useful to check in opposition to your earlier variations of your bot as a result of in the event that they’re too similiar they’ll typically draw so much 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 in opposition to actual opponents that you just’ll probably face in ranked video games and the tournaments.

It didn’t cross my thoughts till the tip of the match when one other staff shared that they did this, however I feel it’s a extremely good concept to make a script to robotically queue video games in opposition to different groups and with some form of analysis to see in case your adjustments are literally higher than earlier than. One script is linked right here

Even in the event you don’t have a script to robotically do that, it’s nonetheless necessary to manually queue video games in opposition to 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 whole unranked scrimmaged which is the eighth most of any staff. And seventh most whole scrimmages with 1,294 in whole.

Watch Your Video games

One of many downsides to an automatic script to queue video games is that you just most likely wouldn’t be watching your video games as typically. It’s tremendous beneficial to look at the video games that you just’re taking part in to see what conditions your bot is doing effectively in and what conditions it’s doing poorly in.

This offers you numerous details about what you possibly can probably 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 staff that’s doing a lot 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 probably even higher than how they’re utilizing it.

Use Neighborhood Sources

The Battlecode neighborhood 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 an effective 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 an effective way to get began and see what different groups are doing. For instance: shared array communication, pathfinding, and so on. All of those are issues that yow will discover 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 study: the sport specs, the competition construction, methods, limitations on the sport, how you can talk between your geese, working video games domestically, and so on.

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 possibly can carry out and what info you possibly can and can’t entry and will be troublesome to work with. I undoubtedly extremely recommend that you just use an IDE with plenty of autocomplete so you possibly can preview all accessible strategies. It feels so much like working with a library like Pandas the place there’s numerous strategies and it’s a must to get used to what’s accessible and form of appears like one other language fully.

It undoubtedly annoyed me initially as a result of I simply wished so as to add some primary options however as a mix of: our mission not being tremendous organized, me not being acquainted with the sport, or how you can write code for it, it was fairly annoying to get began. However it’s price it to push via and get began as a result of it’s numerous enjoyable and also you’ll be taught so much.