1 import { ArrayFun
} from "@/utils/array";
3 // Turn (human) marks into coordinates
4 function getMarkArray(marks
) {
5 if (!marks
|| marks
== "-") return [];
6 let markArray
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, false);
7 const squares
= marks
.split(",");
8 for (let i
= 0; i
< squares
.length
; i
++) {
9 const coords
= V
.SquareToCoords(squares
[i
]);
10 markArray
[coords
.x
][coords
.y
] = true;
15 // Turn (human) shadow indications into coordinates
16 function getShadowArray(shadow
) {
17 if (!shadow
|| shadow
== "-") return [];
18 let shadowArray
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, false);
19 const squares
= shadow
.split(",");
20 for (let i
= 0; i
< squares
.length
; i
++) {
21 const rownum
= V
.size
.x
- parseInt(squares
[i
]);
24 for (let i
= 0; i
< V
.size
.y
; i
++) shadowArray
[rownum
][i
] = true;
27 if (squares
[i
].length
== 1) {
28 // Shadow a full column
29 const colnum
= V
.ColumnToCoord(squares
[i
]);
30 for (let i
= 0; i
< V
.size
.x
; i
++) shadowArray
[i
][colnum
] = true;
33 if (squares
[i
].indexOf("-") >= 0) {
34 // Shadow a range of squares, horizontally or vertically
35 const firstLastSq
= squares
[i
].split("-");
37 V
.SquareToCoords(firstLastSq
[0]),
38 V
.SquareToCoords(firstLastSq
[1])
41 range
[1].x
== range
[0].x
43 : (range
[1].x
- range
[0].x
) / Math
.abs(range
[1].x
- range
[0].x
),
44 range
[1].y
== range
[0].y
46 : (range
[1].y
- range
[0].y
) / Math
.abs(range
[1].y
- range
[0].y
)
48 // Convention: range always from smaller to larger number
50 let x
= range
[0].x
, y
= range
[0].y
;
51 x
<= range
[1].x
&& y
<= range
[1].y
;
52 x
+= step
[0], y
+= step
[1]
54 shadowArray
[x
][y
] = true;
58 // Shadow just one square:
59 const coords
= V
.SquareToCoords(squares
[i
]);
60 shadowArray
[coords
.x
][coords
.y
] = true;
65 // args: object with position (mandatory), and
66 // orientation, marks, shadow (optional)
67 export function getDiagram(args
) {
68 // Obtain the array of pieces images names:
69 const board
= V
.GetBoard(args
.position
);
70 const orientation
= args
.orientation
|| "w";
71 const markArray
= getMarkArray(args
.marks
);
72 const shadowArray
= getShadowArray(args
.shadow
);
73 const vr
= new V(); //just for pieces images paths
75 const [startX
, startY
, inc
] =
76 orientation
== "w" ? [0, 0, 1] : [V
.size
.x
- 1, V
.size
.y
- 1, -1];
77 for (let i
= startX
; i
>= 0 && i
< V
.size
.x
; i
+= inc
) {
78 boardDiv
+= "<div class='row'>";
79 for (let j
= startY
; j
>= 0 && j
< V
.size
.y
; j
+= inc
) {
81 "<div class='board board" +
84 ((i
+ j
) % 2 == 0 ? "light-square-diag" : "dark-square-diag") +
85 (shadowArray
.length
> 0 && shadowArray
[i
][j
] ? " in-shadow" : "") +
87 if (board
[i
][j
] != V
.EMPTY
) {
90 "src='/images/pieces/" +
91 vr
.getPpath(board
[i
][j
]) +
95 if (markArray
.length
> 0 && markArray
[i
][j
])
96 boardDiv
+= "<img src='/images/mark.svg' class='mark-square'/>";