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-tictactoe

v1.0.0

Published

A Simple package to make a tic-tac-toe game

Downloads

1

Readme

simple-tictactoe

Easy to use package to make tictactoe games

Note: This only supports 3x3 tictactoe

Install

npm i simple-tictactoe

Usage

A example of using this package, this is not a fully working example, and just a example of how to use it.

import { TicTacToe, Player} from 'simple-tictactoe';

// use the second parameter to set a custom item that will be used to represent the player
const game = new TicTacToe(new Player('X', { userID: '1111111',   }), new Player('O', { userID: '2222222', someOtherField: true   }));

// use some way to get a input

// this is player 1 which is (x)
game.turn(0, 0)
// next turn is player 2 which is (o)
game.turn(0, 1)
// go on from here on until one wins

// a easy way to get the board on a string
const board = game.boardToString()

console.log(board)

Finding the winner

Every turn returns the following object

{ 
player: currentPlayer, 
board: the ongoing board, 
players: ['Array', 'of', 'the 2 players'], 
nextPlayer: the player who is supposed to have the next turn, 
status: {
	if won: 
		won: true
		player: the player who won
	else if draw:
	    draw: true
		players: ['Array', 'of', 'the 2 players since its a draw']
	else nothing:
		{} - empty object
},
ended: true/false
}
  • We can use the status object to see if the current user who's turn we proceeded with won
const data = game.turn(0, 1);

if(data.status.won) {
	console.log(`${data.status.player.id} won!`)
}

// same for draws

Using a ai

  • the package also includes a simple ai that selects turn randomly from available spots (is this really even a ai??)

following is a fully automated ai game without any use inputs

import { TicTacToe, Player, aiPlayer } from 'simple-tictactoe';

// use the second parameter to set a custom item that will be used to represent the player
const game = new TicTacToe(new Player('X', { userID: '1111111',   }), new Player('O', { userID: '2222222', someOtherField: true   }));

const ai = new aiPlayer('O', game)
const ai2 = new aiPlayer('X', game)

while(game.getEmptySpaces().length > 0 && !game.ended) {
	if(game.currentTurn === 'X') {
		const d = ai2.takeTurn()
		if(d.status.won) {
			console.log('X won!')
		}
		else if(d.status.draw) {
			console.log('Draw!')
		}
	}
	else {
		const d = ai.takeTurn()
		if(d.status.won) {
			console.log('O won!')
		}
		else if(d.status.draw) {
			console.log('Draw!')
		}
	}
}

// print the board to see how it did

const board = game.boardToString()
console.log(board);

Api

new Game(player1, player2)

  • player1 - the first player
  • player2 - the second player

\<Game>.turn(x, y)

used to pick a turn in the game

Parameters

  • x: the x position of board (where to place)
  • y: the y position of the board (where to place)

After every call to this function, the game will check if the game is over (if it is not over, the currentTurn will be changed to the next player)

\<Game>.boardToString()

returns the board as a prettified string

\<Game>.getEmptySpaces()

returns an array of all the available spots on the board

ex

[
	{ x: 0, y: 0, item: '-' },
	{ x: 1, y: 2, item: '-' },
]

Support

Click for discord support