Browse Source

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 1 year ago
parent
commit
fd1e768332
1 changed files with 25 additions and 0 deletions
  1. 25 0
      starfish.js

+ 25 - 0
starfish.js

@@ -84,6 +84,12 @@ class CodeBox {
          * @type {?Date}
          * @type {?Date}
          */
          */
         this.datetime = null;
         this.datetime = null;
+        /**
+         * Should the code box be printed?
+         *
+         * @type {boolean}
+         */
+        this.print = false;
 
 
         this.codeBoxDOM = document.getElementById(codeBoxID);
         this.codeBoxDOM = document.getElementById(codeBoxID);
         if(!this.codeBoxDOM) {
         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
      * Make all the rows in the code box the same length
      *
      *
@@ -429,6 +452,8 @@ class CodeBox {
     }
     }
 
 
     Swim() {
     Swim() {
+        if(this.print) { this.PrintCodeBox(); }
+
         const instruction = this.box[this.pointer.Y][this.pointer.X];
         const instruction = this.box[this.pointer.Y][this.pointer.X];
         this.datetime = new Date();
         this.datetime = new Date();