A framework for easily creating Kotlin and Java Battlesnakes
Out of the box, Battlesnake requires a fair amount of JSON/REST wiring before one can start authoring a snake. That initial exercise can prove problematic for some developers. This repo takes care of the wiring and communications and provides a simple framework for writing Battlesnakes in Kotlin and Java.
-
Define a snake as a subclass of AbstractBattleSnake.
-
Implement the two abstract classes of AbstractBattleSnake:
snakeContext()
andgameStrategy()
. -
Define a SnakeContext object to maintain state between game moves. The framework creates SnakeContext instances at the start of every game (one for each snake your server is supporting).
-
Define a GameStrategy object to produce responses for the
/
,/start
,/move
, and/end
requests. The framework creates a single GameStrategy instance when the server launches.
Examples of simple Battlesnakes created with this framework are here.
Use the Battlesnake_Template to clone the code for your own snake.
object ExampleSnake : AbstractBattleSnake<MySnakeContext>(){
// Called once during server launch
override fun gameStrategy() : GameStrategy<MySnakeContext> =
strategy(verbose = true) {
// DescribeResponse describes snake color and head/tail type
onDescribe { call: ApplicationCall ->
DescribeResponse("#ff00ff", "beluga", "bolt")
}
// MoveResponse can be LEFT, RIGHT, UP or DOWN
onMove { context: MySnakeContext, request: MoveRequest ->
RIGHT
}
}
// Called at the beginning of each game on Start for each snake
override fun snakeContext(): MySnakeContext = MySnakeContext()
// Add any necessary snake-specific data to the SnakeContext class
class MySnakeContext : SnakeContext() {
// Snake-specific context data goes here
}
@JvmStatic
fun main(args: Array<String>) {
run()
}
}
public class ExampleSnake extends AbstractBattleSnake<ExampleSnake.MySnakeContext> {
// Called at the beginning of each game on Start for each snake
@Override
public MySnakeContext snakeContext() {
return new MySnakeContext();
}
// Called once during server launch
@Override
public MyGameStrategy gameStrategy() {
return new MyGameStrategy(true);
}
// Add any necessary snake-specific data to the SnakeContext class
static class MySnakeContext extends SnakeContext {
// Snake-specific context data goes here
}
static class MyGameStrategy extends AbstractGameStrategy<MySnakeContext> {
public MyGameStrategy(boolean verbose) {
super(verbose);
}
// DescibeResponse describes snake color and head/tail type
@Override
public DescibeResponse onDescribe(MySnakeContext context, StartRequest request) {
return new DescribeResponse("me", "#ff00ff", "beluga", "bolt");
}
// MoveResponse can be LEFT, RIGHT, UP or DOWN
@Override
public MoveResponse onMove(MySnakeContext context, MoveRequest request) {
return RIGHT;
}
}
public static void main(String[] args) {
new ExampleSnake().run(8080);
}
}