forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixToString.js
More file actions
89 lines (76 loc) · 1.88 KB
/
Copy pathMatrixToString.js
File metadata and controls
89 lines (76 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Pad = require('../../string/Pad');
var CheckMatrix = require('./CheckMatrix');
/**
* Generates a string (which you can pass to console.log) from the given Array Matrix.
*
* A matrix is a two-dimensional array (array of arrays), where all sub-arrays (rows)
* have the same length. There must be at least two rows. This is an example matrix:
*
* ```
* [
* [ 1, 1, 1, 1, 1, 1 ],
* [ 2, 0, 0, 0, 0, 4 ],
* [ 2, 0, 1, 2, 0, 4 ],
* [ 2, 0, 3, 4, 0, 4 ],
* [ 2, 0, 0, 0, 0, 4 ],
* [ 3, 3, 3, 3, 3, 3 ]
* ]
* ```
*
* @function Phaser.Utils.Array.Matrix.MatrixToString
* @since 3.0.0
*
* @generic T
* @genericUse {T[][]} - [matrix]
*
* @param {T[][]} [matrix] - A 2-dimensional array.
*
* @return {string} A string representing the matrix.
*/
var MatrixToString = function (matrix)
{
var str = '';
if (!CheckMatrix(matrix))
{
return str;
}
for (var r = 0; r < matrix.length; r++)
{
for (var c = 0; c < matrix[r].length; c++)
{
var cell = matrix[r][c].toString();
if (cell !== 'undefined')
{
str += Pad(cell, 2);
}
else
{
str += '?';
}
if (c < matrix[r].length - 1)
{
str += ' |';
}
}
if (r < matrix.length - 1)
{
str += '\n';
for (var i = 0; i < matrix[r].length; i++)
{
str += '---';
if (i < matrix[r].length - 1)
{
str += '+';
}
}
str += '\n';
}
}
return str;
};
module.exports = MatrixToString;