Explorar o código

Add option to print the code box each step

To emulate the `-c` flag from the original implementation, and to aid in
debugging, added a toggle-able flag to enable printing the code box to
the console each tick.
ApisNecros hai 1 ano
pai
achega
fd1e768332
Modificáronse 1 ficheiros con 25 adicións e 0 borrados
  1. 25 0
      starfish.js

+ 25 - 0
starfish.js

@@ -84,6 +84,12 @@ class CodeBox {
          * @type {?Date}
          */
         this.datetime = null;
+        /**
+         * Should the code box be printed?
+         *
+         * @type {boolean}
+         */
+        this.print = false;
 
         this.codeBoxDOM = document.getElementById(codeBoxID);
         if(!this.codeBoxDOM) {
@@ -162,6 +168,23 @@ class CodeBox {
         }
     }
 
+    /** Prints the code box to the console */
+    PrintCodeBox() {
+        let output = "";
+        for (let y = 0; y < this.box.length; y++) {
+            for (let x = 0; x < this.box[y].length; x++) {
+                let instruction = this.box[y][x];
+                if (x == this.pointer.X && y == this.pointer.Y) {
+                    instruction = `*${instruction}*`;
+                }
+                output += `${instruction} `;
+            }
+            output += "\n";
+        }
+
+        console.log(output);
+    }
+
     /**
      * Make all the rows in the code box the same length
      *
@@ -429,6 +452,8 @@ class CodeBox {
     }
 
     Swim() {
+        if(this.print) { this.PrintCodeBox(); }
+
         const instruction = this.box[this.pointer.Y][this.pointer.X];
         this.datetime = new Date();