失败的国际象棋 FEN 创建
Failed chess FEN creation
我正在创建国际象棋引擎,运行 我的 FEN 函数出现问题。代表黑兵的“p”在b
数组中变成了24。但只记录了“p”的第一个实例。我已经进行了一些调试,我(认为)它不是 switch
函数,但除此之外我一无所知。我正在使用 p5.js 库创建它,但为了您的方便,实际的 FEN 函数是原版的。 Link 至网络编辑器:https://editor.p5js.org/KoderM/sketches/jVVn2-Wxc
FEN 函数:
function fromFen(fen) {
const fS = fen.split(" ");
const toMove = fS[1];
const castleAbility = fS[2];
const enpassant = fS[3];
const halfMoves = fS[4];
const fullMoves = fS[5];
let b = [1];
console.log("Filtered: " + fS[0].split("").filter(f => f !== "/"));
fS[0].split("").filter(f => f !== "/").forEach(s => {
const index = b.indexOf(1);
console.log("S = " + s + ". Index = " + index + ".");
if (/^\d$/.test(s)) {
for (let i = 0; i < Number(s); i++) {
b[index - i] = 12;
}
b[index + Number(s)] = 1;
} else {
switch (s) {
case "p":
b[index] = 24;
break;
case "n":
b[index] = 25;
break;
case "b":
b[index] = 26;
break;
case "r":
b[index] = 27;
break;
case "k":
b[index] = 28;
break;
case "q":
b[index] = 29;
break;
case "P":
b[index] = 14;
break;
case "N":
b[index] = 15;
break;
case "B":
b[index] = 16;
break;
case "R":
b[index] = 17;
break;
case "K":
b[index] = 18;
break;
case "Q":
b[index] = 19;
break;
default:
throw new Error("Invalid Fen: " + s);
}
b[index + 1] = 1;
}
});
b.pop();
let result = [];
console.log("result: " + b + " length: " + b.length);
while (b.length > 7) result.push(b.splice(0, 8));
return {
board: result,
turn: toMove,
castle: castleAbility,
halfMoves: halfMoves,
fullMoves: fullMoves
};
}
const FENTest = fromFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
console.log(FENTest.board);
供参考:
Rows = y (v)
Cols = x (>)
board conditions:
2: empty
3: mouseOver
4: pawn
5: knight
6: bishop
7: rook
8: queen
9: king
1: white
2: black
ex: white pawn: 14
ex: black king: 29
ex empty: 12
如评论中所述,尝试将b[index - i] = 12
更改为b[index + i] = 12
,如下:
function fromFen(fen) {
const fS = fen.split(" ");
const toMove = fS[1];
const castleAbility = fS[2];
const enpassant = fS[3];
const halfMoves = fS[4];
const fullMoves = fS[5];
let b = [1];
console.log("Filtered: " + fS[0].split("").filter(f => f !== "/"));
fS[0].split("").filter(f => f !== "/").forEach(s => {
const index = b.indexOf(1);
console.log("S = " + s + ". Index = " + index + ".");
if (/^\d$/.test(s)) {
for (let i = 0; i < Number(s); i++) {
b[index + i] = 12;
}
b[index + Number(s)] = 1;
} else {
switch (s) {
case "p":
b[index] = 24;
break;
case "n":
b[index] = 25;
break;
case "b":
b[index] = 26;
break;
case "r":
b[index] = 27;
break;
case "k":
b[index] = 28;
break;
case "q":
b[index] = 29;
break;
case "P":
b[index] = 14;
break;
case "N":
b[index] = 15;
break;
case "B":
b[index] = 16;
break;
case "R":
b[index] = 17;
break;
case "K":
b[index] = 18;
break;
case "Q":
b[index] = 19;
break;
default:
throw new Error("Invalid Fen: " + s);
}
b[index + 1] = 1;
}
});
b.pop();
let result = [];
console.log("result: " + b + " length: " + b.length);
while (b.length > 7) result.push(b.splice(0, 8));
return {
board: result,
turn: toMove,
castle: castleAbility,
halfMoves: halfMoves,
fullMoves: fullMoves
};
}
const FENTest = fromFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
console.log(FENTest.board);
我正在创建国际象棋引擎,运行 我的 FEN 函数出现问题。代表黑兵的“p”在b
数组中变成了24。但只记录了“p”的第一个实例。我已经进行了一些调试,我(认为)它不是 switch
函数,但除此之外我一无所知。我正在使用 p5.js 库创建它,但为了您的方便,实际的 FEN 函数是原版的。 Link 至网络编辑器:https://editor.p5js.org/KoderM/sketches/jVVn2-Wxc
FEN 函数:
function fromFen(fen) {
const fS = fen.split(" ");
const toMove = fS[1];
const castleAbility = fS[2];
const enpassant = fS[3];
const halfMoves = fS[4];
const fullMoves = fS[5];
let b = [1];
console.log("Filtered: " + fS[0].split("").filter(f => f !== "/"));
fS[0].split("").filter(f => f !== "/").forEach(s => {
const index = b.indexOf(1);
console.log("S = " + s + ". Index = " + index + ".");
if (/^\d$/.test(s)) {
for (let i = 0; i < Number(s); i++) {
b[index - i] = 12;
}
b[index + Number(s)] = 1;
} else {
switch (s) {
case "p":
b[index] = 24;
break;
case "n":
b[index] = 25;
break;
case "b":
b[index] = 26;
break;
case "r":
b[index] = 27;
break;
case "k":
b[index] = 28;
break;
case "q":
b[index] = 29;
break;
case "P":
b[index] = 14;
break;
case "N":
b[index] = 15;
break;
case "B":
b[index] = 16;
break;
case "R":
b[index] = 17;
break;
case "K":
b[index] = 18;
break;
case "Q":
b[index] = 19;
break;
default:
throw new Error("Invalid Fen: " + s);
}
b[index + 1] = 1;
}
});
b.pop();
let result = [];
console.log("result: " + b + " length: " + b.length);
while (b.length > 7) result.push(b.splice(0, 8));
return {
board: result,
turn: toMove,
castle: castleAbility,
halfMoves: halfMoves,
fullMoves: fullMoves
};
}
const FENTest = fromFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
console.log(FENTest.board);
供参考:
Rows = y (v)
Cols = x (>)
board conditions:
2: empty
3: mouseOver
4: pawn
5: knight
6: bishop
7: rook
8: queen
9: king
1: white
2: black
ex: white pawn: 14
ex: black king: 29
ex empty: 12
如评论中所述,尝试将b[index - i] = 12
更改为b[index + i] = 12
,如下:
function fromFen(fen) {
const fS = fen.split(" ");
const toMove = fS[1];
const castleAbility = fS[2];
const enpassant = fS[3];
const halfMoves = fS[4];
const fullMoves = fS[5];
let b = [1];
console.log("Filtered: " + fS[0].split("").filter(f => f !== "/"));
fS[0].split("").filter(f => f !== "/").forEach(s => {
const index = b.indexOf(1);
console.log("S = " + s + ". Index = " + index + ".");
if (/^\d$/.test(s)) {
for (let i = 0; i < Number(s); i++) {
b[index + i] = 12;
}
b[index + Number(s)] = 1;
} else {
switch (s) {
case "p":
b[index] = 24;
break;
case "n":
b[index] = 25;
break;
case "b":
b[index] = 26;
break;
case "r":
b[index] = 27;
break;
case "k":
b[index] = 28;
break;
case "q":
b[index] = 29;
break;
case "P":
b[index] = 14;
break;
case "N":
b[index] = 15;
break;
case "B":
b[index] = 16;
break;
case "R":
b[index] = 17;
break;
case "K":
b[index] = 18;
break;
case "Q":
b[index] = 19;
break;
default:
throw new Error("Invalid Fen: " + s);
}
b[index + 1] = 1;
}
});
b.pop();
let result = [];
console.log("result: " + b + " length: " + b.length);
while (b.length > 7) result.push(b.splice(0, 8));
return {
board: result,
turn: toMove,
castle: castleAbility,
halfMoves: halfMoves,
fullMoves: fullMoves
};
}
const FENTest = fromFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
console.log(FENTest.board);