1 // Assuming V(ariantRules) class is loaded.
2 // args: object with position (mandatory), orientation, marks (optional)
3 function getDiagram(args
)
5 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
6 // Obtain array of pieces images names
7 const board
= VariantRules
.GetBoard(args
.position
);
8 const orientation
= args
.orientation
|| "w";
10 if (!!args
.marks
&& args
.marks
!= "-")
12 // Turn (human) marks into coordinates
13 markArray
= doubleArray(sizeX
, sizeY
, false);
14 let squares
= args
.marks
.split(",");
15 for (let i
=0; i
<squares
.length
; i
++)
17 const coords
= V
.SquareToCoords(squares
[i
]);
18 markArray
[coords
.x
][coords
.y
] = true;
22 if (!!args
.shadow
&& args
.shadow
!= "-")
24 // Turn (human) shadow indications into coordinates
25 shadowArray
= doubleArray(sizeX
, sizeY
, false);
26 let squares
= args
.shadow
.split(",");
27 for (let i
=0; i
<squares
.length
; i
++)
29 const rownum
= V
.size
.x
- parseInt(squares
[i
]);
33 for (let i
=0; i
<V
.size
.y
; i
++)
34 shadowArray
[rownum
][i
] = true;
37 if (squares
[i
].length
== 1)
39 // Shadow a full column
40 const colnum
= V
.ColumnToCoord(squares
[i
]);
41 for (let i
=0; i
<V
.size
.x
; i
++)
42 shadowArray
[i
][colnum
] = true;
45 if (squares
[i
].indexOf("-") >= 0)
47 // Shadow a range of squares, horizontally or vertically
48 const firstLastSq
= squares
[i
].split("-");
51 V
.SquareToCoords(firstLastSq
[0]),
52 V
.SquareToCoords(firstLastSq
[1])
56 range
[1].x
== range
[0].x
58 : (range
[1].x
- range
[0].x
) / Math
.abs(range
[1].x
- range
[0].x
),
59 range
[1].y
== range
[0].y
61 : (range
[1].y
- range
[0].y
) / Math
.abs(range
[1].y
- range
[0].y
)
63 // Convention: range always from smaller to larger number
64 for (let x
=range
[0].x
, y
=range
[0].y
; x
<= range
[1].x
&& y
<= range
[1].y
;
65 x
+= step
[0], y
+= step
[1])
67 shadowArray
[x
][y
] = true;
71 // Shadow just one square:
72 const coords
= V
.SquareToCoords(squares
[i
]);
73 shadowArray
[coords
.x
][coords
.y
] = true;
77 const [startX
,startY
,inc
] = orientation
== 'w'
79 : [sizeX
-1, sizeY
-1, -1];
80 for (let i
=startX
; i
>=0 && i
<sizeX
; i
+=inc
)
82 boardDiv
+= "<div class='row'>";
83 for (let j
=startY
; j
>=0 && j
<sizeY
; j
+=inc
)
85 boardDiv
+= "<div class='board board" + sizeY
+ " " +
86 ((i
+j
)%2==0 ? "light-square-diag" : "dark-square-diag") +
87 (shadowArray
.length
> 0 && shadowArray
[i
][j
] ? " in-shadow" : "") +
89 if (board
[i
][j
] != V
.EMPTY
)
91 boardDiv
+= "<img src='/images/pieces/" +
92 V
.getPpath(board
[i
][j
]) + ".svg' class='piece'/>";
94 if (markArray
.length
> 0 && markArray
[i
][j
])
95 boardDiv
+= "<img src='/images/mark.svg' class='mark-square'/>";