# tris3d

Wanna play? Go to play.tris3d.net!

## Installation

With npm do

``````npm install tris3d
``````

With bower do

``````bower install tris3d
``````

``````<script src="https://cdn.rawgit.com/fibo/tris3d/master/dist/tris3d.min.js"></script>
``````

## Description

Algorithm to play tic tac toe in dimension three.

Imagine a cube, just like the Rubik’s cube where everyone of the little cubes is numbered from 0 to 26 and has a 3d coordinate in the range from `[0, 0, 0]` to `[2, 2, 2]` following a natural order.

``````// Sorted array of points from point[0] to point[26]
var point = [
[0, 0, 0], [1, 0, 0], [2, 0, 0], // 0   1   2
[0, 1, 0], [1, 1, 0], [2, 1, 0], // 3   4   5
[0, 2, 0], [1, 2, 0], [2, 2, 0], // 6   7   8
[0, 0, 1], [1, 0, 1], [2, 0, 1], // 9   10  11
[0, 1, 1], [1, 1, 1], [2, 1, 1], // 12  13  14
[0, 2, 1], [1, 2, 1], [2, 2, 1], // 15  16  17
[0, 0, 2], [1, 0, 2], [2, 0, 2], // 18  19  20
[0, 1, 2], [1, 1, 2], [2, 1, 2], // 21  22  23
[0, 2, 2], [1, 2, 2], [2, 2, 2]  // 24  25  26
]
``````

NOTA BENE: in dimension three the game makes sense only if there are three players (🐨 🐬 🐯), and is more fun if they can negotiate.

## API

### `isTris(p1, p2, p3)`

Takes 3 points given by the coordinate system described above and returns a boolean that is true if they are aligned.

``````var tris3d = require('tris3d')

var isTris = tris3d.isTris

isTris([2, 0, 2], [1, 1, 2], [0, 2, 2]) // true
``````

### `indexOfCoordinates(point)`

Convert point in Z3xZ3xZ3 to index.

``````var tris3d = require('tris3d')

var indexOf = tris3d.indexOfCoordinates

indexOf([0, 0, 0]) // 0
indexOf([1, 0, 0]) // 1
// …
indexOf([2, 2, 2]) // 26
``````

### `coordinatesOfIndex(index)`

Convert index to point in Z3xZ3xZ3.

``````var tris3d = require('tris3d')

var coordOf = tris3d.coordinatesOfIndex

coordOf(0)  // [0, 0, 0]
coordOf(1)  // [1, 0, 0]
// …
coordOf(26) // [2, 2, 2]
``````