Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including tttai with all npm packages installed. Try it out:

var tttai = require("tttai")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

tttai v0.0.3

Get informations about a tic-tac-toe game.

tttai

Get informations about a tic-tac-toe game.

Installation

Install it with npm or yarn.

You can try:

  • npm install tttai
  • ...or yarn add tttai

Usage

var tttai = require('tttai')

This will be an object with some methods:

  • .isFinish(state)
  • .getNextState(player, state)
  • .whoWon(state)

tttai.isFinish(state)

It will check if a game already is finished.

See the example:

tttai.isFinish([
  'x', null, 'o',
  'o', 'x', null,
  null, 'o', 'x'
])
// true

tttai.getNextState(player, state)

It will return the next state of the game for a player.

See the example:

tttai.getNextState('x', [
  'x', null, 'o',
  'o', null, null,
  null, 'o', 'x'
])
// [
//   'x', null, 'o',
//   'o', 'x', null,
//   null, 'o', 'x'
// ]

tttai.whoWon(state)

It will return the winner of the game.

NOTE: if have a draw, it will return null.

See the example:

tttai.whoWon([
  'x', null, 'o',
  'o', 'x', null,
  null, 'o', 'x'
])
// 'x'

License

MIT © Matheus Alves

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free