A simple matrix library for sudoku and board games
npm install simple-matrix-jsbash
npm install simple-matrix
`
使用
`typescript
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(); // 导出为数组
``