Skip to content

Instantly share code, notes, and snippets.

@avivshafir
Created May 26, 2020 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avivshafir/91412db96f379573bd8775fa745e8463 to your computer and use it in GitHub Desktop.
Save avivshafir/91412db96f379573bd8775fa745e8463 to your computer and use it in GitHub Desktop.
printing two dimensional board
let ROWS = 6;
let COLS = 6;
class Board {
constructor(props) {
this.cells = [];
for (let i = 0; i < ROWS; i++) {
for (let j = 0; j < COLS; j++) {
if (!this.cells[i]) {
this.cells[i] = [];
}
this.cells[i][j] = new Cell(i, j);
if (i % 2 === 0) {
this.cells[i][j].symbol = "";
}
}
}
}
}
class Cell {
constructor(x, y) {
this.x = x;
this.y = y;
this.symbol = 'A';
}
}
const board = new Board();
/*
A B C D E F
=========================
1 | | | | | | |
=========================
2 | | | | | | |
=========================
3 | | | | | | |
=========================
4 | | | | | | |
=========================
* */
function printBoard() {
let output = '';
let letter = 'A';
output += " ";
for (let j = 0; j < COLS; j++) {
output += " " + letter;
}
output += "\n";
output += " =";
for (let j = 0; j < COLS; j++) {
output += "====";
}
output += "\n";
for (let i = 0; i < ROWS; i++) {
let rowCharCode = 65;
output += i + 1;
output += ' |';
for (let j = 0; j < COLS; j++) {
//print cells contents
if (board.cells[i][j].symbol) {
output += " " + board.cells[i][j].symbol;
} else {
output += " ";
}
output += ' |';
}
output += "\n =";
for (let j = 0; j < COLS; j++) {
output += "====";
}
// output += String.fromCharCode(rowCharCode + i);
// rowCharCode++;
output += "\n";
}
console.log(output);
}
printBoard();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment