1 of 24

Blockly Tic-Tac-Toe

Getting started with Blockly's API

2 of 24

A Guided Tour

This workshop will connect Blockly with a Tic-Tac-Toe game so that users can program a computer opponent. In the process we will explore the major aspects of Blockly's API.

Estimated time: 1 hour

3 of 24

The Goal

4 of 24

Download Tic-Tac-Toe

First, download the Tic-Tac-Toe game:

drive.google.com/file/d/0B_5TNOBUDB1yMm14WkY2WXNDTUE/view

Test it in a browser: tictactoe.html

It is not an example of great code, you can improve it later if you like.

5 of 24

Tic-Tac-Toe

6 of 24

Download Blockly

Second, download Blockly:

https://github.com/google/blockly

Blockly can be downloaded with Git, Subversion, or as a zip file.

Test it in a browser: demos/fixed/index.html

7 of 24

Blockly

8 of 24

Inject Blockly into tictactoe.html

Modify tictactoe.html to add Blockly:

developers.google.com/blockly/guides/configure/web/fixed-size

Optional: Place the Tic-Tac-Toe game and Blockly in a one-row, two-column table so that they are next to each other:

Tic

Tac

Toe

Blockly

9 of 24

Tic-Tac-Toe + Blockly

10 of 24

Generate Code

Blockly is not a programming language. It is a visual editor that generates code. Generators have been written for JavaScript, Python, PHP, Lua, Dart and more.

Let's add a button that takes the user's blocks and generates JavaScript.

11 of 24

Generate JavaScript

Import this script file:

<script src="javascript_compressed.js"></script>

Add a button to the page with this onclick function:

<button onclick="runJS()">Run Code</button>

<script>

function runJS() {

Blockly.JavaScript.addReservedWords('code');

var code = Blockly.JavaScript.workspaceToCode();

alert(code);

try {

eval(code);

} catch (e) {

alert(e);

}

}

</script>

12 of 24

Running JavaScript

13 of 24

More Blocks

Every use of Blockly needs a different set of blocks.

Blockly comes with more than 50 sample blocks, but you will need to chose which (if any) are relevant.

You also need to create API blocks that are custom to your project.

14 of 24

Core Blocks

Replace the toolbox in tictactoe.html with this monster:

docs.google.com/file/d/0B_5TNOBUDB1yZWVpLWdyT3YzcUE/view

Reload tictactoe.html in a browser to see (nearly) every sample block that comes with Blockly.

Next, prune out those blocks that are irrelevant to Tic-Tac-Toe (such as colours and trigonometry).

15 of 24

Category Toolbox

16 of 24

Create Custom Blocks

API blocks for your application need to be custom built. In the case of Tic Tac Toe we need a block to play in a square (0-8):

We also need a block to get the symbol ("X", "O", or "") in a square (0-8):

The easiest way to design custom blocks is to use the Block Factory:

blockly-demo.appspot.com/static/demos/blockfactory/index.html

17 of 24

Block Factory (set)

18 of 24

Block Factory (get)

19 of 24

Insert Custom Blocks

For each of the new blocks, copy the "Language code" and "Generator stub" from the Block Factory and paste it into a script your tictactoe.html

Then add a new category to the toolbar's XML:

<category name="Tic-Tac-Toe">

<block type="ttt_set"></block>

<block type="ttt_get"></block>

</category>

20 of 24

Custom Blocks

21 of 24

JavaScript for Set Block

The Block Factory can only write a stub for the JavaScript generators. You need to fill in the details. In the case of ttt_set, replace these lines:

// TODO: Assemble JavaScript into code variable.

var code = '...';

With this line:

var code = 'canvasClicked(' + value_square + ');\n';

22 of 24

JavaScript for Get Block

In the case of ttt_get, replace these lines:

// TODO: Assemble JavaScript into code variable.

var code = '...';

With this line:

var code = 'content[' + value_square + ']';

23 of 24

Minimally Playable

24 of 24

Further Steps

  • Change ttt_get's ORDER_NONE to the correct operator precedence (ORDER_MEMBER).
  • Add a block to get which symbol is currently being played.
  • Add an infinite loop check, or go professional and use the JS Interpreter.
  • Use cloud storage on App Engine to allow users to save programs.

For additional help, or just to show off what you've built, join the

Blockly newsgroup.

Blockly is free and open source. Go integrate Blockly into your projects as a friendly UI. It is also more than just for programming UIs (e.g. Blockly Puzzle).