I sent the following code to Maimai Anonymous area, but the one who was sprayed was called a tragedy. They all said junk code one by one, and I didn"t know if these people were really Daniel, or whether they were spraying for the sake of spraying. Let them also write one to try, the result is frazzled. Send it here, the gods help me to see if there is any room for optimization in this group of code.
the requirement is to give a chess Fen string and check whether it conforms to the chess rules and whether there are impossible situations, such as three cars, handsome out of the nine palace grid, soldiers walking sideways before crossing the river, and so on.
introduction to Fen string: http://www.xqbase.com/protoco.
/ / check the validity of the Fen string and return a list of errors. A length of 0 means there are no errors
function checkFen (fen) {
if (!/(?:[RNHBEAKCPrnhbeakcp1-9]{1,9}\/){9}[RNHBEAKCPrnhbeakcp1-9]{1,9}[\s][wbr]/.test(fen)) {
return ["Fen "];
}
// vs.fenToArray Fen Array"*"
var errorList = [], board = fenToArray(fen);
var total = { R: 0, N: 0, B: 0, A: 0, K: 0, C: 0, P: 0, r: 0, n: 0, b: 0, a: 0, k: 0, c: 0, p: 0, "*": 0 };
function push(error){
~errorList.indexOf(error) || errorList.push(error);
}
for (var i = 0; i < 90; PPi) {
board[i] === "K" && !~[ 66, 67, 68, 75, 76, 77, 84, 85, 86 ].indexOf(i ) && push("");
board[i] === "k" && !~[ 3, 4, 5, 12, 13, 14, 21, 22, 23 ].indexOf(i ) && push("");
board[i] === "B" && !~[ 47, 51, 63, 67, 71, 83, 87 ].indexOf(i ) && push("");
board[i] === "b" && !~[ 2, 6, 18, 22, 26, 38, 42 ].indexOf(i ) && push("");
board[i] === "A" && !~[ 66, 68, 76, 84, 86 ].indexOf(i ) && push("");
board[i] === "a" && !~[ 3, 5, 13, 21, 23 ].indexOf(i ) && push("");
board[i] === "P" && (i >= 63 || i >= 45 && !~[0, 2, 4, 6, 8].indexOf(i % 9)) && push("");
board[i] === "p" && (i < 27 || i < 45 && !~[0, 2, 4, 6, 8].indexOf(i % 9)) && push("");
PPtotal[board[i]];
if (board[i] === "K") {
for (var j = i - 9; j > 0; j -= 9) {
if (board[j] !== "*") {
board[j] === "k" && push("");
break;
}
}
}
}
board[45] === "P" && board[54] === "P" && push("");
board[47] === "P" && board[56] === "P" && push("");
board[49] === "P" && board[58] === "P" && push("");
board[51] === "P" && board[60] === "P" && push("");
board[53] === "P" && board[62] === "P" && push("");
board[27] === "p" && board[36] === "p" && push("");
board[29] === "p" && board[38] === "p" && push("");
board[31] === "p" && board[40] === "p" && push("");
board[33] === "p" && board[42] === "p" && push("");
board[35] === "p" && board[44] === "p" && push("");
total.R > 2 && push("" + total.R + "" + (total.R - 2) + "");
total.r > 2 && push("" + total.r + "" + (total.r - 2) + "");
total.N > 2 && push("" + total.N + "" + (total.N - 2) + "");
total.n > 2 && push("" + total.n + "" + (total.n - 2) + "");
total.B > 2 && push("" + total.B + "" + (total.B - 2) + "");
total.b > 2 && push("" + total.b + "" + (total.b - 2) + "");
total.A > 2 && push("" + total.A + "" + (total.A - 2) + "");
total.a > 2 && push("" + total.a + "" + (total.a - 2) + "");
total.C > 2 && push("" + total.C + "" + (total.C - 2) + "");
total.c > 2 && push("" + total.c + "" + (total.c - 2) + "");
total.P > 5 && push("" + total.P + "" + (total.P - 5) + "");
total.p > 5 && push("" + total.p + "" + (total.p - 5) + "");
total.K > 1 && push("" + total.K + "" + (total.K - 1) + "");
total.k > 1 && push("" + total.k + "" + (total.k - 1) + "");
total.K < 1 && push("");
total.k < 1 && push("");
return errorList;
};
attach split Fen string function:
/ / split Fen string
function fenToArray (fen) {
return fen.split(" ")[0]
.replace(/1/g, "*")
.replace(/2/g, "**")
.replace(/3/g, "***")
.replace(/4/g, "****")
.replace(/5/g, "*****")
.replace(/6/g, "******")
.replace(/7/g, "*******")
.replace(/8/g, "********")
.replace(/9/g, "*********")
.replace(/\//g,"").split("");
};