Tutorials/TicTacToeBot

= Tic-tac-toe Bot = In this tutorial, we will create a bot for the tic-tac-toe game. There are many benefits of having bots, but the main one is for load testing.

For the Impatient
If you just want to get bots running and then reverse engineer it yourself, follow these steps:
 * Download this zip.
 * Unzip and in the terminal (at the root of the unzipped directory), run
 * Go to http://localhost:8081 and fill out the form as in the screenshot below:

Walkthrough
Here's how to create your own bot from scratch. It will take around 10 minutes.

First, we will use a Maven archetype that will create a skeleton bot project for you. Head over here and follow the steps: http://www.cubeia.com/index.php/blog/archives/425

Now, we have a file called BotImpl. Open that file and edit the method  so that it looks like this:

public void handleGamePacket(GameTransportPacket packet) { JSONObject json = JSONObject.fromObject(new String(packet.gamedata));

if ("act".equals(json.getString("action"))) { if (json.getInt("pid") == bot.getPid) { // Choose a random cell and try to place our cross or naught there. int cell = random.nextInt(9); System.out.println(bot.getId + " is playing cell " + cell); GameTransportPacket action = new GameTransportPacket; action.pid = bot.getId; action.tableid = table.getId; action.gamedata = String.valueOf(cell).getBytes; scheduleAction(bot, action); }		}	}

private void scheduleAction(final Bot bot, final GameTransportPacket action) { Action runnable = new Action(bot) { public void run { bot.sendPacket(action); }		};

AbstractAI.executor.schedule(runnable, 1000, TimeUnit.MILLISECONDS); }

It's probably pretty straightforward what's going on. We get the game data from the packet, convert it to JSON and then we look at the action to see if it's an "act" request, if so we check the "pid" (playerId) to see if we are supposed to act. If so, it chooses a random cell and tries to place his marker there. Of course this is a bit flawed since the bot will try to play cells that are already taken. The game will go on though because the server will just keep on asking until the bot plays a proper cell.

You also need to add an instance variable for the randomizer:

private Random random = new Random;

And finally, you'll need to add a dependency for the JSON library. In the  file, under , add: net.sf.json-lib json-lib 2.4 			 jdk15

Next, you need to make sure that there's a Firebase server running somewhere, which the bots can connect to. If you haven't already, you should follow the tic-tac-toe server tutorial for how to set that up.

If you're going to be running the bot server and Firebase on the same machine, you'll want to edit the pom.xml and change the port, since Firebase is already using 8080. Under the  part of the firebase-bots-maven-plugin, change the port to 8081 (or any other available port), like this: bot.html Bot 8081

Now, build your project and run the bot server with: mvn package firebase-bots:run

Then, browse to http://localhost:8081 and fill out the form as in the screenshot below:



And then click. You should now see the bots logging in, joining a table and starting to play.

Good luck with your bots!

Renaming Your Bot
If you want to rename your bot, you need to also edit the bot.html file. At the bottom of it there's a hidden field called. Change it like so: 

You can also change the name of the html file and the name of the bot in the menu on the web page by editing the configuration: tic-tac-toe.html Tic-tac-toe Bot