-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfenSet.js
More file actions
111 lines (92 loc) · 3.01 KB
/
Copy pathfenSet.js
File metadata and controls
111 lines (92 loc) · 3.01 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
const isPiece = function(input){
if (typeof fenSet.symbolMap[input] === 'undefined'){ //bit dodgy, but works
return false;
}
else return true;
}
const isEmpty = function(input){
if (input >"0" && input <"9"){
return true;
}
return false;
}
var fenSet = { //reassign this if using a different font.
symbolMap: {
"K":"Kk",
"Q":"Qq",
"B":"Bb",
"N":"Nn",
"R":"Rr",
"P":"Pp",
"k":"Ll",
"q":"Ww",
"b":"Vv",
"n":"Mm",
"r":"Tt",
"p":"Oo",
empty:"zx"
},
fenToDiagram: function(fenString, flip, darkMode){
let row = 0; //row is used to determine square colour
if (darkMode){
++row;
}
let square = 0;
let output = "";
let currSquare = "";
let i = 0;
while(square<64){
currSquare = fenString[i];
if (isPiece(currSquare)){
if (darkMode){
if (currSquare<="Z"){ //checks if uppercase assuming currSquare is an ASCII letter
currSquare = currSquare.toLowerCase();
}
else{
currSquare = currSquare.toUpperCase();
}
}
output += this.symbolMap[currSquare][(square+row)%2];
++square;
}
else if (isEmpty(currSquare)){
while (currSquare > 0){
output += this.symbolMap.empty[(square+row)%2];
++square;
--currSquare;
}
}
else if (currSquare === "/"){
output += "\n";
++row;
if (square%8 != 0){ //if row is the wrong size, the FEN string is invalid
console.log("invalid FEN string!");
return("");
}
}
else{ //if unexpected character, the FEN string is invalid
console.log("invalid FEN string!");
return("");
}
++i;
}
if (flip){
output = output.split(""); //string reversal
output = output.reverse();
output = output.join("");
}
if (fenString[i]!=" "){ //if after 64 squares, there's still things to parse, the FEN string is invalid
console.log("invalid FEN string!");
return("");
}
return(output);
}
}
window.addEventListener('DOMContentLoaded', () => {
var fenDivs = document.getElementsByClassName("fen");
for (var i = 0; i < fenDivs.length; i++){
let darkMode = fenDivs[i].className.includes("darkmode");
let flipped = fenDivs[i].className.includes("flipped");
fenDivs[i].innerText = fenSet.fenToDiagram(fenDivs[i].innerText, flipped, darkMode);
}
})