How to stop my terminal from printing my board array output on multiple lines

Viewed 116

I am trying to print a chess board represented by nested arrays in JS. However when I am trying to check my solutions visually my terminal is printing each nested array onto two separate lines, which makes it hard to visually check. I want my nested arrays to print on their own lines so it looks like a chess board, but I can't see where I might be able to change this and haven't found any solutions online. I have tried to run the code in my VScode terminal as well as my iterm2 terminal. Thanks very much for your time! Code looks like this:

const arraysEqual = (a, b) => {
  if (a === b) return true;
  if (a == null || b == null) return false;
  if (a.length !== b.length) return false;
  for (var i = 0; i < a.length; ++i) {
    if (a[i] !== b[i]) return false;
  }
  return true;
}
const generateBoard = (whiteQueen, blackQueen) => {
  const boardArr = []
  for(let i = 0; i < 8; i++) {
    boardArr.push([])
  }
  for (let y = 0; y < 8; y++) {
    for (let x = 0; x < 8; x++) {
      let currentPos = [y, x];
      if (!(arraysEqual(currentPos, whiteQueen)) && !(arraysEqual(currentPos, blackQueen))){
        boardArr[y][x] = 0; 
      } else if (arraysEqual(currentPos, whiteQueen)) {
        boardArr[y][x] = 1;
      } else if (arraysEqual(currentPos, blackQueen)) {
        boardArr[y][x] = 1;
      }
    }
  }
  return boardArr;
}

const queenThreat = (generatedBoard) => {
  let queen1;
  let queen2;
  for (let y = 0; y < generatedBoard.length; y++) {
    for (let x = 0; x < generatedBoard[y].length; x++){
      let currentNode = generatedBoard[y][x];
      if(currentNode === 1){
        if(queen1 === undefined){
          queen1 = [y,x];
        } else {
          queen2 = [y,x]
          }
      }
    }
  }
  //same row and same column match check
  if (queen2[0] === queen1[0] || queen2[1] === queen1[1]){
    return true
  }
  //diagonal match check
  for (let i = 0; i < 8; i++) {
    if (queen1[0] + i === queen2[0] && queen1[1] + i === queen2[1]){
      return true;
    }
    if (queen1[0] + i === queen2[0] && queen1[1] - i === queen2[1]){
      return true; 
    }
  }
  return false;
}

// let whiteQueen = [0, 0];
// let blackQueen = [5, 7];
// let generatedBoard = generateBoard(whiteQueen, blackQueen);
// console.log(generatedBoard);
// console.log(queenThreat(generatedBoard));

whiteQueen = [0, 6];
blackQueen = [3, 3];
generatedBoard = generateBoard(whiteQueen, blackQueen);
console.log(generatedBoard);
console.log(queenThreat(generatedBoard));

output looks like this:

[
  [
    0, 0, 0, 0,
    0, 0, 1, 0
  ],
  [
    0, 0, 0, 0,
    0, 0, 0, 0
  ],
  [
    0, 0, 0, 0,
    0, 0, 0, 0
  ],
  [
    0, 0, 0, 1,
    0, 0, 0, 0
  ],
  [
    0, 0, 0, 0,
    0, 0, 0, 0
  ],
  [
    0, 0, 0, 0,
    0, 0, 0, 0
  ],
  [
    0, 0, 0, 0,
    0, 0, 0, 0
  ],
  [
    0, 0, 0, 0,
    0, 0, 0, 0
  ]
]
true
1 Answers

Replace the tail end of your script with...

function printBoard( board ) {
  const pieces = [ '-', 'Q' ];
  const headerFooter = '     a  b  c  d  e  f  g  h';
  console.log( headerFooter );
  for (let y = board.length - 1; 0 <= y; y--) {
    let rank = ' ' + ( y + 1 ) + '  ';
    for (let x = 0; x < board[y].length; x++) {
      rank += ' ' + pieces[ board[y][x] ] + ' ';
    }
    rank += '  ' + ( y + 1 );
    console.log( rank );
  }
  console.log( headerFooter );
}

whiteQueen = [0, 6];
blackQueen = [3, 3];
generatedBoard = generateBoard(whiteQueen, blackQueen);
printBoard( generatedBoard );
//printBoard( queenThreat( generatedBoard ) );

...which introduces a function to more cleanly log the array to the console in the traditional format of a chessboard. Note that you'll need to update the pieces array to correspond with the piece representation of the values in the board. (Eg, 0 is empty square, 1 is 'Q', etc.)

The full script will appear as...

const arraysEqual = (a, b) => {
  if (a === b) return true;
  if (a == null || b == null) return false;
  if (a.length !== b.length) return false;
  for (var i = 0; i < a.length; ++i) {
    if (a[i] !== b[i]) return false;
  }
  return true;
}
const generateBoard = (whiteQueen, blackQueen) => {
  const boardArr = []
  for(let i = 0; i < 8; i++) {
    boardArr.push([])
  }
  for (let y = 0; y < 8; y++) {
    for (let x = 0; x < 8; x++) {
      let currentPos = [y, x];
      if (!(arraysEqual(currentPos, whiteQueen)) && !(arraysEqual(currentPos, blackQueen))){
        boardArr[y][x] = 0; 
      } else if (arraysEqual(currentPos, whiteQueen)) {
        boardArr[y][x] = 1;
      } else if (arraysEqual(currentPos, blackQueen)) {
        boardArr[y][x] = 1;
      }
    }
  }
  return boardArr;
}

const queenThreat = (generatedBoard) => {
  let queen1;
  let queen2;
  for (let y = 0; y < generatedBoard.length; y++) {
    for (let x = 0; x < generatedBoard[y].length; x++){
      let currentNode = generatedBoard[y][x];
      if(currentNode === 1){
        if(queen1 === undefined){
          queen1 = [y,x];
        } else {
          queen2 = [y,x]
          }
      }
    }
  }
  //same row and same column match check
  if (queen2[0] === queen1[0] || queen2[1] === queen1[1]){
    return true
  }
  //diagonal match check
  for (let i = 0; i < 8; i++) {
    if (queen1[0] + i === queen2[0] && queen1[1] + i === queen2[1]){
      return true;
    }
    if (queen1[0] + i === queen2[0] && queen1[1] - i === queen2[1]){
      return true; 
    }
  }
  return false;
}

function printBoard( board ) {
  const pieces = [ '-', 'Q' ];
  const headerFooter = '     a  b  c  d  e  f  g  h';
  console.log( headerFooter );
  for (let y = board.length - 1; 0 <= y; y--) {
let rank = ' ' + ( y + 1 ) + '  ';
for (let x = 0; x < board[y].length; x++) {
  rank += ' ' + pieces[ board[y][x] ] + ' ';
}
rank += '  ' + ( y + 1 );
console.log( rank );
  }
  console.log( headerFooter );
}

whiteQueen = [0, 6];
blackQueen = [3, 3];
generatedBoard = generateBoard(whiteQueen, blackQueen);
printBoard( generatedBoard );
//printBoard( queenThreat( generatedBoard ) );

Related