13_1.js 1.8 KB

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