6e095c99ac628ffaacc3482255efcecfffffa128
1 import { ArrayFun
} from "@/utils/array";
3 // Turn (human) marks into coordinates
4 function getMarkArray(marks
)
6 if (!marks
|| marks
== "-")
8 let markArray
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, false);
9 const squares
= marks
.split(",");
10 for (let i
=0; i
<squares
.length
; i
++)
12 const coords
= V
.SquareToCoords(squares
[i
]);
13 markArray
[coords
.x
][coords
.y
] = true;
18 // Turn (human) shadow indications into coordinates
19 function getShadowArray(shadow
)
21 if (!shadow
|| shadow
== "-")
23 let shadowArray
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, false);
24 const squares
= shadow
.split(",");
25 for (let i
=0; i
<squares
.length
; i
++)
27 const rownum
= V
.size
.x
- parseInt(squares
[i
]);
31 for (let i
=0; i
<V
.size
.y
; i
++)
32 shadowArray
[rownum
][i
] = true;
35 if (squares
[i
].length
== 1)
37 // Shadow a full column
38 const colnum
= V
.ColumnToCoord(squares
[i
]);
39 for (let i
=0; i
<V
.size
.x
; i
++)
40 shadowArray
[i
][colnum
] = true;
43 if (squares
[i
].indexOf("-") >= 0)
45 // Shadow a range of squares, horizontally or vertically
46 const firstLastSq
= squares
[i
].split("-");
49 V
.SquareToCoords(firstLastSq
[0]),
50 V
.SquareToCoords(firstLastSq
[1])
54 range
[1].x
== range
[0].x
56 : (range
[1].x
- range
[0].x
) / Math
.abs(range
[1].x
- range
[0].x
),
57 range
[1].y
== range
[0].y
59 : (range
[1].y
- range
[0].y
) / Math
.abs(range
[1].y
- range
[0].y
)
61 // Convention: range always from smaller to larger number
62 for (let x
=range
[0].x
, y
=range
[0].y
; x
<= range
[1].x
&& y
<= range
[1].y
;
63 x
+= step
[0], y
+= step
[1])
65 shadowArray
[x
][y
] = true;
69 // Shadow just one square:
70 const coords
= V
.SquareToCoords(squares
[i
]);
71 shadowArray
[coords
.x
][coords
.y
] = true;
76 // args: object with position (mandatory), and
77 // orientation, marks, shadow (optional)
78 export function getDiagram(args
)
80 // Obtain the array of pieces images names:
81 const board
= V
.GetBoard(args
.position
);
82 const orientation
= args
.orientation
|| "w";
83 const markArray
= getMarkArray(args
.marks
);
84 const shadowArray
= getShadowArray(args
.shadow
);
86 const [startX
,startY
,inc
] = orientation
== 'w'
88 : [V
.size
.x
-1, V
.size
.y
-1, -1];
89 for (let i
=startX
; i
>=0 && i
<V
.size
.x
; i
+=inc
)
91 boardDiv
+= "<div class='row'>";
92 for (let j
=startY
; j
>=0 && j
<V
.size
.y
; j
+=inc
)
94 boardDiv
+= "<div class='board board" + V
.size
.y
+ " " +
95 ((i
+j
)%2==0 ? "light-square-diag" : "dark-square-diag") +
96 (shadowArray
.length
> 0 && shadowArray
[i
][j
] ? " in-shadow" : "") +
98 if (board
[i
][j
] != V
.EMPTY
)
100 boardDiv
+= "<img src='/images/pieces/" +
101 V
.getPpath(board
[i
][j
]) + ".svg' class='piece'/>";
103 if (markArray
.length
> 0 && markArray
[i
][j
])
104 boardDiv
+= "<img src='/images/mark.svg' class='mark-square'/>";
105 boardDiv
+= "</div>";
107 boardDiv
+= "</div>";