Writing a bot
What to build on
We assume you've already went through the Getting Started guide on downloading and compiling the server software as well as the Running the Server guide to starting the server, creating matches, and running Swordfish. The next thing you'll want to do is to look through com.indianapokerbot.bot.PokerClient, which will be your primary model for writing a poker client in Java. We recommend that you subclass PokerClient without modifying it.
Creating a project
Make sure that the IndianaPokerBot project is open in NetBeans (i.e. visible in the Projects pane). Create a "Java Application" project in NetBeans called "TestProject". You don't need a main class. Once the project is created, open its Properties and add IndianaPokerBot as a library (note that you need to have built IndianaPokerBot for this to work).
Let's create a new class called MyFoldingBot that simply folds every turn. Write something like this:
// We need to import this in order to extend it
import com.indianapokerbot.bot.PokerClient;
// We are inheriting the provided PokerClient
public class MyFoldingBot extends PokerClient {
// Our constructor simply passes along the IP and
// port to PokerClient's constructor
public MyFoldingBot(String ip, int port) {
super(ip, port);
}
// We also need to change main to construct a
// MyFoldingBot instead of a PokerClient
public static void main(String[] args) {
log("starting...");
if(args.length < 2) {
System.err.println("Expecting server ip and server port.");
System.exit(1);
}
try {
int port = Integer.parseInt(args[1]);
MyFoldingBot client = new MyFoldingBot(args[0], port);
client.run();
} catch(NumberFormatException ex) {
System.err.println("Invalid port");
System.exit(1);
}
}
}
Compare this classes's main method with PokerClient's main method and you'll see that not much has changed. Now, we need to override handleStateChange. HandleStateChange is your main gateway to reacting and responding to events in the game. Whenever something happens (either a player makes a move or community cards are dealt), handleStateChange is called. Let's add in our own handleStateChange:
public void handleStateChange() throws IOException, SocketException {
// Make sure that we're not sending actions when it's not our turn.
if(isItMyTurn())
sendFold();
}
Running your bot
When you build your project, NetBeans will create a JAR in your project's dist directory. In this example, it would be TestProject/dist/TestProject.jar. Go ahead and copy that JAR into a new folder GlassFrog/bot/. In that same directory, create a script called startme.sh with the following text:
#!/bin/bash # This starts MyFoldingBot in TestProject.jar java -cp "/path/to/IndianaPokerBot.jar;TestProject.jar" MyFoldingBot $1 $2
Note that we provide two JARs for the classpath. Since MyFoldingBot depends on com.indianapokerbot.bot.PokerClient, you need to tell Java where to find PokerClient. This startme.sh script needs to be executable, so you need to run chmod +x startme.sh on it. Otherwise, the server will throw an error when it tries to run that script:
java.io.IOException: Cannot run program "/home/user/indianapokerbot/GlassFrog/bot/startme.sh" (in directory "bot"): error=13, Permission denied
So, you'll have GlassFrog/bot/TestProject.jar and GlassFrog/bot/startme.sh. Now, you can play against your bot by switching to the GlassFrog directory and running tools/matchConstructor.py "myRoom" 5 2Player.limit.gamedef.xml 42 AAAIPLAYER MyBotName 1000 LOCAL bot/startme.sh (a five-round game). Play against it by starting Swordfish separately.


