Welcome to Jaige

Competition
· AI Match Results
· Upload Your AI

Project
· Games
· Project Page
· Download
· Installation
· Report a Bug
· Feature Requests
· Help Wanted

References
· Environment Setup
· Writing an AI
· FAQ
· Javadocs

Writing an AI for Hex

The Quick Guide:

Extend: HexAI

Override the following methods:
voidinitialize()
voiduninitialize()
String getAIName()
String getAuthor()
String getInfo()
Version getVersion()
void processTurnEvent(TurnEvent event)

This is the board:
protected Cell[][] cells

Call this method to make your move:
protected void makeMove(int x, int y)

Check this to see what direction you should be going:
Orientation getOrientation()

Place the class files or a jar file in the plugins directory inside the Jaige installation directory.

The Detailed Guide:

The first step is to extend HexAI. You must have the Jaige jar file in your classpath. This can be done with the -classpath flag, or by including it in your IDE’s libraries. Below is the list of methods and fields that you will have to use or override for your AI to work properly.

Player - Every Jaige game uses this class to represent a player.

Orientation getOrientation()
This is important when creating your AI. Hex uses HORIZONTAL and VERTICAL to determine what direction the bridge needs to be built.

Color getColor()
This is not essential. You can override this method to always be the same color. Otherwise your color will be the color set in the Game Options in the Preferences panel.
void setName(String name)
This sets the name of your Player. Calling super() sets this to getAIName() automatically for you. It can be set to something different if you like, and is used in the GUI.

Implementing TurnListener
void processTurnEvent (TurnEvent event)
Implement this method to know when it is your turn. The event manager will only call this method when it is your turn, so there is no need to check to see who’s turn it really is.

AI - This class is used to identify that the Player is an AI in Jaige. Please use the initialize method to construct your fields and load your AI. Loading 100 AI implementations that each load up a large amount of memory will kill the system. Therefore, only do this in the initialize method, and make sure that you remove your fields and any other memory the implementation uses in the uninitialize method.

String getAIName ()
Override this method! It identifies your AI with a name.
String getAuthor ()
Override this method! This lets you have your name associated with your AI. This is very useful if two different authors use the same name for their AI, like MyHexAI.

String getInfo ()
Override this method! This lets you put any additional information about your AI here. Your AI strategy might be something to include here.

Version getVersion ()
Override this method! This is used to determine the version of the AI playing. If you were to have 2 class files of the same class in your plugins directory, but with each having a different version, this will allow you to identify the differences. This is helpful for testing enhancements to your AI in newer versions. This may return a simple ‘new Version(1,0);’. Make sure you import it from jaige.*

protected void makeMove (int x, int y)
Call this to make your move!

HexAI
protected Cell [][] cells
These are the cells that represent the board. It is a 10x10 matrix. However, since the cells are hexagon shaped, movement between cells is not the same as if they were square. Understanding the relationship between each cell is a major piece of the puzzle. The cells have an X and Y coordinate that should reflect it’s position in on the board and in the matrix. It also has the Player that owns that space, and the color of the space. You can set the color for debugging purposes, but only with cells that have no Player associated with it. You cannot set the Player to yourself. It is cheating, and you will get an exception if you try. Note that if you find any other ways to cheat, please inform the Jaige development team.

Here is a sample AI called RandomAI. Note that it does not check it’s orientation, thus it does not have any shots at winning a match.
GeSHi © 2004, Nigel McNie
  1. package jaige.games.hex.ai;
  2.  
  3. import java.util.Random;
  4. import jaige.event.TurnEvent;
  5. import jaige.Version;
  6.  
  7. public class RandomAI extends HexAI {
  8.   private static final Version VERSION = new Version(1,1);
  9.   private Random rand;
  10.  
  11.   /**
  12.    * Constructs the AI.
  13.    */
  14.   public RandomAI() {
  15.     super();
  16.   }
  17.  
  18.   /**
  19.    * Called when the turn changes.
  20.    * In the case of an AI, this is only called when it is the AIs turn.
  21.    * @param event TurnEvent
  22.    */
  23.   public void processTurnEvent(TurnEvent event) {
  24.     int x = -1, y = -1;
  25.     while (x == -1 || y == -1 || cells[x][y].getPlayer() != null) {
  26.       x = rand.nextInt(cells.length);
  27.       y = rand.nextInt(cells.length);
  28.     }
  29.     makeMove(x, y);
  30.   }
  31.  
  32.   /**
  33.    * Returns information about this AI that the user might want to see.
  34.    * Typical things included in here are Author, background info, etc.
  35.    * @return String Information about this AI that the user might want to see.
  36.    */
  37.   public String getInfo() {
  38.     return "This Random AI just makes completely random moves.\n1.1 Moved to Hex game";
  39.   }
  40.  
  41.   /**
  42.    * Returns the unique name of this AI.
  43.    * It should be set to something that is unique enough that no other AI's could possibly use the same name.
  44.    * @return String The unique name of this AI.
  45.    */
  46.   public String getAIName() {
  47.     return "RandomAI";
  48.   }
  49.  
  50.   /**
  51.    * Returns the name of the author that created this AI.
  52.    * @return Authors name.
  53.    */
  54.   public String getAuthor() {return "Jaige Developers";}
  55.  
  56.   /**
  57.    * This is used to determine the version of the AI playing.
  58.    * This is helpful in testing a newer version of the AI against an older one.
  59.    * It also helps identify newer AI's when they are uploaded to a competative server.
  60.    * @return Version of this AI.
  61.    */
  62.   public Version getVersion() {
  63.     return VERSION;
  64.   }
  65.  
  66.  
  67.   /**
  68.     * Initializes this AI, this should be used to initialize the AI instead of the constructor.
  69.     * <b>The AI constructor should be empty!</b>
  70.     * Place initialization code in this method.
  71.     * Fields should be declared, but not initialized until this method.
  72.    */
  73.   public void initialize() {
  74.     rand = new Random();
  75.   }
  76.  
  77.  
  78.   /**
  79.    * Preforms any cleanup needed on the AI.
  80.    * This includes unloading any databases that may have been loaded.
  81.    * The idea is to make the memory footprint of this class as small as possible until it is needed again.
  82.    * The initialize() method will be called before this AI is called to action again.
  83.    */
  84.   public void uninitialize() {
  85.     rand = null;
  86.   }
  87. }
  88.  
Parsed in 0.050 seconds
Where to put your AI:
The simplest way to get your AI working is to place your class files in the plugins directory inside the Jaige installation directory. It is better to put you classfiles into a jar file. Directions on how to do this is located here. You can also use an IDE to do this (Eclipse requires the fatjar plugin). You may be able to use winzip, and name the output file with the .jer extension, although there are no promises that it will work.