This guide assumes you have a basic understanding on MuleSoft and DataWeave. As well as basic knowledge on git and GitHub repos.
This is intended as a short guide. For detailed instructions please see this article.
Step 1: Create your own repo
Go to the official list of Starter Projects in the Battlesnake docs.
Scroll down until you see the Mule and DataWeave Battlesnake Quickstart project and click on it.
You can use that repo to either fork or use as template for your own repo.
Step 2: Modify the basic setup
Once you forked/created from the template your own repo, you need to modify the code to make your own snake.
Go to
src/main/resources/application.yaml
and modify the properties to match your own settings. See the docs for more information.Go to
src/main/resources/dw/move-snake.dwl
to find the DataWeave code you will use to create the snake logic.
%dw 2.0
output application/json
var body = payload.you.body
var board = payload.board
var head = body[0] // First body part is always head
var neck = body[1] // Second body part is always neck
var moves = ["up", "down", "left", "right"]
// Step 0: Find my neck location so I don't eat myself
var myNeckLocation = neck match {
case neck if neck.x < head.x -> "left" //my neck is on the left of my head
case neck if neck.x > head.x -> "right" //my neck is on the right of my head
case neck if neck.y < head.y -> "down" //my neck is below my head
case neck if neck.y > head.y -> "up" //my neck is above my head
else -> ''
}
// TODO: Step 1 - Don't hit walls.
// Use information from `board` and `head` to not move beyond the game board.
// TODO: Step 2 - Don't hit yourself.
// Use information from `body` to avoid moves that would collide with yourself.
// TODO: Step 3 - Don't collide with others.
// Use information from `payload` to prevent your Battlesnake from colliding with others.
// TODO: Step 4 - Find food.
// Use information in `payload` to seek out and find food.
// food = board.food
// Find safe moves by eliminating neck location and any other locations computed in above steps
var safeMoves = moves - myNeckLocation // - remove other dangerous locations
// Next random move from safe moves
var nextMove = safeMoves[randomInt(sizeOf(safeMoves))]
---
{
move: nextMove,
shout: "Moving $(nextMove)"
}
The starter project gives you a basic script where your snake won't move towards its own neck, but you still have to create the code to avoid hitting walls, avoid hitting your own body, find food, etc.
Step 3: Deploy your Mule API
To try out your snake, you have to expose the API in a public URL that can be accessed by Battlesnake. You can deploy the API directly to CloudHub, or do it locally with tools like ngrok
for quicker testing.
Deploy to CloudHub
You can deploy your API directly from Anypoint Studio to your Anypoint Platform account. You can create a free Anypoint Platform trial account here.
Tip:
If your Anypoint Platform account expires, you can create a new account using the same details as before (name, email, phone, etc.) and just changing your username.
Once you deploy your Mule app, extract the URL. We will use this to connect the snake in the next step.
Example:
http://amartinez-mule-battlesnake.us-e2.cloudhub.io
Deploy locally with ngrok
Deployments to CloudHub can take a lot of time. If you want to be agile and deploy locally to reflect your changes in less time, you can use a tool like ngrok
to get a public URL to your local app.
- Install ngrok.
- Run your Mule application locally.
- Run
ngrok http 8081
. - Retrieve the public URL.
Example:
https://c0cb-2001-1970-5425-e000-00-7e86.ngrok.io
Step 4: Create your Battlesnake
- Create an account at play.battlesnake.com.
- Go to the Battlesnakes tab.
- Click on New Battlesnake.
- Select your snake's name and choose the engine region closest to you.
- In URL, paste the URL you generated with CloudHub/ngrok and add
/api
to the end of the URL. For example,http://...cloudhub.io/api
- In tags, add
MuleSoft
andDataWeave
. - Finish adding the rest of the settings and click Save.
Your Battlesnake should now be created.
Step 5: Create a new game
Now you can start playing with your battlesnake!
- Go to the Create Game tab.
- Leave the default options selected and add your new battlesnake to the game by clicking Add to Game next to your snake.
- Click on Start Game.
Voila!
Additional resources
I have been streaming some Battlesnake live streams and publishing them in this YouTube playlist if you want to take a look.
You can also follow me on Twitch to see me coding live!
Top comments (0)