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

simple_tic_tac_toe_engine

v1.2.0

Published

a tic-tac-toe engine make to easily implement tic-tac-toe in other programs

Downloads

25

Readme

simple_tic_tac_toe_engine

Initialisation

The object Oxo can take as input.

  • nothing (will set an empty grid)
  • a string of length 9 composed of 1, 2 or 3
  • a 1D array of length 9 composed of 1, "1", 2, "2", "3" or 3
  • a 2D of length 3x3 composed of 1, "1", 2, "2", "3" or 3
const Oxo = require("simple_tic_tac_toe_engine");

let oxo = new Oxo();
// by default the oxo grid is empty
let oxo1 = new Oxo();

/*
you can pass a string of 9 (0|1|2)
to the constructor to start from a define position
*/
let oxo2 = new Oxo("020010000");

// you can also pass a 1D array of 9 (0|1|2) elements
let oxo3 = new Oxo([0, 2, 0, 0, 1, 0, 0, 0, 0]);
let oxo4 = new Oxo(["0", "2", "0", "0", "1", "0", "0", "0", "0"]);

// you can also pass a 2D array of 9 (0|1|2) elements
let oxo5 = new Oxo([[0, 2, 0], [0, 1, 0], [0, 0, 0]]);
let oxo6 = new Oxo([["0", "2", "0"], ["0", "1", "0"], ["0", "0", "0"]]);

methods

getStringBoard()

This method returns the current board in the format of a 9 length string of (0|1|2).

oxo.getStringBoard();

get1DArray()

This method returns the current board in the format of a 9 length array of (0|1|2).

Number not string!

//could also be write:  oxo.get1DArray(use_int=true);
// return something like : [0, 0, 0, 0, 0, 0, 0, 0, 0]
oxo.get1DArray();
//could also be write:  oxo.get1DArray(use_int=false);
// return something like : ["0", "0", "0", "0", "0", "0", "0", "0", "0"]
oxo.get1DArray(false);

get2DArray()

This method returns the current board in the format of a 3x3 length array of (0|1|2).

Number not string!

oxo.get2DArray();

getCurrentPlayer()

This method returns the player for which is the turn, on an empty grid it's 1 and after the first turn it's 2.

On a full grid it returns 2, so make sure to use isFinished to check if the game is finished.

oxo.getCurrentPlayer();

getMoves()

This method returns all of the available moves for the current position in an array with the index from 1 to 9 like:

1 2 3
4 5 6
7 8 9
oxo.getMoves();

getBestMove()

This method return the best move, if multiples moves are the best ones it takes the last one. Move is from 1 to 9 like before:

oxo.getBestMove();

getCurrentScore()

This method returns the expected score with perfect play from both players.

  • 1 -> player 1 is winning
  • 2 -> player 2 is winning
  • 0 -> draw
oxo.getCurrentScore();

getWinningMoves(), getLosingMoves(), getDrawingMoves()

These methods returns respectively all of the winning moves, losing moves and drawing moves available for the current position.

oxo.getWinningMoves();
oxo.getLosingMoves();
oxo.getDrawingMoves();

isValidMove(index)

This method check if the index is between 1 and 9 (index >= 1 && index <= 9) and if the square of this index (starting from 1) in the current grid is available.

oxo.isValidMove(1);

isWinning()

This method check if the current position is winning for the last player.

oxo.isWinning();

isFinished()

This method check if the current position has all its squares full.

oxo.isFinished();

makeMove(index)

This method make the move of the given index.es (starting from 1) for the player for which it's the turn. Throw an error if the move is not possible, so make sure to check with isValidMove before.

// the three lines does the same thing
oxo.makeMove(1).makeMove(2).makeMove(3);
oxo.makeMove(1, 2, 3);
oxo.makeMove([1, 2, 3]);

undoMove(index)

This method undo the move of the given index (starting from 1) for the player for which it was the turn. Throw an error if:

  • the square is empty
  • if the square is outside the grid
  • if the player which played on this index wasn't the last player to play
oxo.undoMove(1);

undoLastMove()

This method undo the last move played.

// undo the last move
oxo.undoLastMove();
// undo the two last moves
oxo.undoLastMove(2);