-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotations.js
More file actions
185 lines (144 loc) · 4.51 KB
/
notations.js
File metadata and controls
185 lines (144 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
function isFen(maybeFen) {
const pattern = /^(?:[0-9bknpqrBKNPQR]{1,8}\/){7}[0-9bknpqrBKNPQR]{1,8}$/
return pattern.test(maybeFen);
}
function isLefen(maybeLefen) {
const pattern = /^(?:[0-9bknpqrBKNPQR]{1,8}\/){8}[GgRr]\/(?:-1|[0-9a-f]{4})$/
return pattern.test(maybeLefen);
}
function isLeon(maybeLeon) {
const pattern = /^(?:[0-9bknpqrBKNPQR]{1,8}\/){4}[0-9bknpqrBKNPQR]\/[GgRr]\/(?:-1|[0-9a-f]{4})$/
return pattern.test(maybeLeon);
}
function readFenArrayFromCells(cells) {
const fenArray = []
for (let i = 8; i > 0; i--) {
const rowCells = cells.filter(el => el.id.includes(i.toString()));
const row = myglobals.COL_NAMES.map(
c => {
const cell = rowCells.find(el => el.id.includes(c));
if (cell) {
return cell.getAttribute("data-piece") ?? null
}
return null;
}
);
fenArray.push(row);
}
return fenArray;
}
function readQRFromCells(cells) {
let qrBinaryString = "";
for (let row = 0; row < 4; row++) {
for (let col = 3; col >= 0; col--) {
const cell = cells.find(el => el.id === `b${col}${row}`);
if (cell.classList.contains("black")) {
qrBinaryString += "0";
} else {
qrBinaryString += "1";
}
}
}
return parseInt(qrBinaryString, 2).toString(16);
}
function makeFenFromArray(fenArray) {
const fen = fenArray.map(fenRow => makeFenLine(fenRow)).join("/")
return fen;
}
function makeLefenFromArray(fenArray, colour, qrCode) {
const lefen = `${makeFenFromArray(fenArray)}/${colour}/${qrCode}`;
return lefen;
}
function makeLeonFromArray(fenArray, colour, qrCode) {
const {top, right, bottom, left} = parseSides(fenArray);
const centre = fenArray[3][3] ?? "1";
return `${top}/${right}/${bottom}/${left}/${centre}/${colour}/${qrCode}`;
}
function makeArrayFromFen(fenString) {
const fenArray = [];
for (const fenLine of fenString.split("/")) {
const fenRow = [];
for (const fenChar of fenLine) {
if (isNaN(parseInt(fenChar))) {
fenRow.push(fenChar);
} else {
for (let i = 0; i < parseInt(fenChar); i++) {
fenRow.push(null);
}
}
}
fenArray.push(fenRow);
}
return fenArray;
}
function parseLefen(lefenString) {
const lefenArray = lefenString.split("/");
const fenArray = makeArrayFromFen(lefenArray.slice(0, 8).join("/"));
return {
fenArray,
colour: lefenArray[8],
qrString: lefenArray[9]
};
}
function makeFenArrayFromLeonBoard(leonBoardArray) {
const [top, right, bottom, left, [centre]] = makeArrayFromFen(leonBoardArray);
const fenArray = [top];
for (let i = 1; i < 7; i++) {
const fenRow = Array(8).fill(null);
if (i === 3 || i === 4) {
fenRow[3] = centre;
fenRow[4] = centre;
}
fenRow[0] = left[i]
fenRow[7] = right[i];
fenArray.push(fenRow);
}
fenArray.push(bottom);
console.log(fenArray);
return fenArray;
}
function parseLeon(leonString) {
const leonArray = leonString.split("/");
const fenArray = makeFenArrayFromLeonBoard(leonArray.slice(0, 5).join("/"))
return {
fenArray,
colour: leonArray[5],
qrString: leonArray[6]
};
}
function makeFenLine(symbolLine) {
let line = "";
let emptyNum = 0;
for (const symChar of symbolLine) {
if (!symChar) {
emptyNum++;
continue;
}
if (emptyNum) {
line += emptyNum.toString();
emptyNum = 0;
}
line += symChar;
}
if (emptyNum !== 0) {
line += emptyNum.toString();
}
return line;
}
function getColumn(symbols, colIndex) {
return symbols.map(symbolLine => symbolLine[colIndex]);
}
function parseSides(symbols) {
if (!symbols) return {
top: "8",
bottom: "8",
left: "8",
right: "8",
}
return {
top: makeFenLine(symbols[0]),
bottom: makeFenLine(symbols[symbols.length - 1]),
left: makeFenLine(getColumn(symbols, 0)),
right: makeFenLine(getColumn(symbols, symbols.length - 1)),
}
}