Skip to content

Tutorial: Creating a TicTacToe Bot

Mike Leonard edited this page May 21, 2016 · 9 revisions

Introduction

As an introduction to Merknera this tutorial will guide you through creating a bot to play Tic-Tac-Toe in JavaScript using node.js. The aim of this tutorial is to give you an introduction into creating a bot, registering and having it play its first games - this should give you a good understanding of how JSON-RPC works in the context of Merknera and how to test your bot using ngrok and postman.

A simple an non-effective implementation will be implemented and it will be left as an exercise of the reader to improve upon this.

Prerequisites

You will need to have the following software download and installed before continuing with this tutorial:

Setting Up

  1. Create a new directory and navigate to it at the command line.
  2. Run npm init and following the on-screen instructions.
  3. Run npm install json-rpc2 --save to install and add the dependency to our package.json file created for us in step 2.

Server Implementation

When orchestrating a game Merknera acts as a client and makes calls to the bots participating in the game and the bots act as a server. We will start by implementing the 4 server methods required by Tic-Tac-Toe to play a game: Status.Ping, TicTacToe.NextMove, TicTacToe.Complete & TicTacToe.Error.

Status.Ping is the easiest so we will start with that.

Status.Ping

Status.Ping is called whenever the Merknera server starts to find out which bots are online and it also calls Status.Ping prior to any move being played, this enables Merknera to ensure the bot is still online and to wake up any bots that might be hosted on a free service such as Heroku that goes to sleep after periods of inactivity. Full documentation can be found here

We need to be able to accept a method of Status.Ping with no parameters and return with a result of "ping": "OK".

Clone this wiki locally