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

battleships-engine

v0.2.7

Published

TypeScript engine for the classic game Battleship

Downloads

633

Readme

Battleship Engine

This TypeScript package is a core engine for implementing the classic game of Battleship. It allows for the creation of game boards, the placement of ships, and tracking of hits and misses, as well as determining when a player has lost.

For the React version of the engine, check out Battleship Engine for React, which provides the same functionality via a React hook.

Features

  • Create and manage a game board.
  • Place ships randomly or manually.
  • Track hits, misses, and sunken ships.
  • Detect when a player has lost the game.

Installation

To install the package, run:

npm install battleship-ts-engine

Usage

Importing

You can import the necessary classes and types from the package:

import { GameBoard, TCoords, ShipType, Direction } from "battleships-engine";

Example

Creating a Game Board

You can create a new game board, either with predefined ships or by randomly placing them:

const gameBoard = new GameBoard();
gameBoard.randomlyPlaceShips();

Placing a Ship Manually

gameBoard.placeShip({
    type: "battleship",
    coords: { x: 3, y: 5 },
    direction: "hor", // horizontal
});

Receiving an Attack

gameBoard.receiveAttack({ x: 3, y: 5 });

Checking if a Player has Lost

if (gameBoard.hasLost()) {
    console.log("You have lost the game!");
}

API Reference

GameBoard

The GameBoard class is responsible for managing the state of the game board, including ships, hits, and misses.

  • ships: Map<ShipType, Ship>: A map containing the ships on the board.
  • takenCells: Map<string, ShipType>: A map of all cells occupied by ships.
  • missed: Map<string, boolean>: A map of all missed attacks.

Methods:

  • randomlyPlaceShips(): Randomly places ships on the game board.
  • placeShip(params: { type: ShipType; coords: TCoords; direction: Direction }): Places a ship on the game board at specific coordinates and direction.
  • receiveAttack(coords: TCoords): Processes an attack at the given coordinates.
  • hasLost(): Returns true if all ships are sunk.

Ship

The Ship class defines a ship's properties and actions.

  • type: ShipType: The type of the ship.
  • length: number: The length of the ship.
  • beenHitTimes: number: The number of times the ship has been hit.

Methods:

  • hit(): Marks the ship as hit.
  • isSunk(): Returns true if the ship is completely sunk.

Coords

The Coords class is used to represent a coordinate on the game board.

  • x: number: The x-coordinate.
  • y: number: The y-coordinate.

Methods:

  • toString(): Returns the coordinate in string format as (x,y).