Skip to content
olcai edited this page Feb 21, 2013 · 2 revisions

Crazy Snake - Jfokus Edition, Public API

The public API is now released and you can start implement your brains! Please be sure to check back in from time to time to see updates and when the upload site is open. You can also follow us @citerus_se on twitter to get notified, check for #crazysnake.

For general information about the contest, see citerus.se/jfokus. Watch a video of a match here.

News

Spurt prizes (spurtpriser) will be award the current king of the hill Tuesday at 10.45 and 18.15 and on Wednesday at 12.15! Be sure to upload your first version in time!

Upload is now open, upload your brains at http://crazysnake.citerus.se/upload

Update to rules, this is how snake death is handled by the game: "If both snakes die during the same turn in the game, the heat will be considered a draw."

The Crazy Snake - Jfokus Edition test game engine is here!

You can now test your snake brain using crazysnake_tester:

  1. Download crazysnake_tester.zip.
  2. Unpack the zip-file, cd to the created dir and run ./start.sh (on mac/linux) or start.bat (on windows). You should now see a match start between two brains "Paak0" and "Paak1".
  3. To test your own brain, drop it under ./brains and restart the game. Keep at least one of the Paak-brains to to have an opponent.
  4. Look in start.sh/bat and alter the turns-value to make matches longer or shorter. Note that the number of turns used during Jfokus may be hanged during the venue. You can also alter the delay-ms time to make the game run faster or slower for testing purposes. This only affects the way the game is displayed, but don't set a value to small for your computer to handle.

Keep in mind that the tester is obfuscated and refactored compared to what will be used during Jfokus, so don't spend time trying to reverse engineer it to gain an advantage. We all benefit from fair play!

Also, the rate of which fruits are presented may be altered, so don't optimize your brain for a certain rate o fruit deliveries!

If you have problems getting it all to work or find serious issues, don't hesitate to contact [email protected]!

The upload site will open up on monday afternoon, make sure to check back here and to follow @citerus_se #crazysnake, to get the latest news.

Happy coding every one, and may the best snake win!

Prerequisites

The public API consists of a Maven project with the API and an example project that implements a simple snake brain. To follow this guide you need the following:

  • JDK6
  • Maven 3
  • A Mercurial clone of the source directory or a downloaded zip with the source code (hit the get source button up to the right).
  • A terminal or command prompt. Not required, but preferred and assumed in the remainder of this wiki.

Important

Before you come to Jfokus for the competition, make sure to run the builds of the provided example brains as described below. Maven requires quite a lot to be downloaded to build the projects and this will make the WLAN at Jfokus more available for other tasks.

It is important that you choose a globally unique identifier for the snake brain you are about to implement. The identifier will be the fully qualified name of your implementation class. So please choose package and class name with care, otherwise your upload might be rejected!

Getting Started

The easiest way to implement a brain is to follow the steps below:

  1. Unzip the source code or clone the repository to <SOME-PATH>/crazysnake-api
  2. Open a terminal window and cd to the crazysnake-api directory
  3. Enter the following at the terminal: ./install_api.sh, this will build the api Maven project.
  4. To create a new brain in Java using a maven archetype, run ./generate_brain. You will be prompted with the following:
    • groupId - enter a unique package like identifier for your team/company or organisation (e.g. com.mycompany.crazysnaketeam)
    • artifactId - enter the name of your brain project (e.g. myfirstbrain)
    • version - enter a version or just hit enter to leave it with the default
    • package - enter a package name or use the same as your groupId (default, recommended)
    • brainName - enter the name of your brain class (e.g. MyFirstBrain)
    • Confirm your choices by pressing "y" and hit enter, this will generate a new maven project. Once done, cd to ./<your artifactId> and the issue mvn install, this should build your project and hopefully show "SUCCESS" at the bottom line! If so, you can find your brain implementation jar under ./<your artifact id>/target which can be used by our game engine
  5. To build a Brain in Scala, Clojure or Groovy, cd to crazysnake-api/<lang>-brain of your choice.
    • Open up crazysnake-api/<lang>-brain/pom.xml in a text editor of your choice. Change any entries with your-team-name-here to your team name of choice.
    • Enter the following at the terminal: mvn clean install
    • You should now have a complete brain, packaged as a jar-file in crazysnake-api/<lang>-brain/target/your-team-name-1.0-SNAPSHOT.jar This resulting .jar-file can be used by our game engine to incarnate a snake with your brain and make it part of the game.

