Draft code reorganisation (+ fix Alice rules + stateless VariantRules object)
[vchess.git] / public / javascripts / utils / printDiagram.js
CommitLineData
b6487fb9
BA
1// Turn (human) marks into coordinates
2function getMarkArray(marks)
da06a6eb 3{
b6487fb9
BA
4 if (!marks || marks == "-")
5 return [];
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++)
da06a6eb 9 {
b6487fb9
BA
10 const coords = V.SquareToCoords(squares[i]);
11 markArray[coords.x][coords.y] = true;
da06a6eb 12 }
b6487fb9
BA
13 return markArray;
14}
15
16// Turn (human) shadow indications into coordinates
17function getShadowArray(shadow)
18{
19 if (!shadow || shadow == "-")
20 return [];
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++)
5915f720 24 {
b6487fb9
BA
25 const rownum = V.size.x - parseInt(squares[i]);
26 if (!isNaN(rownum))
5915f720 27 {
b6487fb9
BA
28 // Shadow a full row
29 for (let i=0; i<V.size.y; i++)
30 shadowArray[rownum][i] = true;
31 continue;
32 }
33 if (squares[i].length == 1)
34 {
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;
39 continue;
40 }
41 if (squares[i].indexOf("-") >= 0)
42 {
43 // Shadow a range of squares, horizontally or vertically
44 const firstLastSq = squares[i].split("-");
45 const range =
46 [
47 V.SquareToCoords(firstLastSq[0]),
48 V.SquareToCoords(firstLastSq[1])
49 ];
50 const step =
51 [
52 range[1].x == range[0].x
53 ? 0
54 : (range[1].x - range[0].x) / Math.abs(range[1].x - range[0].x),
55 range[1].y == range[0].y
56 ? 0
57 : (range[1].y - range[0].y) / Math.abs(range[1].y - range[0].y)
58 ];
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])
69f3d801 62 {
b6487fb9 63 shadowArray[x][y] = true;
69f3d801 64 }
b6487fb9 65 continue;
5915f720 66 }
b6487fb9
BA
67 // Shadow just one square:
68 const coords = V.SquareToCoords(squares[i]);
69 shadowArray[coords.x][coords.y] = true;
5915f720 70 }
b6487fb9
BA
71 return shadowArray;
72}
73
74// args: object with position (mandatory), and
75// orientation, marks, shadow (optional)
76function getDiagram(args)
77{
78 // Obtain the array of pieces images names:
79 const board = VariantRules.GetBoard(args.position);
80 const orientation = args.orientation || "w";
81 const markArray = getMarkArray(args.marks);
82 const shadowArray = getShadowArray(args.shadow);
da06a6eb
BA
83 let boardDiv = "";
84 const [startX,startY,inc] = orientation == 'w'
85 ? [0, 0, 1]
b6487fb9
BA
86 : [V.size.x-1, V.size.y-1, -1];
87 for (let i=startX; i>=0 && i<V.size.x; i+=inc)
da06a6eb
BA
88 {
89 boardDiv += "<div class='row'>";
b6487fb9 90 for (let j=startY; j>=0 && j<V.size.y; j+=inc)
da06a6eb 91 {
b6487fb9 92 boardDiv += "<div class='board board" + V.size.y + " " +
5915f720
BA
93 ((i+j)%2==0 ? "light-square-diag" : "dark-square-diag") +
94 (shadowArray.length > 0 && shadowArray[i][j] ? " in-shadow" : "") +
95 "'>";
da06a6eb
BA
96 if (board[i][j] != V.EMPTY)
97 {
98 boardDiv += "<img src='/images/pieces/" +
99 V.getPpath(board[i][j]) + ".svg' class='piece'/>";
100 }
5915f720 101 if (markArray.length > 0 && markArray[i][j])
da06a6eb
BA
102 boardDiv += "<img src='/images/mark.svg' class='mark-square'/>";
103 boardDiv += "</div>";
104 }
105 boardDiv += "</div>";
106 }
107 return boardDiv;
108}