Skip to content

Commit 682ed6b

Browse files
committed
basic matrix tests
1 parent a3914a1 commit 682ed6b

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

packages/matrix/src/matrix.test.ts

+41-8
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,73 @@
1-
import {createFlatMatrix, createMatrix} from "./matrix";
1+
import {
2+
createFlatMatrix,
3+
createMatrix,
4+
getTileCoordinates, getTileFromCoordinates,
5+
getTileRowAndColumn,
6+
} from "./matrix";
27

38
describe('createMatrix', () => {
49
test('createMatrix(1, 1)', () => {
5-
const m = createMatrix(1, 1);
10+
const m = createMatrix({ width: 1, height: 1, tileSize: 16 });
611
expect(m).toEqual([[0]]);
712
});
813

914
test('createMatrix(1, 2)', () => {
10-
const m = createMatrix(1, 2);
15+
const m = createMatrix({ width: 1, height: 2, tileSize: 16 });
1116
expect(m).toEqual([[0], [0]]);
1217
});
1318

1419
test('createMatrix(3, 3)', () => {
15-
const m = createMatrix(3, 3);
20+
const m = createMatrix({ width: 3, height: 3, tileSize: 16 });
1621
expect(m).toEqual([[0,0,0], [0,0,0], [0,0,0]]);
1722
});
1823

1924
test('createMatrix(3, 4)', () => {
20-
const m = createMatrix(3, 4);
25+
const m = createMatrix({ width: 3, height: 4, tileSize: 16 });
2126
expect(m).toEqual([[0,0,0], [0,0,0], [0,0,0], [0,0,0]]);
2227
});
2328
});
2429

2530
describe('createFlatMatrix', () => {
2631
test('createFlatMatrix(1, 1)', () => {
27-
const m = createFlatMatrix(1, 1);
32+
const m = createFlatMatrix({ width: 1, height: 1, tileSize: 16 });
2833
expect(m).toEqual([0]);
2934
});
3035

3136
test('createFlatMatrix(1, 2)', () => {
32-
const m = createFlatMatrix(1, 2);
37+
const m = createFlatMatrix({ width: 1, height: 2, tileSize: 16 });
3338
expect(m).toEqual([0, 0]);
3439
});
3540

3641
test('createFlatMatrix(3, 3)', () => {
37-
const m = createFlatMatrix(3, 3);
42+
const m = createFlatMatrix({ width: 3, height: 3, tileSize: 16 });
3843
expect(m).toEqual([0,0,0,0,0,0,0,0,0]);
3944
})
45+
});
46+
47+
describe('tile', () => {
48+
49+
test('getTileRowAndColumn', () => {
50+
const matrixConfig = { width: 40, height: 30, tileSize: 16 };
51+
52+
expect(getTileRowAndColumn(0, matrixConfig)).toEqual({row: 0, column: 0}); // first tile.
53+
expect(getTileRowAndColumn(129, matrixConfig)).toEqual({row: 3, column: 9});
54+
expect(getTileRowAndColumn(1199, matrixConfig)).toEqual({row: 29, column: 39}); // last tile.
55+
});
56+
57+
test('getTileCoordinates', () => {
58+
const matrixConfig = { width: 40, height: 30, tileSize: 16 };
59+
60+
expect(getTileCoordinates(0, matrixConfig)).toEqual({x: 0, y: 0});
61+
expect(getTileCoordinates(129, matrixConfig)).toEqual({x: 144, y: 48});
62+
expect(getTileCoordinates(1199, matrixConfig)).toEqual({x: 624, y: 464});
63+
});
64+
65+
test('getTileFromCoordinates', () => {
66+
const matrixConfig = { width: 40, height: 30, tileSize: 16 };
67+
68+
expect(getTileFromCoordinates(0, 0, matrixConfig)).toEqual(0);
69+
expect(getTileFromCoordinates(144, 48, matrixConfig)).toEqual(129);
70+
expect(getTileFromCoordinates(624, 464, matrixConfig)).toEqual(1199);
71+
});
72+
4073
});

packages/matrix/src/matrix.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type MatrixConfig = {
1212
}
1313

1414
export function createMatrix(matrixConfig: MatrixConfig): Array<MatrixTile[]> {
15-
const grid = [];
15+
const grid: Array<Array<number>> = [];
1616

1717
for (let row = 1; row <= matrixConfig.height; row++) {
1818
grid[row - 1] = [];
@@ -28,16 +28,23 @@ export function createFlatMatrix(matrixConfig: MatrixConfig) {
2828
return new Array(matrixConfig.width * matrixConfig.height).fill(MatrixTile.FREE);
2929
}
3030

31-
export function getXYFromTile(tile: number, matrixConfig: MatrixConfig) {
31+
export function getTileCoordinates(tile: number, matrixConfig: MatrixConfig) {
3232
return {
33-
x: Math.floor(tile / matrixConfig.width),
34-
y: (tile % matrixConfig.width)
33+
x: (matrixConfig.tileSize * (tile % matrixConfig.width)),
34+
y: (Math.floor(tile / matrixConfig.width) * matrixConfig.tileSize)
3535
}
3636
}
3737

38-
export function getTileFromXY<T extends MatrixConfig>(x: number, y: number, matrixConfig: T): number {
38+
export function getTileRowAndColumn(tile: number, matrixConfig: MatrixConfig) {
39+
return {
40+
row: Math.floor(tile / matrixConfig.width),
41+
column: (tile % matrixConfig.width)
42+
}
43+
}
44+
45+
export function getTileFromCoordinates<T extends MatrixConfig>(x: number, y: number, matrixConfig: T): number {
3946
const tileIndex = Math.floor(x / matrixConfig.tileSize) +
40-
(matrixConfig.width * matrixConfig.tileSize) * Math.floor(y / matrixConfig.tileSize);
47+
matrixConfig.width * Math.floor(y / matrixConfig.tileSize);
4148

4249
if (tileIndex < 0 || tileIndex > ((matrixConfig.width * matrixConfig.height) - 1)) {
4350
throw new Error(`Invalid tile ${tileIndex} resulted from ${x} and ${y}`);

0 commit comments

Comments
 (0)