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

chess-node

v1.1.0

Published

Chess on NODE

Downloads

355

Readme

Chess-Node

Event-Based chess Algorithm for NodeJS

Installation

npm i chess-node

Importing

Typescript

import * as chess from 'chess-node'

or

Only import the Game Class

import Game from 'chess-node'

JavaScript w/ CommonJs

const chess = require('chess-node')

or

Only import the Game Class

const Game = require('chess-node').default

Buckle Up. Things are gonna get complex.

Creating an Instance

The default export or the Game Class takes in 5 arguements while consturcting. 3 of them are optional. Let's go through all of them.

  • EventEmitter
  • gameEventString
  • white
  • black
  • board

1. EventEmitter

It's an instance of the EventEmitter class from the events module of NodeJS

Note: You should provide a fresh instance of the EventEmitter class

2. gameEventString

A unique string with which the Game class can listen to events emmited. We'll come back to this later

3, 4. White, Black

3rd and 4th Param is an instance of the Player Class. You can ignore these as the Game Class will create them for you.

5. Board

The Board class. You can Ignore this as well. Basically it's the chess board for the Game.

Construction Example:

import { EventEmitter } from 'events'
import Game from 'chess-node'

const game = new Game(new EventEmitter(), 'chess-game-1')

Fun Fact: Every Export is a Class.

This fact is not that fun now that I think about it. Well, Anyway.

Starting The Game

Now that you have created a new instance of Game, you can now call methods inside it.

The method start starts a new game in the instance of the Game you just created.

Game.start() takes 4 arguements. 3 of them are required.

  • func
  • whitePlayer
  • blackPlayer
  • pre

1. func

This a function which prints the board each time a move occurs

2. whitePlayer

The ID of the white player

3. blackPlayer

The ID of the black player

4. pre

A function which will execute right after the move has been made.

Example:

game.start((message) => {
    //This will print the board on to the console each time
    console.log(message)
}, 'cool_unique_id', 'cooler_unique_id', () => console.log(game.board.getPieces()))

Fun Fact: Every TypeScript user thinks they're gods. AND THEY'RE RIGHT!

Making A Move

Now we get to the fun part, Making a move. Step 1: Flir- oh wait, Wrong guide. Sorry about that. Where were we? Ah yes, Making a move IN THE GAME.

To make a move, you have to emit an event in the eventEmitter object you passed in while consturcting

Emiting

While emiting you need to pass 3 arguments.

  • gameEventString
  • move
  • func
  • playerString

1. gameEventString

The string you passed in while constructing.

2. move

The move you want to make. It is an object which has 2 fields Those are from the current position of the piece and to the position to the piece move to.

interface Move = {
    to: Tile
    from: Tile
}

genMove()

The genMove(): Tile | null function takes in 1 argument and returns the the Tile object if it's a valid position.

3. func

Function to print the board

4. playerString

The ID of the player whose making the move.

Example:

import { EventEmitter } from 'events'
import Game, { genMove } from 'chess-node'

const game = new Game(new EventEmitter(), 'chess-game-1')
game.start((message) => console.log, 'player_1', 'player_2')

// Moves from B1 to C1
const [from ,to] = [genMove('B1'), genMove('C1')]

// Only allow valid moves to get passed
if (!from || !to) return throw new Error('Invalid Move')

game.eventEmitter.emit('chess-game-1', move, (message) => console.log(message), 'player')

Ending the Game

The eventEmitter will emit gameOver when the game ends. and It'll also print the board and winner in the func parameter while the game ends

game.eventEmitter.emit('chess-game-1', move, (message) => console.log(message), 'player')

if the game ends, it'll log one of the following strings:

  • <Colour_1> is in checkmate, <Colour_2> wins!
  • Stalemate

That's about it. Better docs comming soon, Hopefully. Now try it out. Have a great day!