Failed chess FEN creation

168 views Asked by At

I'm creating a chess engine, and I've run into a problem with my FEN function. The "p" which stands for the black pawn is turned into a 24 in the b array. But only the first instance of "p" is recorded. I've done some amount of debugging, and I (think) it's not the switch function, but beyond that I'm clueless. I'm creating this with the p5.js library, but the actual FEN function is in vanilla for your convenience. Link to the web editor: https://editor.p5js.org/KoderM/sketches/jVVn2-Wxc

FEN function:

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);

For reference:

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
1

There are 1 answers

0
James On BEST ANSWER

As mentioned in a comment, try changing b[index - i] = 12 to b[index + i] = 12, as below:

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);