ba7b1e9707f23b342be7ab1d3f9bdf011d3a7f0b
1 // Turn (human) marks into coordinates
2 function getMarkArray(marks
)
4 if (!marks
|| marks
== "-")
6 let markArray
= doubleArray(V
.size
.x
, V
.size
.y
, false);
7 const squares
= marks
.split(",");
8 for (let i
=0; i
<squares
.length
; i
++)
10 const coords
= V
.SquareToCoords(squares
[i
]);
11 markArray
[coords
.x
][coords
.y
] = true;
16 // Turn (human) shadow indications into coordinates
17 function getShadowArray(shadow
)
19 if (!shadow
|| shadow
== "-")
21 let shadowArray
= doubleArray(V
.size
.x
, V
.size
.y
, false);
22 const squares
= shadow
.split(",");
23 for (let i
=0; i
<squares
.length
; i
++)
25 const rownum
= V
.size
.x
- parseInt(squares
[i
]);
29 for (let i
=0; i
<V
.size
.y
; i
++)
30 shadowArray
[rownum
][i
] = true;
33 if (squares
[i
].length
== 1)
35 // Shadow a full column
36 const colnum
= V
.ColumnToCoord(squares
[i
]);
37 for (let i
=0; i
<V
.size
.x
; i
++)
38 shadowArray
[i
][colnum
] = true;
41 if (squares
[i
].indexOf("-") >= 0)
43 // Shadow a range of squares, horizontally or vertically
44 const firstLastSq
= squares
[i
].split("-");
47 V
.SquareToCoords(firstLastSq
[0]),
48 V
.SquareToCoords(firstLastSq
[1])
52 range
[1].x
== range
[0].x
54 : (range
[1].x
- range
[0].x
) / Math
.abs(range
[1].x
- range
[0].x
),
55 range
[1].y
== range
[0].y
57 : (range
[1].y
- range
[0].y
) / Math
.abs(range
[1].y
- range
[0].y
)
59 // Convention: range always from smaller to larger number
60 for (let x
=range
[0].x
, y
=range
[0].y
; x
<= range
[1].x
&& y
<= range
[1].y
;
61 x
+= step
[0], y
+= step
[1])
63 shadowArray
[x
][y
] = true;
67 // Shadow just one square:
68 const coords
= V
.SquareToCoords(squares
[i
]);
69 shadowArray
[coords
.x
][coords
.y
] = true;
74 // args: object with position (mandatory), and
75 // orientation, marks, shadow (optional)
76 function getDiagram(args
)
78 // Obtain the array of pieces images names:
79 const board
= V
.GetBoard(args
.position
);
80 const orientation
= args
.orientation
|| "w";
81 const markArray
= getMarkArray(args
.marks
);
82 const shadowArray
= getShadowArray(args
.shadow
);
84 const [startX
,startY
,inc
] = orientation
== 'w'
86 : [V
.size
.x
-1, V
.size
.y
-1, -1];
87 for (let i
=startX
; i
>=0 && i
<V
.size
.x
; i
+=inc
)
89 boardDiv
+= "<div class='row'>";
90 for (let j
=startY
; j
>=0 && j
<V
.size
.y
; j
+=inc
)
92 boardDiv
+= "<div class='board board" + V
.size
.y
+ " " +
93 ((i
+j
)%2==0 ? "light-square-diag" : "dark-square-diag") +
94 (shadowArray
.length
> 0 && shadowArray
[i
][j
] ? " in-shadow" : "") +
96 if (board
[i
][j
] != V
.EMPTY
)
98 boardDiv
+= "<img src='/images/pieces/" +
99 V
.getPpath(board
[i
][j
]) + ".svg' class='piece'/>";
101 if (markArray
.length
> 0 && markArray
[i
][j
])
102 boardDiv
+= "<img src='/images/mark.svg' class='mark-square'/>";
103 boardDiv
+= "</div>";
105 boardDiv
+= "</div>";