simple-matrix-js
v1.0.0
Published
A simple matrix library for sudoku and board games
Maintainers
Readme
simple-matrix
一个用于数独和棋类游戏的简单矩阵操作库。
安装
npm install simple-matrix使用
import { SimpleMatrix } from 'simple-matrix';
// 创建一个矩阵(支持空值配置)
const matrix = new SimpleMatrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
], { emptyValue: 0 }); // 配置空值为 0
// 基本操作
const row = matrix.getRow(0); // 获取第0行
const col = matrix.getCol(1); // 获取第1列
const around = matrix.getRound([1, 1]); // 获取周围8个元素
const subGrid = matrix.getSubGrid([1, 1], 3); // 获取3x3宫格
// 斜线操作(五子棋场景)
const diagonal = matrix.getDiagonalBidirectional([2, 2], 2, 2); // 左上2格+右下2格
// 值操作
matrix.setValue([0, 0], 10); // 设置值
matrix.clearValue([0, 0]); // 清空值(自动使用 emptyValue: 0)
// 批量操作
matrix.forEach((val, x, y) => console.log(val, x, y));
const doubled = matrix.map(v => v * 2);
const evens = matrix.filter(v => v % 2 === 0);
// 查找和统计
const found = matrix.find(v => v > 5);
const count = matrix.countValue(5); // 统计值为5的元素数量
// 行列修改
matrix.insertRow(1, [10, 11, 12]);
matrix.deleteRow(0);
matrix.insertCol(1, [20, 21, 22]);
matrix.deleteCol(0);
// 拷贝操作
const cloned = matrix.clone(); // 深拷贝
const array = matrix.toArray(); // 导出为数组✨ 主要功能
基本操作
- ✅ 获取行、列、周围元素
- ✅ 获取相对位置元素
- ✅ 获取九宫格(子矩阵)
斜线和行列操作
- ✅ 获取斜线元素(四个方向)
- ✅ 带范围限制的线获取(五子棋友好)
- ✅ 双向行/列获取
元素操作
- ✅ 元素移动和交换
- ✅ 设置和清空值
- ✅ 空值配置(emptyValue)
批量操作
- ✅ forEach、map、filter
- ✅ find、findAll
- ✅ count、countValue
结构操作
- ✅ 插入/删除行
- ✅ 插入/删除列
工具功能
- ✅ 深拷贝(clone)
- ✅ 导出为数组(toArray)
- ✅ 格式化打印矩阵
API 文档
详细 API 文档:https://www.yuque.com/leeyamaster/simple-matrix.js/
本地文档:查看 doc/API.md 和 doc/demo.html
License
MIT
