On the way to problems: saving state [not functional yet]
[vchess.git] / public / javascripts / utils / printDiagram.js
CommitLineData
da06a6eb
BA
1// Assuming V(ariantRules) class is loaded.
2// args: object with position (mandatory), orientation, marks (optional)
3function getDiagram(args)
4{
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";
9 let markArray = [];
10 if (!!args.marks)
11 {
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++)
16 {
17 const res = /^([a-z]+)([0-9]+)$/i.exec(squares[i]);
18 const x = sizeX - parseInt(res[2]); //white at bottom, so counting is reversed
19 const y = res[1].charCodeAt(0)-97; //always one char: max 26, big enough
20 markArray[x][y] = true;
21 }
22 }
23 let boardDiv = "";
24 const [startX,startY,inc] = orientation == 'w'
25 ? [0, 0, 1]
26 : [sizeX-1, sizeY-1, -1];
27 for (let i=startX; i>=0 && i<sizeX; i+=inc)
28 {
29 boardDiv += "<div class='row'>";
30 for (let j=startY; j>=0 && j<sizeY; j+=inc)
31 {
32 boardDiv += "<div class='board board" + sizeY + " " +
33 ((i+j)%2==0 ? "light-square-diag" : "dark-square-diag") + "'>";
34 if (board[i][j] != V.EMPTY)
35 {
36 boardDiv += "<img src='/images/pieces/" +
37 V.getPpath(board[i][j]) + ".svg' class='piece'/>";
38 }
39 if (!!args.marks && markArray[i][j])
40 boardDiv += "<img src='/images/mark.svg' class='mark-square'/>";
41 boardDiv += "</div>";
42 }
43 boardDiv += "</div>";
44 }
45 return boardDiv;
46}