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

four-in-a-row

v1.0.1

Published

Four-In-A Row game logic library. No need to write the logic for the game yourself

Downloads

24

Readme

Four-In-A-Row.js

Four-In-A Row (aka "Connect Four" or "Four-In-A-Line") game logic library for JavaScript. No need to write the logic for the game yourself.

This was adapted from my Making Four-In-A-Row Using JavaScript blog series.


Table of Contents

Installing

Package Manager

You can use your favourite package manager to install the library.

For example:

npm install four-in-a-row

No build step

No build step required. Add four-in-a-row.js or four-in-a-row.min.js directly to your projects.

Examples

Usage

import { Game } from "four-in-a-row";

const game = new Game();
const startingMoveResult = game.playMove(0); // Yellow Token placed at row: 0, column: 0.
const nextMoveResult = game.playMove(1); // Red Token placed at row: 0, column 1.

Full Example

For a full example of how to use the library, check out out the demo example which is a four-in-a-row HTML5 Canvas game.

Public API

Create a new game

import { Game } from "four-in-a-row";

// Create a new game
const game = new Game();

// Check current status of game
const initialStatus = game.status;

// Get starting color
const startingColor = game.startingColor;

// Get current turn
const turn = game.currentTurn;

// Make a move
const moveResult = game.playMove(0);

// Retrieve game board
const board = game.currentBoard;

// Restart game
game.reset();

Contstants

GameStatus

import { Game, GameStatus } from 'four-in-a-row';

console.log(GameStatus.IN_PROGRESS); // "in-progress"
console.log(GameStatus.START); // "start"
console.log(GameStatus.WIN); // "win"
console.log(GameStatus.DRAW); // "draw"

const game = new Game();
console.log(game.status === GameStatus.START); // true

MoveStatus

import { Game, MoveStatus } from 'four-in-a-row';

console.log(MoveStatus.INVALID); // "invalid"
console.log(MoveStatus.WIN); // "win"
console.log(MoveStatus.SUCCESS); // "success"
console.log(MoveStatus.DRAW); // "draw"

const game = new Game();
const moveResult = game.playMove(0);
console.log(moveResult.status === MoveStatus.Success); // true

PlayerColor

import { Game, PlayerColor} from 'four-in-a-row';

console.log(PlayerColor.NONE); // "none"
console.log(PlayerColor.YELLOW); // "yellow"
console.log(PlayerColor.RED); // "red"

const game = new Game();
console.log(game.startingColor === PlayerColor.YELLOW); // true;
console.log(game.currentTurn === PlayerColor.YELLOW); // true;

game.playMove(0);
console.log(game.currentTurn === PlayerColor.RED); // true;

console.log(game.currentBoard);
// game.currentBoard
// { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
// { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
// { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
// { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
// { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
// { 0: 1, 1: 0, 2: 0, 3: 1, 4: 0, 5: 0, 6: 0 },

BoardDimensions

import { BoardDimensions } from 'four-in-a-row';

console.log(BoardDimensions.ROWS); // 6
console.log(BoardDimensions.COLUMNS); // 7
console.log(BoardDimensions.WIN_LINE_LENGTH); // 4

BoardToken

import { Game, BoardToken } from 'four-in-a-row';

console.log(BoardToken.NONE); // 0
console.log(BoardToken.YELLOW); // 1
console.log(BoardToken.RED); // 2

const game = new Game();
game.playMove(0);
console.log(game.currentBoard);
// Output (`Uint8Array[]`- each Uint8Array element contains a `BoardToken`):
// [
//   { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
//   { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
//   { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
//   { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
//   { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
//   { 0: 1, 1: 0, 2: 0, 3: 1, 4: 0, 5: 0, 6: 0 },
// ]

Instance methods

playMove(columnIndex: number)

Returns a MoveResult object

{
  // Uint8Array[] - Each Uint8Array element contains a `BoardToken`
  nextBoard: [
    { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
    { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
    { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
    { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0 },
    { 0: 0, 1: 0, 2: 0, 3: 0, 4: 2, 5: 2, 6: 2 },
    { 0: 0, 1: 0, 2: 0, 3: 1, 4: 1, 5: 1, 6: 1 },
  ],
  // PlayerColor
  winner: 'yellow',
  // MoveStatus
  status: 'WIN',
  // BoardPosition[]
  winLine:
  [
    { row: 5, column: 3 },
    { row: 5, column: 4 },
    { row: 5, column: 5 },
    { row: 5, column: 6 },
  ]
}

reset()

Restarts the game.

Game Over State Behaviour

You can find out if the game is over by checking for the for a win or draw in the status field of a MoveResult or the status field of a Game.

if the game is over, calls to playMove() will return the last MoveResult (Details of the move that put the game into a game over state).

You'll have to call the reset() to get out of the game over state.