npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

exesjs

v1.1.0

Published

Solution for managing tic tac toe and similar games.

Downloads

45

Readme

Exes.js

A small library for managing Tic-Tac-Toe like games.

Installation

npm i exesjs

CJS

const { Board, search } = require('exesjs');

ESM

import { Board, search } from 'exesjs';

Usage

There are two main components to this library.

Board

Basics

The board manages things like which player's turn it is, placing a move, checking if the game is over, etc.

Board.internal is the internal representation of the game board. It is a one-dimensional array of numbers.

  • -1 Represents an empty space.
  • 0 Represents a space occupied by 'O.'
  • 1 Represents a space occupied by 'X.'

Create a New Board

You can create a board by specifying a width, height, winLength, and turn.

const b = new Board({ width: 3, height: 3, winLength: 3, turn: 1 });
// Or leave the options blank to get a tic tac toe board.
const b = new Board();

Making a Move

Make moves on the board by calling board.move() It accepts two methods of moving.

Standard Move

A standard move can be made by simply passing a number — the position you want to play — to board.move.

const board = new Board();
board.move(4);
/*
-1 -1 -1
-1  1 -1
-1 -1 -1
*/

You can also specify a verbose move.

const board = new Board();
board.move({
  row: 2,
  col: 2,
});
/*
-1 -1 -1
-1  1 -1
-1 -1 -1
*/

You do not need to manually change board.turn, as it does so automatically.

Undoing a Move

You can undo a move by simply passing in the position you want to remove. Once again, the turn is automatically adjusted.

const board = new Board();
board.move(4);
/*
-1 -1 -1
-1  1 -1
-1 -1 -1
*/
board.undo(4);
/*
-1 -1 -1
-1 -1 -1
-1 -1 -1
*/

Load

You can load an array of moves into the board. Note: these moves must not be verbose.

const board = new Board();
board.load([4, 0, 8]);
/*
 0 -1 -1
-1  1 -1
-1 -1  1
*/

Search

You can have the computer pick a move by using the search function.

You must provide the search depth. But use caution, with great boards comes great search time.

const board = new Board();
board.load([4, 0, 8]);
/*
 0 -1 -1
-1  1 -1
-1 -1  1
*/
const bestMove = search(board, 4);

Example

Unbeatable Tic Tac Toe Computer

import { Board, search } from 'exesjs';
import readlineSync from 'readline-sync';

const board = new Board();

while (!board.isOver) {
  const move = search(board, 9);
  board.move(move);
  if (board.isOver) break;

  board.print();

  console.log('Your move: ');
  const input = readlineSync.question();

  board.move(parseInt(input));
}

board.print();
console.log(board.winner + ' wins!');