filterValid(moves) {
    // Remove moves which let the king mate-captured:
    if (moves.length == 0) return [];
    const color = this.turn;
    const oppCol = V.GetOppCol(color);
    return moves.filter(m => {
      let res = true;
      this.play(m);
      if (this.underCheck(color)) {
        res = false;
        const attacked = this.kingPos[color];
        // Try to find a move to escape check
        // TODO: very inefficient method.
        outerLoop: for (let i=0; i<V.size.x; i++) {
          for (let j=0; j<V.size.y; j++) {
            if (this.getColor(i,j) == color) {
              let emoves = [];
              // Artficial turn change to "play twice":
              this.turn = color;
              switch (this.getPiece(i, j)) {
                case V.PAWN:
                  emoves = this.getPotentialPawnMoves([i, j]);
                  break;
                case V.ROOK:
                  emoves = this.getPotentialRookMoves([i, j]);
                  break;
                case V.KNIGHT:
                  emoves = this.getPotentialKnightMoves([i, j]);
                  break;
                case V.BISHOP:
                  emoves = this.getPotentialBishopMoves([i, j]);
                  break;
                case V.QUEEN:
                  emoves = this.getPotentialQueenMoves([i, j]);
                  break;
                case V.KING:
                  emoves = this.getPotentialKingMoves([i, j]);
                  break;
              }
              this.turn = oppCol;
              for (let em of emoves) {
                V.PlayOnBoard(this.board, em);
                let sq = attacked;
                if (em.start.x == attacked[0] && em.start.y == attacked[1])
                  // King moved:
                  sq = [em.appear[0].x, em.appear[0].y];
                if (!this.isAttacked(sq, oppCol))
                  res = true;
                V.UndoOnBoard(this.board, em);
                if (res)
                  // No need to explore more moves
                  break outerLoop;
              }
            }
          }
        }
      }
      this.undo(m);
      return res;
    });
  }
// TODO: allow pieces to "commit suicide"? (Currently yes except king)
allow pieces to "commit suicide". (Currently yes except king)
allow pieces to " commit suicide " . ( Currently yes except king )
filterValid(moves) {
    if (moves.length == 0) return [];
    const color = this.turn;
    const oppCol = V.GetOppCol(color);
    return moves.filter(m => {
      let res = true;
      this.play(m);
      if (this.underCheck(color)) {
        res = false;
        const attacked = this.kingPos[color];
        outerLoop: for (let i=0; i<V.size.x; i++) {
          for (let j=0; j<V.size.y; j++) {
            if (this.getColor(i,j) == color) {
              let emoves = [];
              this.turn = color;
              switch (this.getPiece(i, j)) {
                case V.PAWN:
                  emoves = this.getPotentialPawnMoves([i, j]);
                  break;
                case V.ROOK:
                  emoves = this.getPotentialRookMoves([i, j]);
                  break;
                case V.KNIGHT:
                  emoves = this.getPotentialKnightMoves([i, j]);
                  break;
                case V.BISHOP:
                  emoves = this.getPotentialBishopMoves([i, j]);
                  break;
                case V.QUEEN:
                  emoves = this.getPotentialQueenMoves([i, j]);
                  break;
                case V.KING:
                  emoves = this.getPotentialKingMoves([i, j]);
                  break;
              }
              this.turn = oppCol;
              for (let em of emoves) {
                V.PlayOnBoard(this.board, em);
                let sq = attacked;
                if (em.start.x == attacked[0] && em.start.y == attacked[1])
                  sq = [em.appear[0].x, em.appear[0].y];
                if (!this.isAttacked(sq, oppCol))
                  res = true;
                V.UndoOnBoard(this.board, em);
                if (res)
                  break outerLoop;
              }
            }
          }
        }
      }
      this.undo(m);
      return res;
    });
  }
filterValid ( moves ) { if ( moves . length == 0 ) return [ ] ; const color = this . turn ; const oppCol = V . GetOppCol ( color ) ; return moves . filter ( m => { let res = true ; this . play ( m ) ; if ( this . underCheck ( color ) ) { res = false ; const attacked = this . kingPos [ color ] ; outerLoop : for ( let i = 0 ; i < V . size . x ; i ++ ) { for ( let j = 0 ; j < V . size . y ; j ++ ) { if ( this . getColor ( i , j ) == color ) { let emoves = [ ] ; this . turn = color ; switch ( this . getPiece ( i , j ) ) { case V . PAWN : emoves = this . getPotentialPawnMoves ( [ i , j ] ) ; break ; case V . ROOK : emoves = this . getPotentialRookMoves ( [ i , j ] ) ; break ; case V . KNIGHT : emoves = this . getPotentialKnightMoves ( [ i , j ] ) ; break ; case V . BISHOP : emoves = this . getPotentialBishopMoves ( [ i , j ] ) ; break ; case V . QUEEN : emoves = this . getPotentialQueenMoves ( [ i , j ] ) ; break ; case V . KING : emoves = this . getPotentialKingMoves ( [ i , j ] ) ; break ; } this . turn = oppCol ; for ( let em of emoves ) { V . PlayOnBoard ( this . board , em ) ; let sq = attacked ; if ( em . start . x == attacked [ 0 ] && em . start . y == attacked [ 1 ] ) sq = [ em . appear [ 0 ] . x , em . appear [ 0 ] . y ] ; if ( ! this . isAttacked ( sq , oppCol ) ) res = true ; V . UndoOnBoard ( this . board , em ) ; if ( res ) break outerLoop ; } } } } } this . undo ( m ) ; return res ; } ) ; }
TODO: allow pieces to "commit suicide"?
TODO : allow pieces to " commit suicide " ?
// Remove moves which let the king mate-captured: // Try to find a move to escape check // TODO: very inefficient method. // Artficial turn change to "play twice": // King moved: // No need to explore more moves