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