Tutorials/TicTacToeBot

From CubeiaWiki

Jump to: navigation, search

Contents

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 mvn package firebase-bots:run
  • Go to http://localhost:8081 and fill out the form as in the screenshot below:

File:tictactoe-bot-screenshot.png

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 handleGamePacket 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 pom.xml file, under dependencies, add:

		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>


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 <configuration> part of the firebase-bots-maven-plugin, change the port to 8081 (or any other available port), like this:

<configuration>
	<botControlFile>bot.html</botControlFile>
	<botMenuName>Bot</botMenuName>
	<!-- CONFIGURE PORT HERE -->
	<serverPort>8081</serverPort>
</configuration>

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:

File:tictactoe-bot-screenshot.png

And then click start!. 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 aiclass. Change it like so:

<input type="hidden" name="aiclass" id="mode" value="com.cubeia.tictactoe.bots.TicTacToeBot"/>

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:

<configuration>
	<botControlFile>tic-tac-toe.html</botControlFile>
	<botMenuName>Tic-tac-toe Bot</botMenuName>
</configuration>
Personal tools