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} autoplay If true, the game will autoplay itself * @returns {void} */ function _render(screenState, autoplay = 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; let gameScore = 0; let ballX = 0; let paddleX = 0; for (let i = 0; i < screenState.length; i += 3) { const x = screenState[i]; const y = screenState[i + 1]; const tile = screenState[i + 2]; if (x == -1) { gameScore = tile; } // 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] = []; } // If the current tile is the ball, store the x coord for autoplay if (tile == 3) { paddleX = x; } else if (tile == 4) { ballX = x; } 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"; } return [boardString, gameScore, blockTileCount, ballX, paddleX]; } function renderToConsole(screenState) { const [boardState, gameScore, blockTileCount, ballX, paddleX] = _render(screenState, false, true); console.clear(); console.log("Score: ", gameScore); console.log(boardState); console.log("Block Tile Count: ", blockTileCount); return [ballX, paddleX]; } 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: renderToConsole, };