12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- const gameTiles = [
- ".", // Empty
- "█", // Wall
- "▪", // Block
- "_", // Paddle
- "°", // Ball
- ];
- /**
- * Render the arcade screen
- *
- * @param {number[]} screenState An array of numbers represent the state of the arcade screen
- * @param {boolean} countBlocks Display a count of block tiles on the screen
- * @returns {void}
- */
- function render(screenState, countBlocks = false) {
- if (screenState.length % 3 != 0) {
- console.warn("screenState length is not divisible by three!");
- }
- /** @type {number[][]} */
- let board = [];
- let boardWidth = -1;
- let boardHeight = -1;
- let blockTileCount = 0;
- for (let i = 0; i < screenState.length; i += 3) {
- const x = screenState[i];
- const y = screenState[i + 1];
- const tile = screenState[i + 2];
- // Track the board dimensions
- if (x > boardWidth) { boardWidth = x; }
- if (y > boardHeight) { boardHeight = y; }
- // create a row for the y dimension if it doesn't already exist
- if (!board[y]) { board[y] = []; }
- board[y][x] = tile;
- }
- board = normalizeBoard(board, boardWidth, boardHeight);
- let boardString = "";
- for (const row of board) {
- for (const tile of row) {
- if (tile == 2) {
- blockTileCount++;
- }
- boardString += gameTiles[tile];
- }
- boardString += "\n";
- }
- console.clear();
- console.log(boardString);
- if (countBlocks) {
- console.log("\n", "Block tiles: ", blockTileCount);
- }
- }
- function normalizeBoard(board, width, height) {
- for (let h = 0; h <= height; h++) {
- if (!board[h]) { board[h] = []; }
- for (let w = 0; w <= width; w++) {
- if (board[h][w] == undefined) { board[h][w] = 0; }
- }
- }
- return board;
- }
- module.exports = { render };
|