Complete rewrite of nimm-engine-v1.
- What is Nimm
- Installation
- Content
- How to Play
- API Documentation
- Development
- Local Development with Vite
- Project Structure
- Dependencies
- Documentation
- License
Nim is a mathematical game of strategy in which two players take turns removing (or "nimming") objects from distinct heaps or piles.
From Wikipedia
This implementation features a 2D grid-based variant where players remove stones from rows or columns starting from the edges. See Game Rules for detailed gameplay mechanics.
- Node.js 22.x or higher
- npm or yarn
npm install @monksterfx/nimm-engine-v2 --registry=https://npm.pkg.github.com- Clone the repository:
git clone <repository-url>
cd nimm-engine-v2- Install dependencies:
npm install- Build the project:
npm run buildThe project consists of three main modules:
- engine - Core game logic, move validation, and state management. Handles game rules, move generation, and state transitions. See src/engine/
- cli - Command-line interface for playing the game interactively. Provides a terminal-based game experience. See src/cli/
- ai - AI algorithms including minimax, game graph analysis, and pattern matching for computer opponents. See src/ai/
Start the CLI interface:
npm run cliOr in development mode:
npm run cli:devWhen prompted, enter your move in the format: [dir.row.col]
- dir: Direction/side to take from
r- Rightl- Leftt- Topb- Bottom
- row: Row index (0-based)
- col: Column index (0-based)
Next Move? Format:[dir.row.col]
t.0.2
This takes stones from the top side, column 2 (0-indexed), starting from row 0.
- The game displays the current board state
- Enter your move in the format
[dir.row.col] - The AI opponent makes its move automatically
- The board updates and shows possible patterns (if enabled)
- Continue until one player wins
For detailed game rules, see Game Rules Documentation.
The package exports the following:
import { createGame, GameEngine, GameState, Interfaces } from 'nimm-engine-v2';Creates a new game instance with the specified options.
import { createGame } from 'nimm-engine-v2';
const game = createGame({ size: [6, 6] });
// Returns a GameEngine instanceParameters:
options(optional):GameOptionsobjectsize:[number, number]- Grid dimensions as[rows, columns]
Returns: GameEngine instance
Main game engine class that manages game state and provides move generation.
import { GameEngine, GameState } from 'nimm-engine-v2';
const gameState = new GameState();
gameState.init({ size: [6, 6] });
const engine = new GameEngine(gameState);
// Get a move (currently returns random move)
const move = engine.nextMove();Methods:
nextMove(): Returns aMoveobject for the current playerrandomMove(): Returns a random valid move
Represents the current state of the game board.
import { GameState } from 'nimm-engine-v2';
import { Orientation } from 'nimm-engine-v2';
const game = new GameState();
game.init({ size: [6, 6] });
// Make a move
game.take(row, col, Orientation.TOP);
// Check if game is finished
const finished = game.isFinished();
// Get valid moves for an orientation
const validMoves = game.getValidMoves(Orientation.LEFT);
// Save/load game state
const snapshot = game.save();
const restored = GameState.load(snapshot);Key Methods:
init(options): Initialize the game boardtake(row, col, orientation): Remove stones from a positionisFinished(): Check if the game has endedgetValidMoves(orientation): Get all valid move IDs for an orientationsave(): Export current state as a snapshotload(snapshot): Restore state from a snapshot
TypeScript interfaces and enums for type safety.
import { Interfaces } from 'nimm-engine-v2';
// Available types:
// - Player
// - Move
// - MoveInfo
// - GameOptions
// - Orientation (enum)
// - SnapshotOrientation Enum:
Orientation.TOP = 2Orientation.BOTTOM = -2Orientation.LEFT = 1Orientation.RIGHT = -1
npm run build- Compile TypeScript to JavaScript in thedist/directorynpm run cli- Run the CLI interface using compiled JavaScriptnpm run cli:dev- Run the CLI interface in development mode with ts-nodenpm run vite-pack- Build the project and create an npm package tarball inpackage/directory
- Make changes to TypeScript files in
src/ - Run
npm run buildto compile - Test with
npm run cli:devfor quick iteration - Use
npm run clito test the compiled version - Run
npm run vite-packto create a distributable package
nimm-engine-v2/
├── src/
│ ├── engine/ # Game engine and rules
│ ├── cli/ # Command-line interface
│ ├── ai/ # AI algorithms
│ │ ├── graph/ # Game graph analysis
│ │ ├── tree/ # Tree-based algorithms (minimax)
│ │ └── utils/ # AI utility functions
│ ├── models/ # Data models and interfaces
│ └── index.ts # Main entry point
├── dist/ # Compiled JavaScript output
├── docs/ # Documentation files
│ ├── game-rules.md # Detailed game rules
│ └── ai-integration.md # AI integration guide
├── package.json
└── tsconfig.json
When developing with Vite or other ES module bundlers, npm link may not work correctly due to CommonJS/ES module compatibility issues.
Solution: Use npm pack to create a local package:
# Build and pack the package
npm run vite-pack
# In your Vite project, install the packed tarball
npm install ../nimm-engine-v2/package/nimm-engine-v2-0.0.3.tgzThe vite-pack script:
- Builds the TypeScript project (
npm run build) - Creates an npm package tarball using
npm pack - Saves it to the
package/directory
This creates a distributable package that can be installed in projects using ES modules.
- lodash (^4.17.21) - Utility functions
- readline (^1.3.0) - CLI input handling
- typescript (^5.0.4) - TypeScript compiler
- @types/node (^16.11.9) - Node.js type definitions
- @types/lodash (^4.14.178) - Lodash type definitions
- ts-node (^10.8.2) - TypeScript execution for Node.js
Additional documentation is available in the docs/ directory:
- Game Rules - Detailed explanation of game mechanics and rules
- AI Integration Guide - Guide for integrating AI opponents
MIT