Battlesnake for Swift Starter Project

I've only just discovered that Battlesnake is a thing!

I'm going to be attending an event in a couple of weeks that might have a local battle (if there are enough folks interested), and I am very interested in participating, but there is no battlesnake starter project for Swift! (See that link for a list of languages/environments there are starter projects for already.)

I'm going to take a stab at creating one, and I'd love to have a collaborator or two. (I feel like this would be something relatively easy for someone who already writes Swift server-side applications to "whip up", am I wrong?)

My first priority will be getting it to work locally. Deployment is a secondary concern.

Anyone interested?

I guess the first question is what framework to use. I've only played with Vapor before. Is that overkill for this kind of thing? Performance is important... maybe it makes more sense to start from scratch... :thinking:

11 Likes

Interesting!

I’d say: let’s start with Vapor and see what happens. The performance is dependent on so many factors that without data, optimizing is difficult anyway.

Just looked through the JS example project. That should be easily translated to Swift/Vapor.

I’ll give it a go tonight.

OK, so I ported the JavaScript starter project to Swift. Please have a look.

Note: the AI/movement logic is in Movement.swift. The "todo's" (next steps for making a better snake) are in MoveTests.swift. I.e. you can improve in a TDD way.

Only tested locally, no time to deploy to a real server.

5 Likes

This is amazing! Great work Maarten! I'll be checking it out soon, for sure!

Love to see it. Battlesnake brings back many fond memories from my time in Victoria. Are you taking part in the hackathon (are they even still doing that?) or just playing with it?

The objectives I want to achieve with this are:

  1. Demonstrate Swift as a general purpose, high performing language that is easy to use also for AI related workloads like this by providing a simple yet extensible starter project;
  2. Practice how to write effective tests where emerging behavior is expected. I.e. as the snake becomes smarter, I expect it to surprise me with its moves;
  3. Practice writing effective tests in a server environment.

Note: these (esp 2 and 3) are more or less personal objectives. In the mean time if there are other/better ideas, please let us know.

Was excited first, but realised it's more about creating some optimised collision algorithm?
Not sure then what could be the learning purpose with Swift. Maybe adding some ML stuff? :thinking:

I think the benefits would be similar to any other coding challenge (like advent of code). But coding challenges are more like puzzles. The only "game" aspect is usually in how fast you are able to solve it.

I really like the idea of games that require you to code. (The only other one I know about for Swift is Swift Playgrounds.)

Screeps is a much more involved game that works with anything you can compile to WASM.

There are other games as well that are more restrictive on languages, but are mostly targeted at schools, like CodeCombat

There is even a game now to teach about Git!

3 Likes

The Swift Starter project has been added to the Community Starters on the Battlesnake website: Starter Projects | Battlesnake :tada:

4 Likes