Halite Home

How to submit a bot in any language


#1

I decided I wanted to write a bot in Kotlin, but alas it is not currently supported. Not to be deterred, I did some digging into the compilation and run process for different languages.

I had the realization that I could use a Python bot to bootstrap my bot in another language.

Node Example

JavaScript should be supported as a first class language soon. But until it is...

install.sh

#!/bin/bash

# fail on any error
set -e

# cd to the directory of the script
cd "$(dirname "$0")"

# use curl to download any runtime dependencies
# NOTE: it looks like `.tar.xz` isn't supported. Use `.tar.gz` instead
curl -sL https://nodejs.org/dist/v7.1.0/node-v7.1.0-linux-x64.tar.gz | tar xz

NODE="$(ls node-*/bin/node)"
NPM="$(ls node-*/bin/npm)"

# run npm install to download dependencies from package.json
$NODE $NPM install

MyBot.py

from subprocess import call
from glob import glob
import os
import sys

# cd to the script directory
os.chdir(os.path.dirname(os.path.realpath(__file__)))

# look for the node executable
nodebins = glob('node-*/bin/node')
if len(nodebins) == 0:
    # if it isn't found we're probably running locally
    nodebin = 'node'
else:
    nodebin = nodebins[0]

# call the script with the relative path to node and hook up stdin, stdout, and stderr
call([nodebin, 'JavaScriptBot.js'], stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)

What about JVM languages?

MyBot.jar is now supported. The following example is retained for other who might want to follow it for additional languages

A few of them are already supported, like Scala and Java. But what about others? What if we could execute a jar (first class support for this should be coming soon too).

I decided to use Gradle as my build system. We won't need an install.sh script because we will compile our code into jars before we upload the bot. If your language has a platform specific compilation step, you'll need to do your compilation in an install.sh script.

Step by step

Initialize your project with gradle init.

build.gradle

plugins {
    id 'java'
    id 'application'
    // kotlin support
    id 'nebula.kotlin' version '1.0.5'
}

repositories {
    mavenCentral() // or jcenter()
}

dependencies {
    // include any extra dependencies here
}

mainClassName = 'com.nmalaguti.halite.KotlinBot'

settings.gradle

rootProject.name = 'MyBot'

And layout your files like

kotlin/
β”œβ”€β”€ build //this will be generated by gradle
β”‚   └── distributions
β”‚       └── MyBot.zip
β”œβ”€β”€ build.gradle
β”œβ”€β”€ gradle //generated by gradle init
β”‚   └── wrapper
β”‚       β”œβ”€β”€ gradle-wrapper.jar
β”‚       └── gradle-wrapper.properties
β”œβ”€β”€ gradlew
β”œβ”€β”€ gradlew.bat
β”œβ”€β”€ runGame.sh
β”œβ”€β”€ settings.gradle
└── src
    β”œβ”€β”€ dist
    β”‚   └── MyBot.py // see details below. Include it in src/dist
    └── main
        β”œβ”€β”€ java
        β”‚   └── com
        β”‚       └── nmalaguti
        β”‚           └── halite // put the default Java starter files here
        β”‚               β”œβ”€β”€ Direction.java
        β”‚               β”œβ”€β”€ GameMap.java
        β”‚               β”œβ”€β”€ InitPackage.java
        β”‚               β”œβ”€β”€ Location.java
        β”‚               β”œβ”€β”€ Move.java
        β”‚               β”œβ”€β”€ Networking.java
        β”‚               └── Site.java
        └── kotlin
            └── com
                └── nmalaguti
                    └── halite
                        └── KotlinBot.kt // my custom bot!

MyBot.py

from subprocess import call
import os
import sys

# cd to the script directory
os.chdir(os.path.dirname(os.path.realpath(__file__)))

# the gradle `application` plugin generates a script to run your in `bin`
call(['./bin/MyBot'], stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)

Build your project with ./gradlew distZip and upload it from build/distributions/MyBot.zip!

The only downside to this approach is that your bot will show up as a Python bot instead of your actual language.

PyPy Example


Using PyPy for Python bots
#2

As of half an hour ago, you can use JAR files. Just include a MyBot.jar in your bot's zip file, and it will be run with java -jar MyBot.jar. This should allow you to submit a bot in any JVM language.


Can I use SBT to compile bot?
#3

Javascript is now supported as a first class language. Thank you for the PR @nmalaguti.


#4

You can now set your language to whatever you like, so you don't have to be marked as a Python bot. Just add a LANGUAGE file to the root directory of your zip submission and put the name of your language in there.


Can I use SBT to compile bot?
Tracking Updates and New Features