| 1 | // Assuming V(ariantRules) class is loaded. |
| 2 | // args: object with position (mandatory), orientation, marks (optional) |
| 3 | function 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 && 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 coords = V.SquareToCoords(squares[i]); |
| 18 | markArray[coords.x][coords.y] = true; |
| 19 | } |
| 20 | } |
| 21 | let shadowArray = []; |
| 22 | if (!!args.shadow && args.shadow != "-") |
| 23 | { |
| 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++) |
| 28 | { |
| 29 | const rownum = V.size.x - parseInt(squares[i]); |
| 30 | if (!isNaN(rownum)) |
| 31 | { |
| 32 | // Shadow a full row |
| 33 | for (let i=0; i<V.size.y; i++) |
| 34 | shadowArray[rownum][i] = true; |
| 35 | continue; |
| 36 | } |
| 37 | if (squares[i].length == 1) |
| 38 | { |
| 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; |
| 43 | continue; |
| 44 | } |
| 45 | // Shadow just one square: |
| 46 | const coords = V.SquareToCoords(squares[i]); |
| 47 | shadowArray[coords.x][coords.y] = true; |
| 48 | } |
| 49 | } |
| 50 | let boardDiv = ""; |
| 51 | const [startX,startY,inc] = orientation == 'w' |
| 52 | ? [0, 0, 1] |
| 53 | : [sizeX-1, sizeY-1, -1]; |
| 54 | for (let i=startX; i>=0 && i<sizeX; i+=inc) |
| 55 | { |
| 56 | boardDiv += "<div class='row'>"; |
| 57 | for (let j=startY; j>=0 && j<sizeY; j+=inc) |
| 58 | { |
| 59 | boardDiv += "<div class='board board" + sizeY + " " + |
| 60 | ((i+j)%2==0 ? "light-square-diag" : "dark-square-diag") + |
| 61 | (shadowArray.length > 0 && shadowArray[i][j] ? " in-shadow" : "") + |
| 62 | "'>"; |
| 63 | if (board[i][j] != V.EMPTY) |
| 64 | { |
| 65 | boardDiv += "<img src='/images/pieces/" + |
| 66 | V.getPpath(board[i][j]) + ".svg' class='piece'/>"; |
| 67 | } |
| 68 | if (markArray.length > 0 && markArray[i][j]) |
| 69 | boardDiv += "<img src='/images/mark.svg' class='mark-square'/>"; |
| 70 | boardDiv += "</div>"; |
| 71 | } |
| 72 | boardDiv += "</div>"; |
| 73 | } |
| 74 | return boardDiv; |
| 75 | } |