common.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const gameTiles = [
  2. ".", // Empty
  3. "█", // Wall
  4. "▪", // Block
  5. "_", // Paddle
  6. "°", // Ball
  7. ];
  8. /**
  9. * Render the arcade screen
  10. *
  11. * @param {number[]} screenState An array of numbers represent the state of the arcade screen
  12. * @param {boolean} countBlocks Display a count of block tiles on the screen
  13. * @returns {void}
  14. */
  15. function render(screenState, countBlocks = false) {
  16. if (screenState.length % 3 != 0) {
  17. console.warn("screenState length is not divisible by three!");
  18. }
  19. /** @type {number[][]} */
  20. let board = [];
  21. let boardWidth = -1;
  22. let boardHeight = -1;
  23. let blockTileCount = 0;
  24. for (let i = 0; i < screenState.length; i += 3) {
  25. const x = screenState[i];
  26. const y = screenState[i + 1];
  27. const tile = screenState[i + 2];
  28. // Track the board dimensions
  29. if (x > boardWidth) { boardWidth = x; }
  30. if (y > boardHeight) { boardHeight = y; }
  31. // create a row for the y dimension if it doesn't already exist
  32. if (!board[y]) { board[y] = []; }
  33. board[y][x] = tile;
  34. }
  35. board = normalizeBoard(board, boardWidth, boardHeight);
  36. let boardString = "";
  37. for (const row of board) {
  38. for (const tile of row) {
  39. if (tile == 2) {
  40. blockTileCount++;
  41. }
  42. boardString += gameTiles[tile];
  43. }
  44. boardString += "\n";
  45. }
  46. console.clear();
  47. console.log(boardString);
  48. if (countBlocks) {
  49. console.log("\n", "Block tiles: ", blockTileCount);
  50. }
  51. }
  52. function normalizeBoard(board, width, height) {
  53. for (let h = 0; h <= height; h++) {
  54. if (!board[h]) { board[h] = []; }
  55. for (let w = 0; w <= width; w++) {
  56. if (board[h][w] == undefined) { board[h][w] = 0; }
  57. }
  58. }
  59. return board;
  60. }
  61. module.exports = { render };