General development information

General rule - Play nicely!

By this we mean that nobody will be happy if you try to make the game run bad by for example doing something like:

  • Calling System.exit()
  • Spawning new threads
  • Write to disk
  • Allocate a huge amount of memory
  • And so on...

If you feel hesitant about something being allowed or not, it is possible to ask us but most likely the answer will be that it is not allowed to do any funky stuff.

If you fail to adhere to these rules, or if your brain is running bad for any other reason, we reserve the right to remove the brain implementation from the game at any time. We will also reserve the right to inspect the code of the winning brain before accepting it as a winner. We will also reserve the right to publish the code of your brain implementation.

How to implement getNextMove()

We recommend that you implement your brain by extending the BaseBrain class we have provided for you. It implements the Brain interface and provides you with some facilities that might come in handy. BaseBrain leaves one method unimplemented namely getNextMove(). Below is an example of a simple implementation with some comments to give you a hint on how you can retrieve information about the ongoing game. Your brain will have a dedicated thread of execution for a specified amount of time (50 ms) when getNextMove() is called. If your brain does not respond within this timeout it will automatically move forward until it is responsive again.

Please look into the javadocs and source of the public API to dig deeper into what you can do to implement a killer snake!

#!java
public class MyBrain extends BaseBrain {

    @Override
    public Movement getNextMove(HeatState state) {
		
	// The meta member from BaseBrain has static information about the heat.
	Dimension boardDimensions = meta.getBoardDimensions();
		
	// You can get information about your opponent snake like this.
	Snake opponent = getOpponent(state);
    	
    	// HeatState contains the current dynamic state of the heat and has getters for what you need to know.
	// E.g. the positions of all fruits on the board.
    	List<Position> fruitPositions = state.getFruitPositions();
    	
    	// You tell the game how you want your snake to move by returning a Movement
    	return Movement.FORWARD;	
    }   
}

Changing the brain's class name

Inside the crazysnake-api/<lang>-brain project is a src-directory that contains the brain implementation. If you wish to change the class-name of the brain you must ALSO modify crazysnake-api/<lang>-brain/src/main/resources/META-INF/services/se.citerus.crazysnake.Brain so that the line inside this file contains the name of your Brain-implementation.

Using an alternative language

Currently, we have successfully implemented brains in Java, Scala, Clojure and Groovy. We will provide example projects for these languages, but you are free to use any language you like that will run on the JVM.

Runtime information

During runtime, we will provide some third-party libraries and language libraries for your convenience. We will not support bundling of other third-party libraries. Since the libraries are provided by our game engine, you should not bundle these inside your jar file. Use the example brain-projects provided and you will be fine.

The following libraries are provided at runtime:

Java libraries

  • commons-lang.commons-lang (2.5)

Clojure

  • org.clojure.clojure (1.2.1)
  • org.clojure.clojure-contrib (1.2.0)

Scala

  • org.scala-lang.scala-library (2.8.1)

Groovy

  • org.codehaus.groovy.groovy (1.7.10)

The rules of the game

This is an overview picture of the rules of the game. For details, consult the Javadoc.

How the game is won

The game is played in matches and will be continuously played during Jfokus. The game type is "King of the Hill", which means that the Snake (or snake brain) that stands as King of the Hill at the end of the venue is the winner.

To be King of the Hill, your brain will have to win a match against the current King of the Hill. A match is played in best of at least 3 heats. The snake that eats the most fruit and stays alive wins a heat. Each fruit gives the snake 1 point. If the number of points are even between the two snakes in a heat it is considered a draw, and thus the match will continue until a sole winner can be decided upon.

Collisions

When the head of a snake collides with either another snake, its own tail or a wall it is considered a lethal collision and the snake will die. When a snake dies, it will automatically lose the heat. If both snakes die during the same turn in the game, the heat will be considered a draw.

When a snake collides with a fruit, it is eaten and the snake scores a point.

Uploading your brain

Once you have a jar file with a great Brain inside, you can upload it over the Internet to make it being queued for challenging the current King of the Hill. Once in the game, your brain will have three tries to beat the King of the Hill. After three consecutive loses, it will be automatically removed from the game queue. If you update the brain however, or if you win a match, you will have three new tries. To update a brain, just upload a new jar which contains the same implementation (package + classname must match) in a new improved version.

The upload page is not available yet, but will be a few days before Jfokus start.

Contact

If you have further questions, please send an email to [email protected].