Created
May 26, 2020 22:46
-
-
Save avivshafir/91412db96f379573bd8775fa745e8463 to your computer and use it in GitHub Desktop.
printing two dimensional board
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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