common.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. * @returns {void}
  13. */
  14. function render(screenState) {
  15. if (screenState.length % 3 != 0) {
  16. console.warn("screenState length is not divisible by three!");
  17. }
  18. /** @type {number[][]} */
  19. let board = [];
  20. let boardWidth = -1;
  21. let boardHeight = -1;
  22. let blockTileCount = 0;
  23. for (let i = 0; i < screenState.length; i += 3) {
  24. const x = screenState[i];
  25. const y = screenState[i + 1];
  26. const tile = screenState[i + 2];
  27. // Track the board dimensions
  28. if (x > boardWidth) { boardWidth = x; }
  29. if (y > boardHeight) { boardHeight = y; }
  30. // create a row for the y dimension if it doesn't already exist
  31. if (!board[y]) { board[y] = []; }
  32. board[y][x] = tile;
  33. }
  34. board = normalizeBoard(board, boardWidth, boardHeight);
  35. let boardString = "";
  36. for (const row of board) {
  37. for (const tile of row) {
  38. if (tile == 2) {
  39. blockTileCount++;
  40. }
  41. boardString += gameTiles[tile];
  42. }
  43. boardString += "\n";
  44. }
  45. console.clear();
  46. console.log(boardString);
  47. console.log("\n", "Block tiles: ", blockTileCount);
  48. }
  49. function normalizeBoard(board, width, height) {
  50. for (let h = 0; h <= height; h++) {
  51. if (!board[h]) { board[h] = []; }
  52. for (let w = 0; w <= width; w++) {
  53. if (board[h][w] == undefined) { board[h][w] = 0; }
  54. }
  55. }
  56. return board;
  57. }
  58. module.exports = { render